@@ -458,7 +458,8 @@ def normpath(path):
458
458
# in the case of paths with these prefixes:
459
459
# \\.\ -> device names
460
460
# \\?\ -> literal paths
461
- # do not do any normalization, but return the path unchanged
461
+ # do not do any normalization, but return the path
462
+ # unchanged apart from the call to os.fspath()
462
463
return path
463
464
path = path .replace (altsep , sep )
464
465
prefix , path = splitdrive (path )
@@ -575,7 +576,7 @@ def _getfinalpathname_nonstrict(path):
575
576
return abspath (tail )
576
577
577
578
def realpath (path ):
578
- path = os . fspath (path )
579
+ path = normpath (path )
579
580
if isinstance (path , bytes ):
580
581
prefix = b'\\ \\ ?\\ '
581
582
unc_prefix = b'\\ \\ ?\\ UNC\\ '
@@ -586,6 +587,7 @@ def realpath(path):
586
587
unc_prefix = '\\ \\ ?\\ UNC\\ '
587
588
new_unc_prefix = '\\ \\ '
588
589
cwd = os .getcwd ()
590
+ did_not_exist = not exists (path )
589
591
had_prefix = path .startswith (prefix )
590
592
path = _getfinalpathname_nonstrict (path )
591
593
# The path returned by _getfinalpathname will always start with \\?\ -
@@ -603,7 +605,10 @@ def realpath(path):
603
605
if _getfinalpathname (spath ) == path :
604
606
path = spath
605
607
except OSError as ex :
606
- pass
608
+ # If the path does not exist and originally did not exist, then
609
+ # strip the prefix anyway.
610
+ if ex .winerror in {2 , 3 } and did_not_exist :
611
<
8000
/td>+ path = spath
607
612
return path
608
613
609
614
0 commit comments