Skip to content

Commit 056de5a

Browse files
author
Anselm Kruis
committed
Issue python#107: move the registration of reduce functions into module 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
1 parent 30770d7 commit 056de5a

File tree

2 files changed

+12
-27
lines changed

2 files changed

+12
-27
lines changed

Lib/stackless.py

+7
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,14 @@ def transmogrify():
7373
this function creates a subclass of the ModuleType with properties.
7474
Stackless has historically had module properties, something very unusual in Python.
7575
We need to do that by replacing the current module object as it is being created
76+
77+
Additionally this function performs a few initialisations.
7678
"""
79+
from copyreg import pickle
80+
for name in dir(_wrap):
81+
cls = getattr(_wrap, name, None)
82+
if isinstance(cls, type):
83+
pickle(cls.__bases__[0], cls.__reduce__)
7784

7885
class StacklessModuleType(types.ModuleType):
7986

Stackless/pickling/prickelpit.c

+5-27
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ static struct _typeobject wrap_##type = { \
179179
};
180180

181181
static PyObject *types_mod = NULL;
182-
static PyObject *pickle_reg = NULL;
183182

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

199197
/* we patch the type to use *our* name, which makes no difference */
@@ -216,15 +214,9 @@ static int init_type(PyTypeObject *t, int (*initchain)(void))
216214
func = PyCFunction_New(_new_methoddef, (PyObject *)t);
217215
if (func == NULL || PyDict_SetItemString(t->tp_dict, "__new__", func))
218216
return -1;
219-
/* register with copy_reg */
220-
if (pickle_reg != NULL &&
221-
(retval = PyObject_CallFunction(pickle_reg, "OO",
222-
t->tp_base, reduce)) == NULL)
223-
ret = -1;
224-
Py_XDECREF(retval);
225-
if (ret == 0 && initchain != NULL)
226-
ret = initchain();
227-
return ret;
217+
if (initchain != NULL)
218+
return initchain();
219+
return 0;
228220
}
229221

230222
/* root of init function chain */
@@ -2491,29 +2483,15 @@ static struct PyModuleDef _wrapmodule = {
24912483
PyObject*
24922484
init_prickelpit(void)
24932485
{
2494-
PyObject *copy_reg, *tmp;
2486+
PyObject *tmp;
24952487

24962488
types_mod = PyModule_Create(&_wrapmodule);
24972489
if (types_mod == NULL)
24982490
return NULL;
2499-
copy_reg = PyImport_ImportModule("copyreg");
2500-
if (copy_reg == NULL) {
2501-
Py_CLEAR(types_mod);
2502-
return NULL;
2503-
}
2504-
2505-
pickle_reg = PyObject_GetAttrString(copy_reg, "pickle");
2506-
Py_DECREF(copy_reg);
2507-
if (pickle_reg == NULL) {
2508-
Py_CLEAR(types_mod);
2509-
return NULL;
2510-
}
25112491
if (initchain()) {
2512-
Py_CLEAR(pickle_reg);
25132492
Py_CLEAR(types_mod);
25142493
return NULL;
25152494
}
2516-
Py_CLEAR(pickle_reg);
25172495
tmp = types_mod;
25182496
types_mod = NULL;
25192497
return tmp;

0 commit comments

Comments
 (0)