Lines Matching refs:self
55 def setUp(self):
56 self.directory = tempfile.mkdtemp()
57 self.source_path = os.path.join(self.directory, '_test.py')
58 self.bc_path = importlib.util.cache_from_source(self.source_path)
59 with open(self.source_path, 'w', encoding="utf-8") as file:
61 self.source_path2 = os.path.join(self.directory, '_test2.py')
62 self.bc_path2 = importlib.util.cache_from_source(self.source_path2)
63 shutil.copyfile(self.source_path, self.source_path2)
64 self.subdirectory = os.path.join(self.directory, '_subdir')
65 os.mkdir(self.subdirectory)
66 self.source_path3 = os.path.join(self.subdirectory, '_test3.py')
67 shutil.copyfile(self.source_path, self.source_path3)
69 def tearDown(self):
70 shutil.rmtree(self.directory)
72 def add_bad_source_file(self):
73 self.bad_source_path = os.path.join(self.directory, '_test_bad.py')
74 with open(self.bad_source_path, 'w', encoding="utf-8") as file:
77 def timestamp_metadata(self):
78 with open(self.bc_path, 'rb') as file:
80 mtime = int(os.stat(self.source_path).st_mtime)
85 def test_year_2038_mtime_compilation(self):
89 os.utime(self.source_path, (2**32 - 1, 2**32 - 1))
91 self.skipTest("filesystem doesn't support timestamps near 2**32")
93 self.assertTrue(compileall.compile_file(self.source_path))
95 def test_larger_than_32_bit_times(self):
99 os.utime(self.source_path, (2**35, 2**35))
101 self.skipTest("filesystem doesn't support large timestamps")
103 self.assertTrue(compileall.compile_file(self.source_path))
105 def recreation_check(self, metadata):
110 py_compile.compile(self.source_path)
111 self.assertEqual(*self.timestamp_metadata())
112 with open(self.bc_path, 'rb') as file:
114 with open(self.bc_path, 'wb') as file:
117 self.assertNotEqual(*self.timestamp_metadata())
118 compileall.compile_dir(self.directory, force=False, quiet=True)
119 self.assertTrue(*self.timestamp_metadata())
121 def test_mtime(self):
123 self.recreation_check(struct.pack('<4sLL', importlib.util.MAGIC_NUMBER,
126 def test_magic_number(self):
128 self.recreation_check(b'\0\0\0\0')
130 def test_compile_files(self):
132 for fn in (self.bc_path, self.bc_path2):
137 self.assertTrue(compileall.compile_file(self.source_path,
139 self.assertTrue(os.path.isfile(self.bc_path) and
140 not os.path.isfile(self.bc_path2))
141 os.unlink(self.bc_path)
142 self.assertTrue(compileall.compile_dir(self.directory, force=False,
144 self.assertTrue(os.path.isfile(self.bc_path) and
145 os.path.isfile(self.bc_path2))
146 os.unlink(self.bc_path)
147 os.unlink(self.bc_path2)
149 self.add_bad_source_file()
150 self.assertFalse(compileall.compile_file(self.bad_source_path,
152 self.assertFalse(compileall.compile_dir(self.directory,
155 def test_compile_file_pathlike(self):
156 self.assertFalse(os.path.isfile(self.bc_path))
159 self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path)))
160 self.assertRegex(stdout.getvalue(), r'Compiling ([^WindowsPath|PosixPath].*)')
161 self.assertTrue(os.path.isfile(self.bc_path))
163 def test_compile_file_pathlike_ddir(self):
164 self.assertFalse(os.path.isfile(self.bc_path))
165 self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
168 self.assertTrue(os.path.isfile(self.bc_path))
170 def test_compile_file_pathlike_stripdir(self):
171 self.assertFalse(os.path.isfile(self.bc_path))
172 self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
175 self.assertTrue(os.path.isfile(self.bc_path))
177 def test_compile_file_pathlike_prependdir(self):
178 self.assertFalse(os.path.isfile(self.bc_path))
179 self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
182 self.assertTrue(os.path.isfile(self.bc_path))
184 def test_compile_path(self):
185 with test.test_importlib.util.import_state(path=[self.directory]):
186 self.assertTrue(compileall.compile_path(quiet=2))
188 with test.test_importlib.util.import_state(path=[self.directory]):
189 self.add_bad_source_file()
190 self.assertFalse(compileall.compile_path(skip_curdir=False,
193 def test_no_pycache_in_non_package(self):
196 data_dir = os.path.join(self.directory, 'data')
203 self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__')))
206 def test_compile_file_encoding_fallback(self):
208 self.add_bad_source_file()
210 self.assertFalse(compileall.compile_file(self.bad_source_path))
213 def test_optimize(self):
217 compileall.compile_dir(self.directory, quiet=True, optimize=optimize)
218 cached = importlib.util.cache_from_source(self.source_path,
220 self.assertTrue(os.path.isfile(cached))
221 cached2 = importlib.util.cache_from_source(self.source_path2,
223 self.assertTrue(os.path.isfile(cached2))
224 cached3 = importlib.util.cache_from_source(self.source_path3,
226 self.assertTrue(os.path.isfile(cached3))
228 def test_compile_dir_pathlike(self):
229 self.assertFalse(os.path.isfile(self.bc_path))
231 compileall.compile_dir(pathlib.Path(self.directory))
233 self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)')
234 self.assertTrue(os.path.isfile(self.bc_path))
236 def test_compile_dir_pathlike_stripdir(self):
237 self.assertFalse(os.path.isfile(self.bc_path))
238 self.assertTrue(compileall.compile_dir(pathlib.Path(self.directory),
241 self.assertTrue(os.path.isfile(self.bc_path))
243 def test_compile_dir_pathlike_prependdir(self):
244 self.assertFalse(os.path.isfile(self.bc_path))
245 self.assertTrue(compileall.compile_dir(pathlib.Path(self.directory),
248 self.assertTrue(os.path.isfile(self.bc_path))
252 def test_compile_pool_called(self, pool_mock):
253 compileall.compile_dir(self.directory, quiet=True, workers=5)
254 self.assertTrue(pool_mock.called)
256 def test_compile_workers_non_positive(self):
257 with self.assertRaisesRegex(ValueError,
259 compileall.compile_dir(self.directory, workers=-1)
263 def test_compile_workers_cpu_count(self, pool_mock):
264 compileall.compile_dir(self.directory, quiet=True, workers=0)
265 self.assertEqual(pool_mock.call_args[1]['max_workers'], None)
270 def test_compile_one_worker(self, compile_file_mock, pool_mock):
271 compileall.compile_dir(self.directory, quiet=True)
272 self.assertFalse(pool_mock.called)
273 self.assertTrue(compile_file_mock.called)
278 def test_compile_missing_multiprocessing(self, compile_file_mock):
279 compileall.compile_dir(self.directory, quiet=True, workers=5)
280 self.assertTrue(compile_file_mock.called)
282 def test_compile_dir_maxlevels(self):
285 path = self.directory
290 shutil.copyfile(self.source_path, source)
293 compileall.compile_dir(self.directory, quiet=True, maxlevels=depth - 1)
294 self.assertFalse(os.path.isfile(pyc_filename))
296 compileall.compile_dir(self.directory, quiet=True, maxlevels=depth)
297 self.assertTrue(os.path.isfile(pyc_filename))
299 def _test_ddir_only(self, *, ddir, parallel=True):
302 path = self.directory
311 self.directory, quiet=True, ddir=ddir,
313 self.assertTrue(mods)
315 self.assertTrue(mod.startswith(self.directory), mod)
317 modpath = mod[len(self.directory+os.sep):]
321 self.assertEqual(mod_code_obj.co_filename, expected_in)
322 self.assertIn(f'"{expected_in}"', os.fsdecode(err))
324 def test_ddir_only_one_worker(self):
326 return self._test_ddir_only(ddir="<a prefix>", parallel=False)
329 def test_ddir_multiple_workers(self):
331 return self._test_ddir_only(ddir="<a prefix>", parallel=True)
333 def test_ddir_empty_only_one_worker(self):
335 return self._test_ddir_only(ddir="", parallel=False)
338 def test_ddir_empty_multiple_workers(self):
340 return self._test_ddir_only(ddir="", parallel=True)
342 def test_strip_only(self):
344 path = os.path.join(self.directory, *fullpath)
348 stripdir = os.path.join(self.directory, *fullpath[:2])
352 self.assertIn(
356 self.assertNotIn(
361 def test_prepend_only(self):
363 path = os.path.join(self.directory, *fullpath)
370 expected_in = os.path.join(prependdir, self.directory, *fullpath)
371 self.assertIn(
376 def test_strip_and_prepend(self):
378 path = os.path.join(self.directory, *fullpath)
382 stripdir = os.path.join(self.directory, *fullpath[:2])
388 self.assertIn(
392 self.assertNotIn(
397 def test_strip_prepend_and_ddir(self):
399 path = os.path.join(self.directory, *fullpath)
402 with self.assertRaises(ValueError):
406 def test_multiple_optimization_levels(self):
407 script = script_helper.make_script(self.directory,
419 self.assertTrue(os.path.isfile(bc[opt_level]))
426 def test_ignore_symlink_destination(self):
428 allowed_path = os.path.join(self.directory, "test", "dir", "allowed")
429 symlinks_path = os.path.join(self.directory, "test", "dir", "symlinks")
430 prohibited_path = os.path.join(self.directory, "test", "dir", "prohibited")
447 self.assertTrue(os.path.isfile(allowed_bc))
448 self.assertFalse(os.path.isfile(prohibited_bc))
466 # non-ASCII chars, so _walk_dir() fails to encode self.directory.
472 def setUp(self):
473 self.directory = tempfile.mkdtemp()
474 self.source_path = os.path.join(self.directory, '_test.py')
475 with open(self.source_path, 'w', encoding='utf-8') as file:
479 def tearDown(self):
480 shutil.rmtree(self.directory)
482 def test_error(self):
486 compileall.compile_dir(self.directory)
494 def setUp(self):
495 self.directory = tempfile.mkdtemp()
496 self.addCleanup(os_helper.rmtree, self.directory)
497 self.pkgdir = os.path.join(self.directory, 'foo')
498 os.mkdir(self.pkgdir)
499 self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__')
501 self.initfn = script_helper.make_script(self.pkgdir, '__init__', '')
502 self.barfn = script_helper.make_script(self.pkgdir, 'bar', '')
505 def temporary_pycache_prefix(self):
508 new_prefix = os.path.join(self.directory, '__testcache__')
512 'PYTHONPATH': self.directory,
518 def _get_run_args(self, args):
523 def assertRunOK(self, *args, **env_vars):
525 *self._get_run_args(args), **env_vars,
527 self.assertEqual(b'', err)
530 def assertRunNotOK(self, *args, **env_vars):
532 *self._get_run_args(args), **env_vars,
536 def assertCompiled(self, fn):
538 self.assertTrue(os.path.exists(path))
540 def assertNotCompiled(self, fn):
542 self.assertFalse(os.path.exists(path))
544 def test_no_args_compiles_path(self):
546 bazfn = script_helper.make_script(self.directory, 'baz', '')
547 with self.temporary_pycache_prefix() as env:
548 self.assertRunOK(**env)
549 self.assertCompiled(bazfn)
550 self.assertNotCompiled(self.initfn)
551 self.assertNotCompiled(self.barfn)
554 def test_no_args_respects_force_flag(self):
555 bazfn = script_helper.make_script(self.directory, 'baz', '')
556 with self.temporary_pycache_prefix() as env:
557 self.assertRunOK(**env)
563 self.assertRunOK(**env)
565 self.assertEqual(mtime, mtime2)
567 self.assertRunOK('-f', **env)
569 self.assertNotEqual(mtime, mtime2)
571 def test_no_args_respects_quiet_flag(self):
572 script_helper.make_script(self.directory, 'baz', '')
573 with self.temporary_pycache_prefix() as env:
574 noisy = self.assertRunOK(**env)
575 self.assertIn(b'Listing ', noisy)
576 quiet = self.assertRunOK('-q', **env)
577 self.assertNotIn(b'Listing ', quiet)
586 def f(self, ext=ext, switch=switch):
588 ['-m', 'compileall', '-q', self.pkgdir]))
590 self.assertTrue(os.path.exists(self.pkgdir_cachedir))
593 self.assertEqual(sorted(os.listdir(self.pkgdir_cachedir)), expected)
595 self.assertFalse([fn for fn in os.listdir(self.pkgdir)
599 def test_legacy_paths(self):
602 self.assertRunOK('-b', '-q', self.pkgdir)
604 self.assertFalse(os.path.exists(self.pkgdir_cachedir))
607 self.assertEqual(sorted(os.listdir(self.pkgdir)), expected)
609 def test_multiple_runs(self):
612 self.assertRunOK('-q', self.pkgdir)
614 self.assertTrue(os.path.exists(self.pkgdir_cachedir))
615 cachecachedir = os.path.join(self.pkgdir_cachedir, '__pycache__')
616 self.assertFalse(os.path.exists(cachecachedir))
618 self.assertRunOK('-q', self.pkgdir)
619 self.assertTrue(os.path.exists(self.pkgdir_cachedir))
620 self.assertFalse(os.path.exists(cachecachedir))
623 def test_force(self):
624 self.assertRunOK('-q', self.pkgdir)
625 pycpath = importlib.util.cache_from_source(self.barfn)
630 self.assertRunOK('-q', self.pkgdir)
632 self.assertEqual(mtime, mtime2)
634 self.assertRunOK('-q', '-f', self.pkgdir)
636 self.assertNotEqual(mtime, mtime2)
638 def test_recursion_control(self):
639 subpackage = os.path.join(self.pkgdir, 'spam')
643 self.assertRunOK('-q', '-l', self.pkgdir)
644 self.assertNotCompiled(subinitfn)
645 self.assertFalse(os.path.exists(os.path.join(subpackage, '__pycache__')))
646 self.assertRunOK('-q', self.pkgdir)
647 self.assertCompiled(subinitfn)
648 self.assertCompiled(hamfn)
650 def test_recursion_limit(self):
651 subpackage = os.path.join(self.pkgdir, 'spam')
662 self.assertRunOK('-q', '-r 0', self.pkgdir)
663 self.assertNotCompiled(subinitfn)
664 self.assertFalse(
667 self.assertRunOK('-q', '-r 1', self.pkgdir)
668 self.assertCompiled(subinitfn)
669 self.assertCompiled(hamfn)
670 self.assertNotCompiled(spamfn)
672 self.assertRunOK('-q', '-r 2', self.pkgdir)
673 self.assertCompiled(subinitfn)
674 self.assertCompiled(hamfn)
675 self.assertCompiled(spamfn)
676 self.assertNotCompiled(eggfn)
678 self.assertRunOK('-q', '-r 5', self.pkgdir)
679 self.assertCompiled(subinitfn)
680 self.assertCompiled(hamfn)
681 self.assertCompiled(spamfn)
682 self.assertCompiled(eggfn)
685 def test_symlink_loop(self):
689 pkg = os.path.join(self.pkgdir, 'spam')
693 self.assertRunOK('-q', self.pkgdir)
694 self.assertCompiled(os.path.join(
695 self.pkgdir, 'spam', 'evil', 'evil2', '__init__.py'
698 def test_quiet(self):
699 noisy = self.assertRunOK(self.pkgdir)
700 quiet = self.assertRunOK('-q', self.pkgdir)
701 self.assertNotEqual(b'', noisy)
702 self.assertEqual(b'', quiet)
704 def test_silent(self):
705 script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax')
706 _, quiet, _ = self.assertRunNotOK('-q', self.pkgdir)
707 _, silent, _ = self.assertRunNotOK('-qq', self.pkgdir)
708 self.assertNotEqual(b'', quiet)
709 self.assertEqual(b'', silent)
711 def test_regexp(self):
712 self.assertRunOK('-q', '-x', r'ba[^\\/]*$', self.pkgdir)
713 self.assertNotCompiled(self.barfn)
714 self.assertCompiled(self.initfn)
716 def test_multiple_dirs(self):
717 pkgdir2 = os.path.join(self.directory, 'foo2')
721 self.assertRunOK('-q', self.pkgdir, pkgdir2)
722 self.assertCompiled(self.initfn)
723 self.assertCompiled(self.barfn)
724 self.assertCompiled(init2fn)
725 self.assertCompiled(bar2fn)
727 def test_d_compile_error(self):
728 script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax')
729 rc, out, err = self.assertRunNotOK('-q', '-d', 'dinsdale', self.pkgdir)
730 self.assertRegex(out, b'File "dinsdale')
732 def test_d_runtime_error(self):
733 bazfn = script_helper.make_script(self.pkgdir, 'baz', 'raise Exception')
734 self.assertRunOK('-q', '-d', 'dinsdale', self.pkgdir)
735 fn = script_helper.make_script(self.pkgdir, 'bing', 'import baz')
737 os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc'))
740 self.assertRegex(err, b'File "dinsdale')
742 def test_include_bad_file(self):
743 rc, out, err = self.assertRunNotOK(
744 '-i', os.path.join(self.directory, 'nosuchfile'), self.pkgdir)
745 self.assertRegex(out, b'rror.*nosuchfile')
746 self.assertNotRegex(err, b'Traceback')
747 self.assertFalse(os.path.exists(importlib.util.cache_from_source(
748 self.pkgdir_cachedir)))
750 def test_include_file_with_arg(self):
751 f1 = script_helper.make_script(self.pkgdir, 'f1', '')
752 f2 = script_helper.make_script(self.pkgdir, 'f2', '')
753 f3 = script_helper.make_script(self.pkgdir, 'f3', '')
754 f4 = script_helper.make_script(self.pkgdir, 'f4', '')
755 with open(os.path.join(self.directory, 'l1'), 'w', encoding="utf-8") as l1:
756 l1.write(os.path.join(self.pkgdir, 'f1.py')+os.linesep)
757 l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep)
758 self.assertRunOK('-i', os.path.join(self.directory, 'l1'), f4)
759 self.assertCompiled(f1)
760 self.assertCompiled(f2)
761 self.assertNotCompiled(f3)
762 self.assertCompiled(f4)
764 def test_include_file_no_arg(self):
765 f1 = script_helper.make_script(self.pkgdir, 'f1', '')
766 f2 = script_helper.make_script(self.pkgdir, 'f2', '')
767 f3 = script_helper.make_script(self.pkgdir, 'f3', '')
768 f4 = script_helper.make_script(self.pkgdir, 'f4', '')
769 with open(os.path.join(self.directory, 'l1'), 'w', encoding="utf-8") as l1:
770 l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep)
771 self.assertRunOK('-i', os.path.join(self.directory, 'l1'))
772 self.assertNotCompiled(f1)
773 self.assertCompiled(f2)
774 self.assertNotCompiled(f3)
775 self.assertNotCompiled(f4)
777 def test_include_on_stdin(self):
778 f1 = script_helper.make_script(self.pkgdir, 'f1', '')
779 f2 = script_helper.make_script(self.pkgdir, 'f2', '')
780 f3 = script_helper.make_script(self.pkgdir, 'f3', '')
781 f4 = script_helper.make_script(self.pkgdir, 'f4', '')
782 p = script_helper.spawn_python(*(self._get_run_args(()) + ['-i', '-']))
785 self.assertNotCompiled(f1)
786 self.assertNotCompiled(f2)
787 self.assertCompiled(f3)
788 self.assertNotCompiled(f4)
790 def test_compiles_as_much_as_possible(self):
791 bingfn = script_helper.make_script(self.pkgdir, 'bing', 'syntax(error')
792 rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn,
793 bingfn, self.barfn)
794 self.assertRegex(out, b'rror')
795 self.assertNotCompiled(bingfn)
796 self.assertCompiled(self.initfn)
797 self.assertCompiled(self.barfn)
799 def test_invalid_arg_produces_message(self):
800 out = self.assertRunOK('badfilename')
801 self.assertRegex(out, b"Can't list 'badfilename'")
803 def test_pyc_invalidation_mode(self):
804 script_helper.make_script(self.pkgdir, 'f1', '')
806 os.path.join(self.pkgdir, 'f1.py'))
807 self.assertRunOK('--invalidation-mode=checked-hash', self.pkgdir)
810 self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b11)
811 self.assertRunOK('--invalidation-mode=unchecked-hash', self.pkgdir)
814 self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b01)
817 def test_workers(self):
818 bar2fn = script_helper.make_script(self.directory, 'bar2', '')
821 pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix))
826 self.assertRunOK(self.directory, '-j', '0')
827 self.assertCompiled(bar2fn)
829 self.assertCompiled(file)
832 def test_workers_available_cores(self, compile_dir):
834 new=[sys.executable, self.directory, "-j0"]):
836 self.assertTrue(compile_dir.called)
837 self.assertEqual(compile_dir.call_args[-1]['workers'], 0)
839 def test_strip_and_prepend(self):
841 path = os.path.join(self.directory, *fullpath)
845 stripdir = os.path.join(self.directory, *fullpath[:2])
847 self.assertRunOK("-s", stripdir, "-p", prependdir, path)
850 self.assertIn(
854 self.assertNotIn(
859 def test_multiple_optimization_levels(self):
860 path = os.path.join(self.directory, "optimizations")
874 self.assertRunOK(path, *("-o" + str(n) for n in opt_combination))
876 self.assertTrue(os.path.isfile(bc[int(opt_level)]))
883 def test_ignore_symlink_destination(self):
885 allowed_path = os.path.join(self.directory, "test", "dir", "allowed")
886 symlinks_path = os.path.join(self.directory, "test", "dir", "symlinks")
887 prohibited_path = os.path.join(self.directory, "test", "dir", "prohibited")
902 self.assertRunOK(symlinks_path, "-e", allowed_path)
904 self.assertTrue(os.path.isfile(allowed_bc))
905 self.assertFalse(os.path.isfile(prohibited_bc))
907 def test_hardlink_bad_args(self):
910 self.assertRunNotOK(self.directory, "-o 1", "--hardlink-dupes")
912 def test_hardlink(self):
920 with self.subTest(dedup=dedup):
927 self.assertRunOK(path, *args)
929 self.assertEqual(is_hardlink(pycs[0], pycs[1]), dedup)
930 self.assertEqual(is_hardlink(pycs[1], pycs[2]), dedup)
931 self.assertEqual(is_hardlink(pycs[0], pycs[2]), dedup)
953 def setUp(self):
954 self.path = None
957 def temporary_directory(self):
959 self.path = path
961 self.path = None
963 def make_script(self, code, name="script"):
964 return script_helper.make_script(self.path, name, code)
966 def compile_dir(self, *, dedup=True, optimize=(0, 1, 2), force=False):
967 compileall.compile_dir(self.path, quiet=True, optimize=optimize,
970 def test_bad_args(self):
973 with self.temporary_directory():
974 self.make_script("pass")
975 with self.assertRaises(ValueError):
976 compileall.compile_dir(self.path, quiet=True, optimize=0,
978 with self.assertRaises(ValueError):
981 compileall.compile_dir(self.path, quiet=True, optimize=[0, 0],
984 def create_code(self, docstring=False, assertion=False):
993 def iter_codes(self):
996 code = self.create_code(docstring=docstring, assertion=assertion)
999 def test_disabled(self):
1001 for code, docstring, assertion in self.iter_codes():
1002 with self.subTest(docstring=docstring, assertion=assertion):
1003 with self.temporary_directory():
1004 script = self.make_script(code)
1006 self.compile_dir(dedup=False)
1007 self.assertFalse(is_hardlink(pycs[0], pycs[1]))
1008 self.assertFalse(is_hardlink(pycs[0], pycs[2]))
1009 self.assertFalse(is_hardlink(pycs[1], pycs[2]))
1011 def check_hardlinks(self, script, docstring=False, assertion=False):
1013 self.assertEqual(is_hardlink(pycs[0], pycs[1]),
1015 self.assertEqual(is_hardlink(pycs[0], pycs[2]),
1017 self.assertEqual(is_hardlink(pycs[1], pycs[2]),
1020 def test_hardlink(self):
1022 for code, docstring, assertion in self.iter_codes():
1023 with self.subTest(docstring=docstring, assertion=assertion):
1024 with self.temporary_directory():
1025 script = self.make_script(code)
1026 self.compile_dir()
1027 self.check_hardlinks(script, docstring, assertion)
1029 def test_only_two_levels(self):
1032 with self.subTest(opts=opts):
1033 with self.temporary_directory():
1036 script = self.make_script(self.create_code())
1037 self.compile_dir(optimize=opts)
1040 self.assertTrue(is_hardlink(pyc1, pyc2))
1042 def test_duplicated_levels(self):
1045 with self.temporary_directory():
1048 script = self.make_script(self.create_code())
1049 self.compile_dir(optimize=[1, 0, 1, 0])
1052 self.assertTrue(is_hardlink(pyc1, pyc2))
1054 def test_recompilation(self):
1057 with self.temporary_directory():
1058 script = self.make_script("a = 0")
1059 self.compile_dir()
1061 self.check_hardlinks(script)
1067 script = self.make_script("print(0)")
1070 self.compile_dir(optimize=[0, 2], force=True)
1073 self.assertEqual(inode, os.stat(pycs[1]).st_ino)
1074 self.assertTrue(is_hardlink(pycs[0], pycs[2]))
1075 self.assertNotEqual(inode, os.stat(pycs[2]).st_ino)
1077 self.assertFalse(filecmp.cmp(pycs[1], pycs[2], shallow=True))
1079 def test_import(self):
1082 with self.temporary_directory():
1083 script = self.make_script(self.create_code(), name="module")
1084 self.compile_dir()
1086 self.check_hardlinks(script)
1092 script = self.make_script("print(0)", name="module")
1096 "-O", "-c", "import module", __isolated=False, PYTHONPATH=self.path
1100 self.assertEqual(inode, os.stat(pycs[0]).st_ino)
1101 self.assertEqual(inode, os.stat(pycs[2]).st_ino)
1102 self.assertFalse(is_hardlink(pycs[1], pycs[2]))
1104 self.assertFalse(filecmp.cmp(pycs[1], pycs[2], shallow=True))