-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-125038: Iterator checks are added for some FOR_ITER bytecodes, crash fixed #125051
gh-125038: Iterator checks are added for some FOR_ITER bytecodes, crash fixed #125051
Conversation
Python/bytecodes.c
Outdated
@@ -2804,7 +2804,10 @@ dummy_func( | |||
replaced op(_FOR_ITER, (iter -- iter, next)) { | |||
/* before: [iter]; after: [iter, iter()] *or* [] (and jump over END_FOR.) */ | |||
PyObject *iter_o = PyStackRef_AsPyObjectBorrow(iter); | |||
PyObject *next_o = (*Py_TYPE(iter_o)->tp_iternext)(iter_o); | |||
PyObject *next_o = NULL; | |||
if (PyIter_Check(iter_o)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling PyIter_Check
feels a little wasteful. Instead, I'd assign (*Py_TYPE(iter_o)->tp_iternext)
to a variable and check if it's NULL. If it is NULL, we should raise a TypeError. Your code instead makes it immediately end the loop, which doesn't feel like the right behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I've tried to add a TypeError
Such piece of code is provided:
PyTypeObject *type = Py_TYPE(iter_o);
iternextfunc iternext = type->tp_iternext;
if (iternext == NULL) {
_PyErr_Format(tstate, PyExc_TypeError,
"'for' requires an object with "
"__iter__ method, got %.100s",
type->tp_name);
DECREF_INPUTS();
ERROR_IF(true, error);
}
PyObject *next_o = (*iternext)(iter_o);
And there is a temporarily result of my test case:
-> % ./python -m unittest -v test.test_generators.GeneratorTest.test_issue125038
test_issue125038 (test.test_generators.GeneratorTest.test_issue125038) ... ERROR
======================================================================
ERROR: test_issue125038 (test.test_generators.GeneratorTest.test_issue125038)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/mikhail.efimov/projects/cpython/Lib/test/test_generators.py", line 274, in test_issue125038
l = list(g)
File "/home/mikhail.efimov/projects/cpython/Lib/test/test_generators.py", line 272, in <genexpr>
g = (x for x in range(10))
~~~~~^^^^
TypeError: 'for' requires an object with __iter__ method, got range
----------------------------------------------------------------------
Ran 1 test in 0.001s
It seems like such a message doesn't provide any clarity.
Do you have any better suggestions about error message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems like a pretty good error message, much better than silently stopping the loop.
Lib/test/test_generators.py
Outdated
except TypeError: | ||
return "TypeError" | ||
|
||
# This should not raise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you want with self.assertRaisesRegex
.
SIGSEGV on generators in case of gi_frame.f_locals is fixed. This applies to _FOR_ITER bytecode. Similar checks are added to _FOR_ITER_TIER_TWO and INSTRUMENTED_FOR_ITER bytecode implementations.
44a03a1
to
c353cf1
Compare
We should confirm with @markshannon whether we should fix the interpreter or |
I looked at PEP 668 and it seems to assume that mutating
Yes, that's right; we're adding a NULL check and a branch. However, FOR_ITER has a couple of specialized versions, so the overhead should be minimal when iterating over the most common types. Still, it may be worth benchmarking how bad the overhead is. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks correct to me, but will leave to Mark to see if there's a better solution.
Misc/NEWS.d/next/Core_and_Builtins/2024-10-07-19-26-50.gh-issue-125038.ffSLCz.rst
Outdated
Show resolved
Hide resolved
…e-125038.ffSLCz.rst News improvement Co-authored-by: Jelle Zijlstra <[email protected]>
Thanks for your advice, it definitely looks better now. But I have a question. |
It seems that this code is incorrect now:
But very similar code is correct:
Maybe, some change in framelocalsproxy_setitem function can be provided?
I've provided this change, but it breaks two tests: test_proxy_key_stringlikes_overwrite and test_proxy_key_stringlikes_ftrst_write. Those tests are authored by @encukou and @ncoghlan. |
Setting I would oppose adding special handling in the FrameLocalsProxy that mutates objects; doing so would complicate the implementation and make it less useful for tools like debuggers. |
Actually, there is no real world use case when changing underlying iterator for a generator is needed. Main purpose of my questions is achieving some clarity. |
…cpython into sigsegv_fix_in_iterators
I've moved my test from test_generators.py to test_frame.py, renamed it, and added more test cases to emphasize current code behavior and save it explicitly in tests. |
… section for assignment expression topic (python#125074)
Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: Tomas R <[email protected]>
…re enabled by default (python#123859)
…ython#124657) * pythongh-124612: Use ghcr.io/python/autoconf instead of public image * Update
…ython#124669) Co-authored-by: Alex Waygood <[email protected]>
Co-authored-by: musvaage <[email protected]>
* Replace unicode_compare_eq() with unicode_eq(). * Use unicode_eq() in setobject.c. * Replace _PyUnicode_EQ() with _PyUnicode_Equal(). * Remove unicode_compare_eq() and _PyUnicode_EQ().
…008 implementation issues (python#125151) Skip test_fma_zero_result on NetBSD due to IEE 754-2008 implementation issues
…124974) Now it returns a tuple of up to 100 strings (an empty tuple on most locales). Previously it returned the first item of that tuple or an empty string.
…pire far in the future by default (pythonGH-107594) This allows testing Y2038 with system time set to after that, so that actual Y2038 issues can be exposed, and not masked by expired certificate errors. Signed-off-by: Alexander Kanavin <[email protected]>
See #125178 |
SIGSEGV on generators in case of gi_frame.f_locals is fixed.
This applies to _FOR_ITER bytecode implementation.
Similar checks are added to _FOR_ITER_TIER_TWO and INSTRUMENTED_FOR_ITER bytecode implementations.