From: Michael R S. <mse...@be...> - 2004-06-07 12:49:58
|
I have written a simple C-program that loads a Python Function that connects to a PostgreSQL database. When I only load and execute the Python function all works OK. If I call the Python function a second time I get the following: Called PyObject_CallObject Traceback (most recent call last): File "/home/mike/src/arthur/query_server/query_analyzer/Query_Analyzer.py", line 25, in processQuery from pyPgSQL import PgSQL File "/usr/local/lib/python2.3/site-packages/pyPgSQL/PgSQL.py", line 388, in ? raise ImportError, \ ImportError: You need to install mxDateTime (http://www.egenix.com/files/python/eGenix-mx-Extensions.html) Any ideas? Thanks, Mike Here is the code: (hope it helps) If I call testQuery 1 time and exit all works, call it a second time and the above error occurs #include <stdlib.h> #include <Python.h> static char theResults[1024]; PyObject *BuildValue(char *query) { PyObject *pDict; printf("BuildValue\n"); pDict = Py_BuildValue("{s[{ss}]sssssssss[{ssss}]}", "from","tablename","a", "full_sql","select a.id from a", "command","select","from_sql","from a","select_sql","select a.id", "select","columnname","id","tablename","a"); return pDict; } char *testQuery(char *user, char *query) { char *results = "Select \'Error processing sql\'"; PyObject *pName, *pModule, *pDict, *pFunc; PyObject *pArgs, *pValue, *pResults; PyObject *pQuery; char *LoadModule = "Query_Analyzer"; char *LoadFunction = "processQuery"; pDict = NULL; printf("testQuery enter\n"); Py_Initialize(); pName = PyString_FromString(LoadModule); /* Error checking of pName left out */ pModule = PyImport_Import(pName); Py_DECREF(pName); if (pModule != NULL) { pDict = PyModule_GetDict(pModule); /* pDict is a borrowed reference */ pFunc = PyDict_GetItemString(pDict, LoadFunction); /* pFun: Borrowed reference */ if (pFunc && PyCallable_Check(pFunc)) { pArgs = PyTuple_New( 2); // two parms user/query // Set user parm pValue = PyString_FromString(user); PyTuple_SetItem(pArgs, 0, pValue); // Build the QUERY Dictionary //pDict = Ablddict(parsetree_list, query); // pQuery = PyString_FromString("our_query"); pQuery = BuildValue(query); printf("Calling PyTuple_SetItem(pArgs, 1, pQuery);\n"); PyTuple_SetItem(pArgs, 1, pQuery); printf("Called PyTuple_SetItem(pArgs, 1, pQuery);\n"); // Call the Python module // elog(LOG,"Calling PyObject_CallObject"); printf("Calling PyObject_CallObject\n"); pResults = PyObject_CallObject(pFunc, pArgs); // pResults = PyString_FromString("select mike from seefelt"); // elog(LOG,"Called PyObject_CallObject"); printf("Called PyObject_CallObject\n"); Py_DECREF(pArgs); Py_DECREF(pFunc); // Py_DECREF(pValue); if (pResults != NULL) { //results = query; if ( PyString_Check(pResults) ) { sprintf(theResults,"%s\n",PyString_AsString(pResults)); results = theResults; } else { printf("Opps not a PyString\n"); } Py_DECREF(pResults); } else { Py_DECREF(pModule); PyErr_Print(); // elog(LOG,"Call failed\n"); return results; } /* pDict and pFunc are borrowed and must not be Py_DECREF-ed */ } else { if (PyErr_Occurred()) PyErr_Print(); // elog(LOG, "Cannot find function \"%s\"\n", LoadFunction); sprintf(theResults, "SELECT 'Cannot find function \"%s\"'", LoadFunction); } Py_DECREF(pModule); } else { PyErr_Print(); // elog(LOG, "Failed to load \"%s\"\n", LoadModule); sprintf(theResults, "Cannot find function \"%s\"\n", LoadModule); return results; } Py_Finalize(); free( pModule ); return results; } /* for testing */ int main(int argc, char *argv[]) { PyObject *pName, *pModule, *pDict, *pFunc; PyObject *pArgs, *pValue; int i,j; i = 1; if ( argc > 1 ) i = atoi(argv[1]); for (j=0;j<i;j++) { printf("\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>> PASS %d\n",j+1); testQuery("MIKE","select a.id from a"); } return 0; } |