Skip to content

Add PyFrame_GetVar() #46

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
Nov 8, 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
16 changes: 16 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ Latest version of the header file:
`pythoncapi_compat.h <https://raw.githubusercontent.com/python/pythoncapi-compat/master/pythoncapi_compat.h>`_.


Python 3.12
-----------

.. c:function:: PyObject* PyFrame_GetVar(PyFrameObject *frame, PyObject *name)

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

Not available on PyPy.

.. c:function:: PyObject* PyFrame_GetVarString(PyFrameObject *frame, const char *name)

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

Not available on PyPy.


Python 3.11
-----------

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-11-04: Add ``PyFrame_GetVar()`` and ``PyFrame_GetVarString()``
functions.
* 2022-08-04: Add ``PyCode_GetVarnames()``, ``PyCode_GetFreevars()``
and ``PyCode_GetCellvars()`` functions.
* 2022-06-14: Fix compatibility with C++ older than C++11.
Expand Down
51 changes: 51 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,57 @@ PyFrame_GetLasti(PyFrameObject *frame)
#endif


// gh-91248 added PyFrame_GetVar() to Python 3.12.0a2
#if PY_VERSION_HEX < 0x030C00A2 && !defined(PYPY_VERSION)
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyFrame_GetVar(PyFrameObject *frame, PyObject *name)
{
PyObject *locals, *value;

locals = PyFrame_GetLocals(frame);
if (locals == NULL) {
return NULL;
}
#if PY_VERSION_HEX >= 0x03000000
value = PyDict_GetItemWithError(locals, name);
#else
value = PyDict_GetItem(locals, name);
#endif
Py_DECREF(locals);

if (value == NULL) {
if (PyErr_Occurred()) {
return NULL;
}
#if PY_VERSION_HEX >= 0x03000000
PyErr_Format(PyExc_NameError, "variable %R does not exist", name);
#else
PyErr_SetString(PyExc_NameError, "variable does not exist");
#endif
return NULL;
}
return Py_NewRef(value);
}
#endif


// gh-91248 added PyFrame_GetVarString() to Python 3.12.0a2
#if PY_VERSION_HEX < 0x030C00A2 && !defined(PYPY_VERSION)
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyFrame_GetVarString(PyFrameObject *frame, const char *name)
{
PyObject *name_obj, *value;
name_obj = PyUnicode_FromString(name);
if (name_obj == NULL) {
return NULL;
}
value = PyFrame_GetVar(frame, name_obj);
Py_DECREF(name_obj);
return value;
}
#endif


// bpo-39947 added PyThreadState_GetInterpreter() to Python 3.9.0a5
#if PY_VERSION_HEX < 0x030900A5
PYCAPI_COMPAT_STATIC_INLINE(PyInterpreterState *)
Expand Down
43 changes: 43 additions & 0 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#include "pythoncapi_compat.h"

#ifdef NDEBUG
# error "assertions must be enabled"
#endif

#ifdef Py_LIMITED_API
# error "Py_LIMITED_API is not supported"
#endif
Expand Down Expand Up @@ -138,6 +142,41 @@ test_py_is(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))


#if !defined(PYPY_VERSION)
static void
test_frame_getvar(PyFrameObject *frame)
{
// Make the assumption that test_frame_getvar() is only called by
// _run_tests() of test_pythoncapi_compat.py and so that the "name"
// variable exists.

// test PyFrame_GetVar() and PyFrame_GetVarString()
PyObject *attr = PyUnicode_FromString("name");
assert(attr != NULL);
PyObject *name1 = PyFrame_GetVar(frame, attr);
Py_DECREF(attr);
assert(name1 != NULL);
Py_DECREF(name1);

PyObject *name2 = PyFrame_GetVarString(frame, "name");
assert(name2 != NULL);
Py_DECREF(name2);

// test PyFrame_GetVar() and PyFrame_GetVarString() NameError
PyObject *attr3 = PyUnicode_FromString("dontexist");
assert(attr3 != NULL);
PyObject *name3 = PyFrame_GetVar(frame, attr3);
Py_DECREF(attr3);
assert(name3 == NULL);
assert(PyErr_ExceptionMatches(PyExc_NameError));
PyErr_Clear();

PyObject *name4 = PyFrame_GetVarString(frame, "dontexist");
assert(name4 == NULL);
assert(PyErr_ExceptionMatches(PyExc_NameError));
PyErr_Clear();
}


static PyObject *
test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
{
Expand Down Expand Up @@ -214,6 +253,10 @@ test_frame(PyObject *Py_UNUSED(module), PyObject* Py_UNUSED(ignored))
int lasti = PyFrame_GetLasti(frame);
assert(lasti >= 0);

// test PyFrame_GetVar() and PyFrame_GetVarString()
test_frame_getvar(frame);

// done
Py_DECREF(frame);
Py_RETURN_NONE;
}
Expand Down