exec statement Syntax Error on string pulled from MySQL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gregpinero@gmail.com

    #1

    exec statement Syntax Error on string pulled from MySQL

    It's the strangest thing, I'm pulling some text out of a MySQL table
    and trying to run exec on it, and it keeps giving me a syntax error,
    always at the end of the first line.

    Thanks in advance for any help. I'm really stuck on this one!

    -Greg

    I'm not sure what information would be most useful but here's a start:

    The last code I stored in the table and pulled out was simply:
    print 'greg'
    print 'greg2'

    To which my error log says:
    Traceback (most recent call last):
    File "/home/public/web/webapi.py", line 303, in wsgifunc
    result = func()
    File "/home/public/web/request.py", line 125, in <lambda>
    func = lambda: handle(inp, fvars)
    File "/home/public/web/request.py", line 61, in handle
    return tocall(*([urllib.unquote( x) for x in args] + fna))
    File "/home/public/EZsession.py", line 119, in proxyfunc
    return func(self, *args, **kw)
    File "/home/htdocs/code.py", line 94, in POST
    print utility.run(nam e,revision,inp)
    File "/home/public/utility.py", line 177, in run
    exec code+'\n' in context
    File "<string>", line 1
    print 'greg'
    ^
    SyntaxError: invalid syntax
    (Note the ^ actually appears under after the ' )

    To really get a picture of what is coming out of the DB I had the
    program print out everything about this string using this code:
    print code
    print repr(code)
    print type(code)
    for char in code:
    print ord(char),char

    To which I got:

    print 'greg' print 'greg2'
    "print 'greg'\r\nprint 'greg2'"
    <type 'str'>
    112 p
    114 r
    105 i
    110 n
    116 t
    32
    39 '
    103 g
    114 r
    101 e
    103 g
    39 '
    13
    10
    112 p
    114 r
    105 i
    110 n
    116 t
    32
    39 '
    103 g
    114 r
    101 e
    103 g
    50 2
    39 '

  • Duncan Booth

    #2
    Re: exec statement Syntax Error on string pulled from MySQL

    "gregpinero@gma il.com" <gregpinero@gma il.comwrote:
    To really get a picture of what is coming out of the DB I had the
    program print out everything about this string using this code:
    print code
    print repr(code)
    print type(code)
    for char in code:
    print ord(char),char
    >
    To which I got:
    >
    print 'greg' print 'greg2'
    "print 'greg'\r\nprint 'greg2'"
    >>exec "1\n2"
    >>exec "1\r\n2"
    Traceback (most recent call last):
    File "<pyshell#1 >", line 1, in <module>
    exec "1\r\n2"
    File "<string>", line 1
    1

    ^
    SyntaxError: invalid syntax
    >>>
    Get rid of the carriage return.

    Comment

    • Georg Brandl

      #3
      Re: exec statement Syntax Error on string pulled from MySQL

      gregpinero@gmai l.com schrieb:
      It's the strangest thing, I'm pulling some text out of a MySQL table
      and trying to run exec on it, and it keeps giving me a syntax error,
      always at the end of the first line.
      >
      Thanks in advance for any help. I'm really stuck on this one!
      >
      -Greg
      >
      I'm not sure what information would be most useful but here's a start:
      >
      The last code I stored in the table and pulled out was simply:
      print 'greg'
      print 'greg2'
      >
      To which my error log says:
      Traceback (most recent call last):
      File "/home/public/web/webapi.py", line 303, in wsgifunc
      result = func()
      File "/home/public/web/request.py", line 125, in <lambda>
      func = lambda: handle(inp, fvars)
      File "/home/public/web/request.py", line 61, in handle
      return tocall(*([urllib.unquote( x) for x in args] + fna))
      File "/home/public/EZsession.py", line 119, in proxyfunc
      return func(self, *args, **kw)
      File "/home/htdocs/code.py", line 94, in POST
      print utility.run(nam e,revision,inp)
      File "/home/public/utility.py", line 177, in run
      exec code+'\n' in context
      File "<string>", line 1
      print 'greg'
      ^
      SyntaxError: invalid syntax
      (Note the ^ actually appears under after the ' )
      You have Windows line endings (\r\n) in the string, which Python doesn't like.

      Don't store it like that, or if you must, do a .replace('\r', '') before
      exec'ing it.

      Georg

      --
      Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
      Four shall be the number of spaces thou shalt indent, and the number of thy
      indenting shall be four. Eight shalt thou not indent, nor either indent thou
      two, excepting that thou then proceed to four. Tabs are right out.

      Comment

      • gregpinero@gmail.com

        #4
        Re: exec statement Syntax Error on string pulled from MySQL

        On Apr 10, 4:49 am, Georg Brandl <g.bra...@gmx.n etwrote:
        gregpin...@gmai l.com schrieb:
        >
        >
        >
        It's the strangest thing, I'm pulling some text out of a MySQL table
        and trying to run exec on it, and it keeps giving me a syntax error,
        always at the end of the first line.
        >
        Thanks in advance for any help. I'm really stuck on this one!
        >
        -Greg
        >
        I'm not sure what information would be most useful but here's a start:
        >
        The last code I stored in the table and pulled out was simply:
        print 'greg'
        print 'greg2'
        >
        To which my error log says:
        Traceback (most recent call last):
        File "/home/public/web/webapi.py", line 303, in wsgifunc
        result = func()
        File "/home/public/web/request.py", line 125, in <lambda>
        func = lambda: handle(inp, fvars)
        File "/home/public/web/request.py", line 61, in handle
        return tocall(*([urllib.unquote( x) for x in args] + fna))
        File "/home/public/EZsession.py", line 119, in proxyfunc
        return func(self, *args, **kw)
        File "/home/htdocs/code.py", line 94, in POST
        print utility.run(nam e,revision,inp)
        File "/home/public/utility.py", line 177, in run
        exec code+'\n' in context
        File "<string>", line 1
        print 'greg'
        ^
        SyntaxError: invalid syntax
        (Note the ^ actually appears under after the ' )
        >
        You have Windows line endings (\r\n) in the string, which Python doesn't like.
        >
        Don't store it like that, or if you must, do a .replace('\r', '') before
        exec'ing it.
        Wow,
        exec code.replace('\ r','') in context
        works! Now I just have to figure out how the '\r' are getting in
        there. I entered that piece of code using PHPMyAdmin so that could be
        doing it, or MySQLdb could be doing it when returning it, or it could
        be something about the DB encoding! I'll post back if I find out.

        Thanks for help!

        -Greg

        Comment

        • gregpinero@gmail.com

          #5
          Re: exec statement Syntax Error on string pulled from MySQL

          On Apr 10, 2:19 pm, "gregpin...@gma il.com" <gregpin...@gma il.com>
          wrote:
          Now I just have to figure out how the '\r' are getting in
          there. I entered that piece of code using PHPMyAdmin so that could be
          doing it, or MySQLdb could be doing it when returning it, or it could
          be something about the DB encoding! I'll post back if I find out.
          >
          Hmm, searches didn't turn up anything. The best I can figure is that
          the HTML textarea / browser combination is sending the text in that
          way. I can check for sure on that later.

          -Greg


          Comment

          Working...