This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.path.join() error misleading with path1=None
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.2, Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: hynek Nosy List: chris.jerdonek, hynek, ncoghlan, pitrou, python-dev
Priority: normal Keywords: patch

Created on 2012-07-17 08:11 by chris.jerdonek, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
nicer-error-for-none.diff hynek, 2012-07-17 09:46
nicer-error-for-none-v2.diff hynek, 2012-07-17 10:50
Messages (7)
msg165685 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-07-17 08:11
The error message for os.path.join() is misleading when the first argument is None.  The message should probably say something about mixing "None" and strings.

Python 3.3.0b1 (default:f954ee489896, Jul 16 2012, 22:42:29) 
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
>>> import os
>>> os.path.join(None, 'a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../cpython/Lib/posixpath.py", line 89, in join
    "components.") from None
TypeError: Can't mix strings and bytes in path components.
msg165694 - (view) Author: Hynek Schlawack (hynek) * (Python committer) Date: 2012-07-17 09:46
I propose the following patch (against 3.2) that does the right thing.
msg165696 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-07-17 10:25
I'd be more inclined to tighten up the check for the "can't mix" message to something like:

 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.")
 raise

If people pass in something that isn't a valid argument *at all*, then I'm fine with just letting the underlying exception pass through.

The str/bytes case is just worth special-casing because either on their own *are* valid arguments.
msg165703 - (view) Author: Hynek Schlawack (hynek) * (Python committer) Date: 2012-07-17 10:50
Yeah, we talked about that exact think with Antoine on IRC. New proposal.
msg165704 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2012-07-17 10:54
Looks good to me
msg165706 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-07-17 11:10
New changeset 5553a53a230a by Hynek Schlawack in branch '3.2':
#15377: Make posixpath.join() more strict when checking for str/bytes mix
http://hg.python.org/cpython/rev/5553a53a230a

New changeset d087ef80372d by Hynek Schlawack in branch 'default':
#15377: Make posixpath.join() more strict when checking for str/bytes mix
http://hg.python.org/cpython/rev/d087ef80372d
msg165707 - (view) Author: Hynek Schlawack (hynek) * (Python committer) Date: 2012-07-17 11:11
Fine! :)
History
Date User Action Args
2022-04-11 14:57:32adminsetgithub: 59582
2012-07-17 11:11:41hyneksetstatus: open -> closed
resolution: fixed
messages: + msg165707

stage: patch review -> resolved
2012-07-17 11:10:56python-devsetnosy: + python-dev
messages: + msg165706
2012-07-17 10:54:24ncoghlansetmessages: + msg165704
2012-07-17 10:50:27hyneksetfiles: + nicer-error-for-none-v2.diff

messages: + msg165703
2012-07-17 10:25:48ncoghlansetmessages: + msg165696
2012-07-17 09:49:24hyneksetassignee: hynek
versions: + Python 3.2
2012-07-17 09:46:05hyneksetfiles: + nicer-error-for-none.diff

nosy: + ncoghlan, pitrou
messages: + msg165694

keywords: + patch
stage: patch review
2012-07-17 08:15:29hyneksetnosy: + hynek
2012-07-17 08:11:57chris.jerdonekcreate