multi-line input?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon May 14 22:51:33 EDT 2007


En Mon, 14 May 2007 18:50:35 -0300, <joshusdog at gmail.com> escribió:

> I'm writing a C++ application with an embedded Python interpreter.
> Command text is captured and passed to the interpreter a single line
> at a time. My question is this: is there a simple way of determining
> whether a given input line of text will cause the prompt to change
> from the regular ">>>" to the multi-line "..." before sending the text
> to the interpreter? I suppose the completely correct solution would be
> tokenize and parse the entire string and then examine all the
> constituent parts, but that seems like a lot more work than I really
> want to do.

There is an emulation of the read-eval-print loop written in Python  
itself, see the code module. In particular, compile_command tries to  
determine whether the entered text is a complete Python statement,  
contains a syntax error, or requires more input.
If you want to stay with C code, you could use Py_CompileString and see if  
the already entered code can be compiled or not - but I'm not sure if you  
will actually be able to distinguish a SyntaxError from an incomplete  
statement.

> As far as I can tell, there are only a few ways to trigger the multi-
> line input prompt:
>
> - if-statement, for-loop, function or class definition
> - line continuation (\)
> - block quote (""" or ''')

- Any expression involving an open group of () [] {}

-- 
Gabriel Genellina




More information about the Python-list mailing list