def setUp(self): super().setUp() self._vm = vm.VirtualMachine( errors.ErrorLog(), config.Options.create(python_version=self.python_version), load_pytd.Loader(None, self.python_version))
def setUp(self): self.vm = vm.VirtualMachine(errors.ErrorLog(), config.Options([""])) self.type_type = abstract.get_atomic_value(self.vm.convert.type_type)
def process_file(options, source_text=None, generate_callgraphs=False, preserve_pytype_vm=False): """Process a single file and return cross references. Args: options: A dictionary of pytype options. source_text: Optional text of the file; will be read from the file pointed to by options.input if not supplied. generate_callgraphs: Collect call graph information preserve_pytype_vm: Preserve the pytype vm in the indexer Returns: The Indexer object used for indexing. Raises: PytypeError if pytype fails. """ with config.verbosity_from(options): errorlog = errors.ErrorLog() loader = load_pytd.create_loader(options) src = source_text or io.read_source_file(options.input) vm = analyze.CallTracer(errorlog=errorlog, options=options, generate_unknowns=options.protocols, store_all_calls=True, loader=loader) with io.wrap_pytype_exceptions(PytypeError, filename=options.input): pytd_module, _ = analyze.infer_types(src=src, filename=options.input, errorlog=errorlog, options=options, loader=loader, tracer_vm=vm) major, minor = options.python_version if major == 2: # python2.7 is the only supported py2 version. ast_root_node = ast27.parse(src, options.input) ast = ast27 else: ast_root_node = ast3.parse(src, options.input, feature_version=minor) ast = ast3 # TODO(mdemello): Get from args module_name = "module" src_code = source.Code(src, vm.opcode_traces, VmTrace, filename=options.input) ix = Indexer(ast=ast, src=src_code, loader=vm.loader, module_name=module_name, pytd_module=pytd_module) ix.index(ast_root_node) ix.finalize() # Make the vm available via indexer.vm for post-finalize() functions. ix.vm = vm # Use the indexer as a single object to hold data for calling processes. if generate_callgraphs: ix.function_map = callgraph.collect_function_map(ix) # Release the vm before returning if not preserve_pytype_vm: ix.vm = None return ix
def setUp(self): super().setUp() self.errorlog = errors.ErrorLog() self.vm = analyze.CallTracer(self.errorlog, self.options, self.loader)
def setUp(self): super(BytecodeTest, self).setUp() self.errorlog = errors.ErrorLog() self.trace_vm = TraceVM(self.options, self.loader)
def setUp(self): super(DirectorLineNumbersTest, self).setUp() self.errorlog = errors.ErrorLog() self.vm = analyze.CallTracer(self.errorlog, self.options, self.loader)
def process_file(options, source_text=None, kythe_args=None, keep_pytype_data=False, ast_factory=None, annotate_ast=False): """Process a single file and return cross references. Args: options: A dictionary of pytype options. source_text: Optional text of the file; will be read from the file pointed to by options.input if not supplied. kythe_args: Extra args for generating the kythe index keep_pytype_data: Whether to preserve the Reference.data field. If true, the field will hold the type of the reference as a str or Tuple[str, str] (for attributes). Otherwise, it will be inaccessible. ast_factory: Callable to return an ast-module-compatible object to parse the source text into an ast-compatible object. It is passed the pytype Options object. If not specified, typed_ast will be used. annotate_ast: Whether to annotate the ast with type information. Nodes with type information will have these attributes added: * `.resolved_type`: the pytd information about the type * `.resolved_annotation`: A string representation of the type, as would be written in an annotation. Returns: The Indexer object used for indexing, and the created AST object. The AST object may have been modified if `annotate_ast=True`. Raises: PytypeError if pytype fails. """ # We bind the global ast variable in this function. global ast errorlog = errors.ErrorLog() loader = load_pytd.create_loader(options) src = source_text or io.read_source_file(options.input) vm = analyze.CallTracer( errorlog=errorlog, options=options, generate_unknowns=options.protocols, store_all_calls=False, loader=loader) with io.wrap_pytype_exceptions(PytypeError, filename=options.input): pytype_ast, _ = analyze.infer_types( src=src, filename=options.input, errorlog=errorlog, options=options, loader=loader, tracer_vm=vm) if ast_factory: ast = ast_factory(options) ast_root_node = ast.parse(src, options.input) else: major, minor = options.python_version if major == 2: # python2.7 is the only supported py2 version. ast_root_node = ast27.parse(src, options.input) ast = ast27 else: ast_root_node = ast3.parse(src, options.input, feature_version=minor) ast = ast3 # TODO(mdemello): Get from args module_name = "module" source = SourceFile(src, vm.opcode_traces, filename=options.input) ix = Indexer( source, vm.loader, module_name, kythe_args, annotate_ast=annotate_ast) ix.index(ast_root_node) ix.finalize(keep_pytype_data, pytype_ast) return ix, ast_root_node
def setUp(self): super(AttributeTest, self).setUp() options = config.Options.create(python_version=self.PYTHON_VERSION) self._vm = vm.VirtualMachine( errors.ErrorLog(), options, load_pytd.Loader(None, self.PYTHON_VERSION))
def setUp(self): options = config.Options.create() self._vm = vm.VirtualMachine( errors.ErrorLog(), options, load_pytd.Loader(None, self.PYTHON_VERSION)) self._program = self._vm.program self._node = self._vm.root_cfg_node.ConnectNew("test_node")
def setUp(self): super(ConvertTest, self).setUp() options = config.Options.create(python_version=self.python_version) self._vm = vm.VirtualMachine( errors.ErrorLog(), options, load_pytd.Loader(None, self.python_version))
def setUp(self): self._vm = vm.VirtualMachine(errors.ErrorLog(), config.Options([""])) self._program = cfg.Program() self._node = self._program.NewCFGNode("test_node")
def process_one_file(options): """Check a .py file or generate a .pyi for it, according to options. Args: options: config.Options object. Returns: An error code (0 means no error). """ log.info("Process %s => %s", options.input, options.output) errorlog = errors.ErrorLog() result = pytd_builtins.DEFAULT_SRC ast = pytd_builtins.GetDefaultAst(options.python_version) loader = load_pytd.create_loader(options) try: if options.check: check_py(input_filename=options.input, errorlog=errorlog, options=options, loader=loader) else: result, ast = generate_pyi(input_filename=options.input, errorlog=errorlog, options=options, loader=loader) except utils.UsageError as e: logging.error("Usage error: %s\n", utils.message(e)) return 1 except pyc.CompileError as e: errorlog.python_compiler_error(options.input, e.lineno, e.error) except IndentationError as e: errorlog.python_compiler_error(options.input, e.lineno, e.msg) except tokenize.TokenError as e: msg, (lineno, unused_column) = e.args # pylint: disable=unbalanced-tuple-unpacking errorlog.python_compiler_error(options.input, lineno, msg) except directors.SkipFile: result += "# skip-file found, file not analyzed" except Exception as e: # pylint: disable=broad-except if options.nofail: log.warn("***Caught exception: %s", str(e), exc_info=True) if not options.check: result += ( # pytype: disable=name-error "# Caught error in pytype: " + str(e).replace("\n", "\n#") + "\n# " + "\n# ".join(traceback.format_exc().splitlines())) else: e.args = ( str(utils.message(e)) + "\nFile: %s" % options.input,) + e.args[1:] raise if not options.check: if options.output == "-" or not options.output: sys.stdout.write(result) else: log.info("write pyi %r => %r", options.input, options.output) with open(options.output, "w") as fi: fi.write(result) if options.output_pickled: write_pickle(ast, loader, options) exit_status = handle_errors(errorlog, options) # If we have set return_success, set exit_status to 0 after the regular error # handler has been called. if options.return_success: exit_status = 0 # Touch output file upon success. if options.touch and not exit_status: with open(options.touch, "a"): os.utime(options.touch, None) return exit_status
def test_diamond(self): program = cfg.Program() # Disassembled from: # | if []: # | y = 1 # | elif []: # | y = 2 # | elif []: # | y = None # | return y code = self.make_code([ 0x67, 0, 0, # 0 BUILD_LIST, arg=0, 0x72, 15, 0, # 3 POP_JUMP_IF_FALSE, dest=15, 0x64, 1, 0, # 6 LOAD_CONST, arg=1 (1), 0x7d, 1, 0, # 9 STORE_FAST, arg=1 "y", 0x6e, 30, 0, # 12 JUMP_FORWARD, dest=45, 0x67, 0, 0, # 15 BUILD_LIST, arg=0, 0x72, 30, 0, # 18 POP_JUMP_IF_FALSE, dest=30, 0x64, 2, 0, # 21 LOAD_CONST, arg=2 (2), 0x7d, 1, 0, # 24 STORE_FAST, arg=1 "y", 0x6e, 15, 0, # 27 JUMP_FORWARD, dest=45, 0x67, 0, 0, # 30 BUILD_LIST, arg=0, 0x72, 45, 0, # 33 POP_JUMP_IF_FALSE, dest=45, 0x64, 0, 0, # 36 LOAD_CONST, arg=0 (None), 0x7d, 1, 0, # 39 STORE_FAST, arg=1 "y", 0x6e, 0, 0, # 42 JUMP_FORWARD, dest=45, 0x7c, 1, 0, # 45 LOAD_FAST, arg=1, 0x53, # 48 RETURN_VALUE ]) code = blocks.process_code(code) v = vm.VirtualMachine(self.PYTHON_VERSION, errors.ErrorLog()) v.run_bytecode(program.NewCFGNode(), code)
def _create(self, src, disable=()): self._errorlog = errors.ErrorLog() self._director = directors.Director(src, self._errorlog, _TEST_FILENAME, disable)
def setUp(self): self.options = config.Options.create( python_version=self.PYTHON_VERSION, python_exe=self.PYTHON_EXE) self.errorlog = errors.ErrorLog() self.loader = load_pytd.Loader(None, self.options) self.trace_vm = TraceVM(self.options, self.loader)
def setUp(self): super().setUp() options = config.Options.create(python_version=self.python_version) self._ctx = context.Context(errors.ErrorLog(), options, load_pytd.Loader(options))
def setUp(self): options = config.Options.create() self._vm = vm.VirtualMachine(errors.ErrorLog(), options, load_pytd.Loader(None, options))
def setUp(self): options = config.Options.create() self.vm = vm.VirtualMachine( errors.ErrorLog(), options, load_pytd.Loader(None, self.PYTHON_VERSION)) self.type_type = self.vm.convert.type_type
def setUp(self): self._vm = vm.VirtualMachine(errors.ErrorLog(), config.Options.create(), load_pytd.Loader(None, (2, 7)))
def setUp(self): super(GetViewsTest, self).setUp() self._vm = vm.VirtualMachine( errors.ErrorLog(), config.Options.create(python_version=self.PYTHON_VERSION), load_pytd.Loader(None, self.PYTHON_VERSION))
def setUp(self): super().setUp() options = config.Options.create(python_version=self.python_version) self.vm = vm.VirtualMachine( errors.ErrorLog(), options, load_pytd.Loader(None, self.python_version)) self.vm._fold_constants = True