File tree Expand file tree Collapse file tree 3 files changed +26
-22
lines changed Expand file tree Collapse file tree 3 files changed +26
-22
lines changed Original file line number Diff line number Diff line change @@ -496,38 +496,36 @@ def normpath(path):
496
496
comps .append (curdir )
497
497
return prefix + sep .join (comps )
498
498
499
+ def _abspath_fallback (path ):
500
+ """Return the absolute version of a path as a fallback function in case
501
+ `nt._getfullpathname` is not available or raises OSError. See bpo-31047 for
502
+ more.
503
+
504
+ """
505
+
506
+ path = os .fspath (path )
507
+ if not isabs (path ):
508
+ if isinstance (path , bytes ):
509
+ cwd = os .getcwdb ()
510
+ else :
511
+ cwd = os .getcwd ()
512
+ path = join (cwd , path )
513
+ return normpath (path )
499
514
500
515
# Return an absolute path.
501
516
try :
502
517
from nt import _getfullpathname
503
518
504
519
except ImportError : # not running on Windows - mock up something sensible
505
- def abspath (path ):
506
- """Return the absolute version of a path."""
507
- path = os .fspath (path )
508
- if not isabs (path ):
509
- if isinstance (path , bytes ):
510
- cwd = os .getcwdb ()
511
- else :
512
- cwd = os .getcwd ()
513
- path = join (cwd , path )
514
- return normpath (path )
520
+ abspath = _abspath_fallback
515
521
516
522
else : # use native Windows method on Windows
517
523
def abspath (path ):
518
524
"""Return the absolute version of a path."""
519
-
520
- if path : # Empty path must return current working directory.
521
- path = os .fspath (path )
522
- try :
523
- path = _getfullpathname (path )
524
- except OSError :
525
- pass # Bad path - return unchanged.
526
- elif isinstance (path , bytes ):
527
- path = os .getcwdb ()
528
- else :
529
- path = os .getcwd ()
530
- return normpath (path )
525
+ try :
526
+ return _getfullpathname (path )
527
+ except OSError :
528
+ return _abspath_fallback (path )
531
529
532
530
# realpath is a no-op on systems without islink support
533
531
realpath = abspath
Original file line number Diff line number Diff line change @@ -280,6 +280,10 @@ def test_expanduser(self):
280
280
@unittest .skipUnless (nt , "abspath requires 'nt' module" )
281
281
def test_abspath (self ):
282
282
tester ('ntpath.abspath("C:\\ ")' , "C:\\ " )
283
+ with support .temp_cwd (support .TESTFN ) as cwd_dir : # bpo-31047
284
+ tester ('ntpath.abspath("")' , cwd_dir )
285
+ tester ('ntpath.abspath(" ")' , cwd_dir + "\\ " )
286
+ tester ('ntpath.abspath("?")' , cwd_dir + "\\ ?" )
283
287
284
288
def test_relpath (self ):
285
289
tester ('ntpath.relpath("a")' , 'a' )
Original file line number Diff line number Diff line change
1
+ Fix ``ntpath.abspath `` for invalid paths on windows. Patch by Franz
2
+ Woellert.
You can’t perform that action at this time.
0 commit comments