Title: `itertools.tee()` calls `iter()` before checking `n`, and never calls it on the iterator it buffers · Issue #9512 · RustPython/RustPython · GitHub
Open Graph Title: `itertools.tee()` calls `iter()` before checking `n`, and never calls it on the iterator it buffers · Issue #9512 · RustPython/RustPython
X Title: `itertools.tee()` calls `iter()` before checking `n`, and never calls it on the iterator it buffers · Issue #9512 · RustPython/RustPython
Description: Feature itertools.tee() gets the order and the count of its iter() calls wrong: a negative n is reported only after iter(iterable) succeeds, and an iterator whose own __iter__ is broken is accepted. CPython checks n first, returns () for...
Open Graph Description: Feature itertools.tee() gets the order and the count of its iter() calls wrong: a negative n is reported only after iter(iterable) succeeds, and an iterator whose own __iter__ is broken is accepted...
X Description: Feature itertools.tee() gets the order and the count of its iter() calls wrong: a negative n is reported only after iter(iterable) succeeds, and an iterator whose own __iter__ is broken is accepted...
Opengraph URL: https://github.com/RustPython/RustPython/issues/9512
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`itertools.tee()` calls `iter()` before checking `n`, and never calls it on the iterator it buffers","articleBody":"## Feature\n\n`itertools.tee()` gets the order and the count of its `iter()` calls wrong: a negative `n` is reported only after `iter(iterable)` succeeds, and an iterator whose own `__iter__` is broken is accepted. CPython checks `n` first, returns `()` for `n == 0` without calling `iter()`, and then calls `iter()` once on the iterable and once more on the iterator it gets back.\n\n### Reproduction\n\n```python\nimport itertools\n\n\nclass NoneIter:\n def __iter__(self):\n return None\n\n\nclass BadIterator:\n def __iter__(self):\n return None\n\n def __next__(self):\n return None\n\n\nclass BadIterable:\n def __iter__(self):\n return BadIterator()\n\n\ntry:\n print(itertools.tee(NoneIter(), -1))\nexcept Exception as e:\n print(f\"{type(e).__name__}: {e}\")\ntry:\n print(type(itertools.tee(BadIterable())))\nexcept Exception as e:\n print(f\"{type(e).__name__}: {e}\")\n```\n\n**RustPython:**\n\n```\nTypeError: NoneIter.__iter__() must return an iterator, not NoneType\n\u003cclass 'tuple'>\n```\n\n**CPython:**\n\n```\nValueError: n cannot be negative\nTypeError: BadIterator.__iter__() must return an iterator, not NoneType\n```\n\n`tee(NoneIter(), 0)` raises the same `TypeError` instead of returning `()`, and an iterator that defines `__copy__` has it called by `tee()`, which CPython stopped doing in gh-123884.\n\n### Root cause\n\n[`TeeArgs`](https://github.com/RustPython/RustPython/blob/713e14cfc78d06372c68a1804240de52a8340548/crates/vm/src/stdlib/itertools.rs#L1138-L1144) declares `iterable` as `PyIter`, so `iter()` runs during argument binding, before [`tee`](https://github.com/RustPython/RustPython/blob/713e14cfc78d06372c68a1804240de52a8340548/crates/vm/src/stdlib/itertools.rs#L1146-L1170) checks `n`. `tee` then copies an iterator with `__copy__` directly, and [`PyItertoolsTee::from_iter`](https://github.com/RustPython/RustPython/blob/713e14cfc78d06372c68a1804240de52a8340548/crates/vm/src/stdlib/itertools.rs#L1107-L1120) stores any other iterator without calling `iter()` on it, where CPython's `tee_fromiterable` calls `PyObject_GetIter` on the iterator it is given.\n\n### Fix\n\nBind `iterable` as `PyObjectRef`, return an empty tuple for `n == 0`, call `get_iter` only after that, drop the `__copy__` shortcut, and call `get_iter` again at the top of `from_iter`.\n\n### Environment\n\n- RustPython [713e14c](https://github.com/RustPython/RustPython/tree/713e14cfc78d06372c68a1804240de52a8340548) (Python 3.15.0a), also reproduced on 0.4.0 and 0.6.1\n- CPython v3.15.2, also v3.14.4 and v3.12.13 (those say `n must be >= 0` and `iter() returned non-iterator of type 'NoneType'`)\n- OS: macOS arm64 (713e14c), Debian 12 (0.4.0, 0.6.1)\n\n## Python Documentation or reference to CPython source code\n\n- CPython `itertools_tee_impl`: [`Modules/itertoolsmodule.c#L1161-L1179`](https://github.com/python/cpython/blob/v3.15.2/Modules/itertoolsmodule.c#L1161-L1179) returns for `n == 0` before calling `iter()`, after the generated wrapper has rejected a negative `n` ([`Modules/clinic/itertoolsmodule.c.h#L348-L352`](https://github.com/python/cpython/blob/v3.15.2/Modules/clinic/itertoolsmodule.c.h#L348-L352)):\n\n ```c\n result = PyTuple_New(n);\n if (result == NULL)\n return NULL;\n if (n == 0)\n return result;\n it = PyObject_GetIter(iterable);\n ```\n\n- CPython `tee_fromiterable`: [`Modules/itertoolsmodule.c#L1051-L1058`](https://github.com/python/cpython/blob/v3.15.2/Modules/itertoolsmodule.c#L1051-L1058) calls `iter()` again on the iterator `tee()` passes it:\n\n ```c\n it = PyObject_GetIter(iterable);\n if (it == NULL)\n return NULL;\n ```\n\n- Documentation: [`itertools.tee`](https://docs.python.org/3.15/library/itertools.html#itertools.tee)\n\nAI disclosure: this report was drafted with Claude Code (claude-opus-5-5) and reviewed by me before submission. The divergence comes from a differential-testing run against CPython 3.12.13, and every source reference above was checked against the linked revisions.\n","author":{"url":"https://github.com/jseop-lim","@type":"Person","name":"jseop-lim"},"datePublished":"2026-11-01T04:00:29Z","url":"https://github.com/RustPython/RustPython/issues/9512"}
| 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:4dcf009a-780a-1f7a-8584-cd5c3f54764f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9A32:E0ADF:F972E9:13BB066:6B01F726 |
| html-safe-nonce | 41796165eafc282b18f4651e542abdd910f78533082faa2561f5721d57fcdac9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QTMyOkUwQURGOkY5NzJFOToxM0JCMDY2OjZCMDFGNzI2IiwidmlzaXRvcl9pZCI6Ijg2NDgxNjYxNjMwOTkxMzM4MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | af6e5d75b2108688be760596a2d808aeadbc33b958b4e3491e8d87ad28b1808b |
| hovercard-subject-tag | issue:6143004632 |
| 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/9512/issue_layout |
| twitter:image | https://opengraph.githubassets.com/30d8e11ca446c8b0edb9d267ecb9799a01fa6075dea1f0c525a1224bdb3a39ac/RustPython/RustPython/issues/9512 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/30d8e11ca446c8b0edb9d267ecb9799a01fa6075dea1f0c525a1224bdb3a39ac/RustPython/RustPython/issues/9512 |
| og:image:alt | Feature itertools.tee() gets the order and the count of its iter() calls wrong: a negative n is reported only after iter(iterable) succeeds, and an iterator whose own __iter__ is broken is accepted... |
| 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 | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width