Repository navigation
Conversation
| "repeat argument cannot be negative"); | ||
| return NULL; | ||
| } | ||
| if (repeat < 0) { |
There was a problem hiding this comment.
Lets move this check into AC. Py_ssize_t converter has an allow_negative option.
repeat: Py_ssize_t(allow_negative=False) = 1
There was a problem hiding this comment.
Updated to use allow_negative. Now raises ValueError: repeat cannot be negative instead of ValueError: repeat argument cannot be negative for negative repeat, added in PR description.
| for (i=0; i < tuplesize; i++) { | ||
| PyObject *item = PyTuple_GET_ITEM(args, i); | ||
| PyObject *item = iterables[i]; | ||
| PyObject *it = PyObject_GetIter(item); |
There was a problem hiding this comment.
this can run arbitrary code, need to make sure to turn fillvalue into a strong reference from a borrowed reference:
import functools, itertools
class Evil:
def __iter__(self):
p.keywords.clear() # drops the only ref to fillvalue
return iter([1])
p = functools.partial(itertools.zip_longest, fillvalue=object())
list(p(Evil(), [1, 2, 3]))There was a problem hiding this comment.
Added strong reference of fillvalue; route early returns through error; release fillvalue, ittuple, and result at error.
There was a problem hiding this comment.
@rhettinger: ^ See the code above which segfaults without the additional reference counting
(functools.partial also has refcount bugs, which is what makes this visible in python. Generally it should be only visible in C API calling C API)
arbitrary __iter__()
|
There is redundant refcounting. Again, the strong reference seems necessary. We don't do it elsewhere and there are no tests to prove that it is needed. The other itertools vectorcall PR also changes the clinic checksum, so this one will need to be updated once the other PR goes in. |
|
The extra reference counting is needed. This is the "Thin Ice" (https://docs.python.org/3/extending/extending.html#thin-ice) case in the docs. The other AC Vectorcall conversions which can invoke arbitrary python also make sure to take strong references before invoking arbitray python that could clear the borrowewd references. |
Replace hand-written constructors with AC + vectorcall.
product(..., repeat=1, bad='a')now raisesTypeError: got an unexpected keyword argument 'bad'instead ofTypeError: takes at most 1 keyword argument (2 given);product(..., repeat=-1) now raisesValueError: repeat cannot be negativeinstead ofValueError: repeat argument cannot be negative`.zip_longest(..., bad='a')now raisesTypeError: zip_longest() got an unexpected keyword argument 'bad'instead ofTypeError: zip_longest() got an unexpected keyword argument. Other error text for bad argument is unchanged.Benchmark:
Details