List Info

Thread: Extending/Embedding FAQ: How do I tell "incomplete input" from "invalid input"?




Extending/Embedding FAQ: How do I tell "incomplete input" from "invalid input"?
country flaguser name
Germany
2008-04-22 21:13:28
Hi,

some time ago I programmed an interactive python interpreter
shell
using paragraph 16 of the Extending/Embedding FAQ

  - 16   How do I tell "incomplete input" from
"invalid input"?

http://www.python.org/
doc/faq/extending/#how-do-i-tell-incomplete-input-from-inval
id-input

For emulating the interactive behaviour of the python shell,
the
following problem has to be solved - in the words of the
FAQ:

  Sometimes you want to emulate the Python interactive
interpreter's
  behavior, where it gives you a continuation prompt when
the input is
  incomplete (e.g. you typed the start of an "if"
statement or you
  didn't close your parentheses or triple string quotes),
but it gives
  you a syntax error message immediately when the input is
invalid.

The code relies on python error messages and the return
values of
PyParser_ParseString() in order to evaluate if an input is
syntactically erroneous or just incomplete.  But as both -
error
messages and the return values of PyParser_ParseString() -
have
changed the code does not work anymore.

I wrote two postings to the python-list explaining my
problems:

  - http://mail.python.org/pipermail/python-list/
2008-April/487616.html
  - http://mail.python.org/pipermail/python-list/
2008-April/487644.html

but as I didn't get any answer I decided to post another
message here.

Is there anybody who has been able to write some similar
code which 
also works for the current Python version?

Thanks for your help, 

Dietrich


By the way: Probably the FAQ should be corrected also.  Does
anybody
know who is responsible for the "Extending/Embedding
FAQ" (
http://www.p
ython.org/doc/faq/extending/ ) ?



_______________________________________________
C++-sig mailing list
C++-sigpython.org
http:
//mail.python.org/mailman/listinfo/c++-sig

Re: Extending/Embedding FAQ: How do I tell "incomplete input" from "invalid input"?
country flaguser name
United States
2008-04-22 21:25:47
On Dienstag 22 April 2008, Dietrich Bollmann wrote:
> Hi,
>
> some time ago I programmed an interactive python
interpreter shell
> using paragraph 16 of the Extending/Embedding FAQ

Knee-Jerk reaction: Why don't you just use
InteractiveConsole from the std 
library?

http://do
cs.python.org/lib/module-code.html

Andreas

_______________________________________________
C++-sig mailing list
C++-sigpython.org
http:
//mail.python.org/mailman/listinfo/c++-sig

Re: Extending/Embedding FAQ: How do I tell "incomplete input" from "invalid input"?
country flaguser name
Germany
2008-04-23 04:30:36
Hi Andreas,

On Tue, 2008-04-22 at 22:25 -0400, Andreas Klöckner wrote:
> On Dienstag 22 April 2008, Dietrich Bollmann wrote:
> > Hi,
> >
> > some time ago I programmed an interactive python
interpreter shell
> > using paragraph 16 of the Extending/Embedding FAQ
> 
> Knee-Jerk reaction: Why don't you just use
InteractiveConsole from the std 
> library?
> 
> http://do
cs.python.org/lib/module-code.html

The reason was that the shell is split into a server part
and a client
part: The code is executed in the server but comes from the
client which
implements the shell.

The other reason was, that I found the FAQ and that I
preferred do do
everything in c if I am already developing the client and
server in c
rather than wrapping python into c code. 

But I found another solution thanks to you answer 

The function `compile_command()':

  This function is useful for programs that want to emulate
Python's
  interpreter main loop (a.k.a. the read-eval-print loop).
The tricky
  part is to determine when the user has entered an
incomplete command
  that can be completed by entering more text (as opposed to
a complete
  command or a syntax error). This function almost always
makes the same
  decision as the real interpreter main loop.

  (from http://do
cs.python.org/lib/module-code.html )

wrote a c wrapper around it and substituted it for
`Py_CompileString()'.

As `compile_command()' contrary to `Py_CompileString()'
understands if a
command is just incomplete or erroneous I got my code
working again 

So you saved my day, thank you very much 

`compile_command()' uses a kind of strange strategy to
decide if the
code is complete or not:

  Approach:

  First, check if the source consists entirely of blank
lines and
  comments; if so, replace it with 'pass', because the
built-in
  parser doesn't always do the right thing for these.

  Compile three times: as is, with n, and with nn
appended.  If it
  compiles as is, it's complete.  If it compiles with one n
appended,
  we expect more.  If it doesn't compile either way, we
compare the
  error we get when compiling with n or nn appended.  If
the errors
  are the same, the code is broken.  But if the errors are
different, we
  expect more.  Not intuitive; not even guaranteed to hold
in future
  releases; but this matches the compiler's behavior from
Python 1.4
  through 2.2, at least.

  (from the source file header)

but for the moment it works and I hope that enough people
depend on the
code so that I do not have to fix my code for every new
Python
release...

Still, the Python Extending/Embedding FAQ should be updated
as other
people might stumble over the same problem.

Does anybody know who is responsible for the FAQ?

Thanks for putting me into a good mood again 

Dietrich

> Andreas

_______________________________________________
C++-sig mailing list
C++-sigpython.org
http:
//mail.python.org/mailman/listinfo/c++-sig
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )