I have a solution idea also myself -- I could instrument the code (or AST) by wrapping all (sub)expressions in a dummy method call, which does nothing more than returning its argument. Eg.
x = f(sin(x + y))
becomes
x = dummy(f(dummy(sin(dummy(dummy(x) + dummy(y))))))
This way I'm guaranteed to be notified after each subexpression gets evaluated and I also get the values. I can also add extra location/AST information about which part of the expression is currently dealt with, eg:
...dummy(x, line = 23, col = 13, length = 1)...
Enter a recursive debugger that steps through the code argument (which is an arbitrary expression or statement to be executed in the current environment).,Evaluate the expression (given as a string or a code object) under debugger control. When runeval() returns, it returns the value of the expression. Otherwise this function is similar to run().,Execute the statement (given as a string or a code object) under debugger control. The debugger prompt appears before any code is executed; you can set breakpoints and type continue, or you can step through the statement using step or next (all these commands are explained below). The optional globals and locals arguments specify the environment in which the code is executed; by default the dictionary of the module __main__ is used. (See the explanation of the built-in exec() or eval() functions.),The debugger’s prompt is (Pdb). Typical usage to run a program under control of the debugger is:
>>> import pdb
>>> import mymodule
>>> pdb.run('mymodule.test()')
> <string>(0)?()
(Pdb) continue
> <string>(1)?()
(Pdb) continue
NameError: 'spam'
> <string>(1)?()
(Pdb)
python3 - m pdb myscript.py
import pdb;
pdb.set_trace()
>>> import pdb
>>> import mymodule
>>> mymodule.test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "./mymodule.py", line 4, in test
test2()
File "./mymodule.py", line 3, in test2
print(spam)
NameError: spam
>>> pdb.pm()
> ./mymodule.py(3)test2()
-> print(spam)
(Pdb)
import pdb;
pdb.Pdb(skip = ['django.*']).set_trace()
(Pdb) commands 1
(com) p some_variable(com) end(Pdb)
In this tutorial, we will learn about the Python eval() method with the help of examples.,The eval() method parses the expression passed to this method and runs python expression (code) within the program.,It is also possible to change the name of the method available for the expression as to your wish:,If both parameters are omitted (as in our earlier examples), the expression is executed in the current scope. You can check the available variables and methods using following code:
Example
number = 9 # eval performs the multiplication passed as argument square_number = eval('number * number') print(square_number) # Output: 81
Example
number = 9 # eval performs the multiplication passed as argument square_number = eval('number * number') print(square_number) # Output: 81
The syntax of eval()
is:
eval(expression, globals = None, locals = None)
Example 1: How eval() works in Python
x = 1
print(eval('x + 1'))
Example 2: Practical Example to Demonstrate Use of eval()
# Perimeter of Square
def calculatePerimeter(l):
return 4 * l
# Area of Square
def calculateArea(l):
return l * l
expression = input("Type a function: ")
for l in range(1, 5):
if (expression == 'calculatePerimeter(l)'):
print("If length is ", l, ", Perimeter = ", eval(expression))
elif(expression == 'calculateArea(l)'):
print("If length is ", l, ", Area = ", eval(expression))
else:
print('Wrong Function')
break
If you are using eval(input())
in your code, it is a good idea to check which variables and methods the user can use. You can see which variables and methods are available using dir() method.
from math
import *
print(eval('dir()'))
GATE CS 2021 Syllabus
Output:
3
For example, if you input like this:
Enter the
function(in terms of x): secret_function()
Enter the value of x: 0
You will get the output:
result: Secret key is 1234
We get the output:
NameError: name 'secret_function'
is not defined
- First, we create a list of methods we want to allow as safe_list.
- Next, we create a dictionary of safe methods. In this dictionary, keys are the method names and values are their local namespaces.
safe_dict = {} for safe_key in safe_list: safe_dict[safe_key] = locals().get(safe_key)