@@ -756,6 +756,7 @@ def to_filehandle(fname, flag='rU', return_opened=False):
756
756
files is automatic, if the filename ends in .gz. *flag* is a
757
757
read/write flag for :func:`file`
758
758
"""
759
+ fname = fspath_no_except (fname )
759
760
if is_string_like (fname ):
760
761
if fname .endswith ('.gz' ):
761
762
# get rid of 'U' in flag for gzipped files.
@@ -815,6 +816,7 @@ def get_sample_data(fname, asfileobj=True):
815
816
root = matplotlib .rcParams ['examples.directory' ]
816
817
else :
817
818
root = os .path .join (matplotlib ._get_data_path (), 'sample_data' )
819
+ fname = fspath_no_except (fname )
818
820
path = os .path .join (root , fname )
819
821
820
822
if asfileobj :
@@ -1019,6 +1021,7 @@ def __init__(self):
1019
1021
self ._cache = {}
1020
1022
1021
1023
def __call__ (self , path ):
1024
+ path = fspath_no_except (path )
1022
1025
result = self ._cache .get (path )
1023
1026
if result is None :
1024
1027
realpath = os .path .realpath (path )
@@ -1173,7 +1176,7 @@ def listFiles(root, patterns='*', recurse=1, return_folders=0):
1173
1176
pattern_list = patterns .split (';' )
1174
1177
results = []
1175
1178
1176
- for dirname , dirs , files in os .walk (root ):
1179
+ for dirname , dirs , files in os .walk (fspath_no_except ( root ) ):
1177
1180
# Append to results all relevant files (and perhaps folders)
1178
1181
for name in files :
1179
1182
fullname = os .path .normpath (os .path .join (dirname , name ))
@@ -1197,10 +1200,10 @@ def get_recursive_filelist(args):
1197
1200
files = []
1198
1201
1199
1202
for arg in args :
1200
- if os .path .isfile (arg ):
1203
+ if os .path .isfile (fspath_no_except ( arg ) ):
1201
1204
files .append (arg )
1202
1205
continue
1203
- if os .path .isdir (arg ):
1206
+ if os .path .isdir (fspath_no_except ( arg ) ):
1204
1207
newfiles = listFiles (arg , recurse = 1 , return_folders = 1 )
1205
1208
files .extend (newfiles )
1206
1209
@@ -1680,6 +1683,7 @@ def simple_linear_interpolation(a, steps):
1680
1683
1681
1684
1682
1685
def recursive_remove (path ):
1686
+ path = fspath_no_except (path )
1683
1687
if os .path .isdir (path ):
1684
1688
for fname in (glob .glob (os .path .join (path , '*' )) +
1685
1689
glob .glob (os .path .join (path , '.*' ))):
@@ -2599,7 +2603,7 @@ class Locked(object):
2599
2603
LOCKFN = '.matplotlib_lock'
2600
2604
2601
2605
def __init__ (self , path ):
2602
- self .path = path
2606
+ self .path = fspath_no_except ( path )
2603
2607
self .end = "-" + str (os .getpid ())
2604
2608
self .lock_path = os .path .join (self .path , self .LOCKFN + self .end )
2605
2609
self .pattern = os .path .join (self .path , self .LOCKFN + '-*' )
@@ -2635,3 +2639,44 @@ def __exit__(self, exc_type, exc_value, traceback):
2635
2639
os .rmdir (path )
2636
2640
except OSError :
2637
2641
pass
2642
+
2643
+
2644
+ try :
2645
+ from os import fspath
2646
+ except ImportError :
2647
+ def fspath (path ):
2648
+ """
2649
+ Return the string representation of the path.
2650
+ If str or bytes is passed in, it is returned unchanged.
2651
+ This code comes from PEP 519, modified to support earlier versions of
2652
+ python.
2653
+
2654
+ This is required for python < 3.6.
2655
+ """
2656
+ if isinstance (path , (six .text_type , six .binary_type )):
2657
+ return path
2658
+
2659
+ # Work from the object's type to match method resolution of other magic
2660
+ # methods.
2661
+ path_type = type (path )
2662
+ try :
2663
+ return path_type .__fspath__ (path )
2664
+ except AttributeError :
2665
+ if hasattr (path_type , '__fspath__' ):
2666
+ raise
2667
+ try :
2668
+ import pathlib
2669
+ except ImportError :
2670
+ pass
2671
+ else :
2672
+ if isinstance (path , pathlib .PurePath ):
2673
+ return six .text_type (path )
2674
+
2675
+ raise TypeError ("expected str, bytes or os.PathLike object, not "
2676
+ + path_type .__name__ )
2677
+
2678
+ def fspath_no_except (path ):
2679
+ try :
2680
+ return fspath (path )
2681
+ except TypeError :
2682
+ return path
0 commit comments