Skip to content

Instantly share code, notes, and snippets.

@lazka

lazka/run.py Secret

Created April 22, 2018 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lazka/871913fb1442d214e9f4d77d3ff7396c to your computer and use it in GitHub Desktop.
Save lazka/871913fb1442d214e9f4d77d3ff7396c to your computer and use it in GitHub Desktop.
import tp_init_bug
i_was_called = False
class Foo(object):
def __init__(self):
global i_was_called
i_was_called = True
x = object.__new__(Foo)
tp_init_bug.call_dunder_init(x)
assert i_was_called, "__init__ wasn't called :("
from distutils.core import setup, Extension
setup(ext_modules=[Extension("tp_init_bug", ["tp_init_bug.c"])])
#include <Python.h>
static PyObject *call_dunder_init(PyObject *self, PyObject *args)
{
PyObject *obj, *init_args, *init_kwargs;
if (!PyArg_ParseTuple(args, "O", &obj))
return NULL;
init_args = PyTuple_New(0);
init_kwargs = PyDict_New();
if (Py_TYPE(obj)->tp_init(obj, init_args, init_kwargs) == -1)
PyErr_Print();
Py_DECREF (init_args);
Py_DECREF (init_kwargs);
Py_RETURN_NONE;
}
static PyMethodDef module_methods[] = {
{"call_dunder_init", call_dunder_init, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC inittp_init_bug(void)
{
PyObject *m = Py_InitModule3("tp_init_bug", module_methods, NULL);
if (m == NULL)
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment