Commit b4cda4da authored by Tres Seaver's avatar Tres Seaver

Fix module initialization dance under Py3k.

parent f108be3a
...@@ -480,38 +480,38 @@ static struct PyModuleDef moduledef = { ...@@ -480,38 +480,38 @@ static struct PyModuleDef moduledef = {
#endif #endif
void static PyObject*
INITMODULE (void) module_init(void)
{ {
PyObject *m, *d, *c; PyObject *module, *mod_dict, *interfaces, *conflicterr;
#ifdef KEY_TYPE_IS_PYOBJECT #ifdef KEY_TYPE_IS_PYOBJECT
object_ = PyTuple_GetItem(Py_None->ob_type->tp_bases, 0); object_ = PyTuple_GetItem(Py_TYPE(Py_None)->tp_bases, 0);
if (object_ == NULL) if (object_ == NULL)
return; return NULL;
#endif #endif
sort_str = INTERN("sort"); sort_str = INTERN("sort");
if (!sort_str) if (!sort_str)
return; return NULL;
reverse_str = INTERN("reverse"); reverse_str = INTERN("reverse");
if (!reverse_str) if (!reverse_str)
return; return NULL;
__setstate___str = INTERN("__setstate__"); __setstate___str = INTERN("__setstate__");
if (!__setstate___str) if (!__setstate___str)
return; return NULL;
_bucket_type_str = INTERN("_bucket_type"); _bucket_type_str = INTERN("_bucket_type");
if (!_bucket_type_str) if (!_bucket_type_str)
return; return NULL;
/* Grab the ConflictError class */ /* Grab the ConflictError class */
m = PyImport_ImportModule("BTrees.Interfaces"); interfaces = PyImport_ImportModule("BTrees.Interfaces");
if (m != NULL) if (interfaces != NULL)
{ {
c = PyObject_GetAttrString(m, "BTreesConflictError"); conflicterr = PyObject_GetAttrString(interfaces, "BTreesConflictError");
if (c != NULL) if (conflicterr != NULL)
ConflictError = c; ConflictError = conflicterr;
Py_DECREF(m); Py_DECREF(interfaces);
} }
if (ConflictError == NULL) if (ConflictError == NULL)
...@@ -529,7 +529,7 @@ INITMODULE (void) ...@@ -529,7 +529,7 @@ INITMODULE (void)
"persistent.cPersistence", "CAPI"); "persistent.cPersistence", "CAPI");
#endif #endif
if (cPersistenceCAPI == NULL) if (cPersistenceCAPI == NULL)
return; return NULL;
#ifdef PY3K #ifdef PY3K
#define _SET_TYPE(typ) ((PyObject*)(&typ))->ob_type = &PyType_Type #define _SET_TYPE(typ) ((PyObject*)(&typ))->ob_type = &PyType_Type
...@@ -544,75 +544,88 @@ INITMODULE (void) ...@@ -544,75 +544,88 @@ INITMODULE (void)
BTreeType.tp_new = PyType_GenericNew; BTreeType.tp_new = PyType_GenericNew;
TreeSetType.tp_new = PyType_GenericNew; TreeSetType.tp_new = PyType_GenericNew;
if (!init_persist_type(&BucketType)) if (!init_persist_type(&BucketType))
return; return NULL;
if (!init_persist_type(&BTreeType)) if (!init_persist_type(&BTreeType))
return; return NULL;
if (!init_persist_type(&SetType)) if (!init_persist_type(&SetType))
return; return NULL;
if (!init_persist_type(&TreeSetType)) if (!init_persist_type(&TreeSetType))
return; return NULL;
if (PyDict_SetItem(BTreeType.tp_dict, _bucket_type_str, if (PyDict_SetItem(BTreeType.tp_dict, _bucket_type_str,
(PyObject *)&BucketType) < 0) (PyObject *)&BucketType) < 0)
{ {
fprintf(stderr, "btree failed\n"); fprintf(stderr, "btree failed\n");
return; return NULL;
} }
if (PyDict_SetItem(TreeSetType.tp_dict, _bucket_type_str, if (PyDict_SetItem(TreeSetType.tp_dict, _bucket_type_str,
(PyObject *)&SetType) < 0) (PyObject *)&SetType) < 0)
{ {
fprintf(stderr, "bucket failed\n"); fprintf(stderr, "bucket failed\n");
return; return NULL;
} }
/* Create the module and add the functions */
#ifdef PY3K #ifdef PY3K
m = PyModule_Create(&moduledef); module = PyModule_Create(&moduledef);
#else #else
/* Create the module and add the functions */ module = Py_InitModule4("_" MOD_NAME_PREFIX "BTree",
m = Py_InitModule4("_" MOD_NAME_PREFIX "BTree",
module_methods, BTree_module_documentation, module_methods, BTree_module_documentation,
(PyObject *)NULL, PYTHON_API_VERSION); (PyObject *)NULL, PYTHON_API_VERSION);
#endif #endif
/* Add some symbolic constants to the module */ /* Add some symbolic constants to the module */
d = PyModule_GetDict(m); mod_dict = PyModule_GetDict(module);
if (PyDict_SetItemString(d, MOD_NAME_PREFIX "Bucket", if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "Bucket",
(PyObject *)&BucketType) < 0) (PyObject *)&BucketType) < 0)
return; return NULL;
if (PyDict_SetItemString(d, MOD_NAME_PREFIX "BTree", if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "BTree",
(PyObject *)&BTreeType) < 0) (PyObject *)&BTreeType) < 0)
return; return NULL;
if (PyDict_SetItemString(d, MOD_NAME_PREFIX "Set", if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "Set",
(PyObject *)&SetType) < 0) (PyObject *)&SetType) < 0)
return; return NULL;
if (PyDict_SetItemString(d, MOD_NAME_PREFIX "TreeSet", if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "TreeSet",
(PyObject *)&TreeSetType) < 0) (PyObject *)&TreeSetType) < 0)
return; return NULL;
if (PyDict_SetItemString(d, MOD_NAME_PREFIX "TreeIterator", if (PyDict_SetItemString(mod_dict, MOD_NAME_PREFIX "TreeIterator",
(PyObject *)&BTreeIter_Type) < 0) (PyObject *)&BTreeIter_Type) < 0)
return; return NULL;
/* We also want to be able to access these constants without the prefix /* We also want to be able to access these constants without the prefix
* so that code can more easily exchange modules (particularly the integer * so that code can more easily exchange modules (particularly the integer
* and long modules, but also others). The TreeIterator is only internal, * and long modules, but also others). The TreeIterator is only internal,
* so we don't bother to expose that. * so we don't bother to expose that.
*/ */
if (PyDict_SetItemString(d, "Bucket", if (PyDict_SetItemString(mod_dict, "Bucket",
(PyObject *)&BucketType) < 0) (PyObject *)&BucketType) < 0)
return; return NULL;
if (PyDict_SetItemString(d, "BTree", if (PyDict_SetItemString(mod_dict, "BTree",
(PyObject *)&BTreeType) < 0) (PyObject *)&BTreeType) < 0)
return; return NULL;
if (PyDict_SetItemString(d, "Set", if (PyDict_SetItemString(mod_dict, "Set",
(PyObject *)&SetType) < 0) (PyObject *)&SetType) < 0)
return; return NULL;
if (PyDict_SetItemString(d, "TreeSet", if (PyDict_SetItemString(mod_dict, "TreeSet",
(PyObject *)&TreeSetType) < 0) (PyObject *)&TreeSetType) < 0)
return; return NULL;
#if defined(ZODB_64BIT_INTS) && defined(NEED_LONG_LONG_SUPPORT) #if defined(ZODB_64BIT_INTS) && defined(NEED_LONG_LONG_SUPPORT)
if (PyDict_SetItemString(d, "using64bits", Py_True) < 0) if (PyDict_SetItemString(mod_dict, "using64bits", Py_True) < 0)
return; return NULL;
#else #else
if (PyDict_SetItemString(d, "using64bits", Py_False) < 0) if (PyDict_SetItemString(mod_dict, "using64bits", Py_False) < 0)
return; return NULL;
#endif #endif
return module;
}
#ifdef PY3K
PyMODINIT_FUNC INITMODULE(void)
{
return module_init();
} }
#else
PyMODINIT_FUNC INITMODULE(void)
{
module_init();
}
#endif
...@@ -25,10 +25,17 @@ ...@@ -25,10 +25,17 @@
#define PERSISTENT #define PERSISTENT
#define MOD_NAME_PREFIX "IF" #define MOD_NAME_PREFIX "IF"
#define INITMODULE init_IFBTree
#define DEFAULT_MAX_BUCKET_SIZE 120 #define DEFAULT_MAX_BUCKET_SIZE 120
#define DEFAULT_MAX_BTREE_SIZE 500 #define DEFAULT_MAX_BTREE_SIZE 500
#include "_compat.h"
#include "intkeymacros.h" #include "intkeymacros.h"
#include "floatvaluemacros.h" #include "floatvaluemacros.h"
#ifdef PY3K
#define INITMODULE PyInit__IFBTree
#else
#define INITMODULE init_IFBTree
#endif
#include "BTreeModuleTemplate.c" #include "BTreeModuleTemplate.c"
...@@ -25,10 +25,17 @@ ...@@ -25,10 +25,17 @@
#define PERSISTENT #define PERSISTENT
#define MOD_NAME_PREFIX "II" #define MOD_NAME_PREFIX "II"
#define INITMODULE init_IIBTree
#define DEFAULT_MAX_BUCKET_SIZE 120 #define DEFAULT_MAX_BUCKET_SIZE 120
#define DEFAULT_MAX_BTREE_SIZE 500 #define DEFAULT_MAX_BTREE_SIZE 500
#include "_compat.h"
#include "intkeymacros.h" #include "intkeymacros.h"
#include "intvaluemacros.h" #include "intvaluemacros.h"
#ifdef PY3K
#define INITMODULE PyInit__IIBTree
#else
#define INITMODULE init_IIBTree
#endif
#include "BTreeModuleTemplate.c" #include "BTreeModuleTemplate.c"
...@@ -23,10 +23,17 @@ ...@@ -23,10 +23,17 @@
#define PERSISTENT #define PERSISTENT
#define MOD_NAME_PREFIX "IO" #define MOD_NAME_PREFIX "IO"
#define DEFAULT_MAX_BUCKET_SIZE 60 #define DEFAULT_MAX_BUCKET_SIZE 60
#define DEFAULT_MAX_BTREE_SIZE 500 #define DEFAULT_MAX_BTREE_SIZE 500
#define INITMODULE init_IOBTree
#include "_compat.h"
#include "intkeymacros.h" #include "intkeymacros.h"
#include "objectvaluemacros.h" #include "objectvaluemacros.h"
#ifdef PY3K
#define INITMODULE PyInit__IOBTree
#else
#define INITMODULE init_IOBTree
#endif
#include "BTreeModuleTemplate.c" #include "BTreeModuleTemplate.c"
...@@ -25,12 +25,19 @@ ...@@ -25,12 +25,19 @@
#define PERSISTENT #define PERSISTENT
#define MOD_NAME_PREFIX "LF" #define MOD_NAME_PREFIX "LF"
#define INITMODULE init_LFBTree
#define DEFAULT_MAX_BUCKET_SIZE 120 #define DEFAULT_MAX_BUCKET_SIZE 120
#define DEFAULT_MAX_BTREE_SIZE 500 #define DEFAULT_MAX_BTREE_SIZE 500
#define ZODB_64BIT_INTS #define ZODB_64BIT_INTS
#include "_compat.h"
#include "intkeymacros.h" #include "intkeymacros.h"
#include "floatvaluemacros.h" #include "floatvaluemacros.h"
#ifdef PY3K
#define INITMODULE PyInit__LFBTree
#else
#define INITMODULE init_LFBTree
#endif
#include "BTreeModuleTemplate.c" #include "BTreeModuleTemplate.c"
...@@ -25,12 +25,19 @@ ...@@ -25,12 +25,19 @@
#define PERSISTENT #define PERSISTENT
#define MOD_NAME_PREFIX "LL" #define MOD_NAME_PREFIX "LL"
#define INITMODULE init_LLBTree
#define DEFAULT_MAX_BUCKET_SIZE 120 #define DEFAULT_MAX_BUCKET_SIZE 120
#define DEFAULT_MAX_BTREE_SIZE 500 #define DEFAULT_MAX_BTREE_SIZE 500
#define ZODB_64BIT_INTS #define ZODB_64BIT_INTS
#include "_compat.h"
#include "intkeymacros.h" #include "intkeymacros.h"
#include "intvaluemacros.h" #include "intvaluemacros.h"
#ifdef PY3K
#define INITMODULE PyInit__LLBTree
#else
#define INITMODULE init_LLBTree
#endif
#include "BTreeModuleTemplate.c" #include "BTreeModuleTemplate.c"
...@@ -23,12 +23,19 @@ ...@@ -23,12 +23,19 @@
#define PERSISTENT #define PERSISTENT
#define MOD_NAME_PREFIX "LO" #define MOD_NAME_PREFIX "LO"
#define DEFAULT_MAX_BUCKET_SIZE 60 #define DEFAULT_MAX_BUCKET_SIZE 60
#define DEFAULT_MAX_BTREE_SIZE 500 #define DEFAULT_MAX_BTREE_SIZE 500
#define INITMODULE init_LOBTree
#define ZODB_64BIT_INTS #define ZODB_64BIT_INTS
#include "_compat.h"
#include "intkeymacros.h" #include "intkeymacros.h"
#include "objectvaluemacros.h" #include "objectvaluemacros.h"
#ifdef PY3K
#define INITMODULE PyInit__LOBTree
#else
#define INITMODULE init_LOBTree
#endif
#include "BTreeModuleTemplate.c" #include "BTreeModuleTemplate.c"
...@@ -23,10 +23,17 @@ ...@@ -23,10 +23,17 @@
#define PERSISTENT #define PERSISTENT
#define MOD_NAME_PREFIX "OI" #define MOD_NAME_PREFIX "OI"
#define INITMODULE init_OIBTree
#define DEFAULT_MAX_BUCKET_SIZE 60 #define DEFAULT_MAX_BUCKET_SIZE 60
#define DEFAULT_MAX_BTREE_SIZE 250 #define DEFAULT_MAX_BTREE_SIZE 250
#include "_compat.h"
#include "objectkeymacros.h" #include "objectkeymacros.h"
#include "intvaluemacros.h" #include "intvaluemacros.h"
#ifdef PY3K
#define INITMODULE PyInit__OIBTree
#else
#define INITMODULE init_OIBTree
#endif
#include "BTreeModuleTemplate.c" #include "BTreeModuleTemplate.c"
...@@ -23,12 +23,19 @@ ...@@ -23,12 +23,19 @@
#define PERSISTENT #define PERSISTENT
#define MOD_NAME_PREFIX "OL" #define MOD_NAME_PREFIX "OL"
#define INITMODULE init_OLBTree
#define DEFAULT_MAX_BUCKET_SIZE 60 #define DEFAULT_MAX_BUCKET_SIZE 60
#define DEFAULT_MAX_BTREE_SIZE 250 #define DEFAULT_MAX_BTREE_SIZE 250
#define ZODB_64BIT_INTS #define ZODB_64BIT_INTS
#include "_compat.h"
#include "objectkeymacros.h" #include "objectkeymacros.h"
#include "intvaluemacros.h" #include "intvaluemacros.h"
#ifdef PY3K
#define INITMODULE PyInit__OLBTree
#else
#define INITMODULE init_OLBTree
#endif
#include "BTreeModuleTemplate.c" #include "BTreeModuleTemplate.c"
...@@ -23,10 +23,17 @@ ...@@ -23,10 +23,17 @@
#define PERSISTENT #define PERSISTENT
#define MOD_NAME_PREFIX "OO" #define MOD_NAME_PREFIX "OO"
#define INITMODULE init_OOBTree
#define DEFAULT_MAX_BUCKET_SIZE 30 #define DEFAULT_MAX_BUCKET_SIZE 30
#define DEFAULT_MAX_BTREE_SIZE 250 #define DEFAULT_MAX_BTREE_SIZE 250
#include "_compat.h"
#include "objectkeymacros.h" #include "objectkeymacros.h"
#include "objectvaluemacros.h" #include "objectvaluemacros.h"
#ifdef PY3K
#define INITMODULE PyInit__OOBTree
#else
#define INITMODULE init_OOBTree
#endif
#include "BTreeModuleTemplate.c" #include "BTreeModuleTemplate.c"
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#ifndef BTREES__COMPAT_H #ifndef BTREES__COMPAT_H
#define BTREES__COMPAT_H #define BTREES__COMPAT_H
#if PY_MAJOR_VERSION >= 3 #include "Python.h"
#ifdef INTERN #ifdef INTERN
#undef INTERN #undef INTERN
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
#undef INT_CHECK #undef INT_CHECK
#endif #endif
#if PY_MAJOR_VERSION >= 3
#define PY3K #define PY3K
#define INTERN PyUnicode_InternFromString #define INTERN PyUnicode_InternFromString
...@@ -30,6 +32,7 @@ ...@@ -30,6 +32,7 @@
PyObject_RichCompareBool((lhs), (rhs), Py_LT) > 0 ? -1 : \ PyObject_RichCompareBool((lhs), (rhs), Py_LT) > 0 ? -1 : \
(PyObject_RichCompareBool((lhs), (rhs), Py_EQ) > 0 ? 0 : 1) (PyObject_RichCompareBool((lhs), (rhs), Py_EQ) > 0 ? 0 : 1)
#else #else
#define INTERN PyString_InternFromString #define INTERN PyString_InternFromString
......
...@@ -31,10 +31,11 @@ typedef unsigned char char6[6]; ...@@ -31,10 +31,11 @@ typedef unsigned char char6[6];
#define PERSISTENT #define PERSISTENT
#define MOD_NAME_PREFIX "fs" #define MOD_NAME_PREFIX "fs"
#define INITMODULE init_fsBTree
#define DEFAULT_MAX_BUCKET_SIZE 500 #define DEFAULT_MAX_BUCKET_SIZE 500
#define DEFAULT_MAX_BTREE_SIZE 500 #define DEFAULT_MAX_BTREE_SIZE 500
#include "_compat.h"
/*#include "intkeymacros.h"*/ /*#include "intkeymacros.h"*/
#define KEYMACROS_H "$Id$\n" #define KEYMACROS_H "$Id$\n"
...@@ -80,6 +81,11 @@ static PyObject *bucket_fromString(PyObject *self, PyObject *state); ...@@ -80,6 +81,11 @@ static PyObject *bucket_fromString(PyObject *self, PyObject *state);
{"fromString", (PyCFunction) bucket_fromString, METH_O, \ {"fromString", (PyCFunction) bucket_fromString, METH_O, \
"fromString(s) -- Set the state of the object from a string"}, \ "fromString(s) -- Set the state of the object from a string"}, \
#ifdef PY3K
#define INITMODULE PyInit__fsBTree
#else
#define INITMODULE init_fsBTree
#endif
#include "BTreeModuleTemplate.c" #include "BTreeModuleTemplate.c"
static PyObject * static PyObject *
......
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