@@ -92,10 +92,13 @@ def __init__(self,
9292 """Create an OSFS instance.
9393 """
9494 super (OSFS , self ).__init__ ()
95- root_path = fsdecode (fspath (root_path ))
95+ if isinstance (root_path , bytes ):
96+ root_path = fsdecode (root_path )
97+ self .root_path = root_path
98+ _root_path = fsdecode (fspath (root_path ))
9699 _root_path = os .path .expanduser (os .path .expandvars (root_path ))
97100 _root_path = os .path .normpath (os .path .abspath (_root_path ))
98- self .root_path = _root_path
101+ self ._root_path = _root_path
99102
100103 if create :
101104 try :
@@ -137,22 +140,24 @@ def __init__(self,
137140 def __repr__ (self ):
138141 # type: () -> str
139142 _fmt = "{}({!r})"
140- return _fmt . format ( self .__class__ .__name__ ,
141- self .root_path )
143+ _class_name = self .__class__ .__name__
144+ return _fmt . format ( _class_name , self .root_path )
142145
143146 def __str__ (self ):
144147 # type: () -> str
145148 fmt = "<{} '{}'>"
146- return fmt . format ( self .__class__ .__name__ . lower (),
147- self .root_path )
149+ _class_name = self .__class__ .__name__
150+ return fmt . format ( _class_name . lower (), self .root_path )
148151
149152 def _to_sys_path (self , path ):
150153 # type: (Text) -> Text
151154 """Convert a FS path to a path on the OS.
152155 """
153- sys_path = os .path .join (
154- self .root_path ,
155- path .lstrip ('/' ).replace ('/' , os .sep )
156+ sys_path = fsencode (
157+ os .path .join (
158+ self ._root_path ,
159+ path .lstrip ('/' ).replace ('/' , os .sep )
160+ )
156161 )
157162 return sys_path
158163
@@ -231,7 +236,7 @@ def _get_type_from_stat(cls, _stat):
231236 def _gettarget (self , sys_path ):
232237 # type: (Text) -> Optional[Text]
233238 try :
234- target = os .readlink (sys_path )
239+ target = os .readlink (fsencode ( sys_path ) )
235240 except OSError :
236241 return None
237242 else :
@@ -250,9 +255,9 @@ def getinfo(self, path, namespaces=None):
250255 sys_path = self .getsyspath (_path )
251256 _lstat = None
252257 with convert_os_errors ('getinfo' , path ):
253- _stat = os .stat (sys_path )
258+ _stat = os .stat (fsencode ( sys_path ) )
254259 if 'lstat' in namespaces :
255- _stat = os .lstat (sys_path )
260+ _stat = os .lstat (fsencode ( sys_path ) )
256261
257262 info = {
258263 'basic' : {
@@ -285,8 +290,9 @@ def listdir(self, path):
285290 _path = self .validatepath (path )
286291 sys_path = self ._to_sys_path (_path )
287292 with convert_os_errors ('listdir' , path , directory = True ):
288- names = os .listdir (sys_path )
289- return names
293+ names = os .listdir (fsencode (sys_path ))
294+ return [fsdecode (name ) for name in names ]
295+ #return names
290296
291297 def makedir (self , # type: _O
292298 path , # type: Text
@@ -368,7 +374,10 @@ def opendir(self, path, factory=None):
368374
369375 def getsyspath (self , path ):
370376 # type: (Text) -> Text
371- sys_path = self ._to_sys_path (path )
377+ sys_path = os .path .join (
378+ self ._root_path ,
379+ path .lstrip ('/' ).replace ('/' , os .sep )
380+ )
372381 return sys_path
373382
374383 def geturl (self , path , purpose = 'download' ):
@@ -455,7 +464,7 @@ def _scandir(self, path, namespaces=None):
455464 for dir_entry in scandir (sys_path ):
456465 info = {
457466 "basic" : {
458- "name" : dir_entry .name ,
467+ "name" : fsdecode ( dir_entry .name ) ,
459468 "is_dir" : dir_entry .is_dir ()
460469 }
461470 }
@@ -493,14 +502,16 @@ def _scandir(self, path, namespaces=None):
493502 self .check ()
494503 namespaces = namespaces or ()
495504 _path = self .validatepath (path )
496- sys_path = self ._to_sys_path (_path )
505+ sys_path = self .getsyspath (_path )
506+ _sys_path = fsencode (sys_path )
497507 with convert_os_errors ('scandir' , path , directory = True ):
498508 for entry_name in os .listdir (sys_path ):
499- entry_path = os .path .join (sys_path , entry_name )
500- stat_result = os .stat (entry_path )
509+ _entry_name = fsdecode (entry_name )
510+ entry_path = os .path .join (sys_path , _entry_name )
511+ stat_result = os .stat (fsencode (entry_path ))
501512 info = {
502513 "basic" : {
503- "name" : entry_name ,
514+ "name" : _entry_name ,
504515 "is_dir" : stat .S_ISDIR (stat_result .st_mode ),
505516 }
506517 } # type: Dict[Text, Dict[Text, Any]]
0 commit comments