parsing pyton code

Philippe C. Martin philippecmartin at sbcglobal.net
Sat Nov 6 21:38:55 CET 2004


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