[issue38445] os.path.exists() takes bool as argument and returns True

Eryk Sun report at bugs.python.org
Fri Oct 11 08:17:27 EDT 2019


Eryk Sun <eryksun at gmail.com> added the comment:

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.

----------
nosy: +eryksun

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38445>
_______________________________________


More information about the Python-bugs-list mailing list