-
-
Save lazka/871913fb1442d214e9f4d77d3ff7396c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 :(" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from distutils.core import setup, Extension | |
setup(ext_modules=[Extension("tp_init_bug", ["tp_init_bug.c"])]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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