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
2 changes: 2 additions & 0 deletions mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,8 @@ PyObject *CPyBytes_Concat(PyObject *a, PyObject *b);
PyObject *CPyBytes_Join(PyObject *sep, PyObject *iter);
CPyTagged CPyBytes_Ord(PyObject *obj);
PyObject *CPyBytes_Multiply(PyObject *bytes, CPyTagged count);
PyObject *CPyBytes_RMultiply(CPyTagged count, PyObject *bytes);
PyObject *CPyBytes_CheckResult(PyObject *res);
int CPyBytes_Startswith(PyObject *self, PyObject *subobj);
int CPyBytes_Endswith(PyObject *self, PyObject *subobj);
int CPyBytes_Compare(PyObject *left, PyObject *right);
Expand Down
3 changes: 2 additions & 1 deletion mypyc/lib-rt/bytes_extra_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ PyObject *CPyBytes_Translate(PyObject *bytes, PyObject *table) {
}

// Fallback to Python method call for non-exact types or non-standard tables
return PyObject_CallMethodOneArg(bytes, mypyc_interned_str.translate, table);
return CPyBytes_CheckResult(
PyObject_CallMethodOneArg(bytes, mypyc_interned_str.translate, table));
}
33 changes: 28 additions & 5 deletions mypyc/lib-rt/bytes_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,28 @@ PyObject *CPyBytes_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end) {
return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING(obj) + startn, slice_len);
}
}
return CPyObject_GetSlice(obj, start, end);
return CPyBytes_CheckResult(CPyObject_GetSlice(obj, start, end));
}

// Like _PyBytes_Join but fallback to dynamic call if 'sep' is not bytes
// (mostly commonly, for bytearrays)
// An overriding method in a bytes subclass can return an arbitrary object. Check that
// the result is bytes, since callers rely on the result type.
PyObject *CPyBytes_CheckResult(PyObject *res) {
if (res != NULL && !PyBytes_Check(res)) {
CPy_TypeError("bytes", res);
Py_DECREF(res);
return NULL;
}
return res;
}

// Like _PyBytes_Join but fallback to dynamic call if 'sep' is not exact bytes
// (a subclass may override join)
PyObject *CPyBytes_Join(PyObject *sep, PyObject *iter) {
if (PyBytes_CheckExact(sep)) {
return PyBytes_Join(sep, iter);
} else {
return PyObject_CallMethodOneArg(sep, mypyc_interned_str.join, iter);
return CPyBytes_CheckResult(
PyObject_CallMethodOneArg(sep, mypyc_interned_str.join, iter));
}
}

Expand Down Expand Up @@ -151,7 +163,18 @@ PyObject *CPyBytes_Multiply(PyObject *bytes, CPyTagged count) {
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
return NULL;
}
return PySequence_Repeat(bytes, temp_count);
return CPyBytes_CheckResult(PySequence_Repeat(bytes, temp_count));
}

PyObject *CPyBytes_RMultiply(CPyTagged count, PyObject *bytes) {
if (PyBytes_CheckExact(bytes)) {
return CPyBytes_Multiply(bytes, count);
}
// Use the generic operation so that __rmul__ is called on subclasses
PyObject *count_obj = CPyTagged_AsObject(count);
PyObject *res = PyNumber_Multiply(count_obj, bytes);
Py_DECREF(count_obj);
return CPyBytes_CheckResult(res);
}

int CPyBytes_Startswith(PyObject *self, PyObject *subobj) {
Expand Down
3 changes: 1 addition & 2 deletions mypyc/primitives/bytes_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@
name="*",
arg_types=[int_rprimitive, bytes_rprimitive],
return_type=bytes_rprimitive,
c_function_name="CPyBytes_Multiply",
c_function_name="CPyBytes_RMultiply",
error_kind=ERR_MAGIC,
ordering=[1, 0],
)

# bytes[begin:end]
Expand Down
2 changes: 1 addition & 1 deletion mypyc/test-data/irbuild-bytes.test
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def i_times_b(s, n):
n :: int
r0 :: bytes
L0:
r0 = CPyBytes_Multiply(s, n)
r0 = CPyBytes_RMultiply(n, s)
return r0

[case testBytesTranslate]
Expand Down
75 changes: 75 additions & 0 deletions mypyc/test-data/run-bytes.test
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,78 @@ def test_optional_ne() -> None:
assert ne_b_b_opt(b'x', b'y')
assert ne_b_b_opt(b'y', b'x')
assert ne_b_b_opt(b'x', None)

[case testBytesOpsOverridingSubclass]
from testutil import assertRaises
from bytessub import BadBytes, GoodBytes

def mul(b: bytes, n: int) -> bytes:
return b * n

def rmul(b: bytes, n: int) -> bytes:
return n * b

def join(b: bytes) -> bytes:
return b.join([b'a', b'b'])

def translate(b: bytes, table: bytes) -> bytes:
return b.translate(table)

def get_slice(b: bytes, start: int, end: int) -> bytes:
return b[start:end]

BIG = 1 << 70

table = bytes(range(256))

def test_bad_result_type() -> None:
b: bytes = BadBytes(b'x')
with assertRaises(TypeError, "bytes object expected; got int"):
mul(b, 2)
with assertRaises(TypeError, "bytes object expected; got int"):
rmul(b, 2)
with assertRaises(TypeError, "bytes object expected; got int"):
join(b)
with assertRaises(TypeError, "bytes object expected; got int"):
translate(b, table)
with assertRaises(TypeError, "bytes object expected; got int"):
get_slice(b, 0, BIG)
with assertRaises(TypeError, "bytes object expected; got int"):
get_slice(b, -BIG, 1)

def test_overrides_called() -> None:
b: bytes = GoodBytes(b'x')
assert mul(b, 2) == b'xx!'
assert rmul(b, 2) == b'xx?'
assert type(rmul(b, 2)) is GoodBytes
assert join(b) == b'axb.'
assert translate(b, table) == b'x#'
assert get_slice(b, 0, BIG) == b'x^'
assert rmul(b'x', 2) == b'xx'

[file bytessub.py]
from typing import Any

class BadBytes(bytes):
def __mul__(self, n: Any) -> Any:
return 12345678901234567890
def __rmul__(self, n: Any) -> Any:
return 12345678901234567891
def join(self, it: Any) -> Any:
return 12345678901234567892
def translate(self, table: Any, delete: Any = b'') -> Any:
return 12345678901234567893
def __getitem__(self, i: Any) -> Any:
return 12345678901234567894

class GoodBytes(bytes):
def __mul__(self, n: Any) -> Any:
return bytes(self) * n + b'!'
def __rmul__(self, n: Any) -> Any:
return GoodBytes(n * bytes(self) + b'?')
def join(self, it: Any) -> Any:
return bytes(self).join(it) + b'.'
def translate(self, table: Any, delete: Any = b'') -> Any:
return bytes(self).translate(table) + b'#'
def __getitem__(self, i: Any) -> Any:
return bytes(self)[i] + b'^'
Loading