MCPcopy Create free account
hub / github.com/numpy/numpy / safe_eval

Function safe_eval

numpy/lib/utils.py:1027–1078  ·  view source on GitHub ↗

Protected string evaluation. Evaluate a string containing a Python literal expression without allowing the execution of arbitrary non-literal code. .. warning:: This function is identical to :py:meth:`ast.literal_eval` and has the same security implications. It m

(source)

Source from the content-addressed store, hash-verified

1025
1026
1027def safe_eval(source):
1028 """
1029 Protected string evaluation.
1030
1031 Evaluate a string containing a Python literal expression without
1032 allowing the execution of arbitrary non-literal code.
1033
1034 .. warning::
1035
1036 This function is identical to :py:meth:`ast.literal_eval` and
1037 has the same security implications. It may not always be safe
1038 to evaluate large input strings.
1039
1040 Parameters
1041 ----------
1042 source : str
1043 The string to evaluate.
1044
1045 Returns
1046 -------
1047 obj : object
1048 The result of evaluating `source`.
1049
1050 Raises
1051 ------
1052 SyntaxError
1053 If the code has invalid Python syntax, or if it contains
1054 non-literal code.
1055
1056 Examples
1057 --------
1058 >>> np.safe_eval('1')
1059 1
1060 >>> np.safe_eval('[1, 2, 3]')
1061 [1, 2, 3]
1062 >>> np.safe_eval('{"foo": ("bar", 10.0)}')
1063 {'foo': ('bar', 10.0)}
1064
1065 >>> np.safe_eval('import os')
1066 Traceback (most recent call last):
1067 ...
1068 SyntaxError: invalid syntax
1069
1070 >>> np.safe_eval('open("/home/user/.ssh/id_dsa").read()')
1071 Traceback (most recent call last):
1072 ...
1073 ValueError: malformed node or string: <_ast.Call object at 0x...>
1074
1075 """
1076 # Local import to speed up numpy's import time.
1077 import ast
1078 return ast.literal_eval(source)
1079
1080
1081def _median_nancheck(data, result, axis):

Callers 1

_read_array_headerFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected