)]}'
{
  "commit": "4ad3a3c5d1aaa618200bf54e9a253f59bbc4a6ab",
  "tree": "f00ad99ed2655811086e4feaa4a7714e1faabc55",
  "parents": [
    "dd73c5d190e9cd08f88f9b616c321e10511b81ba"
  ],
  "author": {
    "name": "Jahongir Qurbonov",
    "email": "109198731+Jahongir-Qurbonov@users.noreply.github.com",
    "time": "Fri Nov 20 17:04:04 2026 +0500"
  },
  "committer": {
    "name": "GitHub",
    "email": "noreply@github.com",
    "time": "Fri Nov 20 13:04:04 2026 +0100"
  },
  "message": "[mypyc] Fix crashes and compile errors from the result of native __len__ methods (#22485)\n\nThe `sq_length`/`mp_length` slot of a native class didn\u0027t check the\nvalue returned by `__len__` like CPython\u0027s `slot_sq_length` does, and\nthe compiled `len()` of a native class instance only partly did:\n\n- A negative result from `__len__` declared to return `int` or `Any` was\nreturned from the slot as is, without an exception set. `len(x)` then\nraised `SystemError: \u003cbuilt-in function len\u003e returned NULL without\nsetting an exception` when called from interpreted code, and crashed the\ninterpreter (segfault) in some other cases, such as `len(x)` in compiled\ncode where `x` has type `Any`.\n- `__len__` declared to return `bool` or a native int type (`i64`,\n`i32`, `i16`, `i8`, `u8`, ...) generated `PyLong_AsSsize_t(retval)` with\na C integer `retval`, which is a C compile error (`-Wint-conversion`)\nwith current GCC and clang.\n- With `-\u003e Any`, a result that isn\u0027t an int raised `TypeError: an\ninteger is required`, while Python accepts any object with `__index__`.\n- The compiled `len(x)`, where `x` is a native class instance, checked\nthat the length isn\u0027t negative, but returned a value that doesn\u0027t fit in\n`Py_ssize_t` (such as `2**63`) instead of raising `OverflowError`.\n\n```python\nclass C:\n    def __init__(self, n: int) -\u003e None:\n        self.n \u003d n\n\n    def __len__(self) -\u003e int:\n        return self.n\n\nclass B:\n    def __len__(self) -\u003e bool:  # C compile error\n        return True\n```\n\n```python\n\u003e\u003e\u003e len(C(-1))\nSystemError: \u003cbuilt-in function len\u003e returned NULL without setting an exception\n```\n\nCPython\u0027s `slot_sq_length` converts the result with `__index__`, raises\n`ValueError: __len__() should return \u003e\u003d 0` if it\u0027s negative, and\n`OverflowError: cannot fit \u0027int\u0027 into an index-sized integer` if it\ndoesn\u0027t fit in `Py_ssize_t`.\n\nNow:\n\n- `generate_len_wrapper()` handles a short int inline. Anything else\ngoes through the new `CPy_LenFromResult()` in `generic_ops.c`, which\ndoes what `slot_sq_length` does, after boxing the value if needed (like\n`generate_hash_wrapper()` after #22453).\n- In the compiled `len()` of a native class instance, the length check\nis the new `CPyTagged_CheckLen()` in `CPy.h`. It\u0027s inline for short\nints, and for long ints it also raises `OverflowError` if the value\ndoesn\u0027t fit in `Py_ssize_t`. If `__len__` returns something other than\nan int type (for example `Any`), the result is converted by\n`CPyObject_LenFromResult()` like in the slot.\n\nTests: the new run test `testDundersLenResult` covers `-\u003e int`, `-\u003e\nAny`, `-\u003e bool`, `-\u003e i64`, `-\u003e i8` and `-\u003e u8`, through both the\ncompiled `len()` and the slot (`len()` of an `Any` value), with negative\nvalues, values that don\u0027t fit in `Py_ssize_t`, `True`, an object with\n`__index__` and results that aren\u0027t ints. I updated `testDundersLen` in\n`irbuild-dunders.test` and added `testDundersLenAny`.",
  "tree_diff": [
    {
      "type": "modify",
      "old_id": "92d32937872d98434c60711a4ee2b9d9afc6592f",
      "old_mode": 33188,
      "old_path": "mypyc/codegen/emitwrapper.py",
      "new_id": "707524c61097ad199fcfd33d4864f3673e03531c",
      "new_mode": 33188,
      "new_path": "mypyc/codegen/emitwrapper.py"
    },
    {
      "type": "modify",
      "old_id": "3cd6b5d48b2a5edeb3eab538cda5a58412ce9465",
      "old_mode": 33188,
      "old_path": "mypyc/irbuild/ll_builder.py",
      "new_id": "6cbb786c126c1dedced427479b3066493b1dd1a8",
      "new_mode": 33188,
      "new_path": "mypyc/irbuild/ll_builder.py"
    },
    {
      "type": "modify",
      "old_id": "d9f33959492f4dcc01b231976b20d96f4095ee0e",
      "old_mode": 33188,
      "old_path": "mypyc/lib-rt/CPy.h",
      "new_id": "e0212fc7421272f7d2f3b72b652ae994701b5ffe",
      "new_mode": 33188,
      "new_path": "mypyc/lib-rt/CPy.h"
    },
    {
      "type": "modify",
      "old_id": "d421b53ebf0ada41ee7311416afa9deee92f98a5",
      "old_mode": 33188,
      "old_path": "mypyc/lib-rt/generic_ops.c",
      "new_id": "e267f104c98282330321114a3a952e6aa765b359",
      "new_mode": 33188,
      "new_path": "mypyc/lib-rt/generic_ops.c"
    },
    {
      "type": "modify",
      "old_id": "1355619211f9673b888feaf96fa879a5420cf981",
      "old_mode": 33188,
      "old_path": "mypyc/primitives/generic_ops.py",
      "new_id": "a6a8a1c4ae2c8e18d4033b79c6da702e5d550dfd",
      "new_mode": 33188,
      "new_path": "mypyc/primitives/generic_ops.py"
    },
    {
      "type": "modify",
      "old_id": "1796a7e2160e43fad052a6395820f3c815025370",
      "old_mode": 33188,
      "old_path": "mypyc/test-data/irbuild-dunders.test",
      "new_id": "9e2d34437cf764acb70d3045ce0ca8e5f664a7c2",
      "new_mode": 33188,
      "new_path": "mypyc/test-data/irbuild-dunders.test"
    },
    {
      "type": "modify",
      "old_id": "4f932b684c8da1784e4be73c9e9ffb06075e6565",
      "old_mode": 33188,
      "old_path": "mypyc/test-data/run-dunders.test",
      "new_id": "127cc82a6f933515f5aba5604f85dd2070548e19",
      "new_mode": 33188,
      "new_path": "mypyc/test-data/run-dunders.test"
    }
  ]
}
