def testPytdBuiltinIsPackage(self): """Verify 'import importlib' for python3.""" subdir = "stdlib/3" path, _ = pytd_utils.GetPredefinedFile(subdir, "importlib", as_package=True) self.assertEqual(path, "pytd/stdlib/3/importlib/__init__.pytd")
def testPytdBuiltin3(self): """Verify 'import sys' for python3.""" subdir = "builtins/3" _, import_contents = pytd_utils.GetPredefinedFile(subdir, "__builtin__") with open(os.path.join(os.path.dirname(pytd.__file__), subdir, "__builtin__.pytd"), "rb") as fi: file_contents = fi.read() self.assertMultiLineEqual(import_contents, file_contents)
def test_pytd_builtin2(self): """Verify 'import sys' for python2.""" subdir = "builtins/2" _, import_contents = pytd_utils.GetPredefinedFile(subdir, "__builtin__") with open(os.path.join(os.path.dirname(pytd.__file__), subdir, "__builtin__.pytd"), "r") as fi: file_contents = fi.read() self.assertMultiLineEqual(import_contents.decode("utf-8"), file_contents)
def test_get_predefined_file_throws(self): # smoke test, only checks that it does throw with six.assertRaisesRegex( self, IOError, r"File not found|Resource not found|No such file or directory" ): pytd_utils.GetPredefinedFile(self.BUILTINS, "-this-file-does-not-exist")
def test_pytd_builtin3(self): """Verify 'import sys' for python3.""" subdir = "builtins" _, import_contents = pytd_utils.GetPredefinedFile(subdir, "builtins") with open( os.path.join(os.path.dirname(file_utils.__file__), "stubs", subdir, "builtins.pytd"), "r") as fi: file_contents = fi.read() self.assertMultiLineEqual(import_contents, file_contents)
def _parse_predefined(self, pytd_subdir, module, as_package=False): """Parse a pyi/pytd file in the pytype source tree.""" try: filename, src = pytd_utils.GetPredefinedFile(pytd_subdir, module, as_package=as_package) except IOError: return None ast = parser.parse_string(src, filename=filename, name=module, python_version=self.python_version) assert ast.name == module return ast
def ParsePredefinedPyTD(pytd_subdir, module, python_version): """Load and parse a *.pytd from "pytd/{pytd_subdir}/{module}.pytd". Args: pytd_subdir: the directory where the module should be found module: the module name (without any file extension) python_version: sys.version_info[:2] Returns: The AST of the module; None if the module doesn't exist in pytd_subdir. """ try: src = pytd_utils.GetPredefinedFile(pytd_subdir, module) except IOError: return None filename = os.path.join(pytd_subdir, module + ".pytd") return ParsePyTD(src, filename=filename, module=module, python_version=python_version).Replace(name=module)
def ParsePredefinedPyTD(pytd_subdir, module, python_version, as_package=False): """Load and parse a *.pytd from "pytd/{pytd_subdir}/{module}.pytd". Args: pytd_subdir: the directory where the module should be found module: the module name (without any file extension) python_version: sys.version_info[:2] as_package: load the module as a directory with a __init__ file Returns: The AST of the module; None if the module doesn't exist in pytd_subdir. """ try: path, src = pytd_utils.GetPredefinedFile(pytd_subdir, module, as_package=as_package) except IOError: return None return ParsePyTD(src, filename=path, module=module, python_version=python_version).Replace(name=module)
def test_get_predefined_file_basic(self): # smoke test, only checks that it doesn't throw, the filepath is correct, # and the result is a string path, src = pytd_utils.GetPredefinedFile(self.BUILTINS, "__builtin__") self.assertEqual(path, "pytd/builtins/3/__builtin__.pytd") self.assertIsInstance(src, bytes)
def _FindBuiltinFile(name, python_version, extension=".pytd"): subdir = file_utils.get_versioned_path("builtins", python_version) _, src = pytd_utils.GetPredefinedFile(subdir, name, extension) return src
def test_builtins(self): _, text = pytd_utils.GetPredefinedFile( "builtins/%d" % self.python_version[0], "builtins") self.check(None, text)
def testGetPredefinedFileReturnsString(self): # smoke test, only checks that it doesn't throw and the result is a string self.assertIsInstance( pytd_utils.GetPredefinedFile(self.BUILTINS, "__builtin__"), str)
def _FindStdlibFile(name, extension=".pytd"): return pytd_utils.GetPredefinedFile("stdlib", name, extension)
def _FindBuiltinFile(name, python_version, extension=".pytd"): subdir = utils.get_builtins_path(python_version) return pytd_utils.GetPredefinedFile(subdir, name, extension)
def get_builtins_source(python_version): return pytd_utils.GetPredefinedFile("builtins/%d" % python_version[0], "builtins")[1]
def test_builtins(self): _, builtins = pytd_utils.GetPredefinedFile("builtins", "builtins") self.check(builtins, expected=parser_test_base.IGNORE)
def test_pytd_builtin_is_package(self): subdir = "builtins/2and3" path, _ = pytd_utils.GetPredefinedFile(subdir, "attr", as_package=True) self.assertEqual(path, "pytd/builtins/2and3/attr/__init__.pytd")
def _parse_predefined(self, name, options): _, src = pytd_utils.GetPredefinedFile("builtins", name, ".pytd") mod = parser.parse_string(src, name=name, options=options) return mod