#15377: Make posixpath.join() more strict when checking for str/bytes mix

Based on a patch by Nick Coghlan.
This commit is contained in:
Hynek Schlawack 2012-07-17 13:05:43 +02:00
parent eb3e62f1c8
commit c5a4566922
2 changed files with 18 additions and 13 deletions

View File

@ -83,11 +83,12 @@ def join(a, *p):
else:
path += sep + b
except TypeError:
strs = [isinstance(s, str) for s in (a, ) + p]
if any(strs) and not all(strs):
valid_types = all(isinstance(s, (str, bytes, bytearray))
for s in (a, ) + p)
if valid_types:
# Must have a mixture of text and binary data
raise TypeError("Can't mix strings and bytes in path components.")
else:
raise
raise
return path

View File

@ -1,6 +1,7 @@
import unittest
from test import support, test_genericpath
import itertools
import posixpath
import os
import sys
@ -56,18 +57,21 @@ class PosixPathTest(unittest.TestCase):
self.assertEqual(posixpath.join(b"/foo/", b"bar/", b"baz/"),
b"/foo/bar/baz/")
# Check for friendly str/bytes mixing message
for args in [[b'bytes', 'str'],
[bytearray(b'bytes'), 'str']]:
for _ in range(2):
def check_error_msg(list_of_args, msg):
"""Check posixpath.join raises friendly TypeErrors."""
for args in (item for perm in list_of_args
for item in itertools.permutations(perm)):
with self.assertRaises(TypeError) as cm:
posixpath.join(*args)
self.assertEqual(
"Can't mix strings and bytes in path components.",
cm.exception.args[0]
)
args.reverse() # check both orders
self.assertEqual(msg, cm.exception.args[0])
check_error_msg([[b'bytes', 'str'], [bytearray(b'bytes'), 'str']],
"Can't mix strings and bytes in path components.")
# regression, see #15377
with self.assertRaises(TypeError) as cm:
os.path.join(None, 'str')
self.assertNotEqual("Can't mix strings and bytes in path components.",
cm.exception.args[0])
def test_split(self):
self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))