diff options
author | Eli Bendersky <eliben@gmail.com> | 2012-07-17 11:22:01 (GMT) |
---|---|---|
committer | Eli Bendersky <eliben@gmail.com> | 2012-07-17 11:22:01 (GMT) |
commit | ceb23316c85022541bac39c7ce814a659c7e1962 (patch) | |
tree | cbcfac900321e46b39d944a14fe5afb41a80ed65 | |
parent | 66099b0cdcce7ee6d0d384a6f7687499bc67f15e (diff) | |
parent | c5a45669229906a2ff8ea87bd69d1df2feac8ffc (diff) | |
download | cpython-ceb23316c85022541bac39c7ce814a659c7e1962.zip cpython-ceb23316c85022541bac39c7ce814a659c7e1962.tar.gz cpython-ceb23316c85022541bac39c7ce814a659c7e1962.tar.bz2 |
merge heads
-rw-r--r-- | Lib/posixpath.py | 9 | ||||
-rw-r--r-- | Lib/test/test_posixpath.py | 25 | ||||
-rw-r--r-- | Lib/test/test_runpy.py | 2 |
3 files changed, 22 insertions, 14 deletions
diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 84bcc13..7a4daa8b 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -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 diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 54de0cf..89c4f9e 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -1,6 +1,7 @@ import unittest from test import support, test_genericpath +import itertools import posixpath import os import sys @@ -56,15 +57,21 @@ class PosixPathTest(unittest.TestCase): self.assertEqual(posixpath.join(b"/foo/", b"bar/", b"baz/"), b"/foo/bar/baz/") - with self.assertRaises(TypeError) as e: - posixpath.join(b'bytes', 'str') - self.assertIn("Can't mix strings and bytes", e.args[0]) - with self.assertRaises(TypeError) as e: - posixpath.join('str', b'bytes') - self.assertIn("Can't mix strings and bytes", e.args[0]) - with self.assertRaises(TypeError) as e: - posixpath.join('str', bytearray(b'bytes')) - self.assertIn("Can't mix strings and bytes", e.args[0]) + 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(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")) diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py index 85eb76f..6e9c4fc 100644 --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -182,7 +182,7 @@ class RunModuleTestCase(unittest.TestCase, CodeExecutionMixin): def _make_pkg(self, source, depth, mod_base="runpy_test"): pkg_name = "__runpy_pkg__" test_fname = mod_base+os.extsep+"py" - pkg_dir = sub_dir = tempfile.mkdtemp() + pkg_dir = sub_dir = os.path.realpath(tempfile.mkdtemp()) if verbose > 1: print(" Package tree in:", sub_dir) sys.path.insert(0, pkg_dir) if verbose > 1: print(" Updated sys.path:", sys.path[0]) |