Skip to content

Conversation

@priyanshu2282-cyber
Copy link

@priyanshu2282-cyber priyanshu2282-cyber commented Jan 13, 2026

__index__ can change the array’s class during multiplication. After that,
the old function is still called, which can cause a crash. This adds a
check to avoid that and raises TypeError instead.

A regression test is also added.

@bedevere-app

This comment was marked as resolved.

@bedevere-app

This comment was marked as resolved.

priyanshu2282-cyber and others added 2 commits January 13, 2026 19:52
Co-authored-by: AN Long <aisk@users.noreply.github.com>
Co-authored-by: AN Long <aisk@users.noreply.github.com>
@priyanshu2282-cyber
Copy link
Author

Thanks, fixed.

@@ -9716,13 +9714,19 @@ wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
ssizeargfunc func = (ssizeargfunc)wrapped;
PyObject* o;
Py_ssize_t i;
PyTypeObject *type_before = Py_TYPE(self);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not very sure about this, but I'm wondering should we increase the refcount for type_before, because we will call user's code next, thus it may be GCed (like user delete the type in __index__)?

Choose a reason for hiding this comment

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

Yes, that makes sense. Since we call user code next, the type could indeed be GCed if its last reference is dropped. I’ve updated the code to INCREF type_before before calling user code and DECREF it on all exit paths. I also ran the regression test locally and it passes.

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

Why do we need to make those changes in typeobject? is it only relevant for objects implementing __mul__? or is it relevant only to array.__mul__?

self.assertRaises(TypeError, op, v, z)


class IndexMutationDuringNumericOpTest(unittest.TestCase):
Copy link
Member

Choose a reason for hiding this comment

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

Why is it in the numeric tower??? it has nothing to do with numeric tower.

@bedevere-app
Copy link

bedevere-app bot commented Jan 13, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

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