]> granicus.if.org Git - python/commitdiff
Issue #28226: compileall now supports pathlib
authorBerker Peksag <berker.peksag@gmail.com>
Fri, 30 Sep 2016 21:54:18 +0000 (00:54 +0300)
committerBerker Peksag <berker.peksag@gmail.com>
Fri, 30 Sep 2016 21:54:18 +0000 (00:54 +0300)
Doc/library/compileall.rst
Lib/compileall.py
Lib/test/test_compileall.py
Misc/NEWS

index 91bdd18d70b7b8ddfbde0e2b442a334d221d3281..c1af02b0d8220a57531ce2725dda5599fdd4a7b7 100644 (file)
@@ -153,6 +153,9 @@ Public functions
       The *legacy* parameter only writes out ``.pyc`` files, not ``.pyo`` files
       no matter what the value of *optimize* is.
 
+   .. versionchanged:: 3.6
+      Accepts a :term:`path-like object`.
+
 .. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1)
 
    Compile the file with path *fullname*. Return a true value if the file
@@ -221,6 +224,9 @@ subdirectory and all its subdirectories::
    import re
    compileall.compile_dir('Lib/', rx=re.compile(r'[/\\][.]svn'), force=True)
 
+   # pathlib.Path objects can also be used.
+   import pathlib
+   compileall.compile_dir(pathlib.Path('Lib/'), force=True)
 
 .. seealso::
 
index 67c5f5ac7223550eac89c316c198ab51d684d926..3e45785a667a5442ffadc8d658bab602ec6bee0f 100644 (file)
@@ -25,6 +25,8 @@ from functools import partial
 __all__ = ["compile_dir","compile_file","compile_path"]
 
 def _walk_dir(dir, ddir=None, maxlevels=10, quiet=0):
+    if quiet < 2 and isinstance(dir, os.PathLike):
+        dir = os.fspath(dir)
     if not quiet:
         print('Listing {!r}...'.format(dir))
     try:
@@ -105,6 +107,8 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
     optimize:  optimization level or -1 for level of the interpreter
     """
     success = True
+    if quiet < 2 and isinstance(fullname, os.PathLike):
+        fullname = os.fspath(fullname)
     name = os.path.basename(fullname)
     if ddir is not None:
         dfile = os.path.join(ddir, name)
index 1f05e78c2303b33812e5ddfc1540d01e45ee55b2..30ca3feee404d59c7e949d2e99a73e1b72d69b41 100644 (file)
@@ -102,6 +102,22 @@ class CompileallTests(unittest.TestCase):
         self.assertFalse(compileall.compile_dir(self.directory,
                                                 force=False, quiet=2))
 
+    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.assertEqual(stdout.getvalue(),
+                         "Compiling '{}'...\n".format(self.source_path))
+        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_compile_path(self):
         with test.test_importlib.util.import_state(path=[self.directory]):
             self.assertTrue(compileall.compile_path(quiet=2))
@@ -138,6 +154,13 @@ class CompileallTests(unittest.TestCase):
                                                    optimization=opt)
         self.assertTrue(os.path.isfile(cached3))
 
+    def test_compile_dir_pathlike(self):
+        self.assertFalse(os.path.isfile(self.bc_path))
+        with support.captured_stdout() as stdout:
+            compileall.compile_dir(pathlib.Path(self.directory))
+        self.assertIn("Listing '{}'...".format(self.directory), stdout.getvalue())
+        self.assertTrue(os.path.isfile(self.bc_path))
+
     @mock.patch('compileall.ProcessPoolExecutor')
     def test_compile_pool_called(self, pool_mock):
         compileall.compile_dir(self.directory, quiet=True, workers=5)
index 5befca176c56cdf00ad004f06f9fd01579212691..62fc004d93ecd0b36909804fcbb5164b8dac8f95 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -46,6 +46,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #28226: compileall now supports pathlib.
+
 - Issue #28314: Fix function declaration (C flags) for the getiterator() method
   of xml.etree.ElementTree.Element.