Skip to content

Conversation

@sergey-miryanov
Copy link
Contributor

@sergey-miryanov sergey-miryanov commented Nov 9, 2025

Only integers should be allowed for the start value in longrangeiter.

Two extra checks were added:

  1. The first one checks that setting a float value for __setstate__ for rangeiter fails
  2. The second check does the same for longrangeiter.

I made the error message for longrangeiter similar to that for rangeiter.

@sergey-miryanov
Copy link
Contributor Author

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

Copy link
Member

@serhiy-storchaka serhiy-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 check state? Normally, it can only be int.

Using PyLong_Check() is not enough -- it would pass for an int subclass with overridden __radd__, __mul__, etc. PyLong_CheckExact() is needed.

@sergey-miryanov
Copy link
Contributor Author

Should we change PyLong_Check to PyLong_CheckExact in compute_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);
return NULL;
}
Copy link
Member

@picnixz picnixz Nov 9, 2025

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);
return NULL;
}
if (!PyLong_CheckExact(state)) {
PyErr_Format(PyExc_TypeError, "state must be an int, not %T", state);
return NULL;
}

Copy link
Contributor Author

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 what rangeiter 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.

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

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

rangeiter_setstate() supports int subclasses and classes with the __index__ method. If we want to support them here (__index__ is not currently supported), we should use PyNumber_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 commented Nov 9, 2025

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.

Comment on lines 474 to 476
with self.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 calling setstate.
  • Check that custom int subclasses are rejected. What is the current behavior with range objects not having exact ints?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Range iter give following error messages before last changes:

  1. float: 'float' object cannot be interpreted as an integer
  2. I() (int-like object): no error message
  3. str: 'str' object cannot be interpreted as an integer

{
if (!PyLong_CheckExact(state)) {
PyErr_Format(PyExc_TypeError,
"'%T' object cannot be interpreted as an integer", state);
Copy link
Member

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 an int subclass of has the __index__ method. But this is not needed here, only int 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?

@sergey-miryanov
Copy link
Contributor Author

It is ready for review.
@serhiy-storchaka @picnixz Could you please take a look?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants