By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,985 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,985 IT Pros & Developers. It's quick & easy.

multi-line input?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.