Skip to content

Instantly share code, notes, and snippets.

@ibaned
Created February 15, 2019 16:12
Show Gist options
  • Save ibaned/f2127d30346673f291444b0781ce0beb to your computer and use it in GitHub Desktop.
Save ibaned/f2127d30346673f291444b0781ce0beb to your computer and use it in GitHub Desktop.
static void dumpPythonStack() {
PyFrameObject* frame = PyEval_GetFrame();
if (!frame) {
fprintf(stderr, "Got NULL frame...\n");
return;
}
while (frame) {
int lineno = PyFrame_GetLineNumber(frame);
PyCodeObject* code = frame->f_code;
PyObject* filename = code->co_filename;
Py_ssize_t size;
char *filename_string;
filename_string = PyUnicode_AsUTF8AndSize(filename, &size);
if (!filename_string) {
fprintf(stderr, "Got NULL filename...\n");
return;
}
fprintf(stdout, "%s:%d\n", filename_string, lineno);
frame = frame->f_back;
}
fflush(stdout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment