|
From: David B. <dav...@us...> - 2004-11-28 08:22:06
|
Update of /cvsroot/frontierkernel/Frontier/Common/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14927/source Modified Files: Tag: sidewinder-branch about.c langpython.c langvalue.c Log Message: misc cleanup Index: langvalue.c =================================================================== RCS file: /cvsroot/frontierkernel/Frontier/Common/source/langvalue.c,v retrieving revision 1.5.4.1 retrieving revision 1.5.4.2 diff -C2 -d -r1.5.4.1 -r1.5.4.2 *** langvalue.c 28 Nov 2004 00:40:48 -0000 1.5.4.1 --- langvalue.c 28 Nov 2004 06:51:47 -0000 1.5.4.2 *************** *** 8101,8105 **** langsetthisvalue (currenthashtable, htable, bsname); ! fl = callpythonscript (pythoncode, hparam1, bsname, vreturned); langpopsourcecode (); --- 8101,8105 ---- langsetthisvalue (currenthashtable, htable, bsname); ! fl = callpythonscript (pythoncode, hparam1, bsname, vreturned, false); langpopsourcecode (); Index: about.c =================================================================== RCS file: /cvsroot/frontierkernel/Frontier/Common/source/about.c,v retrieving revision 1.4 retrieving revision 1.4.4.1 diff -C2 -d -r1.4 -r1.4.4.1 *** about.c 31 Oct 2004 13:02:23 -0000 1.4 --- about.c 28 Nov 2004 06:51:47 -0000 1.4.4.1 *************** *** 173,176 **** --- 173,178 ---- "\x23" "© 1992-" copyright_year_string " UserLand Software, Inc.", + + //"\x45" "(Portions © 2001, 2002, 2003 Python Software Foundation.)", #ifdef PIKE Index: langpython.c =================================================================== RCS file: /cvsroot/frontierkernel/Frontier/Common/source/langpython.c,v retrieving revision 1.2.4.17 retrieving revision 1.2.4.18 diff -C2 -d -r1.2.4.17 -r1.2.4.18 *** langpython.c 28 Nov 2004 00:40:48 -0000 1.2.4.17 --- langpython.c 28 Nov 2004 06:51:47 -0000 1.2.4.18 *************** *** 519,563 **** static PyObject *frontierModule = NULL; - typedef struct { - PyObject *pyObject; - time_t cookie; - } FrontierPythonPtr; - - static boolean - getPythonPtr(FrontierPythonPtr *fpp, PyObject **pptr) { - if (fpp->cookie == currentCookie) { - *pptr = fpp->pyObject; - return true; - } - *pptr = NULL; - return false; - } - - static boolean - frontierToPython(tyvaluerecord *tvr, PyObject **pptr) { - if (tvr->valuetype != binaryvaluetype) - return false; - - if (gethandlesize(tvr->data.binaryvalue) != sizeof(FrontierPythonPtr)) - return false; - - if (!getPythonPtr((FrontierPythonPtr *)(*(tvr->data.binaryvalue)), pptr)) - return false; - - return true; - } - - static boolean - pythonToFrontier(PyObject *pptr, tyvaluerecord *tvr) { - FrontierPythonPtr fpp; - fpp.pyObject = pptr; - fpp.cookie = currentCookie; - - if (!newheapvalue(&fpp, sizeof(FrontierPythonPtr), binaryvaluetype, tvr)) - return false; - - return true; - } - static boolean convertPython(PyObject *, hdlhashtable, bigstring); --- 519,522 ---- *************** *** 1416,1419 **** --- 1375,1387 ---- } + static void + safeaddobject(PyObject *m, char *name, PyObject *obj) { + if (obj) { + PyModule_AddObject(m, name, obj); + } else { + printf("safeaddobject: null for %s\n", name); + } + } + static boolean addModuleVars(PyObject *m) { *************** *** 1429,1438 **** return false; ! //PyModule_AddObject(m, "examples", fetchAux("examples")); ! //PyModule_AddObject(m, "scratchpad", fetchAux("scratchpad")); ! //PyModule_AddObject(m, "suites", fetchAux("suites")); ! //PyModule_AddObject(m, "user", fetchAux("user")); ! //PyModule_AddObject(m, "websites", fetchAux("websites")); ! //PyModule_AddObject(m, "workspace", fetchAux("workspace")); return true; --- 1397,1406 ---- return false; ! safeaddobject(m, "examples", fetchAux("examples")); ! safeaddobject(m, "scratchpad", fetchAux("scratchpad")); ! safeaddobject(m, "suites", fetchAux("suites")); ! safeaddobject(m, "user", fetchAux("user")); ! safeaddobject(m, "websites", fetchAux("websites")); ! safeaddobject(m, "workspace", fetchAux("workspace")); return true; *************** *** 1561,1713 **** } - boolean runstringverb(hdltreenode hp1, tyvaluerecord *v) { - Handle pythonCode; - tyvaluerecord adrOutput; - tyvaluerecord adrError; - hdlhashtable htableOutput; - hdlhashtable htableError; - bigstring locOutput; - bigstring locError; - PyObject *locals; - PyObject *result; - PyObject *err; - - if (!ispythonloaded()) - return false; - - // if Python hasn't been started, start it and create our globals. - // - startInterpreter(); - - locals = globals; // hack alert -- confirm why I need to do this - - // set up default frontier return value - // - if (!setbooleanvalue(true, v)) - return false; - - // first param: handle to python code string - // - if (!gettextvalue (hp1, 1, &pythonCode)) - return (false); - - // second param: pointer to var to store output - // - if (!getaddressparam(hp1, 2, &adrOutput)) - return (false); - - // third param: pointer to var for error output - // - if (!getaddressparam(hp1, 3, &adrError)) - return false; - - // derefernce pointer to output variable - // - if (!getaddressvalue(adrOutput, &htableOutput, locOutput)) - return (false); - - // dereference pointer to error output - // - if (!getaddressvalue(adrError, &htableError, locError)) - return (false); - - // convert the text to a C string - // - if (!enlargehandle(pythonCode, gethandlesize(pythonCode) + 1, "\0")) - return false; - - // set up the interpreter to capture the output - // - result = PyRun_String("from cStringIO import StringIO; import sys; _f_old_output = sys.stdout; _f_output = StringIO(); sys.stdout = _f_output; _f_old_err = sys.stderr; _f_err = StringIO(); sys.stderr = _f_err; del StringIO", Py_file_input, globals, locals); - if (result) Py_DECREF(result); - - // run the python code - // - lockhandle (pythonCode); - result = PyRun_String(*pythonCode, Py_file_input, globals, locals); - unlockhandle (pythonCode); - if (result) Py_DECREF(result); - err = PyErr_Occurred(); - - if (err) { - PyObject *ferr; - PyObject *outputString; - - PyErr_Print(); - - // get the error output - // - ferr = PyDict_GetItemString(locals, "_f_err"); - outputString = PyObject_CallMethod(ferr, "getvalue", NULL); - result = PyObject_CallMethod(ferr, "close", NULL); - if (result) Py_DECREF(result); - - bundle { - tyvaluerecord errorValue; - char *output = PyString_AsString(outputString); - - if (!newheapvalue(output, strlen(output), stringvaluetype, &errorValue)) { - Py_DECREF(outputString); - return false; - } - - exemptfromtmpstack(&errorValue); - - if (!hashtableassign(htableError, locError, errorValue)) { - Py_DECREF(outputString); - return false; - } - - if (!setbooleanvalue(false, v)) { - Py_DECREF(outputString); - return false; - } - - Py_DECREF(outputString); - } - } - - bundle { - PyObject *foutput; - PyObject *outputString; - char *output; - tyvaluerecord outputValue; - boolean retcode = true; - - // get the output - // - foutput = PyDict_GetItemString(locals, "_f_output"); - outputString = PyObject_CallMethod(foutput, "getvalue", NULL); - result = PyObject_CallMethod(foutput, "close", NULL); - if (result) Py_DECREF(result); - - // convert the output from a python string into a cstring - // - output = PyString_AsString(outputString); - - // copy the output into the frontier variable that was passed in - // - if (!newheapvalue(output, strlen(output), stringvaluetype, &outputValue)) - retcode = false; - - exemptfromtmpstack(&outputValue); - - if (retcode && !hashtableassign(htableOutput, locOutput, outputValue)) - retcode = false; - - // erase all sign of us - // - result = PyRun_String("sys.stdout = _f_old_output; sys.stderr = _f_old_err", Py_file_input, globals, locals); - if (result) Py_DECREF(result); - - Py_DECREF(outputString); - //Py_DECREF(locals); - - return retcode; - } - } - static boolean pythonfunctionvalue (short token, hdltreenode hparam1, tyvaluerecord *vreturned, bigstring bserror) { ! hdltreenode hp1 = hparam1; tyvaluerecord *v = vreturned; --- 1529,1534 ---- } static boolean pythonfunctionvalue (short token, hdltreenode hparam1, tyvaluerecord *vreturned, bigstring bserror) { ! //hdltreenode hp1 = hparam1; tyvaluerecord *v = vreturned; *************** *** 1722,1727 **** return stopInterpreter(); ! case runstringfunc: ! return runstringverb(hp1, v); default: --- 1543,1548 ---- return stopInterpreter(); ! //case runstringfunc: ! // return runstringverb(hp1, v); default: *************** *** 1748,1864 **** } - boolean pythongetdict(Handle htext, tyvaluerecord *tvr) { - PyObject *dict; - PyObject *result; - - startInterpreter(); - - dict = PyDict_New(); - - makeGlobals(dict, "__compile__"); - - // execute the python - - // convert the text to a C string - // - if (!enlargehandle(htext, gethandlesize(htext) + 1, "\0")) - return false; - - // lock the text - lockhandle(htext); - - convertreturns(htext); - result = PyRun_String(*htext, Py_file_input, dict, dict); - if (result) Py_DECREF(result); - - // unlock the text - unlockhandle(htext); - - // handle errors - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - return false; - } - - // store the python dict in the tvr - if (!pythonToFrontier(dict, tvr)) - return false; - - return true; - } - - boolean callpython(hdltreenode htree, tyvaluerecord *vreturned) { - PyObject *globals; - PyObject *result; - Handle htext; - - startInterpreter(); - - globals = PyDict_New(); - - makeGlobals(globals, "__compile__"); - - // execute the python - - // convert the text to a C string - // - htext = (**htree).nodeval.data.stringvalue; - if (!enlargehandle(htext, gethandlesize(htext) + 1, "\0")) - return false; - - // lock the text - lockhandle(htext); - - convertreturns(htext); - result = PyRun_String(*htext, Py_file_input, globals, globals); - if (result) Py_DECREF(result); - - // unlock the text - unlockhandle(htext); - - // handle errors - if (PyErr_Occurred()) { - PyErr_Print(); - PyErr_Clear(); - return false; - } - - bundle { // run the code - PyObject *keys; - int len, i; - PyObject *value; - PyObject *func = NULL; - - // find the function that was just defined - // - // for right now, just find the first function defined in the namespace - // - keys = PyDict_Keys(globals); - len = PyList_GET_SIZE(keys); - for (i = 0; i < len; i++) { - value = PyDict_GetItem(globals, PyList_GET_ITEM(keys, i)); - if (PyCallable_Check(value)) { - func = value; - break; - } - } - - if (!func) - return false; - - // BUGBUG stopgap coding - - // convert the calling parameters - - // call it - PyObject_CallFunction(func, "()"); - - // convert the return value - } - - return true; - } - boolean compilepython(Handle htext, tyvaluerecord *tvr) { Handle newtext; --- 1569,1572 ---- *************** *** 1904,1916 **** } ! boolean callpythonscript(tyvaluerecord code, hdltreenode hparam1, bigstring bsname, tyvaluerecord *vreturned) { PyObject *globals; PyObject *result; Handle htext; startInterpreter(); globals = PyDict_New(); ! makeGlobals(globals, "__compile__"); // execute the python --- 1612,1628 ---- } ! boolean callpythonscript(tyvaluerecord code, hdltreenode hparam1, bigstring bsname, tyvaluerecord *vreturned, boolean runbutton) { PyObject *globals; PyObject *result; Handle htext; + startInterpreter(); globals = PyDict_New(); ! if (runbutton) ! makeGlobals(globals, "__main__"); ! else ! makeGlobals(globals, "__compile__"); // execute the python *************** *** 1925,1928 **** --- 1637,1641 ---- lockhandle(htext); + // run it convertreturns(htext); result = PyRun_String(*htext, Py_file_input, globals, globals); *************** *** 1939,1943 **** } ! bundle { // run the code PyObject *func = NULL; PyObject *argList; --- 1652,1656 ---- } ! if (!runbutton) { // run the code PyObject *func = NULL; PyObject *argList; *************** *** 1946,1956 **** // find the function that was called - // func = PyDict_GetItemString(globals, funcName); if (!func) return false; - // BUGBUG stopgap coding - // convert the calling parameters argList = pythonconvertparameters(hparam1); --- 1659,1666 ---- *************** *** 1959,1962 **** --- 1669,1674 ---- result = PyObject_CallObject(func, argList); + Py_DECREF(argList); + // handle errors if (PyErr_Occurred()) { *************** *** 1977,1980 **** --- 1689,1699 ---- } + boolean callpython(hdltreenode htree, tyvaluerecord *vreturned) { + boolean result; + bigstring bs; + copyctopstring("", bs); + result = callpythonscript((**htree).nodeval, NULL, bs, vreturned, true); + return result; + } #endif |