From 2621f777421bf09cf521a878d0e1ca1b6a615b86 Mon Sep 17 00:00:00 2001 From: Gilles DAVID Date: Tue, 2 May 2017 18:16:04 +0200 Subject: [PATCH] [PY3] Convert py2yacs into Python3 --- src/py2yacs/Test/Py2yacsTest.cxx | 40 +++++++++++++++++++++----------- src/py2yacs/py2yacs.cxx | 29 +++++++++++------------ src/py2yacs/py2yacs.py | 29 +++++++---------------- 3 files changed, 48 insertions(+), 50 deletions(-) diff --git a/src/py2yacs/Test/Py2yacsTest.cxx b/src/py2yacs/Test/Py2yacsTest.cxx index 8572bd3ff..a86802431 100644 --- a/src/py2yacs/Test/Py2yacsTest.cxx +++ b/src/py2yacs/Test/Py2yacsTest.cxx @@ -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 +} diff --git a/src/py2yacs/py2yacs.cxx b/src/py2yacs/py2yacs.cxx index 982b0124c..3f244aa74 100644 --- a/src/py2yacs/py2yacs.cxx +++ b/src/py2yacs/py2yacs.cxx @@ -74,7 +74,7 @@ const std::list& Py2yacs::getFunctionProperties()const static std::string copyList(PyObject *pyList, std::list& 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& cppList) } else { - int n = PyList_Size(pyList); - for(int i=0; i& 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; iedAddOutputPort(*it, tc_double); return schema; -} \ No newline at end of file +} diff --git a/src/py2yacs/py2yacs.py b/src/py2yacs/py2yacs.py index 2e372aab6..f5f01ce3d 100644 --- a/src/py2yacs/py2yacs.py +++ b/src/py2yacs/py2yacs.py @@ -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 -- 2.39.2