mirror of
https://github.com/python/cpython.git
synced 2025-12-09 02:35:14 +00:00
GH-100479: Add pathlib.PurePath.with_segments() (GH-103975)
Add `pathlib.PurePath.with_segments()`, which creates a path object from arguments. This method is called whenever a derivative path is created, such as from `pathlib.PurePath.parent`. Subclasses may override this method to share information between path objects. Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
1afe0e0320
commit
d00d942149
5 changed files with 108 additions and 47 deletions
|
|
@ -29,11 +29,12 @@ except ImportError:
|
|||
#
|
||||
|
||||
class _BasePurePathSubclass(object):
|
||||
init_called = False
|
||||
def __init__(self, *pathsegments, session_id):
|
||||
super().__init__(*pathsegments)
|
||||
self.session_id = session_id
|
||||
|
||||
def __init__(self, *args):
|
||||
super().__init__(*args)
|
||||
self.init_called = True
|
||||
def with_segments(self, *pathsegments):
|
||||
return type(self)(*pathsegments, session_id=self.session_id)
|
||||
|
||||
|
||||
class _BasePurePathTest(object):
|
||||
|
|
@ -121,20 +122,21 @@ class _BasePurePathTest(object):
|
|||
self._check_str_subclass('a/b.txt')
|
||||
self._check_str_subclass('/a/b.txt')
|
||||
|
||||
def test_init_called_common(self):
|
||||
def test_with_segments_common(self):
|
||||
class P(_BasePurePathSubclass, self.cls):
|
||||
pass
|
||||
p = P('foo', 'bar')
|
||||
self.assertTrue((p / 'foo').init_called)
|
||||
self.assertTrue(('foo' / p).init_called)
|
||||
self.assertTrue(p.joinpath('foo').init_called)
|
||||
self.assertTrue(p.with_name('foo').init_called)
|
||||
self.assertTrue(p.with_stem('foo').init_called)
|
||||
self.assertTrue(p.with_suffix('.foo').init_called)
|
||||
self.assertTrue(p.relative_to('foo').init_called)
|
||||
self.assertTrue(p.parent.init_called)
|
||||
p = P('foo', 'bar', session_id=42)
|
||||
self.assertEqual(42, (p / 'foo').session_id)
|
||||
self.assertEqual(42, ('foo' / p).session_id)
|
||||
self.assertEqual(42, p.joinpath('foo').session_id)
|
||||
self.assertEqual(42, p.with_name('foo').session_id)
|
||||
self.assertEqual(42, p.with_stem('foo').session_id)
|
||||
self.assertEqual(42, p.with_suffix('.foo').session_id)
|
||||
self.assertEqual(42, p.with_segments('foo').session_id)
|
||||
self.assertEqual(42, p.relative_to('foo').session_id)
|
||||
self.assertEqual(42, p.parent.session_id)
|
||||
for parent in p.parents:
|
||||
self.assertTrue(parent.init_called)
|
||||
self.assertEqual(42, parent.session_id)
|
||||
|
||||
def _get_drive_root_parts(self, parts):
|
||||
path = self.cls(*parts)
|
||||
|
|
@ -1647,6 +1649,26 @@ class _BasePathTest(object):
|
|||
env['HOME'] = os.path.join(BASE, 'home')
|
||||
self._test_home(self.cls.home())
|
||||
|
||||
def test_with_segments(self):
|
||||
class P(_BasePurePathSubclass, self.cls):
|
||||
pass
|
||||
p = P(BASE, session_id=42)
|
||||
self.assertEqual(42, p.absolute().session_id)
|
||||
self.assertEqual(42, p.resolve().session_id)
|
||||
self.assertEqual(42, p.with_segments('~').expanduser().session_id)
|
||||
self.assertEqual(42, (p / 'fileA').rename(p / 'fileB').session_id)
|
||||
self.assertEqual(42, (p / 'fileB').replace(p / 'fileA').session_id)
|
||||
if os_helper.can_symlink():
|
||||
self.assertEqual(42, (p / 'linkA').readlink().session_id)
|
||||
for path in p.iterdir():
|
||||
self.assertEqual(42, path.session_id)
|
||||
for path in p.glob('*'):
|
||||
self.assertEqual(42, path.session_id)
|
||||
for path in p.rglob('*'):
|
||||
self.assertEqual(42, path.session_id)
|
||||
for dirpath, dirnames, filenames in p.walk():
|
||||
self.assertEqual(42, dirpath.session_id)
|
||||
|
||||
def test_samefile(self):
|
||||
fileA_path = os.path.join(BASE, 'fileA')
|
||||
fileB_path = os.path.join(BASE, 'dirB', 'fileB')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue