469,362 Members | 2,367 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

multi-line input?

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.

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 help would be greatly appreciated.

May 14 '07 #1
2 2765
En Mon, 14 May 2007 18:50:35 -0300, <jo*******@gmail.comescribió:
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

May 15 '07 #2
Ah, thanks. Using the information you provided, I found the following
page:

http://archives.free.net.ph/message/...a616bf.en.html

It's not perfect, but it's close enough for what I need.

May 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

37 posts views Thread by ajikoe | last post: by
4 posts views Thread by Frank Jona | last post: by
5 posts views Thread by bobwansink | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.