Skip to content

Add PyCode_GetVarnames() #44

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,30 @@ Latest version of the header file:
Python 3.11
-----------

.. c:function:: PyObject* PyCode_GetCellvars(PyCodeObject *code)

See `PyCode_GetCellvars() documentation <https://docs.python.org/dev/c-api/code.html#c.PyCode_GetCellvars>`__.

Not available on PyPy.

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

See `PyCode_GetCode() documentation <https://docs.python.org/dev/c-api/code.html#c.PyCode_GetCode>`__.

Not available on PyPy.

.. c:function:: PyObject* PyCode_GetFreevars(PyCodeObject *code)

See `PyCode_GetFreevars() documentation <https://docs.python.org/dev/c-api/code.html#c.PyCode_GetFreevars>`__.

Not available on PyPy.

.. c:function:: PyObject* PyCode_GetVarnames(PyCodeObject *code)

See `PyCode_GetVarnames() documentation <https://docs.python.org/dev/c-api/code.html#c.PyCode_GetVarnames>`__.

Not available on PyPy.

.. c:function:: PyObject* PyFrame_GetBuiltins(PyFrameObject *frame)

See `PyFrame_GetBuiltins() documentation <https://docs.python.org/dev/c-api/frame.html#c.PyFrame_GetBuiltins>`__.
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Changelog
=========

* 2022-08-04: Add ``PyCode_GetVarnames()``, ``PyCode_GetFreevars()``
and ``PyCode_GetCellvars()`` functions.
* 2022-06-14: Fix compatibility with C++ older than C++11.
* 2022-05-03: Add ``PyCode_GetCode()`` function.
* 2022-04-26: Rename the project from ``pythoncapi_compat`` to
Expand Down
28 changes: 28 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,34 @@ PyCode_GetCode(PyCodeObject *code)
#endif


// gh-95008 added PyCode_GetVarnames() to Python 3.11.0rc1
#if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyCode_GetVarnames(PyCodeObject *code)
{
return Py_NewRef(code->co_varnames);
}
#endif

// gh-95008 added PyCode_GetFreevars() to Python 3.11.0rc1
#if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyCode_GetFreevars(PyCodeObject *code)
{
return Py_NewRef(code->co_freevars);
}
#endif

// gh-95008 added PyCode_GetCellvars() to Python 3.11.0rc1
#if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyCode_GetCellvars(PyCodeObject *code)
{
return Py_NewRef(code->co_cellvars);
}
#endif


// Py_UNUSED() was added to Python 3.4.0b2.
#if PY_VERSION_HEX < 0x030400B2 && !defined(Py_UNUSED)
# if defined(__GNUC__) || defined(__clang__)
Expand Down
4 changes: 3 additions & 1 deletion tests/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
'-std=c99',
]
CPPFLAGS = list(COMMON_FLAGS)
if sys.version_info >= (3, 12):
# FIXME: _Py_CAST() emits C++ compilers on Python 3.12.
# See: https://github.com/python/cpython/issues/94731
if 0:
CPPFLAGS.extend((
'-Wold-style-cast',
'-Wzero-as-null-pointer-constant',
Expand Down
4 changes: 3 additions & 1 deletion tests/test_pythoncapi_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ def main():

# Implementing PyFrame_GetLocals() and PyCode_GetCode() require the
# internal C API in Python 3.11 alpha versions.
if 0x30b0000 <= sys.hexversion < 0x30b00b1:
# Implementing PyCode_GetVarnames() requires the internal C API
# in Python 3.11 beta versions.
if 0x30b0000 <= sys.hexversion < 0x30b00c1:
version = sys.version.split()[0]
print("SKIP TESTS: Python %s is not supported" % version)
return
Expand Down
38 changes: 34 additions & 4 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,10 +479,40 @@ test_code(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
}
PyCodeObject *code = PyFrame_GetCode(frame);

PyObject *co_code = PyCode_GetCode(code);
assert(co_code != _Py_NULL);
assert(PyBytes_Check(co_code));
Py_DECREF(co_code);
// PyCode_GetCode()
{
PyObject *co_code = PyCode_GetCode(code);
assert(co_code != _Py_NULL);
assert(PyBytes_Check(co_code));
Py_DECREF(co_code);
}

// PyCode_GetVarnames
{
PyObject *co_varnames = PyCode_GetVarnames(code);
assert(co_varnames != NULL);
assert(PyTuple_CheckExact(co_varnames));
assert(PyTuple_GET_SIZE(co_varnames) != 0);
Py_DECREF(co_varnames);
}

// PyCode_GetCellvars
{
PyObject *co_cellvars = PyCode_GetCellvars(code);
assert(co_cellvars != NULL);
assert(PyTuple_CheckExact(co_cellvars));
assert(PyTuple_GET_SIZE(co_cellvars) == 0);
Py_DECREF(co_cellvars);
}

// PyCode_GetFreevars
{
PyObject *co_freevars = PyCode_GetFreevars(code);
assert(co_freevars != NULL);
assert(PyTuple_CheckExact(co_freevars));
assert(PyTuple_GET_SIZE(co_freevars) == 0);
Py_DECREF(co_freevars);
}

Py_DECREF(code);
Py_DECREF(frame);
Expand Down