Python Forum

Full Version: CPython: Knowing when the interpreter expects more input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have embedded Python (currently 2.7.16).

After using Py_CompileString() to build user-entered Python code and then checking for errors, I use PyEval_EvalCode() to execute the resulting PyObject *.

My question: Is there a way to know from the interpreter after calling PyEval_EvalCode() that it is expecting more input? I've looked through the API, and I don't seen anything immediately apparent.

I'm currently coding that for myself, the heuristics being the interpreter needs more input if the line ends with a colon, or the current input has unbalanced parentheses ("("), brackets ("["), braces ("{") or strings.

Thanks in advance for any insights. Smile
It seems to me that PyCompile_String() should fail if there are unbalanced parentheses. Have you tried this?