parsing pyton code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philippe C. Martin

    #1

    parsing pyton code

    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(va l1,' ')
    for l_c in l:
    if l_c == 'EOF':
    l_eof = True
    break
    if l_eof == True:
    print 'YES'
  • Jean Brouwers

    #2
    Re: parsing pyton code


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

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


    /Jean Brouwers
    ProphICy Semiconductor, Inc.


    <mailman.6015.1 099773489.5135. python-list@python.org >, Philippe C.
    Martin <philippecmarti n@sbcglobal.net > wrote:
    [color=blue]
    > 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(va l1,' ')
    > for l_c in l:
    > if l_c == 'EOF':
    > l_eof = True
    > break
    > if l_eof == True:
    > print 'YES'[/color]

    Comment

    Working...