[BACK]Return to ox_python.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_python

Diff for /OpenXM/src/ox_python/ox_python.c between version 1.3 and 1.5

version 1.3, 2018/09/08 05:04:34 version 1.5, 2021/12/31 07:43:48
Line 1 
Line 1 
 /* $OpenXM: OpenXM/src/ox_python/ox_python.c,v 1.2 2018/09/08 03:05:19 takayama Exp $  /* $OpenXM: OpenXM/src/ox_python/ox_python.c,v 1.4 2019/03/22 00:14:50 takayama Exp $
 */  */
   
 #include <stdio.h>  #include <stdio.h>
Line 374  int sm_executeFunction()
Line 374  int sm_executeFunction()
     }else if (strcmp(func->s,"PyRun_String")==0) {      }else if (strcmp(func->s,"PyRun_String")==0) {
         my_PyRun_String();          my_PyRun_String();
     }else if (strcmp(func->s,"eval")==0) {      }else if (strcmp(func->s,"eval")==0) {
         my_eval2b();          my_eval2();
     }else {      }else {
         push(make_error2("sm_executeFunction, unknown function",NULL,0,-1));          push(make_error2("sm_executeFunction, unknown function",NULL,0,-1));
         return -1;          return -1;
Line 526  int main(int argc,char *argv[])
Line 526  int main(int argc,char *argv[])
 #endif  #endif
   
     /* Initialize python */      /* Initialize python */
     Py_SetProgramName(argv[0]);  /* optional but recommended */      Py_SetProgramName((wchar_t *) argv[0]);  /* optional but recommended */
     Py_Initialize();      Py_Initialize();
   
   
Line 590  int my_PyRun_String() {
Line 590  int my_PyRun_String() {
   if (py_main == NULL) py_main = PyImport_AddModule("__main__");    if (py_main == NULL) py_main = PyImport_AddModule("__main__");
   if (py_dict == NULL) py_dict = PyModule_GetDict(py_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_single_input,py_dict,py_dict);    pyRes = PyRun_String(cmd,Py_eval_input,py_dict,py_dict);
   if (pyRes==NULL) {    if (pyRes==NULL) {
     push(make_error2("PyRun_String: exception",NULL,0,-1));      push(make_error2("PyRun_String: exception",NULL,0,-1));
     PyRun_SimpleString("\n");      PyRun_SimpleString("\n");
Line 602  int my_PyRun_String() {
Line 602  int my_PyRun_String() {
 }  }
   
 int push_python_result(PyObject *pyRes) {  int push_python_result(PyObject *pyRes) {
   if (PyString_Check(pyRes)) {    if (PyUnicode_Check(pyRes)) {
     push((cmo *)new_cmo_string(PyString_AsString(pyRes)));      push((cmo *)new_cmo_string(PyBytes_AsString(PyUnicode_AsEncodedString(pyRes,"UTF-8","strict"))));
     return(0);      return(0);
   }else if (PyInt_Check(pyRes)) {    }else if (PyLong_Check(pyRes)) {
     push((cmo *)new_cmo_int32((int) PyInt_AS_LONG(pyRes)));      push((cmo *)new_cmo_int32((int) PyLong_AsLong(pyRes)));
     return(0);      return(0);
   }else {    }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);      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));  //    push(make_error2("PyRun_String returns an object which as not been implemented.",NULL,0,-1));
 //    return(-1);  //    return(-1);
   }    }
Line 634  int my_eval() {
Line 636  int my_eval() {
   printf("my_eval cmd=%s\n",cmd);    printf("my_eval cmd=%s\n",cmd);
   
   // code from https://docs.python.jp/2.7/extending/embedding.html    // 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) pModule = PyImport_Import(pName);
   
   if (pModule != NULL) {    if (pModule != NULL) {
     if (pFunc==NULL) pFunc = PyObject_GetAttrString(pModule, "eval");      if (pFunc==NULL) pFunc = PyObject_GetAttrString(pModule, "eval");
     if (pFunc && PyCallable_Check(pFunc)) {      if (pFunc && PyCallable_Check(pFunc)) {
       pArgs = PyTuple_New(3);        pArgs = PyTuple_New(3);
       PyTuple_SetItem(pArgs,0,PyString_FromString(cmd));        PyTuple_SetItem(pArgs,0,PyBytes_FromString(cmd));
       PyTuple_SetItem(pArgs,1,PyEval_GetGlobals());        PyTuple_SetItem(pArgs,1,PyEval_GetGlobals());
       PyTuple_SetItem(pArgs,2,PyEval_GetLocals());        PyTuple_SetItem(pArgs,2,PyEval_GetLocals());
       pValue = PyObject_CallObject(pFunc, pArgs);        pValue = PyObject_CallObject(pFunc, pArgs);
Line 698  int my_eval2() {
Line 700  int my_eval2() {
   if (pModule != NULL) {    if (pModule != NULL) {
     if (pFunc && PyCallable_Check(pFunc)) {      if (pFunc && PyCallable_Check(pFunc)) {
       pArgs = PyTuple_New(1);        pArgs = PyTuple_New(1);
       PyTuple_SetItem(pArgs,0,PyString_FromString(cmd));        PyTuple_SetItem(pArgs,0,PyBytes_FromString(cmd));
       pValue = PyObject_CallObject(pFunc, pArgs);        pValue = PyObject_CallObject(pFunc, pArgs);
       Py_DECREF(pArgs);        Py_DECREF(pArgs);
       if (pValue != NULL) {        if (pValue != NULL) {

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.5

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>