Private helper to parse content of '__text_signature__' and return a Signature based on it.
(cls, obj, s, skip_bound_arg=True)
| 2142 | |
| 2143 | |
| 2144 | def _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): |
no test coverage detected