Skip to content

Commit 3094398

Browse files
committed
Remove flavour.join(), flavour.sep and flavour.altsep
1 parent 0faa6d0 commit 3094398

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

Lib/pathlib.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,10 @@ class _Flavour(object):
5050
"""A flavour implements a particular (platform-specific) set of path
5151
semantics."""
5252

53-
def __init__(self):
54-
self.join = self.sep.join
55-
5653
def parse_parts(self, parts):
5754
parsed = []
58-
sep = self.sep
59-
altsep = self.altsep
55+
sep = self.pathmod.sep
56+
altsep = self.pathmod.altsep
6057
drv = root = ''
6158
it = reversed(parts)
6259
for part in it:
@@ -113,8 +110,6 @@ class _WindowsFlavour(_Flavour):
113110
# Reference for Windows paths can be found at
114111
# http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx
115112

116-
sep = '\\'
117-
altsep = '/'
118113
has_drv = True
119114
pathmod = ntpath
120115

@@ -129,7 +124,8 @@ class _WindowsFlavour(_Flavour):
129124
# - extended paths are always absolute; "relative" extended paths will
130125
# fail.
131126

132-
def splitroot(self, part, sep=sep):
127+
def splitroot(self, part):
128+
sep = self.pathmod.sep
133129
first = part[0:1]
134130
second = part[1:2]
135131
if (second == sep and first == sep):
@@ -189,14 +185,13 @@ def _split_extended_path(self, s, ext_prefix=ext_namespace_prefix):
189185

190186

191187
class _PosixFlavour(_Flavour):
192-
sep = '/'
193-
altsep = ''
194188
has_drv = False
195189
pathmod = posixpath
196190

197191
is_supported = (os.name != 'nt')
198192

199-
def splitroot(self, part, sep=sep):
193+
def splitroot(self, part):
194+
sep = self.pathmod.sep
200195
if part and part[0] == sep:
201196
stripped_part = part.lstrip(sep)
202197
# According to POSIX path resolution:
@@ -557,9 +552,9 @@ def _from_parsed_parts(cls, drv, root, parts):
557552
@classmethod
558553
def _format_parsed_parts(cls, drv, root, parts):
559554
if drv or root:
560-
return drv + root + cls._flavour.join(parts[1:])
555+
return drv + root + cls._flavour.pathmod.sep.join(parts[1:])
561556
else:
562-
return cls._flavour.join(parts)
557+
return cls._flavour.pathmod.sep.join(parts)
563558

564559
def _make_child(self, args):
565560
drv, root, parts = self._parse_args(args)
@@ -584,7 +579,7 @@ def as_posix(self):
584579
"""Return the string representation of the path with forward (/)
585580
slashes."""
586581
f = self._flavour
587-
return str(self).replace(f.sep, '/')
582+
return str(self).replace(f.pathmod.sep, '/')
588583

589584
def __bytes__(self):
590585
"""Return the bytes representation of the path. This is only
@@ -704,7 +699,8 @@ def with_name(self, name):
704699
if not self.name:
705700
raise ValueError("%r has an empty name" % (self,))
706701
drv, root, parts = self._flavour.parse_parts((name,))
707-
if (not name or name[-1] in [self._flavour.sep, self._flavour.altsep]
702+
m = self._flavour.pathmod
703+
if (not name or name[-1] in [m.sep, m.altsep]
708704
or drv or root or len(parts) != 1):
709705
raise ValueError("Invalid name %r" % (name))
710706
return self._from_parsed_parts(self._drv, self._root,
@@ -719,8 +715,8 @@ def with_suffix(self, suffix):
719715
has no suffix, add given suffix. If the given suffix is an empty
720716
string, remove the suffix from the path.
721717
"""
722-
f = self._flavour
723-
if f.sep in suffix or f.altsep and f.altsep in suffix:
718+
m = self._flavour.pathmod
719+
if m.sep in suffix or m.altsep and m.altsep in suffix:
724720
raise ValueError("Invalid suffix %r" % (suffix,))
725721
if suffix and not suffix.startswith('.') or suffix == '.':
726722
raise ValueError("Invalid suffix %r" % (suffix))

Lib/test/test_pathlib.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class _BaseFlavourTest(object):
2525

2626
def _check_parse_parts(self, arg, expected):
2727
f = self.flavour.parse_parts
28-
sep = self.flavour.sep
29-
altsep = self.flavour.altsep
28+
sep = self.flavour.pathmod.sep
29+
altsep = self.flavour.pathmod.altsep
3030
actual = f([x.replace('/', sep) for x in arg])
3131
self.assertEqual(actual, expected)
3232
if altsep:
@@ -35,7 +35,7 @@ def _check_parse_parts(self, arg, expected):
3535

3636
def test_parse_parts_common(self):
3737
check = self._check_parse_parts
38-
sep = self.flavour.sep
38+
sep = self.flavour.pathmod.sep
3939
# Unanchored parts.
4040
check([], ('', '', []))
4141
check(['a'], ('', '', ['a']))
@@ -186,8 +186,8 @@ class _BasePurePathTest(object):
186186
def setUp(self):
187187
p = self.cls('a')
188188
self.flavour = p._flavour
189-
self.sep = self.flavour.sep
190-
self.altsep = self.flavour.altsep
189+
self.sep = self.flavour.pathmod.sep
190+
self.altsep = self.flavour.pathmod.altsep
191191

192192
def test_constructor_common(self):
193193
P = self.cls
@@ -615,7 +615,7 @@ def test_with_suffix_common(self):
615615
self.assertRaises(ValueError, P('a/b').with_suffix, './.d')
616616
self.assertRaises(ValueError, P('a/b').with_suffix, '.d/.')
617617
self.assertRaises(ValueError, P('a/b').with_suffix,
618-
(self.flavour.sep, 'd'))
618+
(self.sep, 'd'))
619619

620620
def test_relative_to_common(self):
621621
P = self.cls

0 commit comments

Comments
 (0)