Commit 72e600ce authored by Joseph Weston's avatar Joseph Weston
Browse files

modify module creation

Followed the advice here
http://python3porting.com/cextensions.html#module-initialization
on porting module creation. Should remain compatible with
Python2 this way.
parent ac49b44c
......@@ -1186,7 +1186,7 @@ Whenever an operation is missing from Tinyarray, NumPy can be used directly,\n\
e.g.: numpy.linalg.det(my_tinyarray).");
extern "C"
void inittinyarray()
MOD_INIT_FUNC(tinyarray)
{
// Determine storage formats.
bool be = is_big_endian();
......@@ -1205,11 +1205,12 @@ void inittinyarray()
else
format_by_dtype[int(LONG)] = UNKNOWN;
if (PyType_Ready(&Array<long>::pytype) < 0) return;
if (PyType_Ready(&Array<double>::pytype) < 0) return;
if (PyType_Ready(&Array<Complex>::pytype) < 0) return;
if (PyType_Ready(&Array<long>::pytype) < 0) return MOD_ERROR_VAL;
if (PyType_Ready(&Array<double>::pytype) < 0) return MOD_ERROR_VAL;
if (PyType_Ready(&Array<Complex>::pytype) < 0) return MOD_ERROR_VAL;
PyObject *m = Py_InitModule3("tinyarray", functions, tinyarray_doc);
PyObject *m;
MOD_DEF(m, "tinyarray", functions, tinyarray_doc);
reconstruct = PyObject_GetAttrString(m, "_reconstruct");
......@@ -1239,15 +1240,17 @@ void inittinyarray()
// interpreter does the same, see try_complex_special_method in
// complexobject.c
int_str = PyString_InternFromString("__int__");
if (int_str == 0) return;
if (int_str == 0) return MOD_ERROR_VAL;
long_str = PyString_InternFromString("__long__");
if (long_str == 0) return;
if (long_str == 0) return MOD_ERROR_VAL;
float_str = PyString_InternFromString("__float__");
if (float_str == 0) return;
if (float_str == 0) return MOD_ERROR_VAL;
complex_str = PyString_InternFromString("__complex__");
if (complex_str == 0) return;
if (complex_str == 0) return MOD_ERROR_VAL;
index_str = PyString_InternFromString("__index__");
if (complex_str == 0) return;
if (complex_str == 0) return MOD_ERROR_VAL;
return MOD_SUCCESS_VAL(m);
}
int load_index_seq_as_long(PyObject *obj, long *out, int maxlen)
......
......@@ -55,7 +55,40 @@ protected:
PyVarObject ob_base;
};
extern "C" void inittinyarray();
// Python 3 support macros for module creation
#if PY_MAJOR_VERSION >= 3
// module creation
#define MOD_DEF(ob, name, methods, doc) \
static struct PyModuleDef moduledef = { \
PyModuleDef_HEAD_INIT, \
name, \
doc, \
-1, \
methods, \
}; \
ob = PyModule_Create(&moduledef);
// init function declaration
#define MOD_INIT_FUNC(name) PyMODINIT_FUNC PyInit_##name()
// init function declaration for use in Array class def
#define MOD_INIT_FRIEND(name) PyObject* PyInit_##name()
// init function return values
#define MOD_ERROR_VAL NULL
#define MOD_SUCCESS_VAL(val) val
// Python 2
#else
// module creation
#define MOD_DEF(ob, name, methods, doc) \
ob = Py_InitModule3(name, methods, doc);
// init function declaration
#define MOD_INIT_FUNC(name) PyMODINIT_FUNC init##name()
// init function declaration for use in Array class def
#define MOD_INIT_FRIEND(name) void init##name()
// init function return values
#define MOD_ERROR_VAL
#define MOD_SUCCESS_VAL(val)
#endif
MOD_INIT_FUNC(tinyarray);
template <typename T>
class Array : public Array_base {
......@@ -90,7 +123,7 @@ private:
static PyTypeObject pytype;
friend Dtype get_dtype(PyObject *obj);
friend void inittinyarray();
friend MOD_INIT_FRIEND(tinyarray);
};
int load_index_seq_as_long(PyObject *obj, long *out, int maxlen);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment