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.

Author eryksun
Recipients Mariano Anaya, Nika, eryksun, serhiy.storchaka, taleinat, vstinner
Date 2019-10-12.00:50:14
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1570841415.31.0.436808755077.issue38445@roundup.psfhosted.org>
In-reply-to
Content
> 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.
History
Date User Action Args
2019-10-12 00:50:15eryksunsetrecipients: + eryksun, vstinner, taleinat, serhiy.storchaka, Mariano Anaya, Nika
2019-10-12 00:50:15eryksunsetmessageid: <1570841415.31.0.436808755077.issue38445@roundup.psfhosted.org>
2019-10-12 00:50:15eryksunlinkissue38445 messages
2019-10-12 00:50:14eryksuncreate