mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	Issue #20639: calling Path.with_suffix('') allows removing the suffix again.
Patch by July Tikhonov.
This commit is contained in:
		
							parent
							
								
									7084e736db
								
							
						
					
					
						commit
						e50dafcd63
					
				
					 3 changed files with 12 additions and 4 deletions
				
			
		|  | @ -759,11 +759,10 @@ def with_name(self, name): | ||||||
|     def with_suffix(self, suffix): |     def with_suffix(self, suffix): | ||||||
|         """Return a new path with the file suffix changed (or added, if none).""" |         """Return a new path with the file suffix changed (or added, if none).""" | ||||||
|         # XXX if suffix is None, should the current suffix be removed? |         # XXX if suffix is None, should the current suffix be removed? | ||||||
|         drv, root, parts = self._flavour.parse_parts((suffix,)) |         f = self._flavour | ||||||
|         if drv or root or len(parts) != 1: |         if f.sep in suffix or f.altsep and f.altsep in suffix: | ||||||
|             raise ValueError("Invalid suffix %r" % (suffix)) |             raise ValueError("Invalid suffix %r" % (suffix)) | ||||||
|         suffix = parts[0] |         if suffix and not suffix.startswith('.') or suffix == '.': | ||||||
|         if not suffix.startswith('.'): |  | ||||||
|             raise ValueError("Invalid suffix %r" % (suffix)) |             raise ValueError("Invalid suffix %r" % (suffix)) | ||||||
|         name = self.name |         name = self.name | ||||||
|         if not name: |         if not name: | ||||||
|  |  | ||||||
|  | @ -551,6 +551,9 @@ def test_with_suffix_common(self): | ||||||
|         self.assertEqual(P('/a/b').with_suffix('.gz'), P('/a/b.gz')) |         self.assertEqual(P('/a/b').with_suffix('.gz'), P('/a/b.gz')) | ||||||
|         self.assertEqual(P('a/b.py').with_suffix('.gz'), P('a/b.gz')) |         self.assertEqual(P('a/b.py').with_suffix('.gz'), P('a/b.gz')) | ||||||
|         self.assertEqual(P('/a/b.py').with_suffix('.gz'), P('/a/b.gz')) |         self.assertEqual(P('/a/b.py').with_suffix('.gz'), P('/a/b.gz')) | ||||||
|  |         # Stripping suffix | ||||||
|  |         self.assertEqual(P('a/b.py').with_suffix(''), P('a/b')) | ||||||
|  |         self.assertEqual(P('/a/b').with_suffix(''), P('/a/b')) | ||||||
|         # Path doesn't have a "filename" component |         # Path doesn't have a "filename" component | ||||||
|         self.assertRaises(ValueError, P('').with_suffix, '.gz') |         self.assertRaises(ValueError, P('').with_suffix, '.gz') | ||||||
|         self.assertRaises(ValueError, P('.').with_suffix, '.gz') |         self.assertRaises(ValueError, P('.').with_suffix, '.gz') | ||||||
|  | @ -558,9 +561,12 @@ def test_with_suffix_common(self): | ||||||
|         # Invalid suffix |         # Invalid suffix | ||||||
|         self.assertRaises(ValueError, P('a/b').with_suffix, 'gz') |         self.assertRaises(ValueError, P('a/b').with_suffix, 'gz') | ||||||
|         self.assertRaises(ValueError, P('a/b').with_suffix, '/') |         self.assertRaises(ValueError, P('a/b').with_suffix, '/') | ||||||
|  |         self.assertRaises(ValueError, P('a/b').with_suffix, '.') | ||||||
|         self.assertRaises(ValueError, P('a/b').with_suffix, '/.gz') |         self.assertRaises(ValueError, P('a/b').with_suffix, '/.gz') | ||||||
|         self.assertRaises(ValueError, P('a/b').with_suffix, 'c/d') |         self.assertRaises(ValueError, P('a/b').with_suffix, 'c/d') | ||||||
|         self.assertRaises(ValueError, P('a/b').with_suffix, '.c/.d') |         self.assertRaises(ValueError, P('a/b').with_suffix, '.c/.d') | ||||||
|  |         self.assertRaises(ValueError, P('a/b').with_suffix, './.d') | ||||||
|  |         self.assertRaises(ValueError, P('a/b').with_suffix, '.d/.') | ||||||
| 
 | 
 | ||||||
|     def test_relative_to_common(self): |     def test_relative_to_common(self): | ||||||
|         P = self.cls |         P = self.cls | ||||||
|  |  | ||||||
|  | @ -27,6 +27,9 @@ Core and Builtins | ||||||
| Library | Library | ||||||
| ------- | ------- | ||||||
| 
 | 
 | ||||||
|  | - Issue #20639: calling Path.with_suffix('') allows removing the suffix | ||||||
|  |   again.  Patch by July Tikhonov. | ||||||
|  | 
 | ||||||
| - Issue #21714: Disallow the construction of invalid paths using | - Issue #21714: Disallow the construction of invalid paths using | ||||||
|   Path.with_name().  Original patch by Antony Lee. |   Path.with_name().  Original patch by Antony Lee. | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Antoine Pitrou
						Antoine Pitrou