Skip to content

Commit 5bfc03f

Browse files
Issue #23780: Improved error message in os.path.join() with single argument.
Idea by R. David Murray.
1 parent 6baa0a5 commit 5bfc03f

File tree

5 files changed

+12
-0
lines changed

5 files changed

+12
-0
lines changed

Lib/macpath.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def join(s, *p):
5353
try:
5454
colon = _get_colon(s)
5555
path = s
56+
if not p:
57+
path[:0] + colon #23780: Ensure compatible data type even if p is null.
5658
for t in p:
5759
if (not path) or isabs(t):
5860
path = t

Lib/ntpath.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ def join(path, *paths):
8181
seps = '\\/'
8282
colon = ':'
8383
try:
84+
if not paths:
85+
path[:0] + sep #23780: Ensure compatible data type even if p is null.
8486
result_drive, result_path = splitdrive(path)
8587
for p in paths:
8688
p_drive, p_path = splitdrive(p)

Lib/posixpath.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def join(a, *p):
7676
sep = _get_sep(a)
7777
path = a
7878
try:
79+
if not p:
80+
path[:0] + sep #23780: Ensure compatible data type even if p is null.
7981
for b in p:
8082
if b.startswith(sep):
8183
path = b

Lib/test/test_genericpath.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ def test_join_errors(self):
448448
self.pathmodule.join(42, 'str')
449449
with self.assertRaisesRegex(TypeError, errmsg % 'int'):
450450
self.pathmodule.join('str', 42)
451+
with self.assertRaisesRegex(TypeError, errmsg % 'int'):
452+
self.pathmodule.join(42)
453+
with self.assertRaisesRegex(TypeError, errmsg % 'list'):
454+
self.pathmodule.join([])
451455
with self.assertRaisesRegex(TypeError, errmsg % 'bytearray'):
452456
self.pathmodule.join(bytearray(b'foo'), bytearray(b'bar'))
453457

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Core and Builtins
4949
Library
5050
-------
5151

52+
- Issue #23780: Improved error message in os.path.join() with single argument.
53+
5254
- Issue #6598: Increased time precision and random number range in
5355
email.utils.make_msgid() to strengthen the uniqueness of the message ID.
5456

0 commit comments

Comments
 (0)