GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Misleading exception message from `pathlib.PurePath()` when mixing str and bytes arguments · Issue #103525 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Misleading exception message from pathlib.PurePath() when mixing str and bytes arguments#103525
Copy link
Labels
3.12only security fixesonly security fixestopic-pathlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@barneygale
barneygale
opened on Apr 13, 2023
Issue body actions

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

  • GH-103525: Improve exception message from pathlib.PurePath() #103526

Activity

  1. barneygale
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    3.12only security fixesonly security fixes
    topic-pathlib
    on Apr 13, 2023
  2. barneygale
    added 2 commits that reference this issue on Apr 13, 2023

    pythonGH-103525: Improve exception message from `pathlib.PurePath()`

    7804b2e

    pythonGH-103525: Improve exception message from `pathlib.PurePath()`

    6c1131e
  3. terryjreedy commented on Apr 13, 2023

    @terryjreedy
    terryjreedy
    on Apr 13, 2023
    Member
    More actions

    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.

  4. barneygale commented on Apr 13, 2023

    @barneygale
    barneygale
    on Apr 13, 2023
    ContributorAuthor
    More actions

    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.

  5. barneygale commented on Apr 13, 2023

    @barneygale
    barneygale
    on Apr 13, 2023
    ContributorAuthor
    More actions

    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!

  6. barneygale
    added 2 commits that reference this issue on Apr 29, 2023

    Merge branch 'main' into pythongh-103525-purepath-mix-str-bytes-typee…

    6d2697c

    GH-103525: Improve exception message from `pathlib.PurePath()` (GH-10…

    8611e7b
  7. barneygale commented on May 2, 2023

    @barneygale
    barneygale
    on May 2, 2023
    ContributorAuthor
    More actions

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

  8. barneygale
    closed this as completedon May 2, 2023
  9. carljm
    added 2 commits that reference this issue on May 2, 2023

    Merge branch 'main' into inlinecomp2

    0727d6f

    Merge branch 'main' into cached_property_docs

    b9c39e3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixesonly security fixestopic-pathlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.