Repository navigation
gh-159090: Handle deleted StopIteration values in generators - #159107
Open
Shoryamishra61 wants to merge 1 commit into
Open
Shoryamishra61 wants to merge 1 commit into
Shoryamishra61 wants to merge 1 commit into
Conversation
Safely handle cases where StopIteration.value is deleted or NULL in _PyGen_FetchStopIterationValue and the CLEANUP_THROW bytecode instruction.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes gh-159090
Summary
Fixes a crash when a generator or
yield fromexpression receives aStopIterationinstance whosevalueattribute was deleted.Root cause
When
del exc.valueis executed on aStopIterationinstance,_Py_T_OBJECTmember handling clears the C field((PyStopIterationObject *)exc)->valuetoNULL. Python attribute lookupexc.valuesubstitutesNone, but two internal C consumers dereferenced the field unconditionally:_PyGen_FetchStopIterationValueinObjects/genobject.ccalledPy_NewRef(NULL)before reaching theif (value == NULL)fallback.CLEANUP_THROWinPython/bytecodes.ccalledPyStackRef_FromPyObjectNew(NULL).Fix
_PyGen_FetchStopIterationValue(Objects/genobject.c), usePy_XNewRef(...)so that aNULLfield returnsNULLand triggers the existing fallbackif (value == NULL) value = Py_NewRef(Py_None).CLEANUP_THROW(Python/bytecodes.c), check ifexc_value->valueis non-NULL before creating a stack reference; ifNULL, usePyStackRef_None.make regen-cases.Tests
Lib/test/test_yield_from.pycoveringyield fromiteration andthrow()handling with deletedStopIteration.value, including customStopIterationsubclasses.test_yield_from,test_generators,test_exceptions, andtest_coroutines(322 tests passing).test_yield_fromandtest_generatorswith-R 3:3(0 leaks).Tools/patchcheck/patchcheck.py.Authored with AI assistance and reviewed by human contributor Shoryamishra61.