Skip to content

Conversation

ZeroIntensity
Copy link
Member

@ZeroIntensity ZeroIntensity commented Sep 3, 2025

Raise an exception if __typing_subst__ returns a non-tuple object.

The test case is a little messy because it's based on fuzzer-generated code, so I can come up with my own repro if people aren't happy with using it.

@@ -525,6 +525,15 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
return NULL;
}
if (unpack) {
if (!PyTuple_Check(arg)) {
Copy link
Member

Choose a reason for hiding this comment

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

Do we care about tuple subclasses here? If so, do we want to test this? Or use PyTuple_CheckExact?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it would be fine to do either. It would technically be a breaking change to disallow tuple subclasses here, but I doubt anyone is actually doing that in practice.

Copy link
Member

Choose a reason for hiding this comment

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

It's an internal undocumented API so I wouldn't feel terrible about disallowing tuple subclasses, but we could go either way.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's just use PyTuple_Check for the time being. There's no drawback to keeping subclass support here, so we might as well keep it.

"expected a tuple, not %T", arg);
Py_DECREF(arg);
return NULL;
}
Copy link
Member

Choose a reason for hiding this comment

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

Can't comment there but are we leaking newargs on line 543 below?

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like it. Do you want me to fix that in this PR?

Copy link
Member

Choose a reason for hiding this comment

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

Sure, might as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@AlexWaygood AlexWaygood removed their request for review September 6, 2025 07:41
jarg = tuple_extend(&newargs, jarg,
&PyTuple_GET_ITEM(arg, 0), PyTuple_GET_SIZE(arg));
Py_DECREF(arg);
if (jarg < 0) {
Py_DECREF(item);
Py_XDECREF(tuple_args);
Py_DECREF(newargs);
Copy link
Member

Choose a reason for hiding this comment

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

It is NULL here.

Copy link
Member Author

@ZeroIntensity ZeroIntensity Sep 6, 2025

Choose a reason for hiding this comment

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

Ah, tuple_extend steals the reference on failure. I added a comment here instead. Thanks for clearing this up.

Py_DECREF(item);
Py_XDECREF(tuple_args);
PyErr_Format(PyExc_TypeError,
"expected a tuple, not %T", arg);
Copy link
Member

Choose a reason for hiding this comment

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

This error message is not helpful. Please add what method of what type has been called (this was method __typing_subst__ of original argument PyTuple_GET_ITEM(args, iarg)).

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, it's now expected __typing_subst__ of %T objects to return a tuple, not %T. Is that good?

ZeroIntensity and others added 2 commits September 6, 2025 10:18
Co-authored-by: Serhiy Storchaka <[email protected]>
Also fix incorrect DECREF.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants