parsing pyton code

Discussion in 'Python' started by Philippe C. Martin, Nov 6, 2004.

  1. 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'
    Philippe C. Martin, Nov 6, 2004
    #1
    1. Advertising

  2. 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.


    <>, Philippe C.
    Martin <> 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'
    Jean Brouwers, Nov 6, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen VanDahm

    Securing the Pyton Interpreter?

    Stephen VanDahm, Jul 15, 2003, in forum: Python
    Replies:
    6
    Views:
    366
    Vinoo Vasudevan
    Jul 17, 2003
  2. Steffen Kunze
    Replies:
    2
    Views:
    401
    Steffen Kunze
    Jul 28, 2004
  3. mike
    Replies:
    3
    Views:
    455
    Christopher De Vries
    Feb 1, 2005
  4. Mark de+la+Fuente

    Is pyton for me?

    Mark de+la+Fuente, Jun 10, 2005, in forum: Python
    Replies:
    11
    Views:
    769
    Mike Meyer
    Jun 11, 2005
  5. Daniel Nogradi
    Replies:
    0
    Views:
    413
    Daniel Nogradi
    May 4, 2006
Loading...

Share This Page