Skip to content

os.path.ismount() doesn't properly use byte-paths from an os.DirEntry #96192

Closed

Description

@calestyo

Bug report

It seems that os.path.ismount() doesn't properly use a bytes-path from an os.DirEntry object (despite both claiming to support/be PathLike).

Take e.g. the following code, when called with a bytes path:

def scandirtree(path=b".", xdev=True):
    for p in os.scandir(path):
        yield p
        if p.is_dir(follow_symlinks=False)   and   ( not xdev  or  not os.path.ismount(p) ):
            yield from scandirtree(p, xdev)

That fails with:

Traceback (most recent call last):
  File "/home/calestyo/prj/generate-file-list/src/./generate-file-list", line 65, in <module>
    main()
  File "/home/calestyo/prj/generate-file-list/src/./generate-file-list", line 52, in main
    for p in scandirtree(ap, args.xdev):
  File "/home/calestyo/prj/generate-file-list/src/./generate-file-list", line 25, in scandirtree
    if p.is_dir(follow_symlinks=False)   and   ( not xdev  or  not os.path.ismount(p) ):
  File "/usr/lib/python3.10/posixpath.py", line 201, in ismount
    parent = join(path, '..')
  File "/usr/lib/python3.10/posixpath.py", line 90, in join
    genericpath._check_arg_types('join', a, *p)
  File "/usr/lib/python3.10/genericpath.py", line 155, in _check_arg_types
    raise TypeError("Can't mix strings and bytes in path components") from None
TypeError: Can't mix strings and bytes in path components

See also https://discuss.python.org/t/bug-in-os-path-ismount-or-perhaps-os-direntry/18406

Your environment

  • CPython versions tested on: 3.10.6
  • Operating system and architecture: Debian sid, x86_64

Cheers,
Chris.

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    3.11only security fixes
    3.12only security fixes
    on Aug 23, 2022
  2. eryksun commented on Aug 23, 2022

    @eryksun
    Contributor

    posixpath.ismount() mistakenly checks isinstance(path, bytes) without first ensuring that the type is str or bytes via path = os.fspath(path).

  3. calestyo commented on Aug 23, 2022

    @calestyo
    ContributorAuthor

    Shall I provide a patch?

  4. added a commit that references this issue on Nov 14, 2022
  5. added 2 commits that reference this issue on Nov 14, 2022
  6. hauntsaninja commented on Nov 29, 2022

    @hauntsaninja
    Contributor

    Thanks, looks like this has been fixed!

  7. added a commit that references this issue on Nov 30, 2022
  8. added a commit that references this issue on Jan 22, 2023
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.10 (EOL)end of life3.11only security fixes3.12only security fixeseasystdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions