|
From: Andre R. <and...@us...> - 2004-11-24 02:06:26
|
Update of /cvsroot/frontierkernel/Frontier/Common/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31503 Modified Files: Tag: sidewinder-branch langpython.c Log Message: First cut at dynamically loading the Python library at runtime on Windows, so Frontier will start up normally even if the Python DLL is not present. Index: langpython.c =================================================================== RCS file: /cvsroot/frontierkernel/Frontier/Common/source/langpython.c,v retrieving revision 1.2.4.10 retrieving revision 1.2.4.11 diff -C2 -d -r1.2.4.10 -r1.2.4.11 *** langpython.c 23 Nov 2004 22:12:16 -0000 1.2.4.10 --- langpython.c 24 Nov 2004 02:05:06 -0000 1.2.4.11 *************** *** 63,87 **** #ifdef FRONTIER_PYTHON - /* - 2004-11-23 aradke: To compile with Visual Studio 6.0 on Windows, - you need to grab the Python installer from http://www.python.org/ - and install it on your system. Then select the Options command - from the Tools menu, switch to the Directories rider, and add - the paths to the "include" and "libs" directories of your Python - installation to the "Include files" and "Library files" search - paths respectively. Since these paths depend on where you installed - Python on your system, we can't set them for you in our project file. - */ - #include "langpython.h" #if defined(MACVERSION) && defined(TARGET_RT_MAC_MACHO) #include <Python/Python.h> #else #include <Python.h> #endif #include <time.h> /* forward declarations */ --- 63,374 ---- #ifdef FRONTIER_PYTHON #include "langpython.h" #if defined(MACVERSION) && defined(TARGET_RT_MAC_MACHO) + #include <Python/Python.h> + #else + + #ifdef WIN95VERSION /*otherwise debug build of Frontier requires debug build of python dll*/ + #undef _DEBUG + #endif + #include <Python.h> + #endif #include <time.h> + + #ifdef WIN95VERSION + + /* + 2004-11-23 aradke: To compile with Visual Studio 6.0 on Windows, + you need to grab the Python installer from http://www.python.org/ + and install it on your system. In Visual Studio, select the Options + command from the Tools menu, switch to the Directories rider, and add + the path of the "include" directory of your Python installation to + the "Include files" search path. Since this path depends on where + you installed Python on your system, we can't set it for you in our + project file. + + 2004-11-23 aradke: dynamic linking to Python DLL at runtime + inspired by http://lists.wxwidgets.org/archive/wx-users/msg22930.html + */ + + + + static struct { + + HMODULE hmodule; /*handle to DLL if it was loaded and all symbols were found*/ + + PyObject * (*ptr_PyObject_CallMethod) (PyObject *, char *, char *, ...); /*abstract.h*/ + + PyObject * (*ptr_PyEval_GetBuiltins) (void); /*ceval.h*/ + + PyTypeObject * ptr_PyInstance_Type; /*classobject.h*/ + + PyTypeObject * ptr_PyComplex_Type; /*complexobject.h*/ + double (*ptr_PyComplex_ImagAsDouble) (PyObject *); + double (*ptr_PyComplex_RealAsDouble) (PyObject *); + + PyTypeObject * ptr_PyDict_Type; /*dictobject.h*/ + PyObject * (*ptr_PyDict_GetItem) (PyObject *, PyObject *); + PyObject * (*ptr_PyDict_GetItemString) (PyObject *, const char *); + PyObject * (*ptr_PyDict_Keys) (PyObject *); + PyObject * (*ptr_PyDict_New) (void); + int (*ptr_PyDict_SetItemString) (PyObject *, const char *, PyObject *); + + PyTypeObject * ptr_PyFloat_Type; /*floatobject.h*/ + double (*ptr_PyFloat_AsDouble) (PyObject *); + + PyTypeObject * ptr_PyInt_Type; /*intobject.h*/ + long (*ptr_PyInt_AsLong) (PyObject *); + + int (*ptr_PyList_Append) (PyObject *, PyObject *); /*listobject.h*/ + PyObject * (*ptr_PyList_GetItem) (PyObject *, int); + int (*ptr_PyList_SetItem) (PyObject *, int, PyObject *); + PyObject * (*ptr_PyList_New) (int); + int (*ptr_PyList_Size) (PyObject *); + + PyTypeObject * ptr_PyLong_Type; /*longobject.h*/ + + PyObject * (*ptr_Py_FindMethod) (PyMethodDef[], PyObject *, char *); /*methodobject.h*/ + + PyObject * (*ptr_Py_BuildValue) (char *, ...); /*modsupport.h*/ + PyObject * (*ptr_Py_InitModule4) (char *, PyMethodDef *, char *, PyObject *, int); + int (*ptr_PyArg_ParseTuple) (PyObject *, char *, ...); + int (*ptr_PyModule_AddObject) (PyObject *, char *, PyObject *); + + PyObject * ptr__Py_NoneStruct; /*object.h*/ + PyObject * (*ptr_PyObject_GetAttrString) (PyObject *, char *); + PyObject * (*ptr_PyObject_Str) (PyObject *); + PyObject * (*ptr_PyType_GenericNew) (PyTypeObject *, PyObject *, PyObject *); + int (*ptr_PyType_IsSubtype) (PyTypeObject *, PyTypeObject *); + int (*ptr_PyType_Ready) (PyTypeObject *); + #ifdef Py_REF_DEBUG + long * ptr__Py_RefTotal; + void (*ptr__Py_NegativeRefcount) (const char *, int, PyObject *); + #endif + #ifdef Py_TRACE_REFS + void (*ptr__Py_Dealloc) (PyObject *); + #endif + + PyObject * (*ptr__PyObject_New) (PyTypeObject *); /*objimpl.h*/ + + PyObject ** ptr_PyExc_IndexError; /*pyerrors.h*/ + PyObject ** ptr_PyExc_KeyError; + PyObject ** ptr_PyExc_SyntaxError; + PyObject ** ptr_PyExc_SystemError; + PyObject ** ptr_PyExc_TypeError; + PyObject ** ptr_PyExc_ValueError; + void (*ptr_PyErr_Clear) (void); + PyObject * (*ptr_PyErr_Occurred) (void); + void (*ptr_PyErr_Print) (void); + void (*ptr_PyErr_SetString) (PyObject *, const char *); + + void (*ptr_Py_Finalize) (); /*pythonrun.h*/ + void (*ptr_Py_Initialize) (void); + int (*ptr_Py_IsInitialized) (void); + PyObject * (*ptr_PyRun_String) (const char *, int, PyObject *, PyObject *); + + PyTypeObject * ptr_PyString_Type; /*stringobject.h*/ + char * (*ptr_PyString_AsString) (PyObject *); + PyObject * (*ptr_PyString_FromString) (const char *); + } py; + + + #define PyObject_CallMethod (*py.ptr_PyObject_CallMethod) + + #define PyEval_GetBuiltins (*py.ptr_PyEval_GetBuiltins) + + #define PyInstance_Type (*py.ptr_PyInstance_Type) + + #define PyComplex_Type (*py.ptr_PyComplex_Type) + #define PyComplex_ImagAsDouble (*py.ptr_PyComplex_ImagAsDouble) + #define PyComplex_RealAsDouble (*py.ptr_PyComplex_RealAsDouble) + + #define PyDict_Type (*py.ptr_PyDict_Type) + #define PyDict_GetItem (*py.ptr_PyDict_GetItem) + #define PyDict_GetItemString (*py.ptr_PyDict_GetItemString) + #define PyDict_Keys (*py.ptr_PyDict_Keys) + #define PyDict_New (*py.ptr_PyDict_New) + #define PyDict_SetItemString (*py.ptr_PyDict_SetItemString) + + #define PyFloat_Type (*py.ptr_PyFloat_Type) + #define PyFloat_AsDouble (*py.ptr_PyFloat_AsDouble) + + #define PyInt_Type (*py.ptr_PyInt_Type) + #define PyInt_AsLong (*py.ptr_PyInt_AsLong) + + #define PyList_Append (*py.ptr_PyList_Append) + #define PyList_GetItem (*py.ptr_PyList_GetItem) + #define PyList_SetItem (*py.ptr_PyList_SetItem) + #define PyList_New (*py.ptr_PyList_New) + #define PyList_Size (*py.ptr_PyList_Size) + + #define PyLong_Type (*py.ptr_PyLong_Type) + + #define Py_FindMethod (*py.ptr_Py_FindMethod) + + #define Py_BuildValue (*py.ptr_Py_BuildValue) + #define Py_InitModule4 (*py.ptr_Py_InitModule4) + #define PyArg_ParseTuple (*py.ptr_PyArg_ParseTuple) + #define PyModule_AddObject (*py.ptr_PyModule_AddObject) + + #define _Py_NoneStruct (*py.ptr__Py_NoneStruct) + #define PyObject_GetAttrString (*py.ptr_PyObject_GetAttrString) + #define PyObject_Str (*py.ptr_PyObject_Str) + #define PyType_GenericNew (*py.ptr_PyType_GenericNew) + #define PyType_IsSubtype (*py.ptr_PyType_IsSubtype) + #define PyType_Ready (*py.ptr_PyType_Ready) + #ifdef Py_REF_DEBUG + #define _Py_RefTotal (*py.ptr__Py_RefTotal) + #define _Py_NegativeRefcount (*py.ptr__Py_NegativeRefcount) + #endif + #ifdef Py_TRACE_REFS + #define _Py_Dealloc (*py.ptr__Py_Dealloc) + #endif + + #define _PyObject_New (*py.ptr__PyObject_New) + + #define PyExc_IndexError (*py.ptr_PyExc_IndexError) + #define PyExc_KeyError (*py.ptr_PyExc_KeyError) + #define PyExc_SyntaxError (*py.ptr_PyExc_SyntaxError) + #define PyExc_SystemError (*py.ptr_PyExc_SystemError) + #define PyExc_TypeError (*py.ptr_PyExc_TypeError) + #define PyExc_ValueError (*py.ptr_PyExc_ValueError) + #define PyErr_Clear (*py.ptr_PyErr_Clear) + #define PyErr_Occurred (*py.ptr_PyErr_Occurred) + #define PyErr_Print (*py.ptr_PyErr_Print) + #define PyErr_SetString (*py.ptr_PyErr_SetString) + + #define Py_Finalize (*py.ptr_Py_Finalize) + #define Py_Initialize (*py.ptr_Py_Initialize) + #define Py_IsInitialized (*py.ptr_Py_IsInitialized) + #define PyRun_String (*py.ptr_PyRun_String) + + #define PyString_Type (*py.ptr_PyString_Type) + #define PyString_AsString (*py.ptr_PyString_AsString) + #define PyString_FromString (*py.ptr_PyString_FromString) + + + static boolean importsymbol (HMODULE hm, LPCSTR symbol, void **ptraddress) { + + *ptraddress = (void*) GetProcAddress (hm, symbol); + + return (*ptraddress != NULL); + } /*importsymbol*/ + + + static boolean loadpythonlib (void) { + + HMODULE hm; + boolean fl = true; + + hm = LoadLibraryEx("python23.dll", NULL, 0); + + if (hm == NULL) + return (false); + + fl = fl && importsymbol (hm, "PyObject_CallMethod", (void**) &py.ptr_PyObject_CallMethod); + + fl = fl && importsymbol (hm, "PyEval_GetBuiltins", (void**) &py.ptr_PyEval_GetBuiltins); + + fl = fl && importsymbol (hm, "PyInstance_Type", (void**) &py.ptr_PyInstance_Type); + + fl = fl && importsymbol (hm, "PyComplex_Type", (void**) &py.ptr_PyComplex_Type); + fl = fl && importsymbol (hm, "PyComplex_ImagAsDouble", (void**) &py.ptr_PyComplex_ImagAsDouble); + fl = fl && importsymbol (hm, "PyComplex_RealAsDouble", (void**) &py.ptr_PyComplex_RealAsDouble); + + fl = fl && importsymbol (hm, "PyDict_Type", (void**) &py.ptr_PyDict_Type); + fl = fl && importsymbol (hm, "PyDict_GetItem", (void**) &py.ptr_PyDict_GetItem); + fl = fl && importsymbol (hm, "PyDict_GetItemString", (void**) &py.ptr_PyDict_GetItemString); + fl = fl && importsymbol (hm, "PyDict_Keys", (void**) &py.ptr_PyDict_Keys); + fl = fl && importsymbol (hm, "PyDict_New", (void**) &py.ptr_PyDict_New); + fl = fl && importsymbol (hm, "PyDict_SetItemString", (void**) &py.ptr_PyDict_SetItemString); + + fl = fl && importsymbol (hm, "PyFloat_Type", (void**) &py.ptr_PyFloat_Type); + fl = fl && importsymbol (hm, "PyFloat_AsDouble", (void**) &py.ptr_PyFloat_AsDouble); + + fl = fl && importsymbol (hm, "PyInt_Type", (void**) &py.ptr_PyInt_Type); + fl = fl && importsymbol (hm, "PyInt_AsLong", (void**) &py.ptr_PyInt_AsLong); + + fl = fl && importsymbol (hm, "PyList_Append", (void**) &py.ptr_PyList_Append); + fl = fl && importsymbol (hm, "PyList_GetItem", (void**) &py.ptr_PyList_GetItem); + fl = fl && importsymbol (hm, "PyList_SetItem", (void**) &py.ptr_PyList_SetItem); + fl = fl && importsymbol (hm, "PyList_New", (void**) &py.ptr_PyList_New); + fl = fl && importsymbol (hm, "PyList_Size", (void**) &py.ptr_PyList_Size); + + fl = fl && importsymbol (hm, "PyLong_Type", (void**) &py.ptr_PyLong_Type); + + fl = fl && importsymbol (hm, "Py_FindMethod", (void**) &py.ptr_Py_FindMethod); + + fl = fl && importsymbol (hm, "Py_BuildValue", (void**) &py.ptr_Py_BuildValue); + fl = fl && importsymbol (hm, "Py_InitModule4", (void**) &py.ptr_Py_InitModule4); + fl = fl && importsymbol (hm, "PyArg_ParseTuple", (void**) &py.ptr_PyArg_ParseTuple); + fl = fl && importsymbol (hm, "PyModule_AddObject", (void**) &py.ptr_PyModule_AddObject); + + fl = fl && importsymbol (hm, "_Py_NoneStruct", (void**) &py.ptr__Py_NoneStruct); + fl = fl && importsymbol (hm, "PyObject_GetAttrString", (void**) &py.ptr_PyObject_GetAttrString); + fl = fl && importsymbol (hm, "PyObject_Str", (void**) &py.ptr_PyObject_Str); + fl = fl && importsymbol (hm, "PyType_GenericNew", (void**) &py.ptr_PyType_GenericNew); + fl = fl && importsymbol (hm, "PyType_IsSubtype", (void**) &py.ptr_PyType_IsSubtype); + fl = fl && importsymbol (hm, "PyType_Ready", (void**) &py.ptr_PyType_Ready); + #ifdef Py_REF_DEBUG + fl = fl && importsymbol (hm, "_Py_RefTotal", (void**) &py.ptr__Py_RefTotal); + fl = fl && importsymbol (hm, "_Py_NegativeRefcount", (void**) &py.ptr__Py_NegativeRefcount); + #endif + #ifdef Py_TRACE_REFS + fl = fl && importsymbol (hm, "_Py_Dealloc", (void**) &py.ptr__Py_Dealloc); + #endif + + fl = fl && importsymbol (hm, "_PyObject_New", (void**) &py.ptr__PyObject_New); + + fl = fl && importsymbol (hm, "PyExc_IndexError", (void**) &py.ptr_PyExc_IndexError); + fl = fl && importsymbol (hm, "PyExc_KeyError", (void**) &py.ptr_PyExc_KeyError); + fl = fl && importsymbol (hm, "PyExc_SyntaxError", (void**) &py.ptr_PyExc_SyntaxError); + fl = fl && importsymbol (hm, "PyExc_SystemError", (void**) &py.ptr_PyExc_SystemError); + fl = fl && importsymbol (hm, "PyExc_TypeError", (void**) &py.ptr_PyExc_TypeError); + fl = fl && importsymbol (hm, "PyExc_ValueError", (void**) &py.ptr_PyExc_ValueError); + fl = fl && importsymbol (hm, "PyErr_Clear", (void**) &py.ptr_PyErr_Clear); + fl = fl && importsymbol (hm, "PyErr_Occurred", (void**) &py.ptr_PyErr_Occurred); + fl = fl && importsymbol (hm, "PyErr_Print", (void**) &py.ptr_PyErr_Print); + fl = fl && importsymbol (hm, "PyErr_SetString", (void**) &py.ptr_PyErr_SetString); + + fl = fl && importsymbol (hm, "Py_Finalize", (void**) &py.ptr_Py_Finalize); + fl = fl && importsymbol (hm, "Py_Initialize", (void**) &py.ptr_Py_Initialize); + fl = fl && importsymbol (hm, "Py_IsInitialized", (void**) &py.ptr_Py_IsInitialized); + fl = fl && importsymbol (hm, "PyRun_String", (void**) &py.ptr_PyRun_String); + + fl = fl && importsymbol (hm, "PyString_Type", (void**) &py.ptr_PyString_Type); + fl = fl && importsymbol (hm, "PyString_AsString", (void**) &py.ptr_PyString_AsString); + fl = fl && importsymbol (hm, "PyString_FromString", (void**) &py.ptr_PyString_FromString); + + if (fl) + py.hmodule = hm; /*success!*/ + else + FreeLibrary (hm); + + return (fl); + } /*loadpythonlib*/ + + + static void freepythonlib () { + + if (py.hmodule != NULL) { + + FreeLibrary (py.hmodule); + + py.hmodule = NULL; + } + + } /*freepythonlib*/ + + + #endif /*WIN95VERSION*/ + + /* forward declarations */ *************** *** 1230,1233 **** --- 1517,1525 ---- boolean pythoninitverbs (void) { + + #ifdef WIN95VERSION + loadpythonlib (); + #endif + return (loadfunctionprocessor (idpythonverbs, &pythonfunctionvalue)); } |