112
112
from pyfakefs .helpers import (
113
113
FakeStatResult , FileBufferIO , NullFileBufferIO ,
114
114
is_int_type , is_byte_string , is_unicode_string ,
115
- make_string_path , IS_WIN , to_string )
115
+ make_string_path , IS_WIN , to_string , matching_string
116
+ )
116
117
from pyfakefs import __version__ # noqa: F401 for upwards compatibility
117
118
118
119
__pychecker__ = 'no-reimportself'
@@ -958,24 +959,13 @@ def raise_os_error(self, errno, filename=None, winerror=None):
958
959
raise OSError (errno , message , filename , winerror )
959
960
raise OSError (errno , message , filename )
960
961
961
- @staticmethod
962
- def _matching_string (matched , string ):
963
- """Return the string as byte or unicode depending
964
- on the type of matched, assuming string is an ASCII string.
965
- """
966
- if string is None :
967
- return string
968
- if isinstance (matched , bytes ) and isinstance (string , str ):
969
- return string .encode (locale .getpreferredencoding (False ))
970
- return string
971
-
972
962
def _path_separator (self , path ):
973
963
"""Return the path separator as the same type as path"""
974
- return self . _matching_string (path , self .path_separator )
964
+ return matching_string (path , self .path_separator )
975
965
976
966
def _alternative_path_separator (self , path ):
977
967
"""Return the alternative path separator as the same type as path"""
978
- return self . _matching_string (path , self .alternative_path_separator )
968
+ return matching_string (path , self .alternative_path_separator )
979
969
980
970
def _starts_with_sep (self , path ):
981
971
"""Return True if path starts with a path separator."""
@@ -1035,10 +1025,10 @@ def to_str(string):
1035
1025
path = self .absnormpath (self ._original_path (path ))
1036
1026
if path in self .mount_points :
1037
1027
return self .mount_points [path ]
1038
- mount_path = self . _matching_string (path , '' )
1028
+ mount_path = matching_string (path , '' )
1039
1029
drive = self .splitdrive (path )[:1 ]
1040
1030
for root_path in self .mount_points :
1041
- root_path = self . _matching_string (path , root_path )
1031
+ root_path = matching_string (path , root_path )
1042
1032
if drive and not root_path .startswith (drive ):
1043
1033
continue
1044
1034
if path .startswith (root_path ) and len (root_path ) > len (mount_path ):
@@ -1377,8 +1367,8 @@ def normpath(self, path):
1377
1367
is_absolute_path = path .startswith (sep )
1378
1368
path_components = path .split (sep )
1379
1369
collapsed_path_components = []
1380
- dot = self . _matching_string (path , '.' )
1381
- dotdot = self . _matching_string (path , '..' )
1370
+ dot = matching_string (path , '.' )
1371
+ dotdot = matching_string (path , '..' )
1382
1372
for component in path_components :
1383
1373
if (not component ) or (component == dot ):
1384
1374
continue
@@ -1453,18 +1443,18 @@ def absnormpath(self, path):
1453
1443
or the root directory if path is empty.
1454
1444
"""
1455
1445
path = self .normcase (path )
1456
- cwd = self . _matching_string (path , self .cwd )
1446
+ cwd = matching_string (path , self .cwd )
1457
1447
if not path :
1458
1448
path = self .path_separator
1459
- if path == self . _matching_string (path , '.' ):
1449
+ if path == matching_string (path , '.' ):
1460
1450
path = cwd
1461
1451
elif not self ._starts_with_root_path (path ):
1462
1452
# Prefix relative paths with cwd, if cwd is not root.
1463
- root_name = self . _matching_string (path , self .root .name )
1464
- empty = self . _matching_string (path , '' )
1453
+ root_name = matching_string (path , self .root .name )
1454
+ empty = matching_string (path , '' )
1465
1455
path = self ._path_separator (path ).join (
1466
1456
(cwd != root_name and cwd or empty , path ))
1467
- if path == self . _matching_string (path , '.' ):
1457
+ if path == matching_string (path , '.' ):
1468
1458
path = cwd
1469
1459
return self .normpath (path )
1470
1460
@@ -1489,7 +1479,7 @@ def splitpath(self, path):
1489
1479
1490
1480
starts_with_drive = self ._starts_with_drive_letter (path )
1491
1481
basename = path_components .pop ()
1492
- colon = self . _matching_string (path , ':' )
1482
+ colon = matching_string (path , ':' )
1493
1483
if not path_components :
1494
1484
if starts_with_drive :
1495
1485
components = basename .split (colon )
@@ -1545,7 +1535,7 @@ def splitdrive(self, path):
1545
1535
if sep_index2 == - 1 :
1546
1536
sep_index2 = len (path )
1547
1537
return path [:sep_index2 ], path [sep_index2 :]
1548
- if path [1 :2 ] == self . _matching_string (path , ':' ):
1538
+ if path [1 :2 ] == matching_string (path , ':' ):
1549
1539
return path [:2 ], path [2 :]
1550
1540
return path [:0 ], path
1551
1541
@@ -1579,7 +1569,7 @@ def _join_paths_with_drive_support(self, *all_paths):
1579
1569
result_path = result_path + sep
1580
1570
result_path = result_path + path_part
1581
1571
# add separator between UNC and non-absolute path
1582
- colon = self . _matching_string (base_path , ':' )
1572
+ colon = matching_string (base_path , ':' )
1583
1573
if (result_path and result_path [:1 ] not in seps and
1584
1574
result_drive and result_drive [- 1 :] != colon ):
1585
1575
return result_drive + sep + result_path
@@ -1613,7 +1603,7 @@ def joinpaths(self, *paths):
1613
1603
joined_path_segments .append (sep )
1614
1604
if path_segment :
1615
1605
joined_path_segments .append (path_segment )
1616
- return self . _matching_string (paths [0 ], '' ).join (joined_path_segments )
1606
+ return matching_string (paths [0 ], '' ).join (joined_path_segments )
1617
1607
1618
1608
def _path_components (self , path ):
1619
1609
"""Breaks the path into a list of component names.
@@ -1664,20 +1654,20 @@ def _starts_with_drive_letter(self, file_path):
1664
1654
`True` if drive letter support is enabled in the filesystem and
1665
1655
the path starts with a drive letter.
1666
1656
"""
1667
- colon = self . _matching_string (file_path , ':' )
1657
+ colon = matching_string (file_path , ':' )
1668
1658
return (self .is_windows_fs and len (file_path ) >= 2 and
1669
1659
file_path [:1 ].isalpha and (file_path [1 :2 ]) == colon )
1670
1660
1671
1661
def _starts_with_root_path (self , file_path ):
1672
- root_name = self . _matching_string (file_path , self .root .name )
1662
+ root_name = matching_string (file_path , self .root .name )
1673
1663
file_path = self ._normalize_path_sep (file_path )
1674
1664
return (file_path .startswith (root_name ) or
1675
1665
not self .is_case_sensitive and file_path .lower ().startswith (
1676
1666
root_name .lower ()) or
1677
1667
self ._starts_with_drive_letter (file_path ))
1678
1668
1679
1669
def _is_root_path (self , file_path ):
1680
- root_name = self . _matching_string (file_path , self .root .name )
1670
+ root_name = matching_string (file_path , self .root .name )
1681
1671
return (file_path == root_name or not self .is_case_sensitive and
1682
1672
file_path .lower () == root_name .lower () or
1683
1673
2 <= len (file_path ) <= 3 and
@@ -1860,7 +1850,7 @@ def _resolve_components(self, path_components, raw_io):
1860
1850
def _valid_relative_path (self , file_path ):
1861
1851
if self .is_windows_fs :
1862
1852
return True
1863
- slash_dotdot = self . _matching_string (
1853
+ slash_dotdot = matching_string (
1864
1854
file_path , self .path_separator + '..' )
1865
1855
while file_path and slash_dotdot in file_path :
1866
1856
file_path = file_path [:file_path .rfind (slash_dotdot )]
@@ -2026,7 +2016,7 @@ def lresolve(self, path):
2026
2016
2027
2017
# remove trailing separator
2028
2018
path = self ._path_without_trailing_separators (path )
2029
- if path == self . _matching_string (path , '.' ):
2019
+ if path == matching_string (path , '.' ):
2030
2020
path = self .cwd
2031
2021
path = self ._original_path (path )
2032
2022
@@ -2260,8 +2250,8 @@ def remove_object(self, file_path):
2260
2250
2261
2251
def make_string_path (self , path ):
2262
2252
path = make_string_path (path )
2263
- os_sep = self . _matching_string (path , os .sep )
2264
- fake_sep = self . _matching_string (path , self .path_separator )
2253
+ os_sep = matching_string (path , os .sep )
2254
+ fake_sep = matching_string (path , self .path_separator )
2265
2255
return path .replace (os_sep , fake_sep )
2266
2256
2267
2257
def create_dir (self , directory_path , perm_bits = PERM_DEF ):
@@ -2756,8 +2746,7 @@ def makedir(self, dir_name, mode=PERM_DEF):
2756
2746
parent_dir , _ = self .splitpath (dir_name )
2757
2747
if parent_dir :
2758
2748
base_dir = self .normpath (parent_dir )
2759
- ellipsis = self ._matching_string (
2760
- parent_dir , self .path_separator + '..' )
2749
+ ellipsis = matching_string (parent_dir , self .path_separator + '..' )
2761
2750
if parent_dir .endswith (ellipsis ) and not self .is_windows_fs :
2762
2751
base_dir , dummy_dotdot , _ = parent_dir .partition (ellipsis )
2763
2752
if not self .exists (base_dir ):
@@ -3333,8 +3322,8 @@ def _joinrealpath(self, path, rest, seen):
3333
3322
encountered in the second path.
3334
3323
Taken from Python source and adapted.
3335
3324
"""
3336
- curdir = self . filesystem . _matching_string (path , '.' )
3337
- pardir = self . filesystem . _matching_string (path , '..' )
3325
+ curdir = matching_string (path , '.' )
3326
+ pardir = matching_string (path , '..' )
3338
3327
3339
3328
sep = self .filesystem ._path_separator (path )
3340
3329
if self .isabs (rest ):
@@ -3385,8 +3374,10 @@ def expanduser(self, path):
3385
3374
"""Return the argument with an initial component of ~ or ~user
3386
3375
replaced by that user's home directory.
3387
3376
"""
3388
- return self ._os_path .expanduser (path ).replace (
3389
- self ._os_path .sep , self .sep )
3377
+ path = self ._os_path .expanduser (path )
3378
+ return path .replace (
3379
+ matching_string (path , self ._os_path .sep ),
3380
+ matching_string (path , self .sep ))
3390
3381
3391
3382
def ismount (self , path ):
3392
3383
"""Return true if the given path is a mount point.
0 commit comments