def test_compile_files(self): # Test compiling a single file, and complete directory for fn in (self.bc_path, self.bc_path2): try: os.unlink(fn) except: pass self.assertTrue( compileall.compile_file(self.source_path, force=False, quiet=True)) self.assertTrue( os.path.isfile(self.bc_path) and not os.path.isfile(self.bc_path2)) os.unlink(self.bc_path) self.assertTrue( compileall.compile_dir(self.directory, force=False, quiet=True)) self.assertTrue( os.path.isfile(self.bc_path) and os.path.isfile(self.bc_path2)) os.unlink(self.bc_path) os.unlink(self.bc_path2) # Test against bad files self.add_bad_source_file() self.assertFalse( compileall.compile_file(self.bad_source_path, force=False, quiet=2)) self.assertFalse( compileall.compile_dir(self.directory, force=False, quiet=2))
def test_no_pycache_in_non_package(self): # Bug 8563 reported that __pycache__ directories got created by # compile_file() for non-.py files. data_dir = os.path.join(self.directory, 'data') data_file = os.path.join(data_dir, 'file') os.mkdir(data_dir) # touch data/file with open(data_file, 'w'): pass compileall.compile_file(data_file) self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__')))
def test_compile_file_pathlike(self): self.assertFalse(os.path.isfile(self.bc_path)) # we should also test the output with support.captured_stdout() as stdout: self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path))) self.assertRegex(stdout.getvalue(), r'Compiling ([^WindowsPath|PosixPath].*)') self.assertTrue(os.path.isfile(self.bc_path))
def test_compile_file_pathlike_ddir(self): self.assertFalse(os.path.isfile(self.bc_path)) self.assertTrue( compileall.compile_file(pathlib.Path(self.source_path), ddir=pathlib.Path('ddir_path'), quiet=2)) self.assertTrue(os.path.isfile(self.bc_path))
def test_multiple_optimization_levels(self): script = script_helper.make_script(self.directory, "test_optimization", "a = 0") bc = [] for opt_level in "", 1, 2, 3: bc.append( importlib.util.cache_from_source(script, optimization=opt_level)) test_combinations = [[0, 1], [1, 2], [0, 2], [0, 1, 2]] for opt_combination in test_combinations: compileall.compile_file(script, quiet=True, optimize=opt_combination) for opt_level in opt_combination: self.assertTrue(os.path.isfile(bc[opt_level])) try: os.unlink(bc[opt_level]) except Exception: pass