From 3f82d2c82dc3f3d78b937f7a2f9dec38de4a48b1 Mon Sep 17 00:00:00 2001
From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Date: Sat, 25 Jun 2022 00:05:54 +0800
Subject: [PATCH 1/2] Sync up co_code changes with 3.11

---
 Include/cpython/code.h                                         | 2 +-
 .../2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst              | 3 ++-
 Objects/codeobject.c                                           | 2 +-
 Tools/scripts/deepfreeze.py                                    | 1 +
 4 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/Include/cpython/code.h b/Include/cpython/code.h
index ebc0df9323bf66..2d5a37c444af02 100644
--- a/Include/cpython/code.h
+++ b/Include/cpython/code.h
@@ -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.               \
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst b/Misc/NEWS.d/next/Core and Builtins/2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst
index 04b9e8ba481874..1fe821edf5a148 100644
--- a/Misc/NEWS.d/next/Core and Builtins/2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst	
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst	
@@ -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.
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 707de11d47922f..c38c51b45321c1 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -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;
 }
 
diff --git a/Tools/scripts/deepfreeze.py b/Tools/scripts/deepfreeze.py
index a46cf6c01647f2..f9fd4e36a81baa 100644
--- a/Tools/scripts/deepfreeze.py
+++ b/Tools/scripts/deepfreeze.py
@@ -274,6 +274,7 @@ def generate_code(self, name: str, code: types.CodeType) -> str:
             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]):

From d09a20df9074c197ef5b0ad6b7d80bbc2d47e59a Mon Sep 17 00:00:00 2001
From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Date: Sat, 25 Jun 2022 01:09:31 +0800
Subject: [PATCH 2/2] fix formatting

---
 Include/cpython/code.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Include/cpython/code.h b/Include/cpython/code.h
index 2d5a37c444af02..595cd9e94f31a8 100644
--- a/Include/cpython/code.h
+++ b/Include/cpython/code.h
@@ -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 */    \
-    PyObject *_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.               \