rework to separate out python module

Remove the magic from the magic _db* modules.
Combine as one 'devsup._dbapi'.

libpyDevSup exists only to bootstrap python
interpreter in IOCs.
This commit is contained in:
Michael Davidsaver
2018-11-03 13:33:36 -07:00
parent 662b518338
commit f428d2a4e4
15 changed files with 401 additions and 426 deletions

View File

@ -10,6 +10,25 @@ include $(TOP)/configure/CONFIG_PY
#=============================
# Build the IOC application
USR_CPPFLAGS += -I$(TOP)/devsupApp/src
LIBRARY = pyDevSup$(PY_LD_VER)
SHRLIB_VERSION = 0
DBD += pyDevSup.dbd
pyDevSup$(PY_LD_VER)_SYS_LIBS += python$(PY_LD_VER)
pyDevSup$(PY_LD_VER)_LIBS += $(EPICS_BASE_IOC_LIBS)
setup_CPPFLAGS += -DXEPICS_ARCH=\"$(T_A)\"
setup_CPPFLAGS += -DXPYDEV_BASE=\"$(abspath $(INSTALL_LOCATION))\"
setup_CPPFLAGS += -DXEPICS_BASE=\"$(EPICS_BASE)\"
setup_CPPFLAGS += -DPYDIR=\"python$(PY_VER)\"
pyDevSup$(PY_LD_VER)_SRCS += setup.c
PROD_IOC = softIocPy$(PY_VER)
PRODNAME = $(addsuffix $(EXE),$(PROD))

1
pyIocApp/pyDevSup.dbd Normal file
View File

@ -0,0 +1 @@
registrar(pySetupReg)

133
pyIocApp/setup.c Normal file
View File

@ -0,0 +1,133 @@
/* Global interpreter setup
*/
/* python has its own ideas about which version to support */
#undef _POSIX_C_SOURCE
#undef _XOPEN_SOURCE
#include <Python.h>
#ifdef HAVE_NUMPY
#include <numpy/ndarrayobject.h>
#endif
#include <stdio.h>
#include <epicsVersion.h>
#include <dbCommon.h>
#include <dbAccess.h>
#include <dbStaticLib.h>
#include <dbScan.h>
#include <initHooks.h>
#include <epicsThread.h>
#include <epicsExit.h>
#include <alarm.h>
#include "pydevsup.h"
static void cleanupPy(void *junk)
{
PyThreadState *state = PyGILState_GetThisThreadState();
PyEval_RestoreThread(state);
/* special "fake" hook for shutdown */
//pyhook((initHookState)9999);
Py_Finalize(); // calls python atexit hooks
}
static void extendPath(PyObject *list,
const char *base,
const char *archdir)
{
PyObject *mod, *ret;
mod = PyImport_ImportModule("os.path");
if(!mod)
return;
ret = PyObject_CallMethod(mod, "join", "sss", base, PYDIR, archdir);
if(ret && !PySequence_Contains(list, ret)) {
PyList_Insert(list, 0, ret);
}
Py_XDECREF(ret);
Py_DECREF(mod);
if(PyErr_Occurred()) {
PyErr_Print();
PyErr_Clear();
}
}
static void insertDefaultPath(PyObject *list)
{
const char *basedir, *pydevdir, *top, *arch;
basedir = getenv("EPICS_BASE");
if(!basedir)
basedir = XEPICS_BASE;
pydevdir = getenv("PYDEV_BASE");
if(!pydevdir)
pydevdir = XPYDEV_BASE;
top = getenv("TOP");
arch = getenv("ARCH");
if(!arch)
arch = XEPICS_ARCH;
assert(PyList_Check(list));
assert(PySequence_Check(list));
extendPath(list, basedir, arch);
extendPath(list, pydevdir, arch);
if(top)
extendPath(list, top, arch);
}
static void setupPyPath(void)
{
PyObject *mod, *path = NULL;
mod = PyImport_ImportModule("sys");
if(mod)
path = PyObject_GetAttrString(mod, "path");
Py_XDECREF(mod);
if(path) {
PyObject *cur;
char cwd[PATH_MAX];
insertDefaultPath(path);
/* prepend current directory */
if(getcwd(cwd, sizeof(cwd)-1)) {
cwd[sizeof(cwd)-1] = '\0';
cur = PyString_FromString(cwd);
if(cur)
PyList_Insert(path, 0, cur);
Py_XDECREF(cur);
}
}
Py_XDECREF(path);
}
static void pySetupReg(void)
{
PyGILState_STATE state;
Py_Initialize();
PyEval_InitThreads();
setupPyPath();
if(PyRun_SimpleString("import devsup\n"
"devsup._init(True)\n"
)) {
PyErr_Print();
PyErr_Clear();
}
(void)PyEval_SaveThread();
epicsAtExit(&cleanupPy, NULL);
}
#include <epicsExport.h>
epicsExportRegistrar(pySetupReg);