]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-93382: Sync up `co_code` changes with 3.11 (GH-94227)
authorKen Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Fri, 24 Jun 2022 17:55:18 +0000 (01:55 +0800)
committerGitHub <noreply@github.com>
Fri, 24 Jun 2022 17:55:18 +0000 (01:55 +0800)
Sync up co_code changes with 3.11 commit 852b4d4bcd12b0b6839a015a262ce976b134f6f3.

Include/cpython/code.h
Misc/NEWS.d/next/Core and Builtins/2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst
Objects/codeobject.c
Tools/scripts/deepfreeze.py

index ebc0df9323bf666a0e519a09926f3a023646c699..595cd9e94f31a8a867537554b8aebc169322c188 100644 (file)
@@ -90,7 +90,7 @@ typedef uint16_t _Py_CODEUNIT;
     PyObject *co_qualname;        /* unicode (qualname, for reference) */      \
     PyObject *co_linetable;       /* bytes object that holds location info */  \
     PyObject *co_weakreflist;     /* to support weakrefs to code objects */    \
-    void *_co_code;               /* cached co_code object/attribute */        \
+    PyObject *_co_code;           /* cached co_code object/attribute */        \
     int _co_firsttraceable;       /* index of first traceable instruction */   \
     char *_co_linearray;          /* array of line offsets */                  \
     /* Scratch space for extra data relating to the code object.               \
index 04b9e8ba481874b54dbcef13739b603644e801be..1fe821edf5a1482badeab36aa198eee93794778b 100644 (file)
@@ -1 +1,2 @@
-Speed up the :c:func:`PyCode_GetCode` function which also improves accessing the :attr:`~types.CodeType.co_code` attribute in Python.
+Cache the result of :c:func:`PyCode_GetCode` function to restore the O(1)
+lookup of the :attr:`~types.CodeType.co_code` attribute.
index 707de11d47922f4dda29307df864c1b830eaf925..c38c51b45321c1b1a2362603d5535f5eee52f3db 100644 (file)
@@ -1440,7 +1440,7 @@ _PyCode_GetCode(PyCodeObject *co)
     }
     deopt_code((_Py_CODEUNIT *)PyBytes_AS_STRING(code), Py_SIZE(co));
     assert(co->_co_code == NULL);
-    co->_co_code = (void *)Py_NewRef(code);
+    co->_co_code = Py_NewRef(code);
     return code;
 }
 
index a46cf6c01647f213264e3df87d2fdf19b1853a96..f9fd4e36a81baa93200f1e6d6096f0505a82c6ff 100644 (file)
@@ -274,6 +274,7 @@ class Printer:
             self.write(f".co_name = {co_name},")
             self.write(f".co_qualname = {co_qualname},")
             self.write(f".co_linetable = {co_linetable},")
+            self.write(f"._co_code = NULL,")
             self.write("._co_linearray = NULL,")
             self.write(f".co_code_adaptive = {co_code_adaptive},")
             for i, op in enumerate(code.co_code[::2]):