Skip to content

Instantly share code, notes, and snippets.

@sumerc
Last active December 29, 2022 12:25
Show Gist options
  • Save sumerc/b254f38c5a620b8d47aba7398b3c7791 to your computer and use it in GitHub Desktop.
Save sumerc/b254f38c5a620b8d47aba7398b3c7791 to your computer and use it in GitHub Desktop.
pyframe_getback Crash
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "frameobject.h"
void DebugPrintObjects(unsigned int arg_count, ...)
{
unsigned int i;
va_list vargs;
va_start(vargs, arg_count);
if (!arg_count) {
return;
}
for (i=0; i<arg_count; i++) {
PyObject_Print(va_arg(vargs, PyObject *), stdout, Py_PRINT_RAW);
}
printf("\n");
va_end(vargs);
}
int
PyTraceFunction(
PyObject* obj,
PyFrameObject* frame,
int what,
PyObject* arg)
{
DebugPrintObjects(1, frame);
PyFrame_GetBack(frame);
return 0;
}
static PyObject *
set_trace(PyObject*a, PyObject*b)
{
PyEval_SetProfile(PyTraceFunction, NULL);
Py_RETURN_NONE;
}
static PyObject *
frame_new(PyObject *self, PyObject *args)
{
PyObject *code, *globals, *locals;
if (!PyArg_ParseTuple(args, "OOO", &code, &globals, &locals)) {
return NULL;
}
if (!PyCode_Check(code)) {
PyErr_SetString(PyExc_TypeError, "argument must be a code object");
return NULL;
}
PyThreadState *tstate = PyThreadState_Get();
return (PyObject *)PyFrame_New(tstate, (PyCodeObject *)code, globals, locals);
}
static PyMethodDef methods[] = {
{"set_trace", set_trace, METH_NOARGS, "set_trace"},
{"frame_new", frame_new, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL},
};
static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT, "zzz", "", -1, methods};
PyMODINIT_FUNC
PyInit_zzz(void)
{
return PyModule_Create(&moduledef);
}
import zzz
zzz.set_trace()
#from spacy.tokens import Doc
def dummy():
pass
frame = zzz.frame_new(dummy.__code__, globals(), locals())
print(frame.f_back)
from setuptools import Extension, setup
setup(
version="1.0",
ext_modules=[Extension(
"zzz",
sources=["main.c"],
)],
description="zzz",
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment