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