Skip to content

Commit b67acdb

Browse files
Improve ALL_BUT_LAST.
1 parent db9e13b commit b67acdb

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

Lib/genericpath.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
__all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
1010
'getsize', 'isdevdrive', 'isdir', 'isfile', 'isjunction', 'islink',
11-
'lexists', 'samefile', 'sameopenfile', 'samestat']
11+
'lexists', 'samefile', 'sameopenfile', 'samestat', 'ALL_BUT_LAST']
1212

1313

1414
# Does a path exist?
@@ -189,3 +189,13 @@ def _check_arg_types(funcname, *args):
189189
f'os.PathLike object, not {s.__class__.__name__!r}') from None
190190
if hasstr and hasbytes:
191191
raise TypeError("Can't mix strings and bytes in path components") from None
192+
193+
194+
# A singleton with a true boolean value.
195+
@object.__new__
196+
class ALL_BUT_LAST:
197+
"""Special value for use in realpath()."""
198+
def __repr__(self):
199+
return self.__class__.__name__
200+
def __reduce__(self):
201+
return self.__class__.__name__

Lib/ntpath.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"abspath","curdir","pardir","sep","pathsep","defpath","altsep",
3030
"extsep","devnull","realpath","supports_unicode_filenames","relpath",
3131
"samefile", "sameopenfile", "samestat", "commonpath", "isjunction",
32-
"isdevdrive"]
32+
"isdevdrive", 'ALL_BUT_LAST']
3333

3434
def _get_bothseps(path):
3535
if isinstance(path, bytes):
@@ -597,9 +597,6 @@ def abspath(path):
597597
path = join(getcwd(), path)
598598
return normpath(path)
599599

600-
# A singleton with true boolean value
601-
ALL_BUT_LAST = ['ALL_BUT_LAST']
602-
603600
try:
604601
from nt import _findfirstfile, _getfinalpathname, readlink as _nt_readlink
605602
except ImportError:

Lib/posixpath.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"samefile","sameopenfile","samestat",
3737
"curdir","pardir","sep","pathsep","defpath","altsep","extsep",
3838
"devnull","realpath","supports_unicode_filenames","relpath",
39-
"commonpath", "isjunction","isdevdrive"]
39+
"commonpath", "isjunction","isdevdrive", 'ALL_BUT_LAST']
4040

4141

4242
def _get_sep(path):
@@ -388,9 +388,6 @@ def abspath(path):
388388
# Return a canonical path (i.e. the absolute location of a file on the
389389
# filesystem).
390390

391-
# A singleton with true boolean value
392-
ALL_BUT_LAST = ['ALL_BUT_LAST']
393-
394391
def realpath(filename, *, strict=False):
395392
"""Return the canonical path of the specified filename, eliminating any
396393
symbolic links encountered in the path."""

Lib/test/test_genericpath.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,20 @@ def test_sameopenfile(self):
320320
fd2 = fp2.fileno()
321321
self.assertTrue(self.pathmodule.sameopenfile(fd1, fd2))
322322

323+
def test_all_but_last(self):
324+
ALL_BUT_LAST = self.pathmodule.ALL_BUT_LAST
325+
self.assertEqual(repr(ALL_BUT_LAST), 'ALL_BUT_LAST')
326+
self.assertTrue(ALL_BUT_LAST)
327+
import copy
328+
self.assertIs(copy.copy(ALL_BUT_LAST), ALL_BUT_LAST)
329+
self.assertIs(copy.deepcopy(ALL_BUT_LAST), ALL_BUT_LAST)
330+
import pickle
331+
for proto in range(pickle.HIGHEST_PROTOCOL+1):
332+
with self.subTest(protocol=proto):
333+
pickled = pickle.dumps(ALL_BUT_LAST, proto)
334+
unpickled = pickle.loads(pickled)
335+
self.assertIs(unpickled, ALL_BUT_LAST)
336+
323337

324338
class TestGenericTest(GenericTest, unittest.TestCase):
325339
# Issue 16852: GenericTest can't inherit from unittest.TestCase

0 commit comments

Comments
 (0)