|
From: David B. <dav...@us...> - 2004-11-28 00:41:06
|
Update of /cvsroot/frontierkernel/Frontier/Common/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10468 Modified Files: Tag: sidewinder-branch langevaluate.c langpython.c langvalue.c scripts.c Log Message: enable usertalk to call python Index: scripts.c =================================================================== RCS file: /cvsroot/frontierkernel/Frontier/Common/source/scripts.c,v retrieving revision 1.3 retrieving revision 1.3.4.1 diff -C2 -d -r1.3 -r1.3.4.1 *** scripts.c 31 Oct 2004 13:02:23 -0000 1.3 --- scripts.c 28 Nov 2004 00:40:48 -0000 1.3.4.1 *************** *** 70,73 **** --- 70,77 ---- #endif + #if FRONTIER_PYTHON + #include "langpython.h" + #endif + enum { *************** *** 284,293 **** if (signature == typeLAND) return (langbuildtree (htext, true, hcode)); ! #ifdef flcomponent ! fl = osagetcode (htext, signature, false, &codeval); #else ! fl = false; #endif disposehandle (htext); --- 288,309 ---- if (signature == typeLAND) return (langbuildtree (htext, true, hcode)); ! ! #ifdef FRONTIER_PYTHON ! if (signature == typePython) { ! fl = compilepython (htext, &codeval); ! } ! else { ! #endif ! #ifdef flcomponent ! fl = osagetcode (htext, signature, false, &codeval); #else ! fl = false; ! #endif ! ! #ifdef FRONTIER_PYTHON ! } #endif + disposehandle (htext); *************** *** 300,304 **** return (false); ! (**hstub).nodetype = osascriptop; /* --- 316,326 ---- return (false); ! #ifdef FRONTIER_PYTHON ! if (signature == typePython) ! (**hstub).nodetype = pythonscriptop; ! else ! #endif ! (**hstub).nodetype = osascriptop; ! /* *************** *** 3568,3571 **** --- 3590,3599 ---- bigstring bsname; + #ifdef FRONTIER_PYTHON + // a naive attempt to inject Python into the list + copyctopstring("Python", bsname); + (*visit)(typePython, bsname, refcon); + #endif + if (!havecomponentmanager ()) return (false); Index: langvalue.c =================================================================== RCS file: /cvsroot/frontierkernel/Frontier/Common/source/langvalue.c,v retrieving revision 1.5 retrieving revision 1.5.4.1 diff -C2 -d -r1.5 -r1.5.4.1 *** langvalue.c 31 Oct 2004 21:43:38 -0000 1.5 --- langvalue.c 28 Nov 2004 00:40:48 -0000 1.5.4.1 *************** *** 53,56 **** --- 53,59 ---- #include "osacomponent.h" #endif + #ifdef FRONTIER_PYTHON + #include "langpython.h" + #endif #include "timedate.h" *************** *** 8054,8057 **** --- 8057,8063 ---- tyvaluerecord osacode; #endif + #if (version5orgreater && defined (FRONTIER_PYTHON)) + tyvaluerecord pythoncode; + #endif #if version5orgreater // do special case checking here, not in langrunhandlercode *************** *** 8086,8089 **** --- 8092,8111 ---- #endif // flcomponent + + #ifdef FRONTIER_PYTHON + if (ispythonnode (hcode, &pythoncode)) { + if (!langpushsourcecode (htable, hnode, bsname)) + return (false); + + // 5.0a18 dmb: this probably isn't adequate for osa execution + langsetthisvalue (currenthashtable, htable, bsname); + + fl = callpythonscript (pythoncode, hparam1, bsname, vreturned); + + langpopsourcecode (); + + return (fl); + } + #endif #endif // version5orgreater Index: langevaluate.c =================================================================== RCS file: /cvsroot/frontierkernel/Frontier/Common/source/langevaluate.c,v retrieving revision 1.3 retrieving revision 1.3.4.1 diff -C2 -d -r1.3 -r1.3.4.1 *** langevaluate.c 30 Oct 2004 23:11:36 -0000 1.3 --- langevaluate.c 28 Nov 2004 00:40:47 -0000 1.3.4.1 *************** *** 40,43 **** --- 40,47 ---- #endif + #ifdef FRONTIER_PYTHON + #include "langpython.h" + #endif + #define fltryerrorstackcode false *************** *** 1561,1565 **** op = (**h).nodetype; /*copy into register*/ ! ctparams = (**h).ctparams; --- 1565,1569 ---- op = (**h).nodetype; /*copy into register*/ ! ctparams = (**h).ctparams; *************** *** 1879,1882 **** --- 1883,1892 ---- return (evaluateosascript (&val1, nil, zerostring, valtree)); #endif + + #ifdef FRONTIER_PYTHON + case pythonscriptop: + return callpython (htree, valtree); + + #endif default: Index: langpython.c =================================================================== RCS file: /cvsroot/frontierkernel/Frontier/Common/source/langpython.c,v retrieving revision 1.2.4.16 retrieving revision 1.2.4.17 diff -C2 -d -r1.2.4.16 -r1.2.4.17 *** langpython.c 25 Nov 2004 13:13:58 -0000 1.2.4.16 --- langpython.c 28 Nov 2004 00:40:48 -0000 1.2.4.17 *************** *** 210,213 **** --- 210,221 ---- PyTypeObject * ptr_PyUnicode_Type; /*unicodeobject.h*/ + + int (*ptr_PyCallable_Check) (PyObject *); /*abstract.h*/ + PyObject * (*ptr_PyObject_Call) (PyObject *, PyObject *, PyObject *); + PyObject * (*ptr_PyObject_CallFunction) (PyObject *, char *, ...); + + PyObject * (*ptr_PyList_AsTuple) (PyObject *); + PyObject * (*ptr_PyObject_CallObject) (PyObject *, PyObject *); + } py; *************** *** 302,305 **** --- 310,318 ---- #define PyUnicode_Type (*py.ptr_PyUnicode_Type) + #define PyCallable_Check (*py.ptr_PyCallable_Check) + #define PyObject_Call (*py.ptr_PyObject_Call) + #define PyObject_CallFunction (*py.ptr_PyObject_CallFunction) + #define PyList_AsTuple (*py.ptr_PyList_AsTuple) + #define PyObject_CallObject (*py.ptr_PyObject_CallObject) static boolean importsymbol (typythonlib hm, char *symbol, void **ptraddress) { *************** *** 429,432 **** --- 442,452 ---- fl = fl && IMPORTSYMBOL (hm, PyUnicode_Type); + + fl = fl && IMPORTSYMBOL (hm, PyCallable_Check); + fl = fl && IMPORTSYMBOL (hm, PyObject_Call); + fl = fl && IMPORTSYMBOL (hm, PyObject_CallFunction); + + fl = fl && IMPORTSYMBOL (hm, PyList_AsTuple); + fl = fl && IMPORTSYMBOL (hm, PyObject_CallObject); if (fl) { *************** *** 447,452 **** - #if 0 - static boolean ispythonloaded (void) { --- 467,470 ---- *************** *** 455,458 **** --- 473,478 ---- + #if 0 + static void freepythonlib (void) { *************** *** 504,509 **** } FrontierPythonPtr; - #if 0 - /* static boolean getPythonPtr(FrontierPythonPtr *fpp, PyObject **pptr) { --- 524,527 ---- *************** *** 541,546 **** return true; } - */ - #endif static boolean convertPython(PyObject *, hdlhashtable, bigstring); --- 559,562 ---- *************** *** 571,574 **** --- 587,656 ---- } + static boolean pythontofrontier(PyObject *obj, tyvaluerecord *tvr) { + PyTypeObject *ob_type = obj->ob_type; + + setbooleanvalue(false, tvr); + + if (ob_type == &PyFloat_Type) { + + double d = PyFloat_AsDouble(obj); + if (!setdoublevalue(d, tvr)) + return false; + + } else if (ob_type == &PyInt_Type) { + + long l = PyInt_AsLong(obj); + if (!setlongvalue(l, tvr)) + return false; + + } else if (ob_type == &PyLong_Type) { + + long l = PyInt_AsLong(obj); + if (!setlongvalue(l, tvr)) + return false; + + } else if (ob_type == &PyString_Type) { + + char *s = PyString_AsString(obj); + if (!newheapvalue(s, strlen(s), stringvaluetype, tvr)) + return false; + + } else if (ob_type == &PyDict_Type) { + + //return convertPythonDict(obj, hdl, name); + return false; + + } else if (ob_type == &PyComplex_Type) { + + return false; + + } else if (ob_type == &PyInstance_Type) { + + //return convertPythonDict(PyObject_GetAttrString(obj, "__dict__"), hdl, name); + return false; + + } else if (ob_type == &PyList_Type) { + //printf("list\n"); + } else if (ob_type == &PyFile_Type) { + //printf("file\n"); + } else if (ob_type == &PyMethod_Type) { + //printf("method\n"); + } else if (ob_type == &PyModule_Type) { + //printf("module\n"); + } else if (ob_type == &PySeqIter_Type) { + //printf("seqiter\n"); + } else if (ob_type == &PySlice_Type) { + //printf("slice\n"); + } else if (ob_type == &PyTuple_Type) { + //printf("tuple\n"); + } else if (ob_type == &PyType_Type) { + //printf("type\n"); + } else if (ob_type == &PyUnicode_Type) { + //printf("unicode\n"); + } + + return false; + } + static boolean convertPython(PyObject *obj, hdlhashtable hdl, bigstring name) { *************** *** 617,621 **** if (!langassignnewtablevalue(hdl, name, &newTable)) return false; ! r = PyComplex_RealAsDouble(obj); i = PyComplex_ImagAsDouble(obj); --- 699,703 ---- if (!langassignnewtablevalue(hdl, name, &newTable)) return false; ! r = PyComplex_RealAsDouble(obj); i = PyComplex_ImagAsDouble(obj); *************** *** 644,648 **** return convertPythonDict(PyObject_GetAttrString(obj, "__dict__"), hdl, name); ! } else if (ob_type == &PyList_Type) { //printf("list\n"); --- 726,730 ---- return convertPythonDict(PyObject_GetAttrString(obj, "__dict__"), hdl, name); ! } else if (ob_type == &PyList_Type) { //printf("list\n"); *************** *** 1347,1356 **** 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; --- 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; *************** *** 1415,1428 **** PyObject *globals = 0; ! static boolean initGlobals() { ! if (globals) ! return true; ! ! globals = PyDict_New(); ! if (PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins())) return false; ! if (PyDict_SetItemString(globals, "__name__", PyString_FromString("__main__"))) return false; --- 1497,1505 ---- PyObject *globals = 0; ! static boolean makeGlobals(PyObject *globals, char *name) { if (PyDict_SetItemString(globals, "__builtins__", PyEval_GetBuiltins())) return false; ! if (PyDict_SetItemString(globals, "__name__", PyString_FromString(name))) return false; *************** *** 1433,1436 **** --- 1510,1525 ---- } + static boolean initGlobals() { + if (globals) + return true; + + globals = PyDict_New(); + + if (!makeGlobals(globals, "__main__")) + return false; + + return true; + } + static boolean disposeGlobals() { if (globals) { *************** *** 1442,1445 **** --- 1531,1537 ---- static boolean startInterpreter() { + if (!ispythonloaded()) + return false; + if (!Py_IsInitialized()) { Py_Initialize(); *************** *** 1457,1460 **** --- 1549,1555 ---- static boolean stopInterpreter() { + if (!ispythonloaded()) + return false; + if (Py_IsInitialized()) { disposeGlobals(); *************** *** 1478,1481 **** --- 1573,1579 ---- PyObject *err; + if (!ispythonloaded()) + return false; + // if Python hasn't been started, start it and create our globals. // *************** *** 1642,1644 **** --- 1740,1980 ---- } + static void + convertreturns(Handle htext) { + int len = gethandlesize(htext); + for (int i = 0; i < len; i++) + if ((*htext)[i] == '\r') + (*htext)[i] = '\n'; + } + + 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; + + if (!copyhandle(htext, &newtext)) + return false; + + if (!newheapvalue(*newtext, gethandlesize(newtext), stringvaluetype, tvr)) + return false; + + //exemptfromtmpstack(&newtext); + + return true; + } + + boolean ispythonnode(hdltreenode htree, tyvaluerecord *tvr) { + register hdltreenode h = htree; + + if ((**h).nodetype == moduleop) + h = (**h).param1; + + if ((**h).nodetype != pythonscriptop) + return (false); + + *tvr = (**h).nodeval; + + return (true); + } + + static PyObject * + pythonconvertparameters(hdltreenode hparams) { + register hdltreenode nomad = hparams; + PyObject *argList = PyList_New(0); + + while (nomad != nil) { + PyObject *arg; + convertfrontiervalue(&((**nomad).nodeval), &arg); + PyList_Append(argList, arg); + nomad = (**nomad).link; + } + + return PyList_AsTuple(argList); + } + + 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 + + // convert the text to a C string + // + htext = code.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 *func = NULL; + PyObject *argList; + char funcName[sizeof(bigstring)]; + copyptocstring(bsname, funcName); + + // 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); + + // call it + result = PyObject_CallObject(func, argList); + + // handle errors + if (PyErr_Occurred()) { + PyErr_Print(); + PyErr_Clear(); + return false; + } + + // convert the return value + if (!pythontofrontier(result, vreturned)) { + if (result) Py_DECREF(result); + return false; + } + if (result) Py_DECREF(result); + } + + return true; + } + + #endif |