gh-81137: deprecate assignment of code object to a function of a mismatched type (#111823)

This commit is contained in:
Irit Katriel 2023-11-07 18:54:36 +00:00 committed by GitHub
parent 178861b193
commit 2f9cb7e095
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 0 deletions

View file

@ -2,6 +2,7 @@ import textwrap
import types
import typing
import unittest
import warnings
def global_function():
@ -70,6 +71,27 @@ class FunctionPropertiesTest(FuncAttrsTest):
test.__code__ = self.b.__code__
self.assertEqual(test(), 3) # self.b always returns 3, arbitrarily
def test_invalid___code___assignment(self):
def A(): pass
def B(): yield
async def C(): yield
async def D(x): await x
for src in [A, B, C, D]:
for dst in [A, B, C, D]:
if src == dst:
continue
assert src.__code__.co_flags != dst.__code__.co_flags
prev = dst.__code__
try:
with self.assertWarnsRegex(DeprecationWarning, 'code object of non-matching type'):
dst.__code__ = src.__code__
finally:
with warnings.catch_warnings():
warnings.filterwarnings('ignore', '', DeprecationWarning)
dst.__code__ = prev
def test___globals__(self):
self.assertIs(self.b.__globals__, globals())
self.cannot_set_attr(self.b, '__globals__', 2,