mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	bpo-40447: accept all path-like objects in compileall.compile_file (GH-19883)
(cherry picked from commit 1ecfd1ebf1)
Co-authored-by: Filipe Laíns <lains@riseup.net>
Signed-off-by: Filipe Laíns <lains@archlinux.org>
Signed-off-by: Filipe Laíns <lains@riseup.net>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
			
			
This commit is contained in:
		
							parent
							
								
									4b3b6423c3
								
							
						
					
					
						commit
						d5eb2f4747
					
				
					 3 changed files with 32 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -154,8 +154,8 @@ def compile_file(fullname, ddir=None, force=False, rx=None, quiet=0,
 | 
			
		|||
                          "in combination with stripdir or prependdir"))
 | 
			
		||||
 | 
			
		||||
    success = True
 | 
			
		||||
    if quiet < 2 and isinstance(fullname, os.PathLike):
 | 
			
		||||
    fullname = os.fspath(fullname)
 | 
			
		||||
    stripdir = os.fspath(stripdir) if stripdir is not None else None
 | 
			
		||||
    name = os.path.basename(fullname)
 | 
			
		||||
 | 
			
		||||
    dfile = None
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -167,6 +167,20 @@ def test_compile_file_pathlike_ddir(self):
 | 
			
		|||
                                                quiet=2))
 | 
			
		||||
        self.assertTrue(os.path.isfile(self.bc_path))
 | 
			
		||||
 | 
			
		||||
    def test_compile_file_pathlike_stripdir(self):
 | 
			
		||||
        self.assertFalse(os.path.isfile(self.bc_path))
 | 
			
		||||
        self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
 | 
			
		||||
                                                stripdir=pathlib.Path('stripdir_path'),
 | 
			
		||||
                                                quiet=2))
 | 
			
		||||
        self.assertTrue(os.path.isfile(self.bc_path))
 | 
			
		||||
 | 
			
		||||
    def test_compile_file_pathlike_prependdir(self):
 | 
			
		||||
        self.assertFalse(os.path.isfile(self.bc_path))
 | 
			
		||||
        self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path),
 | 
			
		||||
                                                prependdir=pathlib.Path('prependdir_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))
 | 
			
		||||
| 
						 | 
				
			
			@ -219,6 +233,20 @@ def test_compile_dir_pathlike(self):
 | 
			
		|||
        self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)')
 | 
			
		||||
        self.assertTrue(os.path.isfile(self.bc_path))
 | 
			
		||||
 | 
			
		||||
    def test_compile_dir_pathlike_stripdir(self):
 | 
			
		||||
        self.assertFalse(os.path.isfile(self.bc_path))
 | 
			
		||||
        self.assertTrue(compileall.compile_dir(pathlib.Path(self.directory),
 | 
			
		||||
                                               stripdir=pathlib.Path('stripdir_path'),
 | 
			
		||||
                                               quiet=2))
 | 
			
		||||
        self.assertTrue(os.path.isfile(self.bc_path))
 | 
			
		||||
 | 
			
		||||
    def test_compile_dir_pathlike_prependdir(self):
 | 
			
		||||
        self.assertFalse(os.path.isfile(self.bc_path))
 | 
			
		||||
        self.assertTrue(compileall.compile_dir(pathlib.Path(self.directory),
 | 
			
		||||
                                               prependdir=pathlib.Path('prependdir_path'),
 | 
			
		||||
                                               quiet=2))
 | 
			
		||||
        self.assertTrue(os.path.isfile(self.bc_path))
 | 
			
		||||
 | 
			
		||||
    @skipUnless(_have_multiprocessing, "requires multiprocessing")
 | 
			
		||||
    @mock.patch('concurrent.futures.ProcessPoolExecutor')
 | 
			
		||||
    def test_compile_pool_called(self, pool_mock):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,2 @@
 | 
			
		|||
Accept :class:`os.PathLike` (such as :class:`pathlib.Path`) in the ``stripdir`` arguments of
 | 
			
		||||
:meth:`compileall.compile_file` and :meth:`compileall.compile_dir`.
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue