Visitar URL original
GH-100987: Allow non python frames in frame stack. by markshannon · Pull Request #103010 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Insert introspectable frames for each non-Python call from interpreter.
  • Loading branch information
markshannon committed Mar 15, 2023
commit 3e2c3ab022aaa767bd9ab8530a12570f594dba2f
4 changes: 3 additions & 1 deletion Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def test_return_null_without_error(self):
r'returned NULL without setting an exception\n'
r'\n'
r'Current thread.*:\n'
r' Builtin function return_null_without_error\n'
r' File .*", line 6 in <module>\n')
else:
with self.assertRaises(SystemError) as cm:
Expand Down Expand Up @@ -240,6 +241,7 @@ def test_return_result_with_error(self):
r'returned a result with an exception set\n'
r'\n'
r'Current thread.*:\n'
r' Builtin function return_result_with_error\n'
r' File .*, line 6 in <module>\n')
else:
with self.assertRaises(SystemError) as cm:
Expand All @@ -262,7 +264,6 @@ def test_getitem_with_error(self):
rc, out, err = assert_python_failure('-c', code)
err = decode_stderr(err)
if 'SystemError: ' not in err:
print(err)
self.assertRegex(err,
r'Fatal Python error: _Py_CheckSlotResult: '
r'Slot __getitem__ of type dict succeeded '
Expand All @@ -271,6 +272,7 @@ def test_getitem_with_error(self):
r'ValueError: bug\n'
r'\n'
r'Current thread .* \(most recent call first\):\n'
r' Builtin function getitem_with_error\n'
r' File .*, line 6 in <module>\n'
r'\n'
r'Extension modules: _testcapi \(total: 1\)\n')
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_faulthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,13 @@ def funcA():
' File "<string>", line 19 in <module>'
]
trace, exitcode = self.get_output(code, filename, fd)
if trace != expected:
print("Trace")
print("-----------------")
print(trace)
print("Expected")
print("-----------------")
print(expected)
self.assertEqual(trace, expected)
self.assertEqual(exitcode, 0)

Expand Down
23 changes: 13 additions & 10 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2455,17 +2455,18 @@ dummy_func(
JUMPBY(INLINE_CACHE_ENTRIES_CALL);
DISPATCH_INLINED(new_frame);
}

/* Callable is not a normal Python function */
if (cframe.use_tracing) {
res = trace_call_function(
tstate, callable, args,
positional_args, kwnames);
}
else {
res = PyObject_Vectorcall(
CALL_WITH_NATIVE_FRAME(callable, res = PyObject_Vectorcall(
callable, args,
positional_args | PY_VECTORCALL_ARGUMENTS_OFFSET,
kwnames);
kwnames));
}
kwnames = NULL;
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Expand Down Expand Up @@ -2606,8 +2607,8 @@ dummy_func(
PyTypeObject *tp = (PyTypeObject *)callable;
DEOPT_IF(tp->tp_vectorcall == NULL, CALL);
STAT_INC(CALL, hit);
res = tp->tp_vectorcall((PyObject *)tp, args,
total_args - kwnames_len, kwnames);
CALL_WITH_NATIVE_FRAME(callable, res = tp->tp_vectorcall((PyObject *)tp, args,
total_args - kwnames_len, kwnames));
kwnames = NULL;
/* Free the arguments. */
for (int i = 0; i < total_args; i++) {
Expand Down Expand Up @@ -2640,7 +2641,7 @@ dummy_func(
goto error;
}
PyObject *arg = args[0];
res = _PyCFunction_TrampolineCall(cfunc, PyCFunction_GET_SELF(callable), arg);
CALL_WITH_NATIVE_FRAME(callable, res = _PyCFunction_TrampolineCall(cfunc, PyCFunction_GET_SELF(callable), arg));
_Py_LeaveRecursiveCallTstate(tstate);
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));

Expand All @@ -2666,10 +2667,10 @@ dummy_func(
STAT_INC(CALL, hit);
PyCFunction cfunc = PyCFunction_GET_FUNCTION(callable);
/* res = func(self, args, nargs) */
res = ((_PyCFunctionFast)(void(*)(void))cfunc)(
CALL_WITH_NATIVE_FRAME(callable, res = ((_PyCFunctionFast)(void(*)(void))cfunc)(
PyCFunction_GET_SELF(callable),
args,
total_args);
total_args));
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));

/* Free the arguments. */
Expand Down Expand Up @@ -2704,12 +2705,12 @@ dummy_func(
_PyCFunctionFastWithKeywords cfunc =
(_PyCFunctionFastWithKeywords)(void(*)(void))
PyCFunction_GET_FUNCTION(callable);
res = cfunc(
CALL_WITH_NATIVE_FRAME(callable, res = cfunc(
PyCFunction_GET_SELF(callable),
args,
total_args - KWNAMES_LEN(),
kwnames
);
));
assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
kwnames = NULL;

Expand Down Expand Up @@ -2738,7 +2739,8 @@ dummy_func(
DEOPT_IF(callable != interp->callable_cache.len, CALL);
STAT_INC(CALL, hit);
PyObject *arg = args[0];
Py_ssize_t len_i = PyObject_Length(arg);
Py_ssize_t len_i;
CALL_WITH_NATIVE_FRAME(callable, len_i = PyObject_Length(arg));
if (len_i < 0) {
goto error;
}
Expand Down Expand Up @@ -2767,6 +2769,7 @@ dummy_func(
STAT_INC(CALL, hit);
PyObject *cls = args[1];
PyObject *inst = args[0];

int retval = PyObject_IsInstance(inst, cls);
if (retval < 0) {
goto error;
Expand Down
12 changes: 12 additions & 0 deletions Python/ceval_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,15 @@ GETITEM(PyObject *v, Py_ssize_t i) {

#define KWNAMES_LEN() \
(kwnames == NULL ? 0 : ((int)PyTuple_GET_SIZE(kwnames)))


#define CALL_WITH_NATIVE_FRAME(_callable, _call) \
do { \
assert(cframe.current_frame == &frame->base); \
_PyFrame _bframe; \
_bframe.f_executable = _callable; \
_bframe.previous = &frame->base; \
cframe.current_frame = &_bframe; \
(_call); \
cframe.current_frame = &frame->base; \
} while (0)
23 changes: 13 additions & 10 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 31 additions & 6 deletions Python/traceback.c
Original file line number Diff line number Diff line change
Expand Up @@ -1165,14 +1165,10 @@ _Py_DumpASCII(int fd, PyObject *text)
This function is signal safe. */

static void
dump_frame(int fd, _PyFrame *f)
dump_pyframe(int fd, _PyInterpreterFrame *frame)
{
if (!PyCode_Check(f->f_executable)) {
return;
}
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)f;
PyCodeObject *code =_PyFrame_GetCode(frame);
PUTS(fd, " File ");
PyCodeObject *code =_PyFrame_GetCode(frame);
if (code->co_filename != NULL
&& PyUnicode_Check(code->co_filename))
{
Expand Down Expand Up @@ -1204,6 +1200,35 @@ dump_frame(int fd, _PyFrame *f)
PUTS(fd, "\n");
}

static void
dump_frame(int fd, _PyFrame *f)
{
PyObject * func = f->f_executable;
if (PyCode_Check(func)) {
dump_pyframe(fd, (_PyInterpreterFrame *)f);
return;
}
if (func == Py_None) {
return;
}
if (PyType_Check(func)) {
PUTS(fd, " Class ");
PUTS(fd, ((PyTypeObject *)func)->tp_name);
}
else if (PyCFunction_Check(func)) {
PUTS(fd, " Builtin function ");
PUTS(fd, ((PyCFunctionObject *)func)->m_ml->ml_name);
}
else if (Py_TYPE(func) == &PyMethodDescr_Type) {
PUTS(fd, " Method descriptor ");
PUTS(fd, ((PyMethodDescrObject *)func)->d_method->ml_name);
}
else {
PUTS(fd, " Other");
}
PUTS(fd, "\n");
}

static void
dump_traceback(int fd, PyThreadState *tstate, int write_header)
{
Expand Down