Skip to content

Commit

Permalink
Issue python#107: move the registration of reduce functions into modu…
Browse files Browse the repository at this point in the history
…le stackless

This is part 2 of issue python#107. Fist step. Move the registration code for Stackless specific reduce functions into the module stackless. This simplifies the C code quite a bit. This commit does not modify the behaviour of Stackless once the module stackless has been loaded.

https://bitbucket.org/stackless-dev/stackless/issues/107
  • Loading branch information
Anselm Kruis committed Mar 30, 2017
1 parent 30770d7 commit 056de5a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
7 changes: 7 additions & 0 deletions Lib/stackless.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ def transmogrify():
this function creates a subclass of the ModuleType with properties.
Stackless has historically had module properties, something very unusual in Python.
We need to do that by replacing the current module object as it is being created
Additionally this function performs a few initialisations.
"""
from copyreg import pickle
for name in dir(_wrap):
cls = getattr(_wrap, name, None)
if isinstance(cls, type):
pickle(cls.__bases__[0], cls.__reduce__)

class StacklessModuleType(types.ModuleType):

Expand Down
32 changes: 5 additions & 27 deletions Stackless/pickling/prickelpit.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ static struct _typeobject wrap_##type = { \
};

static PyObject *types_mod = NULL;
static PyObject *pickle_reg = NULL;

static struct PyMethodDef _new_methoddef[] = {
{"__new__", (PyCFunction)_new_wrapper, METH_VARARGS | METH_KEYWORDS,
Expand All @@ -192,8 +191,7 @@ static int init_type(PyTypeObject *t, int (*initchain)(void))
{
PyMethodDescrObject *reduce;
PyWrapperDescrObject *init;
PyObject *retval = NULL, *func;
int ret = 0;
PyObject *func;
const char *name = strrchr(t->tp_name, '.')+1;

/* we patch the type to use *our* name, which makes no difference */
Expand All @@ -216,15 +214,9 @@ static int init_type(PyTypeObject *t, int (*initchain)(void))
func = PyCFunction_New(_new_methoddef, (PyObject *)t);
if (func == NULL || PyDict_SetItemString(t->tp_dict, "__new__", func))
return -1;
/* register with copy_reg */
if (pickle_reg != NULL &&
(retval = PyObject_CallFunction(pickle_reg, "OO",
t->tp_base, reduce)) == NULL)
ret = -1;
Py_XDECREF(retval);
if (ret == 0 && initchain != NULL)
ret = initchain();
return ret;
if (initchain != NULL)
return initchain();
return 0;
}

/* root of init function chain */
Expand Down Expand Up @@ -2491,29 +2483,15 @@ static struct PyModuleDef _wrapmodule = {
PyObject*
init_prickelpit(void)
{
PyObject *copy_reg, *tmp;
PyObject *tmp;

types_mod = PyModule_Create(&_wrapmodule);
if (types_mod == NULL)
return NULL;
copy_reg = PyImport_ImportModule("copyreg");
if (copy_reg == NULL) {
Py_CLEAR(types_mod);
return NULL;
}

pickle_reg = PyObject_GetAttrString(copy_reg, "pickle");
Py_DECREF(copy_reg);
if (pickle_reg == NULL) {
Py_CLEAR(types_mod);
return NULL;
}
if (initchain()) {
Py_CLEAR(pickle_reg);
Py_CLEAR(types_mod);
return NULL;
}
Py_CLEAR(pickle_reg);
tmp = types_mod;
types_mod = NULL;
return tmp;
Expand Down

0 comments on commit 056de5a

Please sign in to comment.