8000 MNT: use less eval · matplotlib/matplotlib@15157e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 15157e5

Browse files
committed
MNT: use less eval
In the textbox example we need eval because we are taking symbolic input from the user and then plotting it. I restricted the namespaces a bit to set a better example, but I think we fundamentally do need to use `eval` in this case. In the qt editor eval was changed to ast.literal_eval which only handles literals so is safer.
1 parent 370547e commit 15157e5

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

galleries/examples/widgets/textbox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def submit(expression):
3232
*expression* is a string using "t" as its independent variable, e.g.
3333
"t ** 3".
3434
"""
35-
ydata = eval(expression)
35+
ydata = eval(expression, {'np': np}, {'t': t})
3636
l.set_ydata(ydata)
3737
ax.relim()
3838
ax.autoscale_view()

lib/matplotlib/backends/qt_editor/_formlayout.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
__version__ = '1.0.10'
4242
__license__ = __doc__
4343

44+
from ast import literal_eval
45+
4446
import copy
4547
import datetime
4648
import logging
@@ -351,7 +353,7 @@ def get(self):
351353
else:
352354
value = date_.toPython()
353355
else:
354-
value = eval(str(field.text()))
356+
value = literal_eval(str(field.text()))
355357
valuelist.append(value)
356358
return valuelist
357359

0 commit comments

Comments
 (0)
0