mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
gh-81137: deprecate assignment of code object to a function of a mismatched type (#111823)
This commit is contained in:
parent
178861b193
commit
2f9cb7e095
4 changed files with 44 additions and 0 deletions
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue