Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading exception message from pathlib.PurePath() when mixing str and bytes arguments #103525

Closed
barneygale opened this issue Apr 13, 2023 · 4 comments
Labels
3.12 bugs and security fixes topic-pathlib type-bug An unexpected behavior, bug, or error

Comments

@barneygale
Copy link
Contributor

barneygale commented Apr 13, 2023

Since 6716254, attempting to create a pathlib.PurePath or Path object with mixed str and bytes arguments raises a TypeError (yay!) with a misleading message (booo!):

>>> import pathlib
>>> pathlib.Path('foo', b'bar')
TypeError: Can't mix strings and bytes in path components

This message implies that bytes are supported, as long as we don't mix them with strings. And yet when we try that:

>>> pathlib.Path(b'foo', b'bar')
TypeError: argument should be a str or an os.PathLike object where __fspath__ returns a str, not 'bytes'

This message is better, and should be used in the former case.

Linked PRs

@barneygale barneygale added type-bug An unexpected behavior, bug, or error 3.12 bugs and security fixes topic-pathlib labels Apr 13, 2023
barneygale added a commit to barneygale/cpython that referenced this issue Apr 13, 2023
Check that arguments are strings before calling `os.path.join()`.

Also improve performance of `PurePath(PurePath(...))` while we're in the
area: we now use the *unnormalized* string path of such arguments.
barneygale added a commit to barneygale/cpython that referenced this issue Apr 13, 2023
Check that arguments are strings before calling `os.path.join()`.

Also improve performance of `PurePath(PurePath(...))` while we're in the
area: we now use the *unnormalized* string path of such arguments.
@terryjreedy
Copy link
Member

terryjreedy commented Apr 13, 2023

The following

>>> pathlib.Path('foo', 1)
Traceback (most recent call last):
  File "C:\Programs\Python312\Lib\pathlib.py", line 703, in __init__
    super().__init__(*args)
  File "C:\Programs\Python312\Lib\pathlib.py", line 279, in __init__
    path = self._flavour.join(*args)
  File "<frozen ntpath>", line 149, in join
  File "<frozen genericpath>", line 164, in _check_arg_types
TypeError: join() argument must be str, bytes, or os.PathLike object, not 'int'

raises a message again implying that bytes would be accepted. It would be nicer if this also got the improved message about Path args.
ADDED: When the new code calls os.fspath(1), the message will be

expected str, bytes or os.PathLike object, not int
which still implies that bytes are accepted.

@barneygale
Copy link
Contributor Author

barneygale commented Apr 13, 2023

That's possible to fix, but it involves either capturing and re-raising the TypeError, or basically re-implementing os.fspath(). This code is pretty performance-sensitive so I don't feel great about it.

Another option would be to add a string_only argument to os.fspath() that rejects bytes and raises an appropriate exception.

@barneygale
Copy link
Contributor Author

That's possible to fix, but it involves either capturing and re-raising the TypeError, or basically re-implementing os.fspath(). This code is pretty performance-sensitive so I don't feel great about it

^ nevermind, I'm wrong. Thanks for the suggestion on the PR!

barneygale added a commit to barneygale/cpython that referenced this issue Apr 29, 2023
barneygale added a commit that referenced this issue May 2, 2023
…3526)

Check that arguments are strings before calling `os.path.join()`.

Also improve performance of `PurePath(PurePath(...))` while we're in the
area: we now use the *unnormalized* string path of such arguments.

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
@barneygale
Copy link
Contributor Author

Fixed in #103526 / 8611e7b. Not backported as the bug only affects 3.12.

carljm added a commit to carljm/cpython that referenced this issue May 2, 2023
* main:
  pythongh-103822: [Calendar] change return value to enum for day and month APIs (pythonGH-103827)
  pythongh-65022: Fix description of tuple return value in copyreg (python#103892)
  pythonGH-103525: Improve exception message from `pathlib.PurePath()` (pythonGH-103526)
  pythongh-84436: Add integration C API tests for immortal objects (pythongh-103962)
  pythongh-103743: Add PyUnstable_Object_GC_NewWithExtraData (pythonGH-103744)
  pythongh-102997: Update Windows installer to SQLite 3.41.2. (python#102999)
  pythonGH-103484: Fix redirected permanently URLs (python#104001)
  Improve assert_type phrasing (python#104081)
  pythongh-102997: Update macOS installer to SQLite 3.41.2. (pythonGH-102998)
  pythonGH-103472: close response in HTTPConnection._tunnel (python#103473)
  pythongh-88496: IDLE - fix another test on macOS (python#104075)
  pythongh-94673: Hide Objects in PyTypeObject Behind Accessors (pythongh-104074)
  pythongh-94673: Properly Initialize and Finalize Static Builtin Types for Each Interpreter (pythongh-104072)
  pythongh-104016: Skip test for deeply neste f-strings on wasi (python#104071)
carljm added a commit to carljm/cpython that referenced this issue May 2, 2023
* main: (760 commits)
  pythonGH-104102: Optimize `pathlib.Path.glob()` handling of `../` pattern segments (pythonGH-104103)
  pythonGH-104104: Optimize `pathlib.Path.glob()` by avoiding repeated calls to `os.path.normcase()` (pythonGH-104105)
  pythongh-103822: [Calendar] change return value to enum for day and month APIs (pythonGH-103827)
  pythongh-65022: Fix description of tuple return value in copyreg (python#103892)
  pythonGH-103525: Improve exception message from `pathlib.PurePath()` (pythonGH-103526)
  pythongh-84436: Add integration C API tests for immortal objects (pythongh-103962)
  pythongh-103743: Add PyUnstable_Object_GC_NewWithExtraData (pythonGH-103744)
  pythongh-102997: Update Windows installer to SQLite 3.41.2. (python#102999)
  pythonGH-103484: Fix redirected permanently URLs (python#104001)
  Improve assert_type phrasing (python#104081)
  pythongh-102997: Update macOS installer to SQLite 3.41.2. (pythonGH-102998)
  pythonGH-103472: close response in HTTPConnection._tunnel (python#103473)
  pythongh-88496: IDLE - fix another test on macOS (python#104075)
  pythongh-94673: Hide Objects in PyTypeObject Behind Accessors (pythongh-104074)
  pythongh-94673: Properly Initialize and Finalize Static Builtin Types for Each Interpreter (pythongh-104072)
  pythongh-104016: Skip test for deeply neste f-strings on wasi (python#104071)
  pythongh-104057: Fix direct invocation of test_super (python#104064)
  pythongh-87092: Expose assembler to unit tests (python#103988)
  pythongh-97696: asyncio eager tasks factory (python#102853)
  pythongh-84436: Immortalize in _PyStructSequence_InitBuiltinWithFlags() (pythongh-104054)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes topic-pathlib type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants