Commit 91bb4e5b authored by Tres Seaver's avatar Tres Seaver

All extensions building w/ tests passing under Python 3.2.

parent 6f25092b
...@@ -13,8 +13,20 @@ ...@@ -13,8 +13,20 @@
****************************************************************************/ ****************************************************************************/
#include "Python.h" #include "Python.h"
#include "bytesobject.h"
#include <time.h> #include <time.h>
#if PY_MAJOR_VERSION >= 3
#define PY3K
#define Py_TPFLAGS_HAVE_RICHCOMPARE 0
#endif
#ifdef PY3K
#define INT_FROM_LONG(x) PyLong_FromLong(x)
#else
#define INT_FROM_LONG(x) PyInt_FromLong(x)
#endif
PyObject *TimeStamp_FromDate(int, int, int, int, int, double); PyObject *TimeStamp_FromDate(int, int, int, int, int, double);
PyObject *TimeStamp_FromString(const char *); PyObject *TimeStamp_FromString(const char *);
...@@ -106,13 +118,41 @@ TimeStamp_dealloc(TimeStamp *ts) ...@@ -106,13 +118,41 @@ TimeStamp_dealloc(TimeStamp *ts)
PyObject_Del(ts); PyObject_Del(ts);
} }
static int static PyObject*
TimeStamp_compare(TimeStamp *v, TimeStamp *w) TimeStamp_richcompare(TimeStamp *self, TimeStamp *other, int op)
{ {
int cmp = memcmp(v->data, w->data, 8); PyObject *result = NULL;
if (cmp < 0) return -1; int cmp;
if (cmp > 0) return 1;
return 0; if (Py_TYPE(other) != Py_TYPE(self)) {
result = Py_NotImplemented;
}
else {
cmp = memcmp(self->data, other->data, 8);
switch (op) {
case Py_LT:
result = (cmp < 0) ? Py_True : Py_False;
break;
case Py_LE:
result = (cmp <= 0) ? Py_True : Py_False;
break;
case Py_EQ:
result = (cmp == 0) ? Py_True : Py_False;
break;
case Py_NE:
result = (cmp != 0) ? Py_True : Py_False;
break;
case Py_GT:
result = (cmp > 0) ? Py_True : Py_False;
break;
case Py_GE:
result = (cmp >= 0) ? Py_True : Py_False;
break;
}
}
Py_XINCREF(result);
return result;
} }
static long static long
...@@ -165,7 +205,7 @@ TimeStamp_year(TimeStamp *self) ...@@ -165,7 +205,7 @@ TimeStamp_year(TimeStamp *self)
{ {
TimeStampParts p; TimeStampParts p;
TimeStamp_unpack(self, &p); TimeStamp_unpack(self, &p);
return PyInt_FromLong(p.y); return INT_FROM_LONG(p.y);
} }
static PyObject * static PyObject *
...@@ -173,7 +213,7 @@ TimeStamp_month(TimeStamp *self) ...@@ -173,7 +213,7 @@ TimeStamp_month(TimeStamp *self)
{ {
TimeStampParts p; TimeStampParts p;
TimeStamp_unpack(self, &p); TimeStamp_unpack(self, &p);
return PyInt_FromLong(p.m); return INT_FROM_LONG(p.m);
} }
static PyObject * static PyObject *
...@@ -181,7 +221,7 @@ TimeStamp_day(TimeStamp *self) ...@@ -181,7 +221,7 @@ TimeStamp_day(TimeStamp *self)
{ {
TimeStampParts p; TimeStampParts p;
TimeStamp_unpack(self, &p); TimeStamp_unpack(self, &p);
return PyInt_FromLong(p.d); return INT_FROM_LONG(p.d);
} }
static PyObject * static PyObject *
...@@ -189,7 +229,7 @@ TimeStamp_hour(TimeStamp *self) ...@@ -189,7 +229,7 @@ TimeStamp_hour(TimeStamp *self)
{ {
TimeStampParts p; TimeStampParts p;
TimeStamp_unpack(self, &p); TimeStamp_unpack(self, &p);
return PyInt_FromLong(p.mi / 60); return INT_FROM_LONG(p.mi / 60);
} }
static PyObject * static PyObject *
...@@ -197,7 +237,7 @@ TimeStamp_minute(TimeStamp *self) ...@@ -197,7 +237,7 @@ TimeStamp_minute(TimeStamp *self)
{ {
TimeStampParts p; TimeStampParts p;
TimeStamp_unpack(self, &p); TimeStamp_unpack(self, &p);
return PyInt_FromLong(p.mi % 60); return INT_FROM_LONG(p.mi % 60);
} }
static PyObject * static PyObject *
...@@ -218,7 +258,7 @@ TimeStamp_timeTime(TimeStamp *self) ...@@ -218,7 +258,7 @@ TimeStamp_timeTime(TimeStamp *self)
static PyObject * static PyObject *
TimeStamp_raw(TimeStamp *self) TimeStamp_raw(TimeStamp *self)
{ {
return PyString_FromStringAndSize((const char*)self->data, 8); return PyBytes_FromStringAndSize((const char*)self->data, 8);
} }
static PyObject * static PyObject *
...@@ -233,7 +273,7 @@ TimeStamp_str(TimeStamp *self) ...@@ -233,7 +273,7 @@ TimeStamp_str(TimeStamp *self)
p.y, p.m, p.d, p.mi / 60, p.mi % 60, p.y, p.m, p.d, p.mi / 60, p.mi % 60,
TimeStamp_sec(self)); TimeStamp_sec(self));
return PyString_FromStringAndSize(buf, len); return PyBytes_FromStringAndSize(buf, len);
} }
...@@ -245,7 +285,7 @@ TimeStamp_laterThan(TimeStamp *self, PyObject *obj) ...@@ -245,7 +285,7 @@ TimeStamp_laterThan(TimeStamp *self, PyObject *obj)
unsigned char new[8]; unsigned char new[8];
int i; int i;
if (obj->ob_type != self->ob_type) { if (Py_TYPE(obj) != Py_TYPE(self)) {
PyErr_SetString(PyExc_TypeError, "expected TimeStamp object"); PyErr_SetString(PyExc_TypeError, "expected TimeStamp object");
return NULL; return NULL;
} }
...@@ -298,42 +338,45 @@ static struct PyMethodDef TimeStamp_methods[] = { ...@@ -298,42 +338,45 @@ static struct PyMethodDef TimeStamp_methods[] = {
{NULL, NULL}, {NULL, NULL},
}; };
#define DEFERRED_ADDRESS(ADDR) 0
static PyTypeObject TimeStamp_type = { static PyTypeObject TimeStamp_type = {
PyObject_HEAD_INIT(NULL) PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(NULL), 0)
0,
"persistent.TimeStamp", "persistent.TimeStamp",
sizeof(TimeStamp), sizeof(TimeStamp), /* tp_basicsize */
0, 0, /* tp_itemsize */
(destructor)TimeStamp_dealloc, /* tp_dealloc */ (destructor)TimeStamp_dealloc, /* tp_dealloc */
0, /* tp_print */ 0, /* tp_print */
0, /* tp_getattr */ 0, /* tp_getattr */
0, /* tp_setattr */ 0, /* tp_setattr */
(cmpfunc)TimeStamp_compare, /* tp_compare */ 0, /* tp_compare */
(reprfunc)TimeStamp_raw, /* tp_repr */ (reprfunc)TimeStamp_raw, /* tp_repr */
0, /* tp_as_number */ 0, /* tp_as_number */
0, /* tp_as_sequence */ 0, /* tp_as_sequence */
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
(hashfunc)TimeStamp_hash, /* tp_hash */ (hashfunc)TimeStamp_hash, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
(reprfunc)TimeStamp_str, /* tp_str */ (reprfunc)TimeStamp_str, /* tp_str */
0, /* tp_getattro */ 0, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ Py_TPFLAGS_DEFAULT |
0, /* tp_doc */ Py_TPFLAGS_BASETYPE |
0, /* tp_traverse */ Py_TPFLAGS_HAVE_RICHCOMPARE, /* tp_flags */
0, /* tp_clear */ 0, /* tp_doc */
0, /* tp_richcompare */ 0, /* tp_traverse */
0, /* tp_weaklistoffset */ 0, /* tp_clear */
0, /* tp_iter */ (richcmpfunc)&TimeStamp_richcompare, /* tp_richcompare */
0, /* tp_iternext */ 0, /* tp_weaklistoffset */
TimeStamp_methods, /* tp_methods */ 0, /* tp_iter */
0, /* tp_members */ 0, /* tp_iternext */
0, /* tp_getset */ TimeStamp_methods, /* tp_methods */
0, /* tp_base */ 0, /* tp_members */
0, /* tp_dict */ 0, /* tp_getset */
0, /* tp_descr_get */ 0, /* tp_base */
0, /* tp_descr_set */ 0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
}; };
PyObject * PyObject *
...@@ -418,6 +461,20 @@ static PyMethodDef TimeStampModule_functions[] = { ...@@ -418,6 +461,20 @@ static PyMethodDef TimeStampModule_functions[] = {
{NULL, NULL}, {NULL, NULL},
}; };
#ifdef PY3K
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_timestamp", /* m_name */
TimeStampModule_doc, /* m_doc */
-1, /* m_size */
TimeStampModule_functions, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
#endif
void void
init_timestamp(void) init_timestamp(void)
...@@ -427,11 +484,19 @@ init_timestamp(void) ...@@ -427,11 +484,19 @@ init_timestamp(void)
if (TimeStamp_init_gmoff() < 0) if (TimeStamp_init_gmoff() < 0)
return; return;
#ifdef PY3K
m = PyModule_Create(&moduledef);
#else
m = Py_InitModule4("_timestamp", TimeStampModule_functions, m = Py_InitModule4("_timestamp", TimeStampModule_functions,
TimeStampModule_doc, NULL, PYTHON_API_VERSION); TimeStampModule_doc, NULL, PYTHON_API_VERSION);
#endif
if (m == NULL) if (m == NULL)
return; return;
#ifdef PY3K
((PyObject*)&TimeStamp_type)->ob_type = &PyType_Type;
#else
TimeStamp_type.ob_type = &PyType_Type; TimeStamp_type.ob_type = &PyType_Type;
#endif
TimeStamp_type.tp_getattro = PyObject_GenericGetAttr; TimeStamp_type.tp_getattro = PyObject_GenericGetAttr;
} }
...@@ -1272,37 +1272,38 @@ static struct PyMethodDef Per_methods[] = { ...@@ -1272,37 +1272,38 @@ static struct PyMethodDef Per_methods[] = {
#define DEFERRED_ADDRESS(ADDR) 0 #define DEFERRED_ADDRESS(ADDR) 0
static PyTypeObject Pertype = { static PyTypeObject Pertype = {
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyPersist_MetaType), 0) PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
"persistent.Persistent", /* tp_name */ "persistent.Persistent", /* tp_name */
sizeof(cPersistentObject), /* tp_basicsize */ sizeof(cPersistentObject), /* tp_basicsize */
0, /* tp_itemsize */ 0, /* tp_itemsize */
(destructor)Per_dealloc, /* tp_dealloc */ (destructor)Per_dealloc, /* tp_dealloc */
0, /* tp_print */ 0, /* tp_print */
0, /* tp_getattr */ 0, /* tp_getattr */
0, /* tp_setattr */ 0, /* tp_setattr */
0, /* tp_compare */ 0, /* tp_compare */
0, /* tp_repr */ 0, /* tp_repr */
0, /* tp_as_number */ 0, /* tp_as_number */
0, /* tp_as_sequence */ 0, /* tp_as_sequence */
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
0, /* tp_hash */ 0, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
0, /* tp_str */ 0, /* tp_str */
(getattrofunc)Per_getattro, /* tp_getattro */ (getattrofunc)Per_getattro, /* tp_getattro */
(setattrofunc)Per_setattro, /* tp_setattro */ (setattrofunc)Per_setattro, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, Py_TPFLAGS_DEFAULT |
/* tp_flags */ Py_TPFLAGS_BASETYPE |
0, /* tp_doc */ Py_TPFLAGS_HAVE_GC, /* tp_flags */
0, /* tp_doc */
(traverseproc)Per_traverse, /* tp_traverse */ (traverseproc)Per_traverse, /* tp_traverse */
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
0, /* tp_iter */ 0, /* tp_iter */
0, /* tp_iternext */ 0, /* tp_iternext */
Per_methods, /* tp_methods */ Per_methods, /* tp_methods */
0, /* tp_members */ 0, /* tp_members */
Per_getsets, /* tp_getset */ Per_getsets, /* tp_getset */
}; };
/* End of code for Persistent objects */ /* End of code for Persistent objects */
......
...@@ -1254,41 +1254,42 @@ static PyMemberDef cc_members[] = { ...@@ -1254,41 +1254,42 @@ static PyMemberDef cc_members[] = {
static PyTypeObject Cctype = { static PyTypeObject Cctype = {
PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0) PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
"persistent.PickleCache", /* tp_name */ "persistent.PickleCache", /* tp_name */
sizeof(ccobject), /* tp_basicsize */ sizeof(ccobject), /* tp_basicsize */
0, /* tp_itemsize */ 0, /* tp_itemsize */
(destructor)cc_dealloc, /* tp_dealloc */ (destructor)cc_dealloc, /* tp_dealloc */
0, /* tp_print */ 0, /* tp_print */
0, /* tp_getattr */ 0, /* tp_getattr */
0, /* tp_setattr */ 0, /* tp_setattr */
0, /* tp_compare */ 0, /* tp_compare */
0, /* tp_repr */ 0, /* tp_repr */
0, /* tp_as_number */ 0, /* tp_as_number */
0, /* tp_as_sequence */ 0, /* tp_as_sequence */
&cc_as_mapping, /* tp_as_mapping */ &cc_as_mapping, /* tp_as_mapping */
0, /* tp_hash */ 0, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
0, /* tp_str */ 0, /* tp_str */
0, /* tp_getattro */ 0, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, Py_TPFLAGS_DEFAULT |
/* tp_flags */ Py_TPFLAGS_BASETYPE |
0, /* tp_doc */ Py_TPFLAGS_HAVE_GC, /* tp_flags */
(traverseproc)cc_traverse, /* tp_traverse */ 0, /* tp_doc */
(traverseproc)cc_traverse, /* tp_traverse */
(inquiry)cc_clear, /* tp_clear */ (inquiry)cc_clear, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
0, /* tp_iter */ 0, /* tp_iter */
0, /* tp_iternext */ 0, /* tp_iternext */
cc_methods, /* tp_methods */ cc_methods, /* tp_methods */
cc_members, /* tp_members */ cc_members, /* tp_members */
cc_getsets, /* tp_getset */ cc_getsets, /* tp_getset */
0, /* tp_base */ 0, /* tp_base */
0, /* tp_dict */ 0, /* tp_dict */
0, /* tp_descr_get */ 0, /* tp_descr_get */
0, /* tp_descr_set */ 0, /* tp_descr_set */
0, /* tp_dictoffset */ 0, /* tp_dictoffset */
(initproc)cc_init, /* tp_init */ (initproc)cc_init, /* tp_init */
}; };
......
...@@ -35,7 +35,7 @@ is_jython = 'java' in sys.platform ...@@ -35,7 +35,7 @@ is_jython = 'java' in sys.platform
# Jython cannot build the C optimizations, while on PyPy they are # Jython cannot build the C optimizations, while on PyPy they are
# anti-optimizations (the C extension compatibility layer is known-slow, # anti-optimizations (the C extension compatibility layer is known-slow,
# and defeats JIT opportunities). # and defeats JIT opportunities).
if is_pypy or is_jython or sys.version_info[0] > 2: if is_pypy or is_jython:
ext_modules = headers = [] ext_modules = headers = []
else: else:
ext_modules = [Extension(name = 'persistent.cPersistence', ext_modules = [Extension(name = 'persistent.cPersistence',
......
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