Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-128198: Add missing error checks for usages ofPyIter_Next#128199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@WolframAlph
Copy link
Contributor

@WolframAlphWolframAlph commentedDec 23, 2024
edited by bedevere-appbot
Loading

@WolframAlphWolframAlphforce-pushed theWolframAlph/fix-pyiternext branch fromca54407 toc6d319fCompareDecember 23, 2024 16:33
@rhettingerrhettinger removed their request for reviewDecember 23, 2024 18:01
Copy link
Member

@ZeroIntensityZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

In 3.14, we have a newPyIter_NextItem for exactly this problem. I think it's worth migrating to that in this PR instead of trying to adapt to the bad design choices ofPyIter_Next.

@WolframAlph
Copy link
ContributorAuthor

👍 Will check it out and update the PR

@WolframAlph
Copy link
ContributorAuthor

@ZeroIntensity I can seePyIter_NextItem being used only in couple places. Should I rename the issue to indicate migration from oldPyIter_Next to newPyIter_NextItem api and work on replacing old one in the entire codebase?

@ZeroIntensity
Copy link
Member

I don't think we need to replacePyIter_Next throughout theentire codebase (though it is probably inevitable), just change it for the cases that are problematic.

Should I rename the issue to indicate migration from old PyIter_Next

Nah, just update the PR title.

@WolframAlph
Copy link
ContributorAuthor

WolframAlph commentedDec 24, 2024
edited
Loading

@ZeroIntensity so if this is inevitable (otherwise why would newPyIter_NextItem be implemented) and I can see ~63 instances ofPyIter_Next usage in the codebase, I would like to spend time on migrating to the new version in this PR. Or at least those instances that do not require significant rewrite and don't introduce more complexity. Any objections from your side? Or if this is still too much for a single PR, maybe create top level issue to track migration and add separate issues for migration in each file where usages occur?

picnixz
picnixz previously requested changesDec 24, 2024
@picnixzpicnixz changed the titlegh-128198: Add missing error checks forPyIter_Nextgh-128198: Add missing error checks for usages ofPyIter_NextDec 24, 2024
@serhiy-storchaka
Copy link
Member

If older Python versions have the same code, it will be needed to backport these changes. In such case it is better to use the older API, unless you want to add a work for yourself and for reviewers.

@ZeroIntensity
Copy link
Member

Ah, right. I would be in favor of a follow-up PR to switch toPyIter_NextItem though.

Copy link
Member

@picnixzpicnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM (I'll just run the refleaks build bots since the previous run was interrupted, just in case)

@picnixzpicnixz added the 🔨 test-with-refleak-buildbotsTest PR w/ refleak buildbots; report in status section labelDec 25, 2024
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by@picnixz for commit3aa46e7 🤖

If you want to schedule another build, you need to add the🔨 test-with-refleak-buildbots label again.

@bedevere-botbedevere-bot removed the 🔨 test-with-refleak-buildbotsTest PR w/ refleak buildbots; report in status section labelDec 25, 2024
@WolframAlph
Copy link
ContributorAuthor

Regarding migration toPyIter_NextItem, can I create top level issue and tag you guys?@serhiy-storchaka@picnixz@ZeroIntensity ?

@picnixz
Copy link
Member

Yes, you can just request my review. But before creating the issue, you should locally verify that:

  • the changes would not impact performances (I don't know which one is faster); try to check ifPyIter_Next is actually faster overall or if it'sPyIter_NextItem).
  • some parts of the code directly usetp_iternext under some assumptions and for performance reasons. Those parts should not be modified.
  • locally investigate how you want to split the task. Changing a lot of places may or may not be easy to review. There are more than 50 usages my IDE found so maybe splitting it up by file would be better.
  • let's first focus on parts that don't need much rewrite; it's fine if some parts are still using the old API though.

FTR, changing the usages could perhaps make future backports harder, so I don't know whether it's worth the shot.

@WolframAlph
Copy link
ContributorAuthor

WolframAlph commentedDec 25, 2024
edited
Loading

locally investigate how you want to split the task. Changing a lot of places may or may not be easy to review. There are more than 50 usages my IDE found so maybe splitting it up by file would be better.

Yes, my initial thought was to create top level issue to track all sub issues for each file separately to make review process easier.

FTR, changing the usages could perhaps make future backports harder, so I don't know whether it's worth the shot.

I presume this is not first time of migrating to newer API. How were those cases handled?

@WolframAlph
Copy link
ContributorAuthor

One more question. When comparing code forPyIter_Next:

cpython/Objects/abstract.c

Lines 2914 to 2919 ind9ed42b

PyIter_Next(PyObject*iter)
{
PyObject*item;
(void)iternext(iter,&item);
returnitem;
}

andPyIter_NextItem:

cpython/Objects/abstract.c

Lines 2891 to 2903 ind9ed42b

PyIter_NextItem(PyObject*iter,PyObject**item)
{
assert(iter!=NULL);
assert(item!=NULL);
if (Py_TYPE(iter)->tp_iternext==NULL) {
*item=NULL;
PyErr_Format(PyExc_TypeError,"expected an iterator, got '%T'",iter);
return-1;
}
returniternext(iter,item);
}

I can see that latter hasPy_TYPE(iter)->tp_iternext == NULL check while former does not. Is it intentional? If so - what is the reason?

@picnixz
Copy link
Member

Yes it's intentional. Docs (https://docs.python.org/3/c-api/iter.html#c.PyIter_Next) say:

The object must be an iterator according toPyIter_Check() (it is up to the caller to check this).

The reason is to make the call more efficient when the caller knows that the object is a correct iterator. OTOHPyIter_NextItem makes the check for the caller (ifPy_TYPE is a function call, this would add one function call and one pointer comparison; on incorrect inputs, we don't care about performances).

I don't know how less (or more?) efficient it would be but it might not necessarily by much. If you're really interested in preserving performance, we could first glance at the assembly and benchmarks some simple statements, though I suspect that naive benchmarks would hide too much noise (but maybe not?).

Now, if the caller is checking the input toPyIter_Next, then you can maybe remove that check and directly usePyIter_NextItem instead (in the end, you'll move the cost of the check toPyIter_NextItem, though it will cost a function call (I assume that the checks are actually inlined checks but maybe not)).

I presume this is not first time of migrating to newer API. How were those cases handled?

I don't know, I only became active with CPython in June :')

@serhiy-storchaka
Copy link
Member

I presume this is not first time of migrating to newer API. How were those cases handled?

We migrate to newer API if it is faster, safer or more convenient. Or if there are plans to remove the older API.

There are no plans to removePyIter_Next(), it is not broken. In theory, it may be marginally faster because it does not need to callPyErr_Occurred(), but on other hand, additional checkPy_TYPE(iter)->tp_iternext == NULL can make it slower.

I would recommend to use a newer API in a new code if it makes the code clearer, and do not touch existing code (unless you have other reasons to rewrite it).

WolframAlph reacted with thumbs up emoji

@serhiy-storchakaserhiy-storchaka merged commit5c814c8 intopython:mainDec 25, 2024
59 checks passed
@serhiy-storchakaserhiy-storchaka added needs backport to 3.12only security fixes needs backport to 3.13bugs and security fixes labelsDec 25, 2024
@miss-islington-app
Copy link

Thanks@WolframAlph for the PR, and@serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app
Copy link

Thanks@WolframAlph for the PR, and@serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry,@WolframAlph and@serhiy-storchaka, I could not cleanly backport this to3.13 due to a conflict.
Please backport usingcherry_picker on command line.

cherry_picker 5c814c83cdd3dc42bd9682106ffb7ade7ce6b5b3 3.13

@miss-islington-app
Copy link

Sorry,@WolframAlph and@serhiy-storchaka, I could not cleanly backport this to3.12 due to a conflict.
Please backport usingcherry_picker on command line.

cherry_picker 5c814c83cdd3dc42bd9682106ffb7ade7ce6b5b3 3.12

serhiy-storchaka pushed a commit to serhiy-storchaka/cpython that referenced this pull requestDec 26, 2024
…_Next() (pythonGH-128199)(cherry picked from commit5c814c8)Co-authored-by: Yan Yanchii <yyanchiy@gmail.com>
@bedevere-app
Copy link

GH-128272 is a backport of this pull request to the3.13 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.13bugs and security fixes labelDec 26, 2024
serhiy-storchaka pushed a commit to serhiy-storchaka/cpython that referenced this pull requestDec 26, 2024
…_Next() (pythonGH-128199)(cherry picked from commit5c814c8)Co-authored-by: Yan Yanchii <yyanchiy@gmail.com>
@bedevere-app
Copy link

GH-128273 is a backport of this pull request to the3.12 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.12only security fixes labelDec 26, 2024
serhiy-storchaka added a commit that referenced this pull requestDec 26, 2024
GH-128199) (GH-128273)(cherry picked from commit5c814c8)Co-authored-by: Yan Yanchii <yyanchiy@gmail.com>
serhiy-storchaka added a commit that referenced this pull requestDec 26, 2024
GH-128199) (GH-128272)(cherry picked from commit5c814c8)Co-authored-by: Yan Yanchii <yyanchiy@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@ZeroIntensityZeroIntensityZeroIntensity left review comments

@serhiy-storchakaserhiy-storchakaserhiy-storchaka approved these changes

@picnixzpicnixzpicnixz approved these changes

@markshannonmarkshannonAwaiting requested review from markshannonmarkshannon is a code owner

@1st11st1Awaiting requested review from 1st11st1 is a code owner

@asvetlovasvetlovAwaiting requested review from asvetlovasvetlov is a code owner

@kumaraditya303kumaraditya303Awaiting requested review from kumaraditya303kumaraditya303 is a code owner

@willingcwillingcAwaiting requested review from willingcwillingc is a code owner

Labels

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

5 participants

@WolframAlph@ZeroIntensity@serhiy-storchaka@bedevere-bot@picnixz

[8]ページ先頭

©2009-2025 Movatter.jp