Here's a minimal reproduction:
» ./python.exe
Python 3.12.0a1+ (heads/main:bded5edd9a, Oct 27 2022, 23:29:21) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class SubStr(str): ...
...
>>> int.from_bytes(b"0", SubStr("big"))
Assertion failed: (PyUnicode_CheckExact(str1)), function _PyUnicode_Equal, file Objects/unicodeobject.c, line 10447.
[1] 18466 abort ./python.exe
I think this happes because of these lines:
|
int |
|
_PyUnicode_Equal(PyObject *str1, PyObject *str2) |
|
{ |
|
assert(PyUnicode_CheckExact(str1)); |
|
assert(PyUnicode_CheckExact(str2)); |
|
if (str1 == str2) { |
|
return 1; |
|
} |
|
return unicode_compare_eq(str1, str2); |
|
} |
_PyUnicode_Equal uses assert(PyUnicode_CheckExact(...)), while many function (including int_from_bytes_impl) use PyUnicode_Check() or just parse str objects from args.
|
else if (_PyUnicode_Equal(byteorder, &_Py_ID(little))) |
|
little_endian = 1; |
|
else if (_PyUnicode_Equal(byteorder, &_Py_ID(big))) |
|
little_endian = 0; |
Probably other functions that use _PyUnicode_Equal are also affected.
I would like to raise a question: shouldn't it be assert(PyUnicode_Check(...)) in _PyUnicode_Equal?
I would like to send a PR with the fix!
Here's a minimal reproduction:
I think this happes because of these lines:
cpython/Objects/unicodeobject.c
Lines 10444 to 10453 in bded5ed
_PyUnicode_Equalusesassert(PyUnicode_CheckExact(...)), while many function (includingint_from_bytes_impl) usePyUnicode_Check()or just parsestrobjects from args.cpython/Objects/longobject.c
Lines 6167 to 6170 in bded5ed
Probably other functions that use
_PyUnicode_Equalare also affected.I would like to raise a question: shouldn't it be
assert(PyUnicode_Check(...))in_PyUnicode_Equal?I would like to send a PR with the fix!