Fix DeprecationWarning while spying on generator functions.
Review Request #14574 — Created Aug. 28, 2025 and updated
Python 3.13 added a new
DeprecationWarning
which is triggered when
callingsetattr()
to replace the code object of a function in the case
that the code object's flags have a mismatch on theCO_GENERATOR
,
CO_COROUTINE
, orCO_ASYNC_GENERATOR
bits. This was done in response
to a bug (https://github.com/python/cpython/issues/81137) where
replacing a lambda's code object with thegi_code
attribute from a
generator expression would then cause cpython to crash. This was purely
a problem with the way thatgi_code
works for generator expressions
(and comprehensions), and does not affect generator functions in the
same way. This means that rather than posing a real issue for kgb, we
just see ugly warnings.In order to prevent these warnings, we now copy true values for those
three flags from the old code object into the new one.
- Commented out the fix and saw the new unit test fail.
- Ran unit tests and saw no more warnings.
- Tested with an experimental test (not included here) that replaces an
async generator and saw that that worked too. - Ran tests on Python 3.8-3.13
- Testing Done:
-
- Commented out the fix and saw the new unit test fail.
- Ran unit tests and saw no more warnings.
- Tested with an experimental test (not included here) that replaces an
async generator and saw that that worked too.
+ - Ran tests on Python 3.8-3.13
- Commits:
-
Summary ID zqxoklumwxzuwspwlvnwwrqmomxznppt nlputqsrwlmlwnrnzwsrzqzxqqxwoqpm - Diff:
-
Revision 2 (+122 -6)