Tagged Questions
49
votes
2answers
16k views
What's the difference between eval, exec, and compile in Python?
I've been looking at dynamic evaluation of Python code, and come across the eval() and compile() functions, and the exec statement.
Can someone please explain the difference between eval and exec, ...
31
votes
7answers
36k views
What is the best way to call a python script from another python script?
I have a script named test1.py which is not in a module. It just has code that should execute when the script itself is run. There are no functions, classes, methods etc. I have another script which ...
16
votes
4answers
5k views
Is Using eval In Python A Bad Practice?
I am using the following class to easily store data of my songs.
class Song:
"""The class to store the details of each song"""
attsToStore=('Name', 'Artist', 'Album', 'Genre', 'Location')
...
16
votes
1answer
273 views
Why is there a length limit to python's eval?
I'm not advocating that this would ever be a good idea, but I've found that you can crash Python (2.7 and 3.2 checked) by running eval on a large enough input string:
def kill_python(N):
S = ...
13
votes
4answers
576 views
How does sympy work? How does it interact with the interactive Python shell, and how does the interactive Python shell work?
What happens internally when I press Enter?
My motivation for asking, besides plain curiosity, is to figure out what happens when you
from sympy import *
and enter an expression. How does it go ...
12
votes
6answers
4k views
Security of Python's eval() on untrusted strings?
If I am evaluating a Python string using eval(), and have a class like:
class Foo(object):
a = 3
def bar(self, x): return x + a
What are the security risks if I do not trust the string? In ...
9
votes
7answers
1k views
Python: How to pass arguments to the __code__ of a function?
The following works:
def spam():
print "spam"
exec(spam.__code__)
spam
But what if spam takes arguments?
def spam(eggs):
print "spam and", eggs
exec(spam.__code__)
TypeError: ...
8
votes
9answers
11k views
Use of eval in Python?
There is an eval() function in Python I stumbled upon while playing around. I cannot think of a case when this function is needed, except maybe as syntactic sugar. Can anyone give an example?
7
votes
3answers
2k views
Python: make eval safe
I want an easy way to do a "calculator api" in python.
Right now I don't care much about the exact set of features the calculator is going to support.
I want it to receive a string, say "1+1" and ...
7
votes
4answers
219 views
Python - Creating a “scripting” system
I'm making a wxpython app that I will compile with the various freezing utility out there to create an executable for multiple platforms.
the program will be a map editer for a tile-based game engine
...
7
votes
2answers
71 views
Assignment order matters unexpectedly with “exec expr in globals(), locals()”
The following code in Python 2.X prints "a : 2" as you'd expect:
def f():
#a = 1
exec "a = 2" in globals(), locals()
for k,v in locals().items(): print k,":",v
#a = 3
f()
But if you ...
6
votes
5answers
2k views
Safety of Python 'eval' For List Deserialization
Are there any security exploits that could occur in this scenario:
eval(repr(unsanitized_user_input), {"__builtins__": None}, {"True":True, "False":False})
where unsanitized_user_input is a str ...
6
votes
6answers
2k views
Parsing a string which represents a list of tuples
I have strings which look like this one:
"(8, 12.25), (13, 15), (16.75, 18.5)"
and I would like to convert each of them into a python data structure. Preferably a list (or tuple) of tuples ...
6
votes
6answers
310 views
Is there a way to secure strings for Python's eval?
There are many questions on SO about using Python's eval on insecure strings (eg.: Security of Python's eval() on untrusted strings?, Python: make eval safe and Security of Python's eval() on ...
6
votes
5answers
1k views
How to evaluate a custom math expression in Python
I'm writing a custom dice rolling parser (snicker if you must) in python. Basically, I want to use standard math evaluation but add the 'd' operator:
#xdy
sum = 0
for each in range(x):
sum += ...