Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b079cc4

Browse files
authored
Add PyCode_GetVarnames() (#44)
1 parent 57aaa18 commit b079cc4

6 files changed

Lines changed: 88 additions & 6 deletions

File tree

docs/api.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,30 @@ Latest version of the header file:
2727
Python 3.11
2828
-----------
2929

30+
.. c:function:: PyObject* PyCode_GetCellvars(PyCodeObject *code)
31+
32+
See `PyCode_GetCellvars() documentation <https://docs.python.org/dev/c-api/code.html#c.PyCode_GetCellvars>`__.
33+
34+
Not available on PyPy.
35+
3036
.. c:function:: PyObject* PyCode_GetCode(PyCodeObject *code)
3137
3238
See `PyCode_GetCode() documentation <https://docs.python.org/dev/c-api/code.html#c.PyCode_GetCode>`__.
3339
3440
Not available on PyPy.
3541
42+
.. c:function:: PyObject* PyCode_GetFreevars(PyCodeObject *code)
43+
44+
See `PyCode_GetFreevars() documentation <https://docs.python.org/dev/c-api/code.html#c.PyCode_GetFreevars>`__.
45+
46+
Not available on PyPy.
47+
48+
.. c:function:: PyObject* PyCode_GetVarnames(PyCodeObject *code)
49+
50+
See `PyCode_GetVarnames() documentation <https://docs.python.org/dev/c-api/code.html#c.PyCode_GetVarnames>`__.
51+
52+
Not available on PyPy.
53+
3654
.. c:function:: PyObject* PyFrame_GetBuiltins(PyFrameObject *frame)
3755
3856
See `PyFrame_GetBuiltins() documentation <https://docs.python.org/dev/c-api/frame.html#c.PyFrame_GetBuiltins>`__.

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Changelog
22
=========
33

4+
* 2022-08-04: Add ``PyCode_GetVarnames()``, ``PyCode_GetFreevars()``
5+
and ``PyCode_GetCellvars()`` functions.
46
* 2022-06-14: Fix compatibility with C++ older than C++11.
57
* 2022-05-03: Add ``PyCode_GetCode()`` function.
68
* 2022-04-26: Rename the project from ``pythoncapi_compat`` to

pythoncapi_compat.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,34 @@ PyCode_GetCode(PyCodeObject *code)
478478
#endif
479479

480480

481+
// gh-95008 added PyCode_GetVarnames() to Python 3.11.0rc1
482+
#if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
483+
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
484+
PyCode_GetVarnames(PyCodeObject *code)
485+
{
486+
return Py_NewRef(code->co_varnames);
487+
}
488+
#endif
489+
490+
// gh-95008 added PyCode_GetFreevars() to Python 3.11.0rc1
491+
#if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
492+
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
493+
PyCode_GetFreevars(PyCodeObject *code)
494+
{
495+
return Py_NewRef(code->co_freevars);
496+
}
497+
#endif
498+
499+
// gh-95008 added PyCode_GetCellvars() to Python 3.11.0rc1
500+
#if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
501+
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
502+
PyCode_GetCellvars(PyCodeObject *code)
503+
{
504+
return Py_NewRef(code->co_cellvars);
505+
}
506+
#endif
507+
508+
481509
// Py_UNUSED() was added to Python 3.4.0b2.
482510
#if PY_VERSION_HEX < 0x030400B2 && !defined(Py_UNUSED)
483511
# if defined(__GNUC__) || defined(__clang__)

tests/setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
'-std=c99',
3434
]
3535
CPPFLAGS = list(COMMON_FLAGS)
36-
if sys.version_info >= (3, 12):
36+
# FIXME: _Py_CAST() emits C++ compilers on Python 3.12.
37+
# See: https://github.com/python/cpython/issues/94731
38+
if 0:
3739
CPPFLAGS.extend((
3840
'-Wold-style-cast',
3941
'-Wzero-as-null-pointer-constant',

tests/test_pythoncapi_compat.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ def main():
166166

167167
# Implementing PyFrame_GetLocals() and PyCode_GetCode() require the
168168
# internal C API in Python 3.11 alpha versions.
169-
if 0x30b0000 <= sys.hexversion < 0x30b00b1:
169+
# Implementing PyCode_GetVarnames() requires the internal C API
170+
# in Python 3.11 beta versions.
171+
if 0x30b0000 <= sys.hexversion < 0x30b00c1:
170172
version = sys.version.split()[0]
171173
print("SKIP TESTS: Python %s is not supported" % version)
172174
return

tests/test_pythoncapi_compat_cext.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,40 @@ test_code(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
479479
}
480480
PyCodeObject *code = PyFrame_GetCode(frame);
481481

482-
PyObject *co_code = PyCode_GetCode(code);
483-
assert(co_code != _Py_NULL);
484-
assert(PyBytes_Check(co_code));
485-
Py_DECREF(co_code);
482+
// PyCode_GetCode()
483+
{
484+
PyObject *co_code = PyCode_GetCode(code);
485+
assert(co_code != _Py_NULL);
486+
assert(PyBytes_Check(co_code));
487+
Py_DECREF(co_code);
488+
}
489+
490+
// PyCode_GetVarnames
491+
{
492+
PyObject *co_varnames = PyCode_GetVarnames(code);
493+
assert(co_varnames != NULL);
494+
assert(PyTuple_CheckExact(co_varnames));
495+
assert(PyTuple_GET_SIZE(co_varnames) != 0);
496+
Py_DECREF(co_varnames);
497+
}
498+
499+
// PyCode_GetCellvars
500+
{
501+
PyObject *co_cellvars = PyCode_GetCellvars(code);
502+
assert(co_cellvars != NULL);
503+
assert(PyTuple_CheckExact(co_cellvars));
504+
assert(PyTuple_GET_SIZE(co_cellvars) == 0);
505+
Py_DECREF(co_cellvars);
506+
}
507+
508+
// PyCode_GetFreevars
509+
{
510+
PyObject *co_freevars = PyCode_GetFreevars(code);
511+
assert(co_freevars != NULL);
512+
assert(PyTuple_CheckExact(co_freevars));
513+
assert(PyTuple_GET_SIZE(co_freevars) == 0);
514+
Py_DECREF(co_freevars);
515+
}
486516

487517
Py_DECREF(code);
488518
Py_DECREF(frame);

0 commit comments

Comments
 (0)