@@ -142,26 +142,26 @@ def _to_sys_path(self, path, as_bytes=False):
142142 If `as_bytes` is True, return fsencoded-bytes instead of Unicode.
143143 """
144144 root_path = self .root_path
145+ _path = path
145146 sep = '/'
146147 os_sep = os .sep
147148
148149 if _NIX_PY2 :
149150 root_path = self .root_path_native
150- path = fsencode (path )
151+ _path = fsencode (path )
151152 sep = b'/'
152153 os_sep = fsencode (os_sep )
153154
154155 sys_path = os .path .join (
155156 root_path ,
156- path .lstrip (sep ).replace (sep , os_sep )
157+ _path .lstrip (sep ).replace (sep , os_sep )
157158 )
158159
160+ sys_path = fsdecode (sys_path )
161+
159162 if as_bytes :
160163 return fsencode (sys_path )
161164
162- if _NIX_PY2 :
163- return fsdecode (sys_path )
164-
165165 return sys_path
166166
167167 @classmethod
@@ -234,10 +234,11 @@ def _get_type_from_stat(cls, _stat):
234234 # --------------------------------------------------------
235235
236236 def _gettarget (self , sys_path ):
237+ _sys_path = sys_path
237238 if _NIX_PY2 :
238- sys_path = fsencode (sys_path )
239+ _sys_path = fsencode (_sys_path )
239240 try :
240- target = os .readlink (sys_path )
241+ target = os .readlink (_sys_path )
241242 except OSError :
242243 return None
243244 else :
@@ -248,16 +249,16 @@ def _gettarget(self, sys_path):
248249 def _make_link_info (self , sys_path ):
249250 _target = self ._gettarget (sys_path )
250251 link = {
251- 'target' : _target and fsdecode (_target ) or _target ,
252+ 'target' : fsdecode (_target ) if _target else _target ,
252253 }
253254 return link
254255
255256 def _get_validated_syspath (self , path ):
256257 """Return a validated, normalized and eventually encoded string or byte
257258 path.
258259 """
259- path = path and fsdecode (path ) or path
260- _path = self .validatepath (path )
260+ _path = fsdecode (path ) if path else path
261+ _path = self .validatepath (_path )
261262 return self ._to_sys_path (_path , as_bytes = _NIX_PY2 )
262263
263264 def getinfo (self , path , namespaces = None ):
@@ -305,8 +306,8 @@ def listdir(self, path):
305306 def makedir (self , path , permissions = None , recreate = False ):
306307 self .check ()
307308 mode = Permissions .get_mode (permissions )
308- path = path and fsdecode (path ) or path
309- _path = self .validatepath (path )
309+ _path = fsdecode (path ) if path else path
310+ _path = self .validatepath (_path )
310311 sys_path = self ._get_validated_syspath (_path )
311312 with convert_os_errors ('makedir' , path , directory = True ):
312313 try :
@@ -324,10 +325,10 @@ def openbin(self, path, mode="r", buffering=-1, **options):
324325 _mode = Mode (mode )
325326 _mode .validate_bin ()
326327 self .check ()
327- path = path and fsdecode (path ) or path
328- sys_path = self ._get_validated_syspath (path )
328+ _path = fsdecode (path ) if path else path
329+ sys_path = self ._get_validated_syspath (_path )
329330 with convert_os_errors ('openbin' , path ):
330- if six .PY2 and _mode .exclusive and self .exists (path ):
331+ if six .PY2 and _mode .exclusive and self .exists (_path ):
331332 raise errors .FileExists (path )
332333 binary_file = io .open (
333334 sys_path ,
@@ -356,11 +357,12 @@ def remove(self, path):
356357
357358 def removedir (self , path ):
358359 self .check ()
359- path = path and fsdecode (path ) or path
360- _path = self .validatepath (path )
360+ _path = fsdecode (path ) if path else path
361+ _path = self .validatepath (_path )
361362 if _path == '/' :
362363 raise errors .RemoveRootError ()
363- sys_path = self ._to_sys_path (path , as_bytes = _NIX_PY2 )
364+
365+ sys_path = self ._to_sys_path (_path , as_bytes = _NIX_PY2 )
364366 with convert_os_errors ('removedir' , path , directory = True ):
365367 os .rmdir (sys_path )
366368
@@ -369,8 +371,8 @@ def removedir(self, path):
369371 # --------------------------------------------------------
370372
371373 def getsyspath (self , path ):
372- path = path and fsdecode (path ) or path
373- sys_path = self ._to_sys_path (path , as_bytes = False )
374+ _path = fsdecode (path ) if path else path
375+ sys_path = self ._to_sys_path (_path , as_bytes = False )
374376 return sys_path
375377
376378 def geturl (self , path , purpose = 'download' ):
@@ -446,9 +448,14 @@ def _scandir(self, path, namespaces=None):
446448 sys_path = self ._get_validated_syspath (path )
447449 with convert_os_errors ('scandir' , path , directory = True ):
448450 for dir_entry in scandir (sys_path ):
451+ entry_name_u = fsdecode (dir_entry .name )
452+ entry_name_b = fsencode (entry_name_u )
453+ entry_path_b = os .path .join (sys_path , entry_name_b )
454+ entry_path_u = fsdecode (entry_path_b )
455+
449456 info = {
450457 "basic" : {
451- "name" : fsdecode ( dir_entry . name ) ,
458+ "name" : entry_name_u ,
452459 "is_dir" : dir_entry .is_dir ()
453460 }
454461 }
@@ -469,12 +476,7 @@ def _scandir(self, path, namespaces=None):
469476 for k in dir (lstat_result ) if k .startswith ('st_' )
470477 }
471478 if 'link' in namespaces :
472- dir_entry_name = dir_entry .name
473- if _NIX_PY2 :
474- dir_entry_name = fsencode (dir_entry_name )
475- info ['link' ] = self ._make_link_info (
476- fsdecode (os .path .join (sys_path , dir_entry_name ))
477- )
479+ info ['link' ] = self ._make_link_info (entry_path_u )
478480 if 'access' in namespaces :
479481 stat_result = dir_entry .stat ()
480482 info ['access' ] = \
@@ -491,8 +493,11 @@ def _scandir(self, path, namespaces=None):
491493 with convert_os_errors ('scandir' , path , directory = True ):
492494 for entry_name in os .listdir (sys_path ):
493495 entry_name_u = fsdecode (entry_name )
494- entry_path = os .path .join (sys_path , entry_name )
495- stat_result = os .stat (entry_path )
496+ entry_name_b = fsencode (entry_name )
497+ entry_path_b = os .path .join (sys_path , entry_name_b )
498+ entry_path_u = fsdecode (entry_path_b )
499+ stat_result = os .stat (entry_path_b )
500+
496501 info = {
497502 "basic" : {
498503 "name" : entry_name_u ,
@@ -508,15 +513,13 @@ def _scandir(self, path, namespaces=None):
508513 for k in dir (stat_result ) if k .startswith ('st_' )
509514 }
510515 if 'lstat' in namespaces :
511- lstat_result = os .lstat (entry_path )
516+ lstat_result = os .lstat (entry_path_b )
512517 info ['lstat' ] = {
513518 k : getattr (lstat_result , k )
514519 for k in dir (lstat_result ) if k .startswith ('st_' )
515520 }
516521 if 'link' in namespaces :
517- info ['link' ] = self ._make_link_info (
518- fsdecode (os .path .join (sys_path , entry_name ))
519- )
522+ info ['link' ] = self ._make_link_info (entry_path_u )
520523 if 'access' in namespaces :
521524 info ['access' ] = \
522525 self ._make_access_from_stat (stat_result )
@@ -525,7 +528,7 @@ def _scandir(self, path, namespaces=None):
525528
526529
527530 def scandir (self , path , namespaces = None , page = None ):
528- path = path and fsdecode (path ) or path
531+ path = fsdecode (path ) if path else path
529532 iter_info = self ._scandir (path , namespaces = namespaces )
530533 if page is not None :
531534 start , end = page
0 commit comments