2
/************************************************************/
3
/*** C header subsection: CPython-extension-module-ness ***/
5
#ifdef COUNT_OP_MALLOCS
6
# define METHODDEF_MALLOC_COUNTERS \
7
{ "malloc_counters", malloc_counters, METH_VARARGS },
9
# define METHODDEF_MALLOC_COUNTERS /* nothing */
12
#define METHODDEF_DEBUGINFO /* nothing, unless overridden by g_debuginfo.h */
14
#define MODULE_INITFUNC(modname) \
15
static PyMethodDef my_methods[] = { \
16
METHODDEF_MALLOC_COUNTERS \
18
{ (char *)NULL, (PyCFunction)NULL } }; \
19
PyMODINIT_FUNC init##modname(void)
21
#define SETUP_MODULE(modname) \
23
PyObject *m = Py_InitModule(#modname, my_methods); \
24
PyModule_AddStringConstant(m, "__sourcefile__", __FILE__); \
25
this_module_globals = PyModule_GetDict(m); \
26
PyGenCFunction_Type.tp_base = &PyCFunction_Type; \
27
PyGenCFunction_Type.tp_getset = PyCFunction_Type.tp_getset; \
28
PyType_Ready(&PyGenCFunction_Type); \
29
RPythonError = PyErr_NewException(#modname ".RPythonError", \
31
if (RPythonError == NULL) \
33
PyModule_AddObject(m, "RPythonError", RPythonError); \
34
errmsg = RPython_StartupCode(); \
36
PyErr_SetString(PyExc_RuntimeError, errmsg); \
39
if (setup_globalfunctions(globalfunctiondefs, #modname) < 0) \
41
if (setup_exportglobalobjects(cpyobjheaddefs) < 0) \
43
if (setup_initcode(frozen_initcode, FROZEN_INITCODE_SIZE) < 0) \
45
if (setup_globalobjects(globalobjectdefs, cpyobjheaddefs) < 0) \
48
/*** table of global objects ***/
50
static PyObject *this_module_globals;
60
void (*setupfn)(PyObject *);
67
} globalfunctiondef_t;
69
/* helper-hook for post-setup */
70
static globalfunctiondef_t *globalfunctiondefsptr;
71
static PyObject *postsetup_get_typedict(PyObject *tp);
72
static PyObject *postsetup_get_methodname(int funcidx);
73
static PyObject *postsetup_build_method(int funcidx, PyObject *type);
74
int call_postsetup(PyObject *m);
78
#ifndef PYPY_NOT_MAIN_FILE
80
static int setup_exportglobalobjects(cpyobjheaddef_t* cpyheadtable)
83
cpyobjheaddef_t* cpydef;
85
/* Store the object given by their heads into the module's dict.
86
Warning: these object heads might still be invalid, e.g.
87
typically their ob_type needs patching!
88
But PyDict_SetItemString() doesn't inspect them...
90
for (cpydef = cpyheadtable; cpydef->name != NULL; cpydef++) {
92
if (obj->ob_type == NULL)
93
obj->ob_type = &PyType_Type;
94
if (PyDict_SetItemString(this_module_globals,
95
cpydef->name, obj) < 0)
101
static int setup_globalobjects(globalobjectdef_t* globtable,
102
cpyobjheaddef_t* cpyheadtable)
105
globalobjectdef_t* def;
106
cpyobjheaddef_t* cpydef;
108
/* Patch all locations that need to contain a specific PyObject*.
109
This must go after the previous loop, otherwise
110
PyDict_GetItemString() might not find some of them.
112
for (def = globtable; def->p != NULL; def++) {
113
obj = PyDict_GetItemString(this_module_globals, def->name);
115
PyErr_Format(PyExc_AttributeError,
116
"initialization code should have "
117
"created '%s'", def->name);
121
*def->p = obj; /* store the object ref in the global var */
123
/* All objects should be valid at this point. Loop again and
124
make sure all types are ready.
126
for (cpydef = cpyheadtable; cpydef->name != NULL; cpydef++) {
127
obj = cpydef->cpyobj;
128
if (PyType_Check(obj)) {
131
if (PyType_Ready((PyTypeObject*) obj) < 0)
135
/* call the user-defined setups *after* all types are ready
136
* in case of dependencies */
137
for (cpydef = cpyheadtable; cpydef->name != NULL; cpydef++) {
138
obj = cpydef->cpyobj;
139
if (cpydef->setupfn) {
140
cpydef->setupfn(obj);
141
if (RPyExceptionOccurred()) {
142
RPyConvertExceptionToCPython();
150
static int setup_globalfunctions(globalfunctiondef_t* def, char* modname)
153
PyObject* modname_o = PyString_FromString(modname);
154
if (modname_o == NULL)
157
for (; def->p != NULL; def++) {
158
fn = PyCFunction_NewEx(&def->ml, NULL, modname_o);
161
fn->ob_type = &PyGenCFunction_Type;
162
*def->p = fn; /* store the object ref in the global var */
164
if (PyDict_SetItemString(this_module_globals,
172
static int setup_initcode(char* frozendata[], int len)
178
int chunk, count = 0;
180
buffer = PyMem_NEW(char, len);
184
while (count < len) {
185
chunk = len-count < 1024 ? len-count : 1024;
186
memcpy(bufp, *frozendata, chunk);
191
co = PyMarshal_ReadObjectFromString(buffer, len);
195
if (!PyCode_Check(co)) {
196
PyErr_SetString(PyExc_TypeError, "uh?");
199
globals = this_module_globals;
200
if (PyDict_GetItemString(globals, "__builtins__") == NULL)
201
PyDict_SetItemString(globals, "__builtins__",
202
PyEval_GetBuiltins());
203
res = PyEval_EvalCode((PyCodeObject *) co, globals, globals);
210
static PyObject *postsetup_get_typedict(PyObject *tp)
212
PyTypeObject *type = (PyTypeObject *)tp;
220
static PyObject *postsetup_get_methodname(int funcidx)
222
globalfunctiondef_t *gfuncdef = &globalfunctiondefsptr[funcidx];
225
return PyString_FromString(gfuncdef->gfunc_name);
230
static PyObject *postsetup_build_method(int funcidx, PyObject *type)
232
globalfunctiondef_t *gfuncdef = &globalfunctiondefsptr[funcidx];
235
return PyDescr_NewMethod((PyTypeObject *)type, &gfuncdef->ml);
240
int call_postsetup(PyObject *m)
242
PyObject *init, *ret;
244
init = PyDict_GetItemString(this_module_globals, "__init__");
249
ret = PyObject_CallFunction(init, "O", m);
255
#endif /* PYPY_NOT_MAIN_FILE */