parsing pyton code

Jean Brouwers mrjean1ATcomcastDOTnet at no.spam.net
Sat Nov 6 18:32:42 EST 2004


Check the source of the 'code' module, especially the runsource()
method of the InteractiveInterpreter class.  Also, see these docs

<http://docs.python.org/lib/module-code.html>


/Jean Brouwers
 ProphICy Semiconductor, Inc.


<mailman.6015.1099773489.5135.python-list at python.org>, Philippe C.
Martin <philippecmartin at sbcglobal.net> wrote:

> Hi,
> 
> I'm looking for a very simple way to figure out if a line of code requires 
> further lines of code ex: for i in range(10): So far I have found the 
> following trick that seem dangerous to me since it assumes the string 'EOF' 
> exists in the exception message.
> 
> Do I have the right to do this ?
> 
> 
> 
> 
> import compiler
> import string
> try:
>     compiler.parse('for i in range(10):')
> except SyntaxError, (val1, val2):
>     print val1
>     print val2
>     l_eof = False
>     l = string.split(val1,' ')
>     for l_c in l:
>         if l_c == 'EOF':
>             l_eof = True
>             break
>     if l_eof == True:
>         print 'YES'



More information about the Python-list mailing list