PyRun_String("if True: 1\nelse: 0", Py_eval_input, globals, globals);
returns error with PyErr_Print() printing out:
File "<string>", line 1
if True: 1
^
What am I doing wrong?
Thank you.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That isn’t a conditional expression: it’s a statement.
Py_eval_inputmeans to treat the string as a single expression. You probably wantPy_single_inputto treat the string as a statement.This is the same as the distinction in Python code between
eval(which is what you asked for) andexec.I am assuming of course that the statement you actually want to execute will be slightly more complex otherwise there isn’t much point using either
evalorexec. Forexecyou’ll want to make sure it has side effects so you can tell the result, e.g. by binding some value to a name.