FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-92154: Expose PyCode_GetCode in the C API by Fidget-Spinner · Pull Request #92168 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .h  (1) .rst  (3) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 14 additions & 0 deletions Doc/c-api/code.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,17 @@ bound into a function.
information is not available for any particular element.

Returns ``1`` if the function succeeds and 0 otherwise.

.. c:function:: PyObject* PyCode_GetCode(PyCodeObject *co)

Equivalent to the Python code ``getattr(co, 'co_code')``.
Returns a strong reference to a :c:type:`PyBytesObject` representing the
bytecode in a code object. On error, ``NULL`` is returned and an exception
is raised.

This ``PyBytesObject`` may be created on-demand by the interpreter and does
not necessarily represent the bytecode actually executed by CPython. The
primary use case for this function is debuggers and profilers.

.. versionadded:: 3.11

5 changes: 5 additions & 0 deletions Doc/whatsnew/3.11.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,11 @@ C API Changes
To get a custom code object: create a code object using the compiler,
then get a modified version with the ``replace`` method.

* :c:type:`PyCodeObject` no longer has a ``co_code`` field. Instead,
use ``PyObject_GetAttrString(code_object, "co_code")`` or
:c:func:`PyCode_GetCode` to get the underlying bytes object.
Comment thread
Fidget-Spinner marked this conversation as resolved.
(Contributed by Brandt Bucher in :issue:`46841` and Ken Jin in :gh:`92154`.)

Comment thread
Fidget-Spinner marked this conversation as resolved.
New Features
------------

Expand Down
3 changes: 3 additions & 0 deletions Include/cpython/code.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ PyAPI_FUNC(int) _PyCode_GetExtra(PyObject *code, Py_ssize_t index,
PyAPI_FUNC(int) _PyCode_SetExtra(PyObject *code, Py_ssize_t index,
void *extra);

/* Equivalent to getattr(code, 'co_code') in Python.
Returns a strong reference to a bytes object. */
PyAPI_FUNC(PyObject *) PyCode_GetCode(PyCodeObject *code);

typedef enum _PyCodeLocationInfoKind {
/* short forms are 0 to 9 */
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added the :c:func:`PyCode_GetCode` function. This function does the
equivalent of the Python code ``getattr(code_object, 'co_code')``.
24 changes: 24 additions & 0 deletions Modules/_testcapimodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5931,6 +5931,29 @@ get_feature_macros(PyObject *self, PyObject *Py_UNUSED(args))
return result;
}

static PyObject *
test_code_api(PyObject *self, PyObject *Py_UNUSED(args))
{
PyCodeObject *co = PyCode_NewEmpty("_testcapi", "dummy", 1);
if (co == NULL) {
return NULL;
}
PyObject *co_code = PyCode_GetCode(co);
if (co_code == NULL) {
Py_DECREF(co);
return NULL;
}
assert(PyBytes_CheckExact(co_code));
if (PyObject_Length(co_code) == 0) {
PyErr_SetString(PyExc_ValueError, "empty co_code");
Py_DECREF(co);
Py_DECREF(co_code);
return NULL;
}
Py_DECREF(co);
Py_DECREF(co_code);
Py_RETURN_NONE;
}

static PyObject *negative_dictoffset(PyObject *, PyObject *);
static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
Expand Down Expand Up @@ -6227,6 +6250,7 @@ static PyMethodDef TestMethods[] = {
{"frame_getbuiltins", frame_getbuiltins, METH_O, NULL},
{"frame_getlasti", frame_getlasti, METH_O, NULL},
{"get_feature_macros", get_feature_macros, METH_NOARGS, NULL},
{"test_code_api", test_code_api, METH_NOARGS, NULL},
{NULL, NULL} /* sentinel */
};

Expand Down
5 changes: 5 additions & 0 deletions Objects/codeobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,11 @@ _PyCode_GetCode(PyCodeObject *co)
return code;
}

PyObject *
PyCode_GetCode(PyCodeObject *co)
{
return _PyCode_GetCode(co);
}

/******************
* PyCode_Type
Expand Down

Back | FazBrowse Home | New Git URL