=================================================================== RCS file: /home/cvs/OpenXM/src/ox_python/ox_python.c,v retrieving revision 1.2 retrieving revision 1.8 diff -u -p -r1.2 -r1.8 --- OpenXM/src/ox_python/ox_python.c 2018/09/08 03:05:19 1.2 +++ OpenXM/src/ox_python/ox_python.c 2022/01/04 01:34:23 1.8 @@ -1,4 +1,4 @@ -/* $OpenXM: OpenXM/src/ox_python/ox_python.c,v 1.1 2018/09/08 00:16:19 takayama Exp $ +/* $OpenXM: OpenXM/src/ox_python/ox_python.c,v 1.7 2022/01/02 02:54:00 takayama Exp $ */ #include @@ -152,14 +152,18 @@ cmo *make_error2(const char *reason,const char *fname, cmo **argv; int n; char *s; + char s_tmp[256]; + s = s_tmp; n = 5; argv = (cmo **) GC_malloc(sizeof(cmo *)*n); - ms = (cmo *)new_cmo_string("Error"); argv[0] = ms; - if (reason != NULL) {s = (char *)GC_malloc(strlen(reason)+1); strcpy(s,reason); - }else strcpy(s,""); + ms = (cmo *)new_cmo_string("error"); argv[0] = ms; + if (reason != NULL) {s = (char *)GC_malloc(strlen(reason)+3); + strcpy(s,"\"");strcat(s,reason);strcat(s,"\""); + }else strcpy(s,"0"); ms = (cmo *) new_cmo_string(s); argv[1] = ms; - if (fname != NULL) {s = (char *)GC_malloc(strlen(fname)+1); strcpy(s,fname); - }else strcpy(s,""); + if (fname != NULL) {s = (char *)GC_malloc(strlen(fname)+3); + strcpy(s,"\"");strcat(s,fname);strcat(s,"\""); + }else strcpy(s,"0"); ms = (cmo *) new_cmo_string(s); argv[2] = ms; err = (cmo *)new_cmo_int32(line); argv[3] = err; err = (cmo *)new_cmo_int32(code); argv[4] = err; @@ -374,7 +378,7 @@ int sm_executeFunction() }else if (strcmp(func->s,"PyRun_String")==0) { my_PyRun_String(); }else if (strcmp(func->s,"eval")==0) { - my_eval(); + my_eval2(); }else { push(make_error2("sm_executeFunction, unknown function",NULL,0,-1)); return -1; @@ -390,6 +394,7 @@ int sm_executeStringByLocalParser() push(make_error2("sm_executeStringByLocalParser, not CMO_STRING",NULL,0,-1)); return -1; } +/*** {FILE *fp; fp=fopen("tmp-debug.txt","a"); fprintf(fp,"strlen=%d\n%s\n",strlen(cmd->s),cmd->s);fclose(fp); } ***/ status=PyRun_SimpleString(cmd->s); // push(make_error2("sm_executeStringByLocalParser",NULL,0,-1)); push((cmo *)new_cmo_int32(status)); @@ -477,7 +482,7 @@ void push_error_from_file() { FILE *fp; #define BUF_SIZE 1024 char logname[BUF_SIZE]; - char cmd[BUF_SIZE]; + char cmd[BUF_SIZE+256]; char file[BUF_SIZE]; char reason[BUF_SIZE]; int gsl_errno, line; @@ -524,9 +529,8 @@ int main(int argc,char *argv[]) #else signal(SIGUSR1,usr1_handler); #endif - /* Initialize python */ - Py_SetProgramName(argv[0]); /* optional but recommended */ + Py_SetProgramName((wchar_t *) argv[0]); /* optional but recommended */ Py_Initialize(); @@ -589,7 +593,8 @@ int my_PyRun_String() { printf("cmd=%s\n",cmd); if (py_main == NULL) py_main = PyImport_AddModule("__main__"); if (py_dict == NULL) py_dict = PyModule_GetDict(py_main); - pyRes = PyRun_String(cmd,Py_single_input,py_dict,py_dict); +// pyRes = PyRun_String(cmd,Py_single_input,py_dict,py_dict); + pyRes = PyRun_String(cmd,Py_eval_input,py_dict,py_dict); if (pyRes==NULL) { push(make_error2("PyRun_String: exception",NULL,0,-1)); PyRun_SimpleString("\n"); @@ -601,15 +606,17 @@ int my_PyRun_String() { } int push_python_result(PyObject *pyRes) { - if (PyString_Check(pyRes)) { - push((cmo *)new_cmo_string(PyString_AsString(pyRes))); + if (PyUnicode_Check(pyRes)) { + push((cmo *)new_cmo_string(PyBytes_AsString(PyUnicode_AsEncodedString(pyRes,"UTF-8","strict")))); return(0); - }else if (PyInt_Check(pyRes)) { - push((cmo *)new_cmo_int32((int) PyInt_AS_LONG(pyRes))); + }else if (PyLong_Check(pyRes)) { + push((cmo *)new_cmo_int32((int) PyLong_AsLong(pyRes))); return(0); }else { - push((cmo *)new_cmo_string(PyString_AsString(PyObject_Str(pyRes)))); + push((cmo *)new_cmo_string(PyBytes_AsString(PyUnicode_AsEncodedString(PyObject_Str(pyRes),"UTF-8","strict")))); return(0); + /* push((cmo *)new_cmo_string(PyBytes_AsString(PyObject_Str(pyRes)))); + return(0); */ // push(make_error2("PyRun_String returns an object which as not been implemented.",NULL,0,-1)); // return(-1); } @@ -627,20 +634,22 @@ int my_eval() { pop(); // pop argc cmd = get_string(); if (cmd == NULL) { - push(make_error2("my_PyRun_Sring: argument is not a string",NULL,0,-1)); + push(make_error2("my_eval: argument is not a string",NULL,0,-1)); return(-1); } printf("my_eval cmd=%s\n",cmd); // code from https://docs.python.jp/2.7/extending/embedding.html - if (pName==NULL) pName = PyString_FromString("__builtin__"); + if (pName==NULL) pName = PyBytes_FromString("__builtin__"); if (pModule==NULL) pModule = PyImport_Import(pName); if (pModule != NULL) { if (pFunc==NULL) pFunc = PyObject_GetAttrString(pModule, "eval"); if (pFunc && PyCallable_Check(pFunc)) { - pArgs = PyTuple_New(1); - PyTuple_SetItem(pArgs,0,PyString_FromString(cmd)); + pArgs = PyTuple_New(3); + PyTuple_SetItem(pArgs,0,PyBytes_FromString(cmd)); + PyTuple_SetItem(pArgs,1,PyEval_GetGlobals()); + PyTuple_SetItem(pArgs,2,PyEval_GetLocals()); pValue = PyObject_CallObject(pFunc, pArgs); Py_DECREF(pArgs); if (pValue != NULL) { @@ -658,6 +667,60 @@ int my_eval() { if (PyErr_Occurred()) PyErr_Print(); fprintf(stderr, "Cannot find function eval\n"); + } + return(-1); + } + else { + PyErr_Print(); + fprintf(stderr, "Failed to load __builtin__\n"); + return -1; + } +} + +int my_eval2() { + static PyObject *pName=NULL; + static PyObject *pModule=NULL; + static PyObject *pDict=NULL; + static PyObject *pFunc=NULL; + PyObject *pArgs, *pValue; + char *cmd; + int i; + char *cmd2; + pop(); // pop argc + cmd = get_string(); + if (cmd == NULL) { + push(make_error2("my_eval2: argument is not a string",NULL,0,-1)); + return(-1); + } + printf("my_eval2 cmd=%s\n",cmd); + + // code from https://stackoverflow.com/questions/48432577/extracting-value-from-python-after-its-embedded-in-c + cmd2=(char *)GC_malloc(strlen(cmd)+256); + sprintf(cmd2,"f = lambda x: eval(x)"); + PyRun_SimpleString(cmd2); + pModule = PyImport_ImportModule("__main__"); + pFunc= PyObject_GetAttrString(pModule,"f"); + + if (pModule != NULL) { + if (pFunc && PyCallable_Check(pFunc)) { + pArgs = PyTuple_New(1); + PyTuple_SetItem(pArgs,0,PyBytes_FromString(cmd)); + pValue = PyObject_CallObject(pFunc, pArgs); + Py_DECREF(pArgs); + if (pValue != NULL) { + push_python_result(pValue); + // Py_DECREF(pValue); + return(0); + } + else { + PyErr_Print(); + push(make_error2("Fail to call PyObjedct_CallObject(eval,...)",NULL,0,-1)); + return(-1); + } + }else { + if (PyErr_Occurred()) + PyErr_Print(); + fprintf(stderr, "Cannot find function f\n"); } return(-1); }