Title: Assorted CPython compatibility divergences (18 items) · Issue #9913 · RustPython/RustPython · GitHub
Open Graph Title: Assorted CPython compatibility divergences (18 items) · Issue #9913 · RustPython/RustPython
X Title: Assorted CPython compatibility divergences (18 items) · Issue #9913 · RustPython/RustPython
Description: Feature Found by differential testing against CPython 3.12. All 18 items reproduce on main at 7abccf0 (the outputs below are from that build), and CPython 3.12.13, 3.14.4 and 3.15.2 agree on each, apart from the wording of one message no...
Open Graph Description: Feature Found by differential testing against CPython 3.12. All 18 items reproduce on main at 7abccf0 (the outputs below are from that build), and CPython 3.12.13, 3.14.4 and 3.15.2 agree on each, ...
X Description: Feature Found by differential testing against CPython 3.12. All 18 items reproduce on main at 7abccf0 (the outputs below are from that build), and CPython 3.12.13, 3.14.4 and 3.15.2 agree on each, ...
Opengraph URL: https://github.com/RustPython/RustPython/issues/9913
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Assorted CPython compatibility divergences (18 items)","articleBody":"## Feature\n\nFound by differential testing against CPython 3.12. All 18 items reproduce on main at 7abccf0cd6 (the outputs below are from that build), and CPython 3.12.13, 3.14.4 and 3.15.2 agree on each, apart from the wording of one message noted in item 13. Every item has a reproduction, both outputs and the cause, with a suggested fix folded underneath.\n\nI have a fix for each item and will open the PRs as they are ready. Anyone is welcome to pick up any item and open a PR for it.\n\n- [x] **1.** `_thread.stack_size(2**64)`: `ValueError` on RustPython, `OverflowError` on CPython\n- [x] **2.** `pow(1+2j, \"x\", 3)`: `ValueError` on RustPython, `TypeError` on CPython\n- [ ] **3.** `format(\"x\", \"\\u0665\")`: `ValueError` on RustPython, `'x '` on CPython\n- [x] **4.** `struct.pack(\"d\", \"a\")`: `TypeError` on RustPython, `struct.error` on CPython\n- [ ] **5.** `struct.pack_into(\"Z\", bytearray(4), 10**100)`: `OverflowError` on RustPython, `struct.error` on CPython\n- [ ] **6.** `io.FileIO(fd, \"r\", None)`, `os.set_blocking(fd, None)`: `TypeError` on RustPython, accepted on CPython\n- [x] **7.** `_codecs.utf_8_decode(b\"\", \"strict\", None)`, `binascii.a2b_qp(b\"a_b\", None)`: `TypeError` on RustPython, accepted on CPython\n- [x] **8.** `marshal.dumps([FloatSubclass(0.5)])`: `bytes` on RustPython, `ValueError` on CPython\n- [x] **9.** `bytes(\"x\", \"utf-8\\0\")`: `b'x'` on RustPython, `ValueError` on CPython\n- [x] **10.** `bytes(L([1, 2, 3]))` with `L.__iter__` overridden: stored items on RustPython, iterated items on CPython\n- [x] **11.** `csv.register_dialect(BadHash(\"x\"))`: accepted on RustPython, the `__hash__` exception on CPython\n- [x] **12.** `type(iter(\"abc\"))`: `str_iterator` on RustPython, `str_ascii_iterator` on CPython\n- [x] **13.** `collections._count_elements(None, IterReturnsInt())`: `AttributeError` on RustPython, `TypeError` on CPython\n- [x] **14.** `operator.concat(UserString(\"a\"), 1)`: `'a1'` on RustPython, `TypeError` on CPython\n- [ ] **15.** `getattr(K(), BadHash(\"x\"), 0)`: `0` on RustPython, the `__hash__` exception on CPython\n- [ ] **16.** `sorted([1, 2, 3], reverse=None)`: `TypeError` on RustPython, `[1, 2, 3]` on CPython\n- [ ] **17.** Interpreter shutdown imports `threading` on RustPython, not on CPython\n- [ ] **18.** `compile(\"if x:\\n a\\n \", \"\u003cfoo>\", \"single\")`: a code object on RustPython, `SyntaxError` on CPython\n\n### 1. `_thread.stack_size()` reports an out-of-range size as `ValueError`, or accepts it\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `_thread.stack_size(2**64)` | `ValueError: size must be at least 53248 bytes` | `OverflowError: Python int too large to convert to C ssize_t` |\n| `_thread.stack_size(-10**100)` | `ValueError: size must be at least 53248 bytes` | `OverflowError: Python int too large to convert to C ssize_t` |\n| `_thread.stack_size(2**63)` | `0` | `OverflowError: Python int too large to convert to C ssize_t` |\n\n`stack_size` ([`_thread.rs#L927-L940`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/stdlib/_thread.rs#L927-L940)) converts its argument to `usize` and reports a failed conversion as the minimum-size `ValueError`, so `2**63` to `2**64 - 1` is accepted. CPython's `thread_stack_size` parses the size as `Py_ssize_t` first ([`_threadmodule.c#L2204-L2212`](https://github.com/python/cpython/blob/v3.15.2/Modules/_threadmodule.c#L2204-L2212)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nIn [`stack_size`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/stdlib/_thread.rs#L927-L940), convert the size to `isize`, raising `OverflowError` when it does not fit, and compare the signed value with the minimum size.\n\n\u003c/details>\n\n### 2. Three-argument `pow()` with a `complex` raises `complex modulo` before looking at the operands\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `pow(1+2j, \"x\", 3)` | `ValueError: complex modulo` | `TypeError: unsupported operand type(s) for ** or pow(): 'complex', 'str', 'int'` |\n| `pow(\"x\", 1+2j, 3)` | `ValueError: complex modulo` | `TypeError: unsupported operand type(s) for ** or pow(): 'str', 'complex', 'int'` |\n\n`complex`'s `power` slot ([`complex.rs#L770-L776`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/builtins/complex.rs#L770-L776)) raises as soon as a modulus is given. CPython's `complex_pow` converts both operands first and returns `NotImplemented` for one that is not a number ([`complexobject.c#L740-L750`](https://github.com/python/cpython/blob/v3.15.2/Objects/complexobject.c#L740-L750)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nRoute the [`power` slot](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/builtins/complex.rs#L770-L776) through `PyComplex::number_op` with or without a modulus, and raise `complex modulo` only once both operands are converted.\n\n\u003c/details>\n\n### 3. A format spec width or precision accepts only ASCII digits\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `format(\"x\", \"\\u0665\")` | `ValueError: Unknown format code '٥' for object of type 'str'` | `'x '` |\n| `format(3, \"\\u0665d\")` | `ValueError: Invalid format specifier '٥d' for object of type 'int'` | `' 3'` |\n| `format(1.5, \"\\u0668.\\u0663f\")` | `ValueError: Invalid format specifier '٨.٣f' for object of type 'float'` | `' 1.500'` |\n\nThe parser reads the number with `get_num_digits` ([`format.rs#L217-L224`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/common/src/format.rs#L217-L224)), which stops at the first character that is not an ASCII digit. CPython's `get_integer` converts each character with `Py_UNICODE_TODECIMAL`, so any Unicode decimal digit counts ([`unicode_formatter.c#L296-L326`](https://github.com/python/cpython/blob/v3.15.2/Objects/unicode_formatter.c#L296-L326)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nRead the number one character at a time in `parse_number`, testing an ASCII character directly and a non-ASCII one with `char_to_decimal`, and keep the overflow check.\n\n\u003c/details>\n\n### 4. `struct.pack()` lets a float conversion error through\n\n```python\nclass RaisingFloat:\n def __float__(self):\n raise RuntimeError\n```\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `struct.pack(\"d\", \"a\")` | `TypeError: must be real number, not str` | `struct.error: required argument is not a float` |\n| `struct.pack(\"\u003cf\", None)` | `TypeError: must be real number, not NoneType` | `struct.error: required argument is not a float` |\n| `struct.pack(\"e\", RaisingFloat())` | `RuntimeError` | `struct.error: required argument is not a float` |\n\nThe `f` and `d` packers ([`buffer.rs#L694-L724`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/buffer.rs#L694-L724)) and the `e` packer ([`#L841-L854`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/buffer.rs#L841-L854)) propagate the argument's conversion error. CPython's float packers replace any conversion failure with `struct.error` ([`_struct.c#L776-L786`](https://github.com/python/cpython/blob/v3.15.2/Modules/_struct.c#L776-L786), [`#L276-L288`](https://github.com/python/cpython/blob/v3.15.2/Modules/_struct.c#L276-L288)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nIn the `e`, `f` and `d` packers, map a failed `ArgIntoFloat` conversion to `struct.error`, as the complex packers already do.\n\n\u003c/details>\n\n### 5. A bad `struct` format is reported after the other arguments are converted\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `struct.pack_into(\"Z\", bytearray(4), 10**100)` | `OverflowError: Python int too large to convert to Rust isize` | `struct.error: bad char in struct format` |\n| `struct.unpack_from(\"Z\", b\"\", 10**100)` | `OverflowError: Python int too large to convert to Rust isize` | `struct.error: bad char in struct format` |\n| `struct.unpack(\"Z\", 5)` | `TypeError: a bytes-like object is required, not 'int'` | `struct.error: bad char in struct format` |\n\nThe module-level functions, such as `pack_into` ([`pystruct.rs#L133-L144`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/stdlib/src/pystruct.rs#L133-L144)), bind every argument and parse the format in the body. CPython runs `cache_struct_converter` on the format before it converts the arguments after it ([`_struct.c.h#L518-L536`](https://github.com/python/cpython/blob/v3.15.2/Modules/clinic/_struct.c.h#L518-L536)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nBind the format of `pack`, `pack_into`, `unpack`, `unpack_from`, `iter_unpack` and `calcsize` as an argument type that parses it through the cache during binding.\n\n\u003c/details>\n\n### 6. Flag arguments in `io` and `os` accept only `int` instances\n\n`fd` below is any open file descriptor.\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `io.IncrementalNewlineDecoder(None, None)` | `TypeError: Expected type bool, not NoneType` | `\u003c_io.IncrementalNewlineDecoder object at 0x...>` |\n| `io.IncrementalNewlineDecoder(None, True).decode(\"a\\r\", None)` | `TypeError: Expected type bool, not NoneType` | `'a'` |\n| `io.FileIO(fd, \"r\", None)` | `TypeError: Expected type bool, not NoneType` | `\u003c_io.FileIO name=3 mode='rb' closefd=False>` |\n| `os.set_blocking(fd, None)` | `TypeError: Expected type bool, not NoneType` | `None` |\n| `os.dup2(fd, 77, None)` | `TypeError: Expected type bool, not NoneType` | `77` |\n\n`translate`, `final`, `closefd`, `blocking` and `inheritable` are bound as a Rust `bool`, whose conversion accepts only `int` instances ([`bool.rs#L19-L32`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/builtins/bool.rs#L19-L32)). CPython declares all five as Argument Clinic `bool`, which calls `PyObject_IsTrue` (for example [`fileio.c.h#L127`](https://github.com/python/cpython/blob/v3.15.2/Modules/_io/clinic/fileio.c.h#L127)), so any object is accepted and an exception from `__bool__` propagates.\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nBind the five flags as `ArgIntoBool` in `crates/vm/src/stdlib/_io.rs` and `crates/vm/src/stdlib/posix.rs`. The Windows versions in `nt.rs`, the positional `target_is_directory` of `os.symlink`, the `closefd` of `open()` and the keyword-only flags in `os` (`follow_symlinks`, `effective_ids`) have the same conversion.\n\n\u003c/details>\n\n### 7. Flag arguments in `_codecs` and `binascii` accept only `int` instances\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `_codecs.utf_8_decode(b\"\", \"strict\", None)` | `TypeError: Expected type bool, not NoneType` | `('', 0)` |\n| `binascii.a2b_qp(b\"a_b\", None)` | `TypeError: Expected type bool, not NoneType` | `b'a_b'` |\n| `binascii.b2a_qp(b\"a b\", False, True, \"x\")` | `TypeError: Expected type bool, not str` | `b'a_b'` |\n\nThe `final` argument of the decoders ([`_codecs.rs#L188-L196`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/stdlib/_codecs.rs#L188-L196)) and the flags of `a2b_qp()`, `b2a_qp()` and `b2a_uu()` ([`binascii.rs#L628-L676`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/stdlib/src/binascii.rs#L628-L676)) are bound as a Rust `bool`, as in item 6. CPython converts each with `PyObject_IsTrue` ([`_codecsmodule.c.h#L517-L523`](https://github.com/python/cpython/blob/v3.15.2/Modules/clinic/_codecsmodule.c.h#L517-L523), [`binascii.c.h#L1655-L1676`](https://github.com/python/cpython/blob/v3.15.2/Modules/clinic/binascii.c.h#L1655-L1676)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nBind `final` as `ArgIntoBool` in `DecodeArgs`, `EscapeTextDecodeArgs` and `ExDecodeArgs`, and `header`, `quotetabs`, `istext` and `backtick` in `binascii.rs`, as the other flags in that module already are. The Windows-only decoders (`mbcs`, `oem`, `code_page`) have the same conversion.\n\n\u003c/details>\n\n### 8. `marshal.dumps()` writes a nested subclass instance as its base type\n\n```python\nclass FloatSubclass(float):\n pass\n```\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `marshal.dumps([FloatSubclass(0.5)])` | `b'[\\x01\\x00\\x00\\x00g\\x00\\x00\\x00\\x00\\x00\\x00\\xe0?'` | `ValueError: unmarshallable object` |\n\n`dumps` runs `check_exact_type` ([`marshal.rs#L982-L1003`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/stdlib/marshal.rs#L982-L1003)) only on the top-level value ([`#L120`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/stdlib/marshal.rs#L120)). Inside a container, `write_object_depth` picks the wire type with `downcast_ref`, which also matches subclasses. CPython's `w_complex_object` tests every object with `PyFloat_CheckExact` and the like, at any depth.\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nMove the `check_exact_type` call from `dumps` into `write_object_depth`, so it runs on every object written.\n\n\u003c/details>\n\n### 9. A null character in `encoding`, `errors` or `compile()`'s `mode` goes unnoticed\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `bytes(\"x\", \"utf-8\\0\")` | `b'x'` | `ValueError: embedded null character` |\n| `bytearray(\"x\", \"utf-8\", \"strict\\0\")` | `bytearray(b'x')` | `ValueError: embedded null character` |\n| `str(b\"x\", \"utf-8\", \"strict\\0\")` | `'x'` | `ValueError: embedded null character` |\n| `compile(\"x\", \"f\", \"ex\\0ec\", {})` | `TypeError: 'dict' object cannot be interpreted as an integer` | `ValueError: embedded null character` |\n\n`bytes()` and `bytearray()` bind `encoding` and `errors` as plain strings ([`bytes_inner.rs#L169-L177`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/bytes_inner.rs#L169-L177)), `str()` only checks that they are `str` ([`str.rs#L400-L416`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/builtins/str.rs#L400-L416)), and `compile()` does the same for `mode` ([`builtins.rs#L117-L123`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/stdlib/builtins.rs#L117-L123)). CPython converts each to a C string while parsing the arguments and raises there ([`bytesobject.c.h#L1426-L1451`](https://github.com/python/cpython/blob/v3.15.2/Objects/clinic/bytesobject.c.h#L1426-L1451), [`bytearrayobject.c.h#L75-L100`](https://github.com/python/cpython/blob/v3.15.2/Objects/clinic/bytearrayobject.c.h#L75-L100), [`unicodeobject.c.h#L1887-L1912`](https://github.com/python/cpython/blob/v3.15.2/Objects/clinic/unicodeobject.c.h#L1887-L1912), [`bltinmodule.c.h#L411-L426`](https://github.com/python/cpython/blob/v3.15.2/Python/clinic/bltinmodule.c.h#L411-L426)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nBind those arguments as a `str` argument type whose conversion raises `ValueError` for a null character, so the check runs in argument order. `compile()`'s `filename` then has to be converted during binding too, to stay ahead of `mode`. `str.encode()`, `bytes.decode()` and `bytearray.decode()` accept a null character in the same two arguments.\n\n\u003c/details>\n\n### 10. `bytes()` and `bytearray()` read a `list` or `tuple` subclass's stored items\n\n```python\nclass L(list):\n def __iter__(self):\n return iter([65, 66])\n```\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `bytes(L([1, 2, 3]))` | `b'\\x01\\x02\\x03'` | `b'AB'` |\n| `bytearray(L([1, 2, 3]))` | `bytearray(b'\\x01\\x02\\x03')` | `bytearray(b'AB')` |\n\n`map_iterable_object_inner` ([`vm/mod.rs#L3735-L3762`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/vm/mod.rs#L3735-L3762)) has `PyList` and `PyTuple` arms that also match subclasses, so an overridden `__iter__` is never called. CPython's `bytes()` takes that shortcut only for an exact `list` or `tuple` ([`bytesobject.c#L3142-L3146`](https://github.com/python/cpython/blob/v3.15.2/Objects/bytesobject.c#L3142-L3146)), and `bytearray()` always iterates ([`bytearrayobject.c#L1129-L1131`](https://github.com/python/cpython/blob/v3.15.2/Objects/bytearrayobject.c#L1129-L1131)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nIn [`map_iterable_object_inner`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/vm/mod.rs#L3735-L3762), iterate anything that is not an exact `list` or `tuple` before reaching the arms that read the items directly.\n\n\u003c/details>\n\n### 11. The `csv` dialect registry does not hash a `str` subclass name\n\n```python\nclass BadHash(str):\n def __hash__(self):\n raise ValueError\n```\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `csv.register_dialect(BadHash(\"x\"))` | `None` | `ValueError` |\n| `csv.get_dialect(BadHash(\"excel\"))` | `\u003c_csv.Dialect object at 0x...>` | `ValueError` |\n| `csv.unregister_dialect(BadHash(\"excel\"))` | `None` | `ValueError` |\n\n`register_dialect`, `get_dialect` and `unregister_dialect` ([`csv.rs#L197-L244`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/stdlib/src/csv.rs#L197-L244)) key the registry on the text of a `str` name and hash only a name that is not a `str` (#9514). CPython keeps the registry in a dict and passes the name object to it, which calls its `__hash__` ([`_csv.c#L1635-L1638`](https://github.com/python/cpython/blob/v3.15.2/Modules/_csv.c#L1635-L1638), [`#L162-L170`](https://github.com/python/cpython/blob/v3.15.2/Modules/_csv.c#L162-L170), [`#L1652-L1666`](https://github.com/python/cpython/blob/v3.15.2/Modules/_csv.c#L1652-L1666)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nHave `registry_key()` hash every name before checking that it is a `str`, and hash the name in `register_dialect()` once the dialect is validated, where CPython inserts it.\n\n\u003c/details>\n\n### 12. An ASCII `str` has no `str_ascii_iterator`\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `type(iter(\"abc\")).__name__` | `'str_iterator'` | `'str_ascii_iterator'` |\n| `type(iter(\"abc\")) is type(iter(\"\\xe9\"))` | `True` | `False` |\n\n`PyStr::iter` ([`str.rs#L1737-L1751`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/builtins/str.rs#L1737-L1751)) always builds a `str_iterator`. CPython's `unicode_iter` gives an exact `str` holding only ASCII the separate `str_ascii_iterator` type ([`unicodeobject.c#L14784-L14797`](https://github.com/python/cpython/blob/v3.15.2/Objects/unicodeobject.c#L14784-L14797)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nAdd a `str_ascii_iterator` type with the methods `str_iterator` has, and return it from `PyStr::iter` for an exact `str` whose kind is ASCII.\n\n\u003c/details>\n\n### 13. An `__iter__` that returns a non-iterator is rejected only on the first `next()`\n\n```python\nclass IterReturnsInt:\n def __iter__(self):\n return 1\n```\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `collections._count_elements(None, IterReturnsInt())` | `AttributeError: 'NoneType' object has no attribute 'get'` | `TypeError: IterReturnsInt.__iter__() must return an iterator, not int` |\n\nCPython 3.12 and 3.14 word the message `iter() returned non-iterator of type 'int'`.\n\n`ArgIterable::get_iter` ([`protocol.rs#L99-L104`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/function/protocol.rs#L99-L104)) wraps whatever `__iter__` returns, and `_count_elements` ([`_collections.rs#L1494-L1523`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/stdlib/_collections.rs#L1494-L1523)) reads `mapping.get` before the first `next()`. CPython's `PyObject_GetIter` checks the result before returning it ([`abstract.c#L2825-L2831`](https://github.com/python/cpython/blob/v3.15.2/Objects/abstract.c#L2825-L2831)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nIn [`ArgIterable::get_iter`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/function/protocol.rs#L99-L104), get the iterator through [`PyIter::try_from_object`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/protocol/iter.rs#L139-L151), which already rejects a non-iterator result.\n\n\u003c/details>\n\n### 14. `operator.concat()` and `operator.iconcat()` check only the first operand's `__getitem__`\n\n```python\nclass ListWithIadd(list):\n def __iadd__(self, other):\n return \"iadded\"\n```\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `operator.concat(collections.UserString(\"a\"), 1)` | `'a1'` | `TypeError: 'UserString' object can't be concatenated` |\n| `operator.iconcat(collections.UserList([1]), {2})` | `[1, 2]` | `TypeError: 'UserList' object can't be concatenated` |\n| `operator.iconcat(ListWithIadd([1]), [2])` | `'iadded'` | `[1, 2]` |\n\n`concat` ([`_operator.rs#L163-L173`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/stdlib/_operator.rs#L163-L173)) and `iconcat` ([`#L252-L263`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/stdlib/_operator.rs#L252-L263)) check that the first operand has `__getitem__`, then evaluate `a + b` or `a += b`. CPython's `PySequence_Concat` and `PySequence_InPlaceConcat` use the sequence slots of the first operand and fall back to the number slots only when both operands are sequences ([`abstract.c#L1721-L1744`](https://github.com/python/cpython/blob/v3.15.2/Objects/abstract.c#L1721-L1744), [`#L1778-L1804`](https://github.com/python/cpython/blob/v3.15.2/Objects/abstract.c#L1778-L1804)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nCall [`PySequence::concat`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/protocol/sequence.rs#L182-L199) and [`PySequence::inplace_concat`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/protocol/sequence.rs#L220-L245), keeping the up-front rejection of a `dict` subclass. A class that defines `__iadd__` in Python must then stop inheriting the native `sq_inplace_concat` in `crates/vm/src/types/slot.rs`, as a Python `__add__` already drops `sq_concat`, or `iconcat` would extend such a `list` subclass in place.\n\n\u003c/details>\n\n### 15. Attribute lookup does not hash a `str` subclass name\n\n```python\nclass BadHash(str):\n def __hash__(self):\n raise ValueError\n\nclass K:\n pass\n```\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `getattr(K(), BadHash(\"x\"), 0)` | `0` | `ValueError` |\n| `hasattr(K(), BadHash(\"x\"))` | `False` | `ValueError` |\n| `setattr(K(), BadHash(\"x\"), 0)` | `None` | `ValueError` |\n\n`generic_getattr_opt` ([`object.rs#L282-L290`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/protocol/object.rs#L282-L290)) and `generic_setattr` ([`#L234-L238`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/protocol/object.rs#L234-L238)) look the name up in the instance `__dict__` as a `Py\u003cPyStr>` key, which hashes the string data. CPython's `_PyObject_GenericGetAttrWithDict` passes the name object to `PyDict_GetItemRef`, which hashes it with `PyObject_Hash` ([`object.c#L1935-L1967`](https://github.com/python/cpython/blob/v3.15.2/Objects/object.c#L1935-L1967)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nUse the name object as the `__dict__` key when it is not an exact `str`. CPython creates a function's or a builtin instance's `__dict__` lazily and skips the lookup while there is none, so `getattr(lambda: 0, BadHash(\"x\"), 0)` returns `0` there, and the lookup has to skip an empty `__dict__` on such objects to keep that.\n\n\u003c/details>\n\n### 16. `sorted()` and `list.sort()` accept only an `int` instance as `reverse`\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `sorted([1, 2, 3], reverse=None)` | `TypeError: Expected type bool, not NoneType` | `[1, 2, 3]` |\n| `sorted([1, 2, 3], reverse=\"x\")` | `TypeError: Expected type bool, not str` | `[3, 2, 1]` |\n\n`SortOptions` ([`list.rs#L183-L190`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/builtins/list.rs#L183-L190)) binds `reverse` as a Rust `bool`, as in item 6. CPython converts it with `PyObject_IsTrue` ([`listobject.c.h#L267-L270`](https://github.com/python/cpython/blob/v3.15.2/Objects/clinic/listobject.c.h#L267-L270)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nBind `reverse` as an object and convert it by truth value inside `PyList::sort`. Converting it during binding would call `__bool__` before `sorted()` reads its iterable, and CPython reads the iterable first.\n\n\u003c/details>\n\n### 17. Interpreter shutdown imports `threading`\n\n| Command | RustPython | CPython |\n|---|---|---|\n| `-c \"import atexit, sys; atexit.register(lambda: print('threading' in sys.modules))\"` | `True` | `False` |\n| `-c \"import sys; sys.settrace(print)\"` | 408 lines of frames from `importlib._bootstrap` and `threading` | no output |\n\n`finalize_ex` ([`interpreter.rs#L820-L833`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/vm/interpreter.rs#L820-L833)) imports `threading` to call `_shutdown()`, so a program that never imported it runs the import machinery on exit, with a trace function still installed. CPython's `wait_for_thread_shutdown` looks the module up with `PyImport_GetModule` and returns when it was never imported ([`pylifecycle.c#L3843-L3853`](https://github.com/python/cpython/blob/v3.15.2/Python/pylifecycle.c#L3843-L3853)).\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nIn [`finalize_ex`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/vm/src/vm/interpreter.rs#L820-L833), take `threading` from `sys.modules` and skip `_shutdown()` when it is absent.\n\n\u003c/details>\n\n### 18. `compile(..., 'single')` accepts a compound statement followed by an unterminated blank line\n\n| Expression | RustPython | CPython |\n|---|---|---|\n| `compile(\"if x:\\n a\\n \", \"\u003cfoo>\", \"single\")` | `\u003ccode object \u003cmodule> at 0x..., file \"\u003cfoo>\", line 1>` | `SyntaxError: invalid syntax` |\n| `compile(\"if x:\\n a\\n \", \"\u003cfoo>\", \"single\")` | a code object | `IndentationError: unindent does not match any outer indentation level` |\n| `compile(\"if x:\\n a\\n#c\", \"\u003cfoo>\", \"single\")` | a code object | `SyntaxError: invalid syntax` |\n\nWith a newline after the last line (`\"if x:\\n a\\n \\n\"`) both accept the source.\n\n`'single'` input is parsed as a module ([`lib.rs#L2157-L2163`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/compiler/src/lib.rs#L2157-L2163)), where a trailing whitespace-only or comment-only line is blank whether or not a newline ends it, and the single-mode checks ([`#L1903-L1973`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/compiler/src/lib.rs#L1903-L1973), [`#L1248-L1268`](https://github.com/RustPython/RustPython/blob/7abccf0cd6010f5c71b100c1e64435546eed73b0/crates/compiler/src/lib.rs#L1248-L1268)) do not look at it. CPython adds a final newline only to exec input ([`helpers.c#L313-L318`](https://github.com/python/cpython/blob/v3.15.2/Parser/tokenizer/helpers.c#L313-L318)), so in single mode the tokenizer reaches the end of input inside that line, and the parser turns `ENDMARKER` into a `NEWLINE` while the block is still open ([`pegen.c#L270-L278`](https://github.com/python/cpython/blob/v3.15.2/Parser/pegen.c#L270-L278)), which `single_compound_stmt NEWLINE` cannot match.\n\n\u003cdetails>\u003csummary>Fix suggestion\u003c/summary>\n\nAdd a single-mode check for a compound first statement whose source does not end in a line break and whose last line is only indentation or a comment, comparing that line's indentation with the open blocks to pick `invalid syntax`, `unindent does not match any outer indentation level`, `unexpected indent` or `TabError`.\n\n\u003c/details>\n\n## Python Documentation or reference to CPython source code\n\nThe CPython source for each item is linked in the item above (CPython v3.15.2).\n\nAI disclosure: this report was drafted with Claude Code (claude-fable-5-2) and reviewed by me before submission. The divergences come from differential-testing runs against CPython 3.12.13, and every source reference was checked against the linked revisions.","author":{"url":"https://github.com/jseop-lim","@type":"Person","name":"jseop-lim"},"datePublished":"2026-11-17T16:59:20Z","url":"https://github.com/RustPython/RustPython/issues/9913"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ed627a2b-f068-ee16-1557-ca276ef78382 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8660:3A8E64:E3FBAA:12E52F3:6B01D40E |
| html-safe-nonce | dca72e06dc6b6690db7a4346c875e256d6e7abcd559730b81d635b8755da9103 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NjYwOjNBOEU2NDpFM0ZCQUE6MTJFNTJGMzo2QjAxRDQwRSIsInZpc2l0b3JfaWQiOiIyMTI0NjAyNjE1MjUyNzY3NzU4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2d6cc1da83ebbf2f42bd2f3f0068fd067a596c3152a0bb8b5d4089ac4c8906c8 |
| hovercard-subject-tag | issue:6449766688 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/RustPython/RustPython/9913/issue_layout |
| twitter:image | https://opengraph.githubassets.com/15e9c3241efebd2f584cecdee37be3191c53a92a6fbe29f76d3070e36fd8dc1d/RustPython/RustPython/issues/9913 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/15e9c3241efebd2f584cecdee37be3191c53a92a6fbe29f76d3070e36fd8dc1d/RustPython/RustPython/issues/9913 |
| og:image:alt | Feature Found by differential testing against CPython 3.12. All 18 items reproduce on main at 7abccf0 (the outputs below are from that build), and CPython 3.12.13, 3.14.4 and 3.15.2 agree on each, ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jseop-lim |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width