Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
iritkatriel merged 2 commits intopython:mainfromiritkatriel:gh-81137
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletionsDoc/whatsnew/3.13.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -401,6 +401,12 @@ Deprecated
(as has always been the case for an executing frame).
(Contributed by Irit Katriel in :gh:`79932`.)

* Assignment to a function's ``__code__`` attribute where the new code
object's type does not match the function's type, is deprecated. The
different types are: plain function, generator, async generator and
coroutine.
(Contributed by Irit Katriel in :gh:`81137`.)


Pending Removal in Python 3.14
------------------------------
Expand Down
22 changes: 22 additions & 0 deletionsLib/test/test_funcattrs.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,7 @@
importtypes
importtyping
importunittest
importwarnings


defglobal_function():
Expand DownExpand Up@@ -70,6 +71,27 @@ def test(): pass
test.__code__=self.b.__code__
self.assertEqual(test(),3)# self.b always returns 3, arbitrarily

deftest_invalid___code___assignment(self):
defA():pass
defB():yield
asyncdefC():yield
asyncdefD(x):awaitx

forsrcin [A,B,C,D]:
fordstin [A,B,C,D]:
ifsrc==dst:
continue

assertsrc.__code__.co_flags!=dst.__code__.co_flags
prev=dst.__code__
try:
withself.assertWarnsRegex(DeprecationWarning,'code object of non-matching type'):
dst.__code__=src.__code__
finally:
withwarnings.catch_warnings():
warnings.filterwarnings('ignore','',DeprecationWarning)
dst.__code__=prev

deftest___globals__(self):
self.assertIs(self.b.__globals__,globals())
self.cannot_set_attr(self.b,'__globals__',2,
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Deprecate assignment to a function's ``__code__`` field when the new code
object is of a mismatched type (e.g., from a generator to a plain function).
14 changes: 14 additions & 0 deletionsObjects/funcobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -557,6 +557,20 @@ func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
nclosure,nfree);
return-1;
}

PyObject*func_code=PyFunction_GET_CODE(op);
intold_flags= ((PyCodeObject*)func_code)->co_flags;
intnew_flags= ((PyCodeObject*)value)->co_flags;
intmask=CO_GENERATOR |CO_COROUTINE |CO_ASYNC_GENERATOR;
if ((old_flags&mask)!= (new_flags&mask)) {
if (PyErr_Warn(PyExc_DeprecationWarning,
"Assigning a code object of non-matching type is deprecated "
"(e.g., from a generator to a plain function)")<0)
{
return-1;
}
}

handle_func_event(PyFunction_EVENT_MODIFY_CODE,op,value);
_PyFunction_SetVersion(op,0);
Py_XSETREF(op->func_code,Py_NewRef(value));
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp