473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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(name,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 '

Apr 10 '07 #1
4 3554
"gr********@gmail.com" <gr********@gmail.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.
Apr 10 '07 #2
gr********@gmail.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(name,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.

Apr 10 '07 #3
On Apr 10, 4:49 am, Georg Brandl <g.bra...@gmx.netwrote:
gregpin...@gmail.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(name,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

Apr 10 '07 #4
On Apr 10, 2:19 pm, "gregpin...@gmail.com" <gregpin...@gmail.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
Apr 10 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andr? Roberge | last post by:
I have the following two files: #--testexec.py-- def exec_code(co): try: exec co except: print "error" #-- test.py--
0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
4
by: James E Koehler | last post by:
I can't get the WHILE statement to work in MySQL. The version of MySQL that I am using is: Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32) running on Windows MX. Here is the relevant...
2
by: Alicia | last post by:
Does anyone know why I am getting a "Syntax error in Create Table statement". I am using Microsoft Access SQL View to enter it. Any other problems I may run into? CREATE TABLE weeks (...
1
by: Joe | last post by:
Hello All, I am trying to insert a record in the MS Access DB and for some reason I cannot get rid of error message, System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. ...
2
by: abedt | last post by:
I got the following error: Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where...
6
by: vasudevram | last post by:
Hi group, Question: Do eval() and exec not accept a function definition? (like 'def foo: pass) ? I wrote a function to generate other functions using something like eval("def foo: ....") but...
0
by: Stef Mientki | last post by:
Terry Reedy wrote: The locals of the code block where the instance of this class is created. No I think it's indeed handled as function call. I now that, because the globals (and sometimes ??? the...
7
by: gregory.lielens | last post by:
Hi, I am using a small python file as an input file (defining constants, parameters, input data, ...) for a python application. The input file is simply read by an exec statement in a specific...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.