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-141312: Allow only integers to longrangeiter_setstate state#141317

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

Open
sergey-miryanov wants to merge2 commits intopython:main
base:main
Choose a base branch
Loading
fromsergey-miryanov:141312-longrange-setstate-float

Conversation

@sergey-miryanov
Copy link
Contributor

@sergey-miryanovsergey-miryanov commentedNov 9, 2025
edited by bedevere-appbot
Loading

Only integers should be allowed for the start value inlongrangeiter.

Two extra checks were added:

  1. The first one checks that setting a float value for__setstate__ forrangeiter fails
  2. The second check does the same forlongrangeiter.

I made the error message forlongrangeiter similar to that forrangeiter.

@sergey-miryanov
Copy link
ContributorAuthor

@serhiy-storchaka Could you please take a look?
I'm not sure should we add a news entry here.

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Why not simply checkstate? Normally, it can only beint.

UsingPyLong_Check() is not enough -- it would pass for anint subclass with overridden__radd__,__mul__, etc.PyLong_CheckExact() is needed.

@sergey-miryanov
Copy link
ContributorAuthor

Should we changePyLong_Check toPyLong_CheckExact incompute_range_length then?

Comment on lines +1045 to +1049
if (!PyLong_CheckExact(state)) {
PyErr_Format(PyExc_TypeError,
"'%T' object cannot be interpreted as an integer",state);
returnNULL;
}
Copy link
Member

@picnixzpicnixzNov 9, 2025
edited
Loading

Choose a reason for hiding this comment

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

Suggested change
if (!PyLong_CheckExact(state)) {
PyErr_Format(PyExc_TypeError,
"'%T' object cannot be interpreted as an integer",state);
returnNULL;
}
if (!PyLong_CheckExact(state)) {
PyErr_Format(PyExc_TypeError,"state must be an int, not %T",state);
returnNULL;
}

serhiy-storchaka reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

This is exactly what I added before checking whatrangeiter returns for a wrong state. Is it ok if our messages will be different?

Copy link
Member

Choose a reason for hiding this comment

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

Oh so that is the message of rangeiter. Ok keep it the same.

Choose a reason for hiding this comment

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

The proposed message is better.

Choose a reason for hiding this comment

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

rangeiter_setstate() supportsint subclasses and classes with the__index__ method. If we want to support them here (__index__ is not currently supported), we should usePyNumber_Index().

But there is no good reason to do this. It would only complicate the code without adding any benefit.

@picnixz
Copy link
Member

picnixz commentedNov 9, 2025
edited
Loading

I'm not sure should we add a news entry here.

Yes we should. It's a bug fix. The rule is that most bugfixes have NEWS.

sergey-miryanov reacted with thumbs up emoji

Comment on lines +474 to +476
withself.assertRaisesRegex(TypeError,msg):
it=iter(range(10,100,2))
it.__setstate__(1.0)
Copy link
Member

Choose a reason for hiding this comment

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

  • Make a separate test method for setstate failures.
  • Only assert the exception when callingsetstate.
  • Check that custom int subclasses are rejected. What is the current behavior with range objects not having exact ints?

sergey-miryanov reacted with thumbs up emoji
{
if (!PyLong_CheckExact(state)) {
PyErr_Format(PyExc_TypeError,
"'%T' object cannot be interpreted as an integer",state);

Choose a reason for hiding this comment

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

Such error messages is usually used when the argument can be anint subclass of has the__index__ method. But this is not needed here, onlyint is supported.

Copy link
Member

Choose a reason for hiding this comment

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

See my discussion here:#141317 (comment). Should we change rangeiter to match messages?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka left review comments

@picnixzpicnixzpicnixz left review comments

@efimov-mikhailefimov-mikhailAwaiting requested review from efimov-mikhail

@rhettingerrhettingerAwaiting requested review from rhettinger

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@sergey-miryanov@picnixz@serhiy-storchaka

[8]ページ先頭

©2009-2025 Movatter.jp