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.exists() takes bool as argument and returns True
Type: behavior Stage:
Components: IO Versions: Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: Mariano Anaya, Nika, eryksun, serhiy.storchaka
Priority: normal Keywords:

Created on 2019-10-11 11:16 by Nika, last changed 2022-04-11 14:59 by admin.

Messages (8)
msg354440 - (view) Author: Nika (Nika) Date: 2019-10-11 11:16
os.path.exists() accepts either True or False as argument and returns True.
Reproducible on Windows, e. g.,  in jupyter notebook or in Eclipse, although not in IDLE, which returns False, as expected.

import os
print(os.path.exists(False))
msg354443 - (view) Author: Mariano Anaya (Mariano Anaya) * Date: 2019-10-11 11:47
Hi, I would like to take a look at this issue. Could someone please assign it to me? Thanks
msg354444 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-10-11 12:17
Note that the underlying stat call supports file descriptors, which are non-negative integers. This is a supported and tested capability for genericpath.exists (see GenericTest.test_exists_fd in Lib/test/test_genericpath.py).

False and True are integers with the values 0 and 1: 

    >>> issubclass(bool, int)
    True
    >>> False + 0
    0
    >>> True + 0
    1

That can be useful, but there may be cases where we don't want to conflate bools and integers. IMO, a bool should not be supported as a file descriptor. It's likely a bug that should be caught early instead of meaninglessly propagated. 

A high-level solution would check for bool instances in genericpath.exists. A low-level solution, to make this policy consistent in general, would be to modify _fd_converter in Modules/posixmodule.c to disallow bool instances.
msg354481 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-10-11 18:26
Eryk, I think testing for bool in _fd_converter and issuing a warning is a good idea. Do you want to create a PR?
msg354495 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-10-11 20:58
Maybe os.fspath() can be used:

>>> os.fspath(True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected str, bytes or os.PathLike object, not bool
msg354510 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2019-10-11 22:33
Mariano Anaya, you're welcome to work on this and create a PR.

There's no need to have this issue assigned to you: feel free to just begin working. Anyone reading this will know that you intended to work on this.

(FYI, we only assign issues to core developers, and assigning means something slightly different than "who is working on this.")
msg354514 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-10-12 00:50
> Maybe os.fspath() can be used

I think that would already be the case if genericpath.exists didn't have to support file descriptors. It's documented that the path argument "refers to an existing path or an open file descriptor".

Modifying _fd_converter would provide consistent behavior for all calls that ultimately use argument clinic's path_t(allow_fd=True) and/or dir_fd. Serhiy's suggestion to raise a warning sounds good. 

Here are some examples that currently pass silently in Linux:

    >>> os.chown(False, 1000, 1000)
    >>> os.chmod(False, 0o666)
    >>> os.utime(False, (1500000000, 1500000000))

Probably os.fstat and other "f" functions (e.g. fstatvfs, fchdir, fchown, fchmod, ftruncate, fdatasync, fsync, and fpathconf) should also raise a warning when passed a bool. For example, the following would raise a warning instead of passing silently:

    >>> os.fstat(False).st_size
    0

These cases could be addressed by consistently using an argument clinic type. Some of them already us the fildes type (e.g. fchdir, fsync, fdatasync). However, fildes_converter calls PyObject_AsFileDescriptor, which also supports objects with a fileno() method. That's documented behavior for PyObject_AsFileDescriptor, but nothing in the documentation of fchdir, fsync, and fdatasync suggests to me that they support objects with a fileno() method:

    >>> os.fchdir(sys.stdin)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NotADirectoryError: [Errno 20] Not a directory

If not for this behavior, we could simply change all of the "f" functions to use the fildes type.

PyObject_AsFileDescriptor is used in various other places as well, such as the select module. In the latter case, supporting objects with a fileno() method is clearly documented.

Also consider including open() and os.fdopen by modifying _io_open_impl in Modules/_io/_iomodule.c. For example:

   >>> open(False, closefd=False)
    <_io.TextIOWrapper name=False mode='r' encoding='UTF-8'>

Currently it calls PyNumber_Check(file). If true, a warning could be raised if PyBool_Check(file) is also true.
msg377303 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-09-22 06:20
It is a large change, so I take it.
History
Date User Action Args
2022-04-11 14:59:21adminsetgithub: 82626
2020-09-22 06:20:54serhiy.storchakasetassignee: serhiy.storchaka
messages: + msg377303
2020-09-21 22:30:59iritkatrielsetcomponents: + IO
versions: + Python 3.9, Python 3.10, - Python 3.7
2019-10-14 14:04:07taleinatsetnosy: - taleinat
2019-10-14 13:12:37vstinnersetnosy: - vstinner
2019-10-12 00:50:15eryksunsetmessages: + msg354514
2019-10-11 22:33:37taleinatsetnosy: + taleinat
messages: + msg354510
2019-10-11 20:58:46vstinnersetmessages: + msg354495
2019-10-11 18:26:14serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg354481
2019-10-11 17:46:13nanjekyejoannahsetnosy: + vstinner
2019-10-11 12:17:27eryksunsetnosy: + eryksun
messages: + msg354444
2019-10-11 11:47:55Mariano Anayasetnosy: + Mariano Anaya
messages: + msg354443
2019-10-11 11:16:42Nikacreate