Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8927cf0

Browse files
miss-islingtonzoobagpshead
authored
[3.11]gh-106242: Make ntpath.realpath errors consistent with abspath when there are embedded nulls (GH-108248)
gh-106242: Make ntpath.realpath errors consistent with abspath when there are embedded nulls (GH-108248)---------(cherry picked from commitde33b5c)Co-authored-by: Steve Dower <steve.dower@python.org>Co-authored-by: Gregory P. Smith <greg@krypto.org>
1 parentd22ac0c commit8927cf0

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

‎Lib/ntpath.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,14 @@ def realpath(path, *, strict=False):
695695
try:
696696
path=_getfinalpathname(path)
697697
initial_winerror=0
698+
exceptValueErrorasex:
699+
# gh-106242: Raised for embedded null characters
700+
# In strict mode, we convert into an OSError.
701+
# Non-strict mode returns the path as-is, since we've already
702+
# made it absolute.
703+
ifstrict:
704+
raiseOSError(str(ex))fromNone
705+
path=normpath(path)
698706
exceptOSErrorasex:
699707
ifstrict:
700708
raise
@@ -714,6 +722,10 @@ def realpath(path, *, strict=False):
714722
try:
715723
if_getfinalpathname(spath)==path:
716724
path=spath
725+
exceptValueErrorasex:
726+
# Unexpected, as an invalid path should not have gained a prefix
727+
# at any point, but we ignore this error just in case.
728+
pass
717729
exceptOSErrorasex:
718730
# If the path does not exist and originally did not exist, then
719731
# strip the prefix anyway.

‎Lib/test/test_ntpath.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ def test_realpath_basic(self):
332332
raiseOSError("No free drive letters available")
333333
self.assertEqual(ntpath.realpath(d),d)
334334

335+
# gh-106242: Embedded nulls and non-strict fallback to abspath
336+
self.assertEqual(ABSTFN+"\0spam",
337+
ntpath.realpath(os_helper.TESTFN+"\0spam",strict=False))
338+
335339
@os_helper.skip_unless_symlink
336340
@unittest.skipUnless(HAVE_GETFINALPATHNAME,'need _getfinalpathname')
337341
deftest_realpath_strict(self):
@@ -342,6 +346,8 @@ def test_realpath_strict(self):
342346
self.addCleanup(os_helper.unlink,ABSTFN)
343347
self.assertRaises(FileNotFoundError,ntpath.realpath,ABSTFN,strict=True)
344348
self.assertRaises(FileNotFoundError,ntpath.realpath,ABSTFN+"2",strict=True)
349+
# gh-106242: Embedded nulls should raise OSError (not ValueError)
350+
self.assertRaises(OSError,ntpath.realpath,ABSTFN+"\0spam",strict=True)
345351

346352
@os_helper.skip_unless_symlink
347353
@unittest.skipUnless(HAVE_GETFINALPATHNAME,'need _getfinalpathname')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fixes:func:`~os.path.realpath` to behave consistently when passed a path
2+
containing an embedded null character on Windows. In strict mode, it now
3+
raises:exc:`OSError` instead of the unexpected:exc:`ValueError`, and in
4+
non-strict mode will make the path absolute.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp