support python 3.2

This commit is contained in:
Michael Davidsaver
2013-04-07 08:40:00 -04:00
parent 3601974986
commit c9820d264f
9 changed files with 175 additions and 162 deletions

View File

@@ -115,10 +115,21 @@ static PyObject* pyField_putval(pyField *self, PyObject* args)
OP(DOUBLE,epicsFloat64,PyFloat_AsDouble);
#undef OP
case DBF_STRING: {
char *fld = PyString_AsString(val);
strncpy(self->addr.pfield, fld, MAX_STRING_SIZE);
fld = self->addr.pfield;
fld[MAX_STRING_SIZE-1]='\0';
const char *fld;
char *dest=self->addr.pfield;
#if PY_MAJOR_VERSION >= 3
PyObject *data = PyUnicode_AsEncodedString(val, "ascii", "Encoding error:");
if(!data)
return NULL;
fld = PyUnicode_AS_DATA(data);
#else
fld = PyString_AsString(val);
#endif
strncpy(dest, fld, MAX_STRING_SIZE);
dest[MAX_STRING_SIZE-1]='\0';
#if PY_MAJOR_VERSION >= 3
Py_DECREF(data);
#endif
break;
}
default:
@@ -166,6 +177,7 @@ static PyMethodDef pyField_methods[] = {
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION < 3
static Py_ssize_t pyField_buf_getcount(pyField *self, Py_ssize_t *totallen)
{
if(totallen)
@@ -195,6 +207,7 @@ static Py_ssize_t pyField_buf_getcharbuf(pyField *self, Py_ssize_t bufid, char *
*data = self->addr.pfield;
return self->addr.field_size * self->addr.no_elements;
}
#endif
static int pyField_buf_getbufferproc(pyField *self, Py_buffer *buf, int flags)
{
@@ -205,18 +218,24 @@ static int pyField_buf_getbufferproc(pyField *self, Py_buffer *buf, int flags)
}
static PyBufferProcs pyField_buf_methods = {
#if PY_MAJOR_VERSION < 3
(readbufferproc)pyField_buf_getbuf,
(writebufferproc)pyField_buf_getbuf,
(segcountproc)pyField_buf_getcount,
(charbufferproc)pyField_buf_getcharbuf,
#endif
(getbufferproc)pyField_buf_getbufferproc,
(releasebufferproc)NULL,
};
static PyTypeObject pyField_type = {
#if PY_MAJOR_VERSION >= 3
PyVarObject_HEAD_INIT(NULL, 0)
#else
PyObject_HEAD_INIT(NULL)
0,
#endif
"_dbapi._Field",
sizeof(pyField),
};
@@ -226,7 +245,9 @@ int pyField_prepare(void)
size_t i;
pyField_type.tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE;
#if PY_MAJOR_VERSION < 3
pyField_type.tp_flags |= Py_TPFLAGS_HAVE_GETCHARBUFFER|Py_TPFLAGS_HAVE_NEWBUFFER;
#endif
pyField_type.tp_methods = pyField_methods;
pyField_type.tp_as_buffer = &pyField_buf_methods;
pyField_type.tp_init = (initproc)pyField_Init;

View File

@@ -24,7 +24,7 @@ typedef struct {
static void pyRecord_dealloc(pyRecord *self)
{
dbFinishEntry(&self->entry);
self->ob_type->tp_free((PyObject*)self);
Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyObject* pyRecord_new(PyTypeObject *type, PyObject *args, PyObject *kws)
@@ -57,6 +57,7 @@ static PyObject* pyRecord_ispyrec(pyRecord *self)
return PyBool_FromLong(self->ispyrec);
}
#if PY_MAJOR_VERSION < 3
static int pyRecord_compare(pyRecord *A, pyRecord *B)
{
dbCommon *a=A->entry.precnode->precord,
@@ -66,6 +67,7 @@ static int pyRecord_compare(pyRecord *A, pyRecord *B)
return 0;
return strcmp(a->name, b->name);
}
#endif
static PyObject* pyRecord_name(pyRecord *self)
{
@@ -259,8 +261,12 @@ static PyMethodDef pyRecord_methods[] = {
};
static PyTypeObject pyRecord_type = {
#if PY_MAJOR_VERSION >= 3
PyVarObject_HEAD_INIT(NULL, 0)
#else
PyObject_HEAD_INIT(NULL)
0,
#endif
"_dbapi._Record",
sizeof(pyRecord),
};
@@ -274,7 +280,9 @@ int pyRecord_prepare(void)
pyRecord_type.tp_new = (newfunc)pyRecord_new;
pyRecord_type.tp_dealloc = (destructor)pyRecord_dealloc;
pyRecord_type.tp_init = (initproc)pyRecord_Init;
#if PY_MAJOR_VERSION < 3
pyRecord_type.tp_compare = (cmpfunc)pyRecord_compare;
#endif
if(PyType_Ready(&pyRecord_type)<0)
return -1;

View File

@@ -1,6 +1,16 @@
#ifndef PYDEVSUP_H
#define PYDEVSUP_H
#if PY_MAJOR_VERSION >= 3
#define PyInt_FromLong PyLong_FromLong
#define PyInt_AsLong PyLong_AsLong
#define PyString_FromString PyUnicode_FromString
#define MODINIT_RET(VAL) return (VAL)
#else
#define MODINIT_RET(VAL) return
#endif
void pyDBD_cleanup(void);
int pyField_prepare(void);

View File

@@ -64,43 +64,6 @@ static pystate statenames[] = {
};
#undef INITST
static void pyhook(initHookState state);
static void cleanupPy(void *junk)
{
PyThreadState *state = PyGILState_GetThisThreadState();
PyEval_RestoreThread(state);
/* special "fake" hook for shutdown */
pyhook((initHookState)9999);
pyDBD_cleanup();
pyField_cleanup();
/* release extra reference for hooktable */
Py_DECREF(hooktable);
hooktable = NULL;
Py_Finalize();
}
/* Initialize the interpreter environment
*/
static epicsThreadOnceId setupPyOnceId = EPICS_THREAD_ONCE_INIT;
static void setupPyOnce(void *junk)
{
PyThreadState *state;
Py_Initialize();
PyEval_InitThreads();
state = PyEval_SaveThread();
epicsAtExit(&cleanupPy, NULL);
}
void evalPy(const char* code)
{
PyGILState_STATE state;
@@ -193,8 +156,18 @@ static PyMethodDef devsup_methods[] = {
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef dbapimodule = {
PyModuleDef_HEAD_INIT,
"_dbapi",
NULL,
-1,
devsup_methods
};
#endif
/* initialize "magic" builtin module */
static void init_dbapi(void)
PyMODINIT_FUNC init_dbapi(void)
{
PyObject *mod, *hookdict, *pysuptable;
pystate *st;
@@ -203,23 +176,27 @@ static void init_dbapi(void)
hooktable = PyDict_New();
if(!hooktable)
return;
MODINIT_RET(NULL);
if(pyField_prepare())
return;
MODINIT_RET(NULL);
if(pyRecord_prepare())
return;
MODINIT_RET(NULL);
#if PY_MAJOR_VERSION >= 3
mod = PyModule_Create(&dbapimodule);
#else
mod = Py_InitModule("_dbapi", devsup_methods);
#endif
pysuptable = PySet_New(NULL);
if(!pysuptable)
return;
MODINIT_RET(NULL);
PyModule_AddObject(mod, "_supports", pysuptable);
hookdict = PyDict_New();
if(!hookdict)
return;
MODINIT_RET(NULL);
PyModule_AddObject(mod, "_hooks", hookdict);
for(st = statenames; st->name; st++) {
@@ -231,6 +208,43 @@ static void init_dbapi(void)
pyField_setup(mod);
pyRecord_setup(mod);
MODINIT_RET(mod);
}
static void cleanupPy(void *junk)
{
PyThreadState *state = PyGILState_GetThisThreadState();
PyEval_RestoreThread(state);
/* special "fake" hook for shutdown */
pyhook((initHookState)9999);
pyDBD_cleanup();
pyField_cleanup();
/* release extra reference for hooktable */
Py_DECREF(hooktable);
hooktable = NULL;
Py_Finalize();
}
/* Initialize the interpreter environment
*/
static void setupPyInit(void)
{
PyThreadState *state;
PyImport_AppendInittab("_dbapi", init_dbapi);
Py_Initialize();
PyEval_InitThreads();
state = PyEval_SaveThread();
epicsAtExit(&cleanupPy, NULL);
}
#include <iocsh.h>
@@ -251,7 +265,7 @@ static void pySetupReg(void)
{
PyGILState_STATE state;
epicsThreadOnce(&setupPyOnceId, &setupPyOnce, NULL);
setupPyInit();
iocshRegister(&codeDef, &codeRun);
iocshRegister(&fileDef, &fileRun);
initHookRegister(&pyhook);