MCPcopy Index your code
hub / github.com/RustPython/RustPython / _signature_fromstr

Function _signature_fromstr

Lib/inspect.py:2144–2288  ·  view source on GitHub ↗

Private helper to parse content of '__text_signature__' and return a Signature based on it.

(cls, obj, s, skip_bound_arg=True)

Source from the content-addressed store, hash-verified

2142
2143
2144def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
2145 """Private helper to parse content of '__text_signature__'
2146 and return a Signature based on it.
2147 """
2148 Parameter = cls._parameter_cls
2149
2150 clean_signature, self_parameter = _signature_strip_non_python_syntax(s)
2151
2152 program = "def foo" + clean_signature + ": pass"
2153
2154 try:
2155 module = ast.parse(program)
2156 except SyntaxError:
2157 module = None
2158
2159 if not isinstance(module, ast.Module):
2160 raise ValueError("{!r} builtin has invalid signature".format(obj))
2161
2162 f = module.body[0]
2163
2164 parameters = []
2165 empty = Parameter.empty
2166
2167 module = None
2168 module_dict = {}
2169
2170 module_name = getattr(obj, '__module__', None)
2171 if not module_name:
2172 objclass = getattr(obj, '__objclass__', None)
2173 module_name = getattr(objclass, '__module__', None)
2174
2175 if module_name:
2176 module = sys.modules.get(module_name, None)
2177 if module:
2178 module_dict = module.__dict__
2179 sys_module_dict = sys.modules.copy()
2180
2181 def parse_name(node):
2182 assert isinstance(node, ast.arg)
2183 if node.annotation is not None:
2184 raise ValueError("Annotations are not currently supported")
2185 return node.arg
2186
2187 def wrap_value(s):
2188 try:
2189 value = eval(s, module_dict)
2190 except NameError:
2191 try:
2192 value = eval(s, sys_module_dict)
2193 except NameError:
2194 raise ValueError
2195
2196 if isinstance(value, (str, int, float, bytes, bool, type(None))):
2197 return ast.Constant(value)
2198 raise ValueError
2199
2200 class RewriteSymbolics(ast.NodeTransformer):
2201 def visit_Attribute(self, node):

Callers 3

_signature_from_builtinFunction · 0.85
_signature_from_functionFunction · 0.85
_signature_from_callableFunction · 0.85

Calls 15

isinstanceFunction · 0.85
getattrFunction · 0.85
lenFunction · 0.85
ismoduleFunction · 0.85
chainMethod · 0.80
pFunction · 0.70
clsClass · 0.50
parseMethod · 0.45
formatMethod · 0.45
getMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected