René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:4dcf009a-780a-1f7a-8584-cd5c3f54764f
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9A32:E0ADF:F972E9:13BB066:6B01F726
html-safe-nonce41796165eafc282b18f4651e542abdd910f78533082faa2561f5721d57fcdac9
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QTMyOkUwQURGOkY5NzJFOToxM0JCMDY2OjZCMDFGNzI2IiwidmlzaXRvcl9pZCI6Ijg2NDgxNjYxNjMwOTkxMzM4MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacaf6e5d75b2108688be760596a2d808aeadbc33b958b4e3491e8d87ad28b1808b
hovercard-subject-tagissue:6143004632
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/RustPython/RustPython/9512/issue_layout
twitter:imagehttps://opengraph.githubassets.com/30d8e11ca446c8b0edb9d267ecb9799a01fa6075dea1f0c525a1224bdb3a39ac/RustPython/RustPython/issues/9512
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/30d8e11ca446c8b0edb9d267ecb9799a01fa6075dea1f0c525a1224bdb3a39ac/RustPython/RustPython/issues/9512
og:image:altFeature 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamejseop-lim
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
go-importgithub.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git
octolytics-dimension-user_id39710557
octolytics-dimension-user_loginRustPython
octolytics-dimension-repository_id135201145
octolytics-dimension-repository_nwoRustPython/RustPython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id135201145
octolytics-dimension-repository_network_root_nwoRustPython/RustPython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/RustPython/RustPython/issues/9512#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FRustPython%2FRustPython%2Fissues%2F9512
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FRustPython%2FRustPython%2Fissues%2F9512
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=RustPython%2FRustPython
Reloadhttps://github.com/RustPython/RustPython/issues/9512
Reloadhttps://github.com/RustPython/RustPython/issues/9512
Reloadhttps://github.com/RustPython/RustPython/issues/9512
RustPythonhttps://github.com/RustPython
RustPythonhttps://github.com/RustPython/RustPython
Codehttps://github.com/RustPython/RustPython
Issues195 (195)https://github.com/RustPython/RustPython/issues
Pull requests133 (133)https://github.com/RustPython/RustPython/pulls
Discussionshttps://github.com/RustPython/RustPython/discussions
Actionshttps://github.com/RustPython/RustPython/actions
Projectshttps://github.com/RustPython/RustPython/projects
Wikihttps://github.com/RustPython/RustPython/wiki
Security and qualityhttps://github.com/RustPython/RustPython/security
Insightshttps://github.com/RustPython/RustPython/insights
https://github.com/RustPython/RustPython
New issuehttps://github.com/login?return_to=https://github.com/RustPython/RustPython/issues/9512
New issuehttps://github.com/login?return_to=https://github.com/RustPython/RustPython/issues/9512
#9518https://github.com/RustPython/RustPython/pull/9518
itertools.tee() calls iter() before checking n, and never calls it on the iterator it buffershttps://github.com/RustPython/RustPython/issues/9512#top
#9518https://github.com/RustPython/RustPython/pull/9518
C-compatA discrepancy between RustPython and CPythonhttps://github.com/RustPython/RustPython/issues?q=state%3Aopen%20label%3A%22C-compat%22
https://github.com/jseop-lim
jseop-limhttps://github.com/jseop-lim
on Nov 1, 2026https://github.com/RustPython/RustPython/issues/9512#issue-6143004632
TeeArgshttps://github.com/RustPython/RustPython/blob/713e14cfc78d06372c68a1804240de52a8340548/crates/vm/src/stdlib/itertools.rs#L1138-L1144
teehttps://github.com/RustPython/RustPython/blob/713e14cfc78d06372c68a1804240de52a8340548/crates/vm/src/stdlib/itertools.rs#L1146-L1170
PyItertoolsTee::from_iterhttps://github.com/RustPython/RustPython/blob/713e14cfc78d06372c68a1804240de52a8340548/crates/vm/src/stdlib/itertools.rs#L1107-L1120
713e14chttps://github.com/RustPython/RustPython/tree/713e14cfc78d06372c68a1804240de52a8340548
713e14chttps://github.com/RustPython/RustPython/commit/713e14cfc78d06372c68a1804240de52a8340548
Modules/itertoolsmodule.c#L1161-L1179https://github.com/python/cpython/blob/v3.15.2/Modules/itertoolsmodule.c#L1161-L1179
Modules/clinic/itertoolsmodule.c.h#L348-L352https://github.com/python/cpython/blob/v3.15.2/Modules/clinic/itertoolsmodule.c.h#L348-L352
Modules/itertoolsmodule.c#L1051-L1058https://github.com/python/cpython/blob/v3.15.2/Modules/itertoolsmodule.c#L1051-L1058
itertools.teehttps://docs.python.org/3.15/library/itertools.html#itertools.tee
jseop-limhttps://github.com/jseop-lim
C-compatA discrepancy between RustPython and CPythonhttps://github.com/RustPython/RustPython/issues?q=state%3Aopen%20label%3A%22C-compat%22
on Nov 1, 2026https://github.com/RustPython/RustPython/issues/9512#event-34812879495
youknowonehttps://github.com/youknowone
completedhttps://github.com/RustPython/RustPython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#9518https://github.com/RustPython/RustPython/pull/9518
on Nov 1, 2026https://github.com/RustPython/RustPython/issues/9512#event-34823162226
Sign up for freehttps://github.com/signup?return_to=https://github.com/RustPython/RustPython/issues/9512
Sign in to commenthttps://github.com/login?return_to=https://github.com/RustPython/RustPython/issues/9512
C-compatA discrepancy between RustPython and CPythonhttps://github.com/RustPython/RustPython/issues?q=state%3Aopen%20label%3A%22C-compat%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.