def compile_src(src, filename, python_version, python_exe, mode="exec"): """Compile a string to pyc, and then load and parse the pyc. Args: src: Python source code. filename: The filename the sourcecode is from. python_version: Python version, (major, minor). python_exe: Tuple of the path to Python interpreter and command-line flags. mode: "exec", "eval" or "single". Returns: An instance of loadmarshal.CodeType. Raises: UsageError: If python_exe and python_version are mismatched. """ pyc_data = compile_src_string_to_pyc_string(src, filename, python_version, python_exe, mode) code = parse_pyc_string(pyc_data) if code.python_version != python_version: raise utils.UsageError( "python_exe version %s does not match python version %s" % (utils.format_version( code.python_version), utils.format_version(python_version))) visit(code, AdjustFilename(filename)) return code
def parse_args(argv): """Parse command line args. Arguments: argv: Raw command line args, typically sys.argv[1:] Returns: A tuple of ( parsed_args: argparse.Namespace, kythe_args: kythe.Args, pytype_options: pytype.config.Options) """ parser = make_parser() args = parser.parse_args(argv) cli_args = args.inputs.copy() # If we are passed an imports map we should look for pickled files as well. if args.imports_info: cli_args += [ "--imports_info", args.imports_info, "--use-pickled-files" ] # We need to set this when creating Options (b/128032570) if args.python_version: cli_args += ["-V", pytype_utils.format_version(args.python_version)] pytype_options = pytype_config.Options(cli_args) pytype_options.tweak(**parser.get_pytype_kwargs(args)) kythe_args = kythe.Args(corpus=args.kythe_corpus, root=args.kythe_root) return (args, kythe_args, pytype_options)
def test_generate_builtins_py3(self): self.pytype_args["--generate-builtins"] = self._tmp_path("builtins.py") self.pytype_args["--python_version"] = utils.format_version( utils.full_version_from_major(3)) self._run_pytype(self.pytype_args) self.assertOutputStateMatches(stdout=False, stderr=False, returncode=False)
def test_generate_builtins(self): self.pytype_args["--generate-builtins"] = self._tmp_path("builtins.py") self.pytype_args["--python_version"] = utils.format_version( sys.version_info[:2]) self._run_pytype(self.pytype_args) self.assertOutputStateMatches(stdout=False, stderr=False, returncode=False)
def test_create(self): version = sys.version_info[:2] opts = config.Options.create( input_filename="foo.py", python_version=utils.format_version(version), use_pickled_files=True) self.assertEqual(opts.input, "foo.py") self.assertEqual(opts.use_pickled_files, True) self.assertEqual(opts.python_version, version) self.assertIsNone(opts.python_exe)
def test_subset(self): python_version = sys.version_info[:2] input_options = types.SimpleNamespace( pythonpath=".", python_version=utils.format_version(python_version)) config.Postprocessor( {"python_version"}, input_options, self.output_options).process() with self.assertRaises(AttributeError): _ = self.output_options.pythonpath # not processed self.assertTupleEqual(self.output_options.python_version, python_version)
def parse_src(src, python_version): """Parses a string of source code into a LibCST tree.""" version_str = utils.format_version(python_version) if python_version >= (3, 9): log.warning("LibCST does not support Python %s; parsing with 3.8 instead.", version_str) version_str = "3.8" config = libcst.PartialParserConfig(python_version=version_str) return libcst.parse_module(src, config)
def test_inplace(self): python_version = sys.version_info[:2] input_options = types.SimpleNamespace( disable="import-error,attribute-error", python_version=utils.format_version(python_version)) config.Postprocessor({"disable", "python_version"}, input_options).process() self.assertSequenceEqual(input_options.disable, ["import-error", "attribute-error"]) self.assertTupleEqual(input_options.python_version, python_version)
def test_basic(self): version = sys.version_info[:2] argv = [ "-V", utils.format_version(version), "--use-pickled-files", "-o", "out.pyi", "--pythonpath", "foo:bar", "test.py" ] opts = config.Options(argv, command_line=True) self.assertEqual(opts.python_version, version) self.assertEqual(opts.use_pickled_files, True) self.assertEqual(opts.pythonpath, ["foo", "bar"]) self.assertEqual(opts.output, "out.pyi") self.assertEqual(opts.input, "test.py")
def add_pytype_and_parse(parser, argv): """Add basic pytype options and parse args. Useful to generate a quick CLI for a library. Args: parser: An argparse.ArgumentParser argv: Raw command line args, typically sys.argv[1:] Returns: A tuple of ( parsed_args: argparse.Namespace, pytype_options: pytype.config.Options) """ # Add default --debug and input arguments. parser.add_argument("--debug", action="store_true", dest="debug", default=None, help="Display debug output.") parser.add_argument("inputs", metavar="input", nargs=1, help="A .py file to index") # Add options from pytype-single. wrapper = datatypes.ParserWrapper(parser) pytype_config.add_basic_options(wrapper) pytype_config.add_feature_flags(wrapper) parser = Parser(parser, wrapper.actions) # Parse argv args = parser.parse_args(argv) cli_args = args.inputs.copy() # Make sure we have a valid set of CLI options to pytype ## If we are passed an imports map we should look for pickled files as well. if getattr(args, "imports_info", None): cli_args += [ "--imports_info", args.imports_info, "--use-pickled-files" ] ## We need to set this when creating Options (b/128032570) if args.python_version: cli_args += ["-V", pytype_utils.format_version(args.python_version)] pytype_options = pytype_config.Options(cli_args, command_line=True) pytype_options.tweak(**parser.get_pytype_kwargs(args)) return (args, pytype_options)
def get_python_version(v): return v if v else utils.format_version(sys.version_info[:2])
def test_builtins_determinism3(self): f1, f2 = self._generate_builtins_twice( utils.format_version(utils.full_version_from_major(3))) self.assertBuiltinsPickleEqual(f1, f2)
def _reset_pytype_args(self): self.pytype_args = { "--python_version": utils.format_version(self.python_version), "--verbosity": 1 }
def test_builtins_determinism(self): f1, f2 = self._generate_builtins_twice( utils.format_version(sys.version_info[:2])) self.assertBuiltinsPickleEqual(f1, f2)
def _ResetPytypeArgs(self): self.pytype_args = { "--python_version": utils.format_version(self.PYTHON_VERSION), "--verbosity": 1 }