]> SALOME platform Git repositories - modules/yacs.git/commitdiff
Salome HOME
[PY3] Convert py2yacs into Python3
authorGilles DAVID <gilles-g.david@edf.fr>
Tue, 2 May 2017 16:16:04 +0000 (18:16 +0200)
committerGilles DAVID <gilles-g.david@edf.fr>
Wed, 3 May 2017 16:10:41 +0000 (18:10 +0200)
src/py2yacs/Test/Py2yacsTest.cxx
src/py2yacs/py2yacs.cxx
src/py2yacs/py2yacs.py

index 8572bd3ffe19dcba9910b550212e5d6b94fcec8b..a86802431931a33af946e0165a458dce1bb9e8fd 100644 (file)
@@ -61,6 +61,10 @@ void verifyCorrectPycode(const char * code_py, const char * function_name,
   {
     CPPUNIT_FAIL(e.what());
   }
+  catch(std::exception& e)
+  {
+    CPPUNIT_FAIL(e.what());
+  }
   catch(...)
   {
     CPPUNIT_FAIL("Unknown exception");
@@ -102,6 +106,10 @@ void verifyWrongPycode(const char* code_py, const char* function_name,
     std::cerr << "===============================================" << std::endl;
     CPPUNIT_ASSERT(what.find(error_message) != std::string::npos);
   }
+  catch(std::exception& e)
+  {
+    CPPUNIT_FAIL(e.what());
+  }
 }
 
 static
@@ -125,6 +133,10 @@ void verifyWrongParser(const char* parser_module, const char* parser_function,
     std::cerr << "===============================================" << std::endl;
     CPPUNIT_ASSERT(what.find(error_message) != std::string::npos);
   }
+  catch(std::exception& e)
+  {
+    CPPUNIT_FAIL(e.what());
+  }
 }
 
 void Py2yacsTest::t1()
@@ -152,9 +164,9 @@ void Py2yacsTest::t2()
 void Py2yacsTest::t3()
 {
   const char * code_py = "def f1(a, b, c):\n"
-                         "  print a\n"
-                         "  print b\n"
-                         "  print c\n";
+                         "  print(a)\n"
+                         "  print(b)\n"
+                         "  print(c)\n";
   const char* input_ports[] = {"a", "b", "c"};
   const char* output_ports[] = {};
   verifyCorrectPycode(code_py, "f1", 3, input_ports, 0, output_ports);
@@ -163,7 +175,7 @@ void Py2yacsTest::t3()
 void Py2yacsTest::t4()
 {
   const char * code_py = "def f1():\n"
-                         "  print 'toto'\n"
+                         "  print('toto')\n"
                          "  return\n";
   const char* input_ports[] = {"a", "b", "c"};
   const char* output_ports[] = {};
@@ -237,7 +249,7 @@ void Py2yacsTest::syntaxError()
 {
   const char * code_py = "bla bla bla\n"
                          "  return f1(x)\n";
-  verifyWrongPycode(code_py, "f2", "SyntaxError");
+  verifyWrongPycode(code_py, "f2", "invalid syntax");
 }
 
 void Py2yacsTest::badFunctionName()
@@ -291,19 +303,19 @@ void Py2yacsTest::schemaExec()
 // Test the behaviour when there is an error in the python parser
 void Py2yacsTest::parserErrors()
 {
-  verifyWrongParser("bad_parsers", "p1", "argument");
-  verifyWrongParser("bad_parsers", "p2", "Attribute 'name' not found");
+  verifyWrongParser("bad_parsers", "p1", "0 positional arguments");
+  verifyWrongParser("bad_parsers", "p2", "'str' object has no attribute 'name'");
   verifyWrongParser("bad_parsers", "p3", "should be a python list");
-  verifyWrongParser("bad_parsers", "p4", "Traceback");
+  verifyWrongParser("bad_parsers", "p4", "unsupported operand type");
   verifyWrongParser("bad_parsers", "f", "Cannot find the parsing function");
   verifyWrongParser("err_py2yacs_invalid", "get_properties", "invalid syntax");
   verifyWrongParser("no_file", "f", "Failed to load");
-  verifyWrongParser("bad_parsers", "p5", " ");
-  verifyWrongParser("bad_parsers", "p6", " ");
-  verifyWrongParser("bad_parsers", "p7", " ");
+  verifyWrongParser("bad_parsers", "p5", "is not a string");
+  verifyWrongParser("bad_parsers", "p6", "is not a string");
+  verifyWrongParser("bad_parsers", "p7", "is not a string");
   verifyWrongParser("bad_parsers", "p8", "Attribute 'name' should be a string.");
-  verifyWrongParser("bad_parsers", "p9", " ");
-  verifyWrongParser("bad_parsers", "p10", " ");
+  verifyWrongParser("bad_parsers", "p9", "Not a python list");
+  verifyWrongParser("bad_parsers", "p10", "is not a string");
 }
 
 void Py2yacsTest::globalVerification()
@@ -341,4 +353,4 @@ void Py2yacsTest::globalVerification()
     std::cerr << "-----------------------------------------" << std::endl;
   }
 
-}
\ No newline at end of file
+}
index 982b0124c8dfad62c21f96f83f80f062c489982b..3f244aa7435d1491c88444daec7dc11fcbe757be 100644 (file)
@@ -74,7 +74,7 @@ const std::list<FunctionProperties>& Py2yacs::getFunctionProperties()const
 static
 std::string copyList(PyObject *pyList, std::list<std::string>& cppList)
 {
-  std::string error;
+  std::string error="";
   if(not PyList_Check(pyList))
   {
     error = "Not a python list.\n";
@@ -82,11 +82,11 @@ std::string copyList(PyObject *pyList, std::list<std::string>& cppList)
   }
   else
   {
-    int n = PyList_Size(pyList);
-    for(int i=0; i<n; i++)
+    Py_ssize_t n = PyList_Size(pyList);
+    for(Py_ssize_t i=0; i<n; i++)
     {
       PyObject *elem = PyList_GetItem(pyList,i);
-      if(not PyBytes_Check(elem))
+      if(not PyUnicode_Check(elem))
       {
         std::stringstream message;
         message << "List element number " << i << " is not a string.\n";
@@ -95,8 +95,7 @@ std::string copyList(PyObject *pyList, std::list<std::string>& cppList)
       }
       else
       {
-        const char * portName = PyBytes_AsString(elem);
-        cppList.push_back(portName);
+        cppList.push_back(std::string(PyUnicode_AsUTF8(elem)));
       }
     }
   }
@@ -113,14 +112,14 @@ std::string getPyErrorText()
     PyObject *pystr, *module_name, *pyth_module, *pyth_func;
     PyErr_Fetch(&ptype, &pvalue, &ptraceback);
     pystr = PyObject_Str(pvalue);
-    result = PyBytes_AsString(pystr);
+    result = std::string(PyUnicode_AsUTF8(pystr));
     result += "\n";
     Py_DECREF(pystr);
     
     /* See if we can get a full traceback */
     if(ptraceback)
     {
-      module_name = PyBytes_FromString("traceback");
+      module_name = PyUnicode_FromString("traceback");
       pyth_module = PyImport_Import(module_name);
       Py_DECREF(module_name);
       if (pyth_module)
@@ -136,7 +135,7 @@ std::string getPyErrorText()
             for(int i=0; i<n; i++)
             {
               pystr = PyList_GetItem(pyList,i);
-              result += PyBytes_AsString(pystr);
+              result += std::string(PyUnicode_AsUTF8(pystr));
             }
             Py_DECREF(pyList);
           }
@@ -180,7 +179,7 @@ void Py2yacs::load(const std::string& python_code)
     
     // Py_Initialize();
     YACS::ENGINE::AutoGIL agil;
-    pValue = PyBytes_FromString(_python_parser_module.c_str());
+    pValue = PyUnicode_FromString(_python_parser_module.c_str());
     pModule = PyImport_Import(pValue);
     Py_DECREF(pValue);
 
@@ -198,7 +197,7 @@ void Py2yacs::load(const std::string& python_code)
       if (pFunc && PyCallable_Check(pFunc))
       {
         pArgs = PyTuple_New(1);
-        pValue = PyBytes_FromString(python_code.c_str());
+        pValue = PyUnicode_FromString(python_code.c_str());
         PyTuple_SetItem(pArgs, 0, pValue);
         
         pValue = PyObject_CallObject(pFunc, pArgs);
@@ -211,7 +210,7 @@ void Py2yacs::load(const std::string& python_code)
           {
             errorMessage += "Parsing function should return a tuple of two string lists.\n";
           }
-          int n = PyTuple_Size(pValue);
+          Py_ssize_t n = PyTuple_Size(pValue);
           if(n != 2)
           {
             errorMessage += "Parsing function should return two string lists.\n";
@@ -232,7 +231,7 @@ void Py2yacs::load(const std::string& python_code)
               
               if(pAttribute = checkAndGetAttribute(fpy, "name", errorMessage))
               {
-                if(not PyBytes_Check(pAttribute))
+                if(not PyUnicode_Check(pAttribute))
                 {
                   errorMessage += "Attribute 'name' should be a string.\n";
                   Py_DECREF(pAttribute);
@@ -241,7 +240,7 @@ void Py2yacs::load(const std::string& python_code)
                 {
                   _functions.push_back(FunctionProperties());
                   FunctionProperties& fcpp = _functions.back();
-                  fcpp._name=PyBytes_AsString(pAttribute);
+                  fcpp._name=std::string(PyUnicode_AsUTF8(pAttribute));
                   Py_DECREF(pAttribute);
                   
                   if(pAttribute = checkAndGetAttribute(fpy, "inputs", errorMessage))
@@ -376,4 +375,4 @@ YACS::ENGINE::Proc* Py2yacs::createProc(const std::string& python_function)const
     node->edAddOutputPort(*it, tc_double);
   
   return schema;
-}
\ No newline at end of file
+}
index 2e372aab64f0bb8429690b82ecf248dba9a333f7..f5f01ce3d384a764e6d293f661dc72f4a4efba86 100644 (file)
@@ -41,7 +41,7 @@ class FunctionProperties:
 
 class v(ast.NodeVisitor):
   def visit_Module(self, node):
-    #print type(node).__name__, ":"
+    #print(type(node).__name__, ":")
     accepted_tokens = ["Import", "ImportFrom", "FunctionDef", "ClassDef"]
     #print "module body:"
     self.global_errors=[]
@@ -51,8 +51,8 @@ class v(ast.NodeVisitor):
         error="py2yacs error at line %s: not accepted statement '%s'." % (
                e.lineno, type_name)
         self.global_errors.append(error)
-      #print type_name
-    #print "------------------------------------------------------------------"
+      #print(type_name)
+    #print("------------------------------------------------------------------")
     self.functions=[]
     self.lastfn=""
     self.infunc=False
@@ -60,7 +60,7 @@ class v(ast.NodeVisitor):
     self.generic_visit(node)
     pass
   def visit_FunctionDef(self, node):
-    #print type(node).__name__, ":", node.name
+    #print(type(node).__name__, ":", node.name)
     if not self.infunc:
       self.lastfn = FunctionProperties(node.name)
       self.functions.append(self.lastfn)
@@ -71,24 +71,12 @@ class v(ast.NodeVisitor):
       self.lastfn = None
       self.infunc=False
     pass
-  def visit_arguments(self, node):
-    #print type(node).__name__, ":"
-    self.inargs=True
-    self.generic_visit(node)
-    self.inargs=False
-    pass
-  def visit_Name(self, node):
-    if self.inargs :
-      #print type(node).__name__, ":", node.id
-      self.lastname=node.id
-      self.generic_visit(node)
-    pass
-  def visit_Param(self, node):
-    #print type(node).__name__, ":", self.lastname
-    self.lastfn.inputs.append(self.lastname)
+  def visit_arg(self, node):
+    #print(type(node).__name__, ":", node.arg)
+    self.lastfn.inputs.append(node.arg)
     pass
   def visit_Return(self, node):
-    #print type(node).__name__, ":", node.value
+    #print(type(node).__name__, ":", node.value)
     if self.lastfn.outputs is not None :
       error="py2yacs error at line %s: multiple returns." % node.lineno
       self.lastfn.errors.append(error)
@@ -198,4 +186,3 @@ if __name__ == '__main__':
       print("\n".join(fn_properties.errors))
   else:
     print("Function not found:", fn_name)
-  
\ No newline at end of file