Skip to content

Commit efe7161

Browse files
committed
pyframe fix 9-test
1 parent 43e9c9f commit efe7161

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

Doc/c-api/frame.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ Unless using :pep:`523`, you will not need this.
274274
275275
.. code-block:: c
276276
277-
int kind = PyUnstable_Frame_GetExecutableKind(frame)
277+
int kind = PyUnstable_Frame_GetExecutableKind(frame);
278278
279279
if (kind == PyUnstable_EXECUTABLE_KIND_SKIP) {
280280
continue;

Include/cpython/frameobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ PyAPI_FUNC(void) PyFrame_LocalsToFast(PyFrameObject *, int);
2424
* obsolete or outdated. */
2525

2626
PyAPI_FUNC(int) _PyFrame_IsEntryFrame(PyFrameObject *frame);
27+
PyAPI_FUNC(int) PyUnstable_Frame_GetExecutableKind(PyFrameObject *frame);
2728

2829
PyAPI_FUNC(int) PyFrame_FastToLocalsWithError(PyFrameObject *f);
2930
PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *);

Modules/_testcapi/frame.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,26 @@ frame_getvarstring(PyObject *self, PyObject *args)
114114
}
115115

116116

117+
static PyObject *
118+
test_my_doc_example(PyObject *self, PyObject *arg)
119+
{
120+
PyFrameObject *frame = (PyFrameObject *)PyEval_GetFrame();
121+
if (frame == NULL) {
122+
Py_RETURN_NONE;
123+
}
124+
125+
int kind = PyUnstable_Frame_GetExecutableKind(frame);
126+
127+
if (kind == PyUnstable_EXECUTABLE_KIND_SKIP) {
128+
return PyLong_FromLong(kind);
129+
}
130+
131+
return PyLong_FromLong(kind);
132+
}
133+
134+
117135
static PyMethodDef test_methods[] = {
136+
{"test_my_doc_example", test_my_doc_example, METH_NOARGS, NULL},
118137
{"frame_getlocals", frame_getlocals, METH_O, NULL},
119138
{"frame_getglobals", frame_getglobals, METH_O, NULL},
120139
{"frame_getgenerator", frame_getgenerator, METH_O, NULL},
@@ -131,4 +150,3 @@ _PyTestCapi_Init_Frame(PyObject *m)
131150
{
132151
return PyModule_AddFunctions(m, test_methods);
133152
}
134-

Python/frame.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,23 @@ const PyTypeObject *const PyUnstable_ExecutableKinds[PyUnstable_EXECUTABLE_KINDS
157157
[PyUnstable_EXECUTABLE_KIND_METHOD_DESCRIPTOR] = &PyMethodDescr_Type,
158158
[PyUnstable_EXECUTABLE_KINDS] = NULL,
159159
};
160+
161+
PyAPI_FUNC(int)
162+
PyUnstable_Frame_GetExecutableKind(PyFrameObject *frame)
163+
{
164+
_PyInterpreterFrame *f = frame->f_frame;
165+
166+
PyObject *exec = PyStackRef_AsPyObjectBorrow(f->f_executable);
167+
168+
if (PyCode_Check(exec)) {
169+
return PyUnstable_EXECUTABLE_KIND_PY_FUNCTION;
170+
}
171+
if (PyMethod_Check(exec)) {
172+
return PyUnstable_EXECUTABLE_KIND_BUILTIN_FUNCTION;
173+
}
174+
if (Py_IS_TYPE(exec, &PyMethodDescr_Type)) {
175+
return PyUnstable_EXECUTABLE_KIND_METHOD_DESCRIPTOR;
176+
}
177+
178+
return PyUnstable_EXECUTABLE_KIND_SKIP;
179+
}

0 commit comments

Comments
 (0)