Visitar URL original
bpo-41109: subclasses of pathlib.Path and pathlib.PurePath now call the subclass's __init__() and __new__() functions when returning new objects by websurfer5 · Pull Request #21920 · python/cpython · GitHub
Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
081cbc2
bpo-41109: separate object creation by Path and PurePath member varia…
websurfer5 Aug 16, 2020
0e4d53e
bpo-41109: Windows PurePath tests
websurfer5 Aug 17, 2020
53643f4
bpo-41109: use correct class in PureWindowsSubclassNewAndInitTest
websurfer5 Aug 17, 2020
c418958
bpo-41109: fix comment
websurfer5 Aug 17, 2020
34683c4
bpo-41109: fix class names and comments
websurfer5 Aug 17, 2020
19e0a40
bpo-41109: WindowsPath test suite
websurfer5 Aug 17, 2020
1853c6d
bpo-41109: add __init__() to Path and PurePath
websurfer5 Aug 17, 2020
17c141c
bpo-41109: remove extraneous **kwargs parameter from Path.__init__()
websurfer5 Aug 17, 2020
505cab0
bpo-41109: remove extraneous whitespace
websurfer5 Aug 17, 2020
c47bf06
bpo-41109: remove double-initialization in Path.__new__()
websurfer5 Aug 18, 2020
6607e77
bpo-41109: no longer need to call _init() from PurePath.__init__()
websurfer5 Aug 18, 2020
cf6afb0
bpo-41109: reorganize tests using a common base class for common tests
websurfer5 Aug 18, 2020
d25fdd7
bpo-41109: fix readlink test for Windows
websurfer5 Aug 18, 2020
4be7b7a
bpo-41109: cleanup test_resolve
websurfer5 Aug 18, 2020
1660fa6
bpo-41109: cleanup test_glob
websurfer5 Aug 18, 2020
2a27016
bpo-41109: test all initialization cases
websurfer5 Aug 18, 2020
4014a16
bpo-41109: news blurb
websurfer5 Aug 19, 2020
31b449e
bpo-41109: What's New entry and better news blurb
websurfer5 Aug 19, 2020
ebc8371
bpo-4119: Merge branch 'master' of github.com:python/cpython into fix…
websurfer5 Aug 19, 2020
86bb25b
bpo-41109: improve What's New and News entries
websurfer5 Aug 19, 2020
b2c62a6
bpo-41109: simplify _new_from_parts() and _new_from_parsed_parts() in…
websurfer5 Aug 19, 2020
6f363b9
Merge branch 'master' of github.com:python/cpython into fix-issue-41109
websurfer5 Aug 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bpo-41109: reorganize tests using a common base class for common tests
  • Loading branch information
websurfer5 committed Aug 18, 2020
commit cf6afb073b6b5691838167ce9e3dbf8fbb32e996
190 changes: 83 additions & 107 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2707,8 +2707,88 @@ def test_with_suffix(self):
'c:\\a\\b\\c.bar')


class _BasePathSubclassNewAndInitTest(object):
"""
Test that the __init__() and __new__() functions of subclasses
of Path get called when Path functions
and properties instantiate new objects of the subclass.
"""
def setUp(self):
def cleanup():
os_helper.rmtree(BASE)
self.addCleanup(cleanup)
os.mkdir(BASE)
with open(join(BASE, 'fileA'), 'wb') as f:
f.write(b"this is file A\n")

def validate_object(self, path, value):
self.assertIs(type(path), self.ASubclass)
self.assertTrue(path.new_called)
self.assertTrue(path.init_called)
self.assertEqual(str(path), value)

def test_glob(self):
d = BASE
filename = 'fileA'
path = self.ASubclass(d)
self.validate_object(path, d)
cases = [(path.glob, 'f*', join(d, filename)), # _WildcardSelector
(path.glob, 'fileA', join(d, filename)), # _PreciseSelector
(path.glob, '**', d)] # _RecursiveWildcardSelector

for func, param, value in cases:
n = 0
for name in func(param):
self.validate_object(name, value)
n += 1
self.assertEqual(n, 1)

def test_rglob(self):
d = BASE
filename = 'fileA'
path = self.ASubclass(d)
self.validate_object(path, d)
n = 0
for name in path.rglob('f*'):
self.validate_object(name, join(d, filename))
n += 1
self.assertEqual(n, 1)

def test_iterdir(self):
d = BASE
filename = 'fileA'
path = self.ASubclass(d)
self.validate_object(path, d)
n = 0
for name in path.iterdir():
self.validate_object(name, join(d, filename))
n += 1
self.assertEqual(n, 1)

@os_helper.skip_unless_symlink
def test_readlink(self):
d = BASE
filename = 'fileA'
linkname = 'linkA'

# create a symlink to the file
os.symlink(join(d, filename), join(d, linkname))

path = (self.ASubclass(d) / linkname).readlink()
self.validate_object(path, join(d, filename))
os.unlink(path)

def test_resolve(self):
# create a file in a temp directory
d = BASE
filename = 'fileA'
self.validate_object(self.ASubclass(d, filename).resolve(strict=True),
join(d, filename))


@only_posix
class PosixPathSubclassNewAndInitTest(unittest.TestCase):
class PosixPathSubclassNewAndInitTest(_BasePathSubclassNewAndInitTest,
unittest.TestCase):
"""
Test that the __init__() and __new__() functions of subclasses
of PosixPath get called when PosixPath functions
Expand All @@ -2728,12 +2808,6 @@ def __init__(self, *args, **kwargs):
self.init_called = True
super().__init__(*args, **kwargs)

def validate_object(self, path, value):
self.assertIs(type(path), self.ASubclass)
self.assertTrue(path.new_called)
self.assertTrue(path.init_called)
self.assertEqual(str(path), value)

def test_class_initialization(self):
self.validate_object(self.ASubclass('a/b/c.foo'), 'a/b/c.foo')

Expand All @@ -2752,53 +2826,10 @@ def test_expanduser(self):
env.pop('HOME', None)
self.validate_object(path.expanduser(), join(userhome, 'Documents'))

def test_glob_rglob_iterdir(self):
# create a file in a temp directory
d = tempfile.mkdtemp()
filename = 'fileA'
with open(join(d, filename), 'wb') as f:
f.write(b"this is file A\n")

# create an Asubclass object for testing
path = self.ASubclass(d)
self.validate_object(path, d)

# verify __new__ and __init__ are called
# in the subclass for each created instance
# and the subclass value is correct
cases = [(path.glob, 'f*', join(d, filename)), # _WildcardSelector
(path.glob, 'fileA', join(d, filename)), # _PreciseSelector
(path.glob, '**', d), # _RecursiveWildcardSelector
(path.rglob, 'f*', join(d, filename)),
(path.iterdir, None, join(d, filename))]

for func, param, value in cases:
n = 0
for name in func() if param is None else func(param):
self.validate_object(name, value)
n += 1
self.assertEqual(n, 1)

def test_resolve(self):
relpath = 'a/b/c.foo'
self.validate_object(self.ASubclass(relpath).resolve(),
join(os.getcwd(), relpath))

@os_helper.skip_unless_symlink
def test_readlink(self):
# create a file in a temp directory
d = tempfile.mkdtemp()
with open(join(d, 'fileA'), 'wb') as f:
f.write(b"this is file A\n")
# create a symlink to the file
os.symlink(join(d, 'fileA'), join(d, 'linkA'))

path = (self.ASubclass(d) / 'linkA').readlink()
self.validate_object(path, join(d, 'fileA'))


@only_nt
class WindowsPathSubclassNewAndInitTest(unittest.TestCase):
class WindowsPathSubclassNewAndInitTest(_BasePathSubclassNewAndInitTest,
unittest.TestCase):
"""
Test that the __init__() and __new__() functions of subclasses
of WindowsPath get called when WindowsPath functions
Expand All @@ -2818,12 +2849,6 @@ def __init__(self, *args, **kwargs):
self.init_called = True
super().__init__(*args, **kwargs)

def validate_object(self, path, value):
self.assertIs(type(path), self.ASubclass)
self.assertTrue(path.new_called)
self.assertTrue(path.init_called)
self.assertEqual(str(path), value)

def test_class_initialization(self):
self.validate_object(self.ASubclass('c:\\a\\b\\c.foo'), 'c:\\a\\b\\c.foo')

Expand All @@ -2842,55 +2867,6 @@ def test_expanduser(self):
env.pop('HOME', None)
self.validate_object(path.expanduser(), join(userhome, 'Documents'))

def test_glob_rglob_iterdir(self):
# create a file in a temp directory
d = tempfile.mkdtemp()
filename = 'fileA'
with open(join(d, filename), 'wb') as f:
f.write(b"this is file A\n")

# create an Asubclass object for testing
path = self.ASubclass(d)
self.validate_object(path, d)

# verify __new__ and __init__ are called
# in the subclass for each created instance
# and the subclass value is correct
cases = [(path.glob, 'f*', join(d, filename)), # _WildcardSelector
(path.glob, 'fileA', join(d, filename)), # _PreciseSelector
(path.glob, '**', d), # _RecursiveWildcardSelector
(path.rglob, 'f*', join(d, filename)),
(path.iterdir, None, join(d, filename))]

for func, param, value in cases:
n = 0
for name in func() if param is None else func(param):
self.validate_object(name, value)
n += 1
self.assertEqual(n, 1)

def test_resolve(self):
# create a file in a temp directory
d = tempfile.mkdtemp()
filename = 'fileA'
with open(join(d, filename), 'wb') as f:
f.write(b"this is file A\n")

self.validate_object(self.ASubclass(d, filename).resolve(strict=True),
join(d, filename))

@os_helper.skip_unless_symlink
def test_readlink(self):
# create a file in a temp directory
d = tempfile.mkdtemp()
with open(join(d, 'fileA'), 'wb') as f:
f.write(b"this is file A\n")
# create a symlink to the file
os.symlink('fileA', join(d, 'linkA'))

path = self.ASubclass(d, 'linkA').readlink()
self.validate_object(path, 'fileA')


if __name__ == "__main__":
unittest.main()