def test_function_execution(Script): """ We've been having an issue of a mutable list that was changed inside the function execution. Test if an execution always returns the same result. """ s = """ def x(): return str() x""" func, evaluator = get_definition_and_evaluator(Script, s) # Now just use the internals of the result (easiest way to get a fully # usable function). # Should return the same result both times. assert len(execute_evaluated(func)) == 1 assert len(execute_evaluated(func)) == 1
def _check_isinstance_type(context, element, search_name): try: assert element.type in ('power', 'atom_expr') # this might be removed if we analyze and, etc assert len(element.children) == 2 first, trailer = element.children assert first.type == 'name' and first.value == 'isinstance' assert trailer.type == 'trailer' and trailer.children[0] == '(' assert len(trailer.children) == 3 # arglist stuff arglist = trailer.children[1] args = TreeArguments(context.evaluator, context, arglist, trailer) param_list = list(args.unpack()) # Disallow keyword arguments assert len(param_list) == 2 (key1, lazy_context_object), (key2, lazy_context_cls) = param_list assert key1 is None and key2 is None call = helpers.call_of_leaf(search_name) is_instance_call = helpers.call_of_leaf(lazy_context_object.data) # Do a simple get_code comparison. They should just have the same code, # and everything will be all right. normalize = context.evaluator.grammar._normalize assert normalize(is_instance_call) == normalize(call) except AssertionError: return None context_set = NO_CONTEXTS for cls_or_tup in lazy_context_cls.infer(): if isinstance(cls_or_tup, iterable.Sequence) and cls_or_tup.array_type == 'tuple': for lazy_context in cls_or_tup.py__iter__(): context_set |= lazy_context.infer().execute_evaluated(context) else: context_set |= helpers.execute_evaluated(cls_or_tup) return context_set
def test_simple(evaluator, environment): obj = compiled.create_simple_object(evaluator, u'_str_') upper, = obj.py__getattribute__(u'upper') objs = list(execute_evaluated(upper)) assert len(objs) == 1 if environment.version_info.major == 2: expected = 'unicode' else: expected = 'str' assert objs[0].name.string_name == expected
def infer(self): p = self._signature_param evaluator = self.parent_context.evaluator contexts = NO_CONTEXTS if p.has_default: contexts = ContextSet([create_from_access_path(evaluator, p.default)]) if p.has_annotation: annotation = create_from_access_path(evaluator, p.annotation) contexts |= execute_evaluated(annotation) return contexts
def _literals_to_types(evaluator, result): # Changes literals ('a', 1, 1.0, etc) to its type instances (str(), # int(), float(), etc). new_result = NO_CONTEXTS for typ in result: if is_literal(typ): # Literals are only valid as long as the operations are # correct. Otherwise add a value-free instance. cls = compiled.builtin_from_name(evaluator, typ.name.string_name) new_result |= helpers.execute_evaluated(cls) else: new_result |= ContextSet([typ]) return new_result
def _create(evaluator, access_handle, parent_context, *args): compiled_object = create_cached_compiled_object( evaluator, access_handle, parent_context=parent_context and parent_context.compiled_object) result = _find_syntax_node_name(evaluator, access_handle) # TODO use stub contexts here. If we do that we probably have to care about # generics from stuff like `[1]`. if result is None: return compiled_object module_node, tree_node, file_io, code_lines = result if parent_context is None: # TODO this __name__ is probably wrong. name = compiled_object.get_root_context().py__name__() string_names = tuple(name.split('.')) module_context = ModuleContext( evaluator, module_node, file_io=file_io, string_names=string_names, code_lines=code_lines, is_package=hasattr(compiled_object, 'py__path__'), ) if name is not None: evaluator.module_cache.add(string_names, ContextSet([module_context])) else: assert parent_context.tree_node.get_root_node() == module_node module_context = parent_context.get_root_context() tree_context = module_context.create_context(tree_node, node_is_context=True, node_is_object=True) if tree_node.type == 'classdef': if not access_handle.is_class(): # Is an instance, not a class. tree_context, = execute_evaluated(tree_context) return MixedObject(compiled_object, tree_context=tree_context)
def _create(evaluator, access_handle, parent_context, *args): compiled_object = create_cached_compiled_object( evaluator, access_handle, parent_context=parent_context.compiled_object) result = _find_syntax_node_name(evaluator, access_handle) if result is None: return compiled_object module_node, tree_node, path, code_lines = result if parent_context.tree_node.get_root_node() == module_node: module_context = parent_context.get_root_context() else: # TODO this __name__ is probably wrong. name = compiled_object.get_root_context().py__name__() string_names = tuple(name.split('.')) module_context = ModuleContext( evaluator, module_node, path=path, string_names=string_names, code_lines=code_lines, is_package=hasattr(compiled_object, 'py__path__'), ) if name is not None: evaluator.module_cache.add(string_names, ContextSet([module_context])) tree_context = module_context.create_context(tree_node, node_is_context=True, node_is_object=True) if tree_node.type == 'classdef': if not access_handle.is_class(): # Is an instance, not a class. tree_context, = execute_evaluated(tree_context) return MixedObject(evaluator, parent_context, compiled_object, tree_context=tree_context)
def execute_evaluated(self, *args, **kwargs): return ContextSet.from_sets(execute_evaluated(c, *args, **kwargs) for c in self._set)
def execute_evaluated(self, *value_list): return execute_evaluated(self, *value_list)
def get_string_context_set(evaluator): return execute_evaluated(builtin_from_name(evaluator, u'str'))
def __getattr__(self, name): if self._slice_object is None: context = compiled.builtin_from_name(self._context.evaluator, 'slice') self._slice_object, = execute_evaluated(context) return getattr(self._slice_object, name)