This is a pre-release version (2.5.0.dev0+git20251130.2de293a).Go to latest (2.4.4)
{ } Raw JSON

bundles / numpy 2.5.0.dev0+git20251130.2de293a / 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 : str

The string to evaluate.

Returns

obj : object

The result of evaluating source.

Raises

: SyntaxError

If 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