Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mypyc/lib-rt/int_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ bool CPyTagged_IsLt_(CPyTagged left, CPyTagged right) {

PyObject *CPyLong_FromStrWithBase(PyObject *o, CPyTagged base) {
Py_ssize_t base_size_t = CPyTagged_AsSsize_t(base);
if (base_size_t == -1 && PyErr_Occurred()) {
return NULL;
}
return PyLong_FromUnicodeObject(o, base_size_t);
}

Expand Down
12 changes: 10 additions & 2 deletions mypyc/lib-rt/misc_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,10 @@ PyObject *CPyTagged_Str(CPyTagged n) {
if (CPyTagged_CheckShort(n)) {
return CPyTagged_ShortToStr(CPyTagged_ShortAsSsize_t(n));
} else {
return PyObject_Str(CPyTagged_AsObject(n));
PyObject *obj = CPyTagged_AsObject(n);
PyObject *str = PyObject_Str(obj);
Py_DECREF(obj);
return str;
}
}

Expand All @@ -602,7 +605,12 @@ PyObject *CPyTagged_AsciiBytes(CPyTagged n) {
if (CPyTagged_CheckShort(n)) {
return CPyTagged_ShortToAsciiBytes(CPyTagged_ShortAsSsize_t(n));
}
PyObject *str = PyObject_Str(CPyTagged_AsObject(n));
PyObject *obj = CPyTagged_AsObject(n);
PyObject *str = PyObject_Str(obj);
Py_DECREF(obj);
if (str == NULL) {
return NULL;
}
PyObject *bytes = PyUnicode_AsASCIIString(str);
CPy_DECREF(str);
return bytes;
Expand Down
22 changes: 22 additions & 0 deletions mypyc/test-data/run-bytes.test
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,30 @@ class bytes_subclass(bytes):
return b'spook'

[case testBytesFormatting]
import sys
from typing import Any
from testutil import assertRaises

def format_int(n: int) -> bytes:
return b"%d" % n

def test_bytes_formatting_error_propagation() -> None:
sys_module: Any = sys
if hasattr(sys_module, "set_int_max_str_digits"):
old_limit = sys_module.get_int_max_str_digits()
try:
sys_module.set_int_max_str_digits(640)
n = 10 ** 1000
before = sys_module.getrefcount(n)
for _ in range(3):
with assertRaises(ValueError):
format_int(n)
assert sys_module.getrefcount(n) == before
assert n == 10 ** 1000 # Keep n alive across both refcount checks.
finally:
sys_module.set_int_max_str_digits(old_limit)
assert format_int(123) == b"123"

# https://www.python.org/dev/peps/pep-0461/
def test_bytes_formatting() -> None:
val = 10
Expand Down
9 changes: 9 additions & 0 deletions mypyc/test-data/run-integers.test
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,15 @@ def test_true_divide() -> None:
huge / -2
assert 1 / huge == 0.0

def from_base(s: str, base: int) -> int:
return int(s, base)

def test_base_overflow() -> None:
for base in (2 ** 100, -(2 ** 100)):
with assertRaises(OverflowError):
from_base("10", base)
assert from_base("10", 10) == 10

[case testIntMinMax]
def test_int_min_max() -> None:
x: int = 200
Expand Down
Loading