Validating Syntax only with PyParser_SimpleParseString ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gaston.gloesener@web.de

    #1

    Validating Syntax only with PyParser_SimpleParseString ?


    I am seeking for a method to parse single lines of Python code (infact
    they are only formulas, so I generate a line like RESULT=<formula >). I
    do only want to know if the syntax is correct and if there is an error
    best would be to know where.

    I did find PyParser_Simple ParseString which does return a node
    structure. If there is an error it seems to return NULL, while I did
    not find this documented.

    Th eproblem with this is that first I do not get th elopcation of the
    syntax error, and second I don't know what is the proper method to free
    the node structure after I got it, since I will not use it. Or is there
    any better method doing this anyway.

    Environment: Windows / Borland C++

  • Fuzzyman

    #2
    Re: Validating Syntax only with PyParser_Simple ParseString ?


    gaston.gloesene r@web.de wrote:[color=blue]
    > I am seeking for a method to parse single lines of Python code (infact
    > they are only formulas, so I generate a line like RESULT=<formula >). I
    > do only want to know if the syntax is correct and if there is an error
    > best would be to know where.
    >
    > I did find PyParser_Simple ParseString which does return a node
    > structure. If there is an error it seems to return NULL, while I did
    > not find this documented.
    >
    > Th eproblem with this is that first I do not get th elopcation of the
    > syntax error, and second I don't know what is the proper method to free
    > the node structure after I got it, since I will not use it. Or is there
    > any better method doing this anyway.
    >[/color]
    A better way might be to use the standard library compiler module. A
    file called cptools.py (part of the cherrypy project) has a very simple
    example called 'unrepr' which shows how to do it.

    I only have restricted internet access or I would find the URL for you
    (sorry).

    Fuzzyman
    http://www.voidspace.org.uk/python/index.shtml
    [color=blue]
    > Environment: Windows / Borland C++[/color]

    Comment

    • olsongt@verizon.net

      #3
      Re: Validating Syntax only with PyParser_Simple ParseString ?


      gaston.gloesene r@web.de wrote:[color=blue]
      > I am seeking for a method to parse single lines of Python code (infact
      > they are only formulas, so I generate a line like RESULT=<formula >). I
      > do only want to know if the syntax is correct and if there is an error
      > best would be to know where.
      >
      > I did find PyParser_Simple ParseString which does return a node
      > structure. If there is an error it seems to return NULL, while I did
      > not find this documented.
      >
      > Th eproblem with this is that first I do not get th elopcation of the
      > syntax error, and second I don't know what is the proper method to free
      > the node structure after I got it, since I will not use it. Or is there
      > any better method doing this anyway.
      >
      > Environment: Windows / Borland C++[/color]

      The builtin function compile will throw a SyntaxError if the syntax is
      bad.
      [color=blue][color=green][color=darkred]
      >>> compile("1=a"," ","single")[/color][/color][/color]
      Traceback (most recent call last):
      File "<interacti ve input>", line 1, in ?
      SyntaxError: can't assign to literal[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Comment

      • gaston.gloesener@web.de

        #4
        Re: Validating Syntax only with PyParser_Simple ParseString ?

        Thanks for th eanswers so far.

        But as you can see from the function I use, I do need an interface
        function to do th ejob from C++. I canno timagine there is no clean way
        to do this. Calling th epython interpreter to execute a compule
        function in python and then get the output parsed to get th eerror
        message i snot an option. The strange thing however is that I have
        tried to use th ecompile interface function first and this one did
        return an error. I supposed this was because th ewhole context did not
        exist so I went to the "parse only" function.

        The main question I have is what to do with the pointer to the node
        structure returned by this function, as freeing th epointer with free()
        throws an error.

        Comment

        Working...