{
CPPUNIT_FAIL(e.what());
}
+ catch(std::exception& e)
+ {
+ CPPUNIT_FAIL(e.what());
+ }
catch(...)
{
CPPUNIT_FAIL("Unknown exception");
std::cerr << "===============================================" << std::endl;
CPPUNIT_ASSERT(what.find(error_message) != std::string::npos);
}
+ catch(std::exception& e)
+ {
+ CPPUNIT_FAIL(e.what());
+ }
}
static
std::cerr << "===============================================" << std::endl;
CPPUNIT_ASSERT(what.find(error_message) != std::string::npos);
}
+ catch(std::exception& e)
+ {
+ CPPUNIT_FAIL(e.what());
+ }
}
void Py2yacsTest::t1()
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);
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[] = {};
{
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()
// 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()
std::cerr << "-----------------------------------------" << std::endl;
}
-}
\ No newline at end of file
+}
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";
}
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";
}
else
{
- const char * portName = PyBytes_AsString(elem);
- cppList.push_back(portName);
+ cppList.push_back(std::string(PyUnicode_AsUTF8(elem)));
}
}
}
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)
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);
}
// 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);
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);
{
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";
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);
{
_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))
node->edAddOutputPort(*it, tc_double);
return schema;
-}
\ No newline at end of file
+}
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=[]
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
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)
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)
print("\n".join(fn_properties.errors))
else:
print("Function not found:", fn_name)
-
\ No newline at end of file