# Bug in os.path.ismount() or perhaps os.DirEntry()?

**URL:** <https://discuss.python.org/t/bug-in-os-path-ismount-or-perhaps-os-direntry/18406>\
**Category:** Python Help\
**Tags:** solved\
**Created:** [August 22, 2022, 10:19pm UTC](https://discuss.python.org/t/bug-in-os-path-ismount-or-perhaps-os-direntry/18406 "2022-08-22T22:19:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![calestyo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/calestyo/32/8584_2.png) [@calestyo](https://discuss.python.org/u/calestyo)\
**Post date:** [August 22, 2022, 10:19pm UTC](https://discuss.python.org/t/bug-in-os-path-ismount-or-perhaps-os-direntry/18406/1 "2022-08-22T22:19:41Z")

</div>

Hey.

I wonder whether the following is a bug in Python (using 3.10.6).

With this code:

```auto
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)

```

I get a:

```auto
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

```

I use the function with bytes (since pathnames are bytes),… `p` is a `os.DirEntry` (which is documented to support the PathLike-interface, as is `os.path.ismount()`).

So my expectation would have been, that I can use `os.path.ismount(p)` (which does work when I use strings) and it would still work.  
However, I need to explicitly use `os.path.ismount(p.path)` for Python to realise that I want bytes.

Cheers,  
Chris.

---

<div class="post-metadata">

**Author:** ![eryksun](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/eryksun/32/697_2.png) [@eryksun](https://discuss.python.org/u/eryksun)\
**Post date:** [August 22, 2022, 10:41pm UTC](https://discuss.python.org/t/bug-in-os-path-ismount-or-perhaps-os-direntry/18406/2 "2022-08-22T22:41:02Z")

</div>

> [@calestyo](#):
>
> However, I need to explicitly use `os.path.ismount(p.path)` for Python to realise that I want bytes.

This is a bug in `posixpath.ismount()`. It uses the path argument without first converting to `str` or `bytes` via `os.fspath()`.

---

<div class="post-metadata">

**Author:** ![calestyo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/calestyo/32/8584_2.png) [@calestyo](https://discuss.python.org/u/calestyo)\
**Post date:** [August 22, 2022, 10:42pm UTC](https://discuss.python.org/t/bug-in-os-path-ismount-or-perhaps-os-direntry/18406/3 "2022-08-22T22:42:16Z")

</div>

Thanks, I’ll file it in the github.

---

<div class="post-metadata">

**Author:** ![calestyo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/calestyo/32/8584_2.png) [@calestyo](https://discuss.python.org/u/calestyo)\
**Post date:** [August 22, 2022, 10:47pm UTC](https://discuss.python.org/t/bug-in-os-path-ismount-or-perhaps-os-direntry/18406/4 "2022-08-22T22:47:23Z")

</div>

For the records:

> <https://github.com/python/cpython/issues/96192>
>
> \# Bug report
> 
> It seems that \`os.path.ismount()\` doesn't properly use a bytes-p…ath 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.

---

<div class="post-metadata">

**Author:** ![Senthil](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/senthil/32/18300_2.png) [@Senthil](https://discuss.python.org/u/Senthil)\
**Post date:** [August 24, 2022, 4:55pm UTC](https://discuss.python.org/t/bug-in-os-path-ismount-or-perhaps-os-direntry/18406/5 "2022-08-24T16:55:33Z")

</div>

Thanks for the report and PR, @calestyo . A test addition will be helpful here to prevent regressions.
