@@ -756,6 +756,7 @@ def to_filehandle(fname, flag='rU', return_opened=False):
756756 files is automatic, if the filename ends in .gz. *flag* is a
757757 read/write flag for :func:`file`
758758 """
759+ fname = fspath_no_except (fname )
759760 if is_string_like (fname ):
760761 if fname .endswith ('.gz' ):
761762 # get rid of 'U' in flag for gzipped files.
@@ -815,6 +816,7 @@ def get_sample_data(fname, asfileobj=True):
815816 root = matplotlib .rcParams ['examples.directory' ]
816817 else :
817818 root = os .path .join (matplotlib ._get_data_path (), 'sample_data' )
819+ fname = fspath_no_except (fname )
818820 path = os .path .join (root , fname )
819821
820822 if asfileobj :
@@ -1019,6 +1021,7 @@ def __init__(self):
10191021 self ._cache = {}
10201022
10211023 def __call__ (self , path ):
1024+ path = fspath_no_except (path )
10221025 result = self ._cache .get (path )
10231026 if result is None :
10241027 realpath = os .path .realpath (path )
@@ -1173,7 +1176,7 @@ def listFiles(root, patterns='*', recurse=1, return_folders=0):
11731176 pattern_list = patterns .split (';' )
11741177 results = []
11751178
1176- for dirname , dirs , files in os .walk (root ):
1179+ for dirname , dirs , files in os .walk (fspath_no_except ( root ) ):
11771180 # Append to results all relevant files (and perhaps folders)
11781181 for name in files :
11791182 fullname = os .path .normpath (os .path .join (dirname , name ))
@@ -1197,10 +1200,10 @@ def get_recursive_filelist(args):
11971200 files = []
11981201
11991202 for arg in args :
1200- if os .path .isfile (arg ):
1203+ if os .path .isfile (fspath_no_except ( arg ) ):
12011204 files .append (arg )
12021205 continue
1203- if os .path .isdir (arg ):
1206+ if os .path .isdir (fspath_no_except ( arg ) ):
12041207 newfiles = listFiles (arg , recurse = 1 , return_folders = 1 )
12051208 files .extend (newfiles )
12061209
@@ -1680,6 +1683,7 @@ def simple_linear_interpolation(a, steps):
16801683
16811684
16821685def recursive_remove (path ):
1686+ path = fspath_no_except (path )
16831687 if os .path .isdir (path ):
16841688 for fname in (glob .glob (os .path .join (path , '*' )) +
16851689 glob .glob (os .path .join (path , '.*' ))):
@@ -2599,7 +2603,7 @@ class Locked(object):
25992603 LOCKFN = '.matplotlib_lock'
26002604
26012605 def __init__ (self , path ):
2602- self .path = path
2606+ self .path = fspath_no_except ( path )
26032607 self .end = "-" + str (os .getpid ())
26042608 self .lock_path = os .path .join (self .path , self .LOCKFN + self .end )
26052609 self .pattern = os .path .join (self .path , self .LOCKFN + '-*' )
@@ -2635,3 +2639,44 @@ def __exit__(self, exc_type, exc_value, traceback):
26352639 os .rmdir (path )
26362640 except OSError :
26372641 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