bundles / numpy latest / numpy / lib / _utils_impl / safe_eval
function
numpy.lib._utils_impl:safe_eval
source: build-install/usr/lib/python3.14/site-packages/numpy/lib/_utils_impl.py:583
Signature
def safe_eval(source)Summary
Protected string evaluation.
Extended Summary
Evaluate a string containing a Python literal expression without allowing the execution of arbitrary non-literal code.
Parameters
source: strThe string to evaluate.
Returns
obj: objectThe result of evaluating
source.
Raises
: SyntaxErrorIf the code has invalid Python syntax, or if it contains non-literal code.
Examples
np.safe_eval('1') np.safe_eval('[1, 2, 3]') np.safe_eval('{"foo": ("bar", 10.0)}')⚠
Traceback (most recent call last):
File "<doctest numpy.lib._utils_impl:safe_eval[0]>", line 1, in <module>
np.safe_eval('{"foo": ("bar", 10.0)}')
^^^^^^^^^^^^
File "/home/runner/dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/__init__.py", line 778, in __getattr__
raise AttributeError(
...<3 lines>...
)
AttributeError: `np.safe_eval` was removed in the NumPy 2.0 release. Use `ast.literal_eval` instead.
np.safe_eval('import os')
✗Traceback (most recent call last):
File "<doctest numpy.lib._utils_impl:safe_eval[0]>", line 1, in <module>
np.safe_eval('import os')
^^^^^^^^^^^^
File "/home/runner/dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/__init__.py", line 778, in __getattr__
raise AttributeError(
...<3 lines>...
)
AttributeError: `np.safe_eval` was removed in the NumPy 2.0 release. Use `ast.literal_eval` instead.
np.safe_eval('open("/home/user/.ssh/id_dsa").read()')
✗Traceback (most recent call last):
File "<doctest numpy.lib._utils_impl:safe_eval[0]>", line 1, in <module>
np.safe_eval('open("/home/user/.ssh/id_dsa").read()')
^^^^^^^^^^^^
File "/home/runner/dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/__init__.py", line 778, in __getattr__
raise AttributeError(
...<3 lines>...
)
AttributeError: `np.safe_eval` was removed in the NumPy 2.0 release. Use `ast.literal_eval` instead.
Aliases
numpy.lib._utils_impl.safe_eval