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: posixpath.c:_fd_converter() should use PyObject_AsFileDescriptor()
Type: enhancement Stage: resolved
Components: Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, christian.heimes, vstinner
Priority: normal Keywords:

Created on 2013-06-21 11:12 by christian.heimes, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (10)
msg191563 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-06-21 11:12
I suggest that posixpath.c:_fd_converter() should  PyObject_AsFileDescriptor() to convert a Python object to a fd integer. With PyObject_AsFileDescriptor() functions such as os.chmod() can be called with an opened file as first argument:

Now:

  with open("somefile") as f:
     os.chmod(f.fileno(), 0o644)

With PyObject_AsFileDescriptor():

    with open("somefile") as f:
     os.chmod(f, 0o644)

_fd_converter() also has more elaborate overflow checks. These checks should be added to PyObject_AsFileDescriptor(), too.
msg191564 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2013-06-21 11:20
Do we want the low-level os.write() to work with file objects, or sockets?
msg191565 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-06-21 11:29
This issue looks to be related to #18269.
msg191569 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-06-21 11:52
os.write() already works with file and sockets object but you have to call ob.fileno() first. The select module uses PyObject_AsFileDescriptor() all over the module to get the file descriptor number from file and socket objects.

Let's unify os' and select's behavior!
msg191572 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2013-06-21 12:08
> Let's unify os' and select's behavior!
OK, let's do that on Windows first :-)
msg191573 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-06-21 12:11
> OK, let's do that on Windows first :-)

Gotcha :( Damn you winsock.dll!
msg191574 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-06-21 12:27
> OK, let's do that on Windows first :-)

For the PEP 433, I did something like that in os.get_cloexec() and os.set_cloexec():

http://hg.python.org/features/pep-433/file/f32c2b09f332/Modules/posixmodule.c#l10198

On Windows, I added two code paths: one for HANDLE (to support sockets), one for file descriptors (classic files). It means that you need a C function supports HANDLE as input type, rather than int (file descriptor).

Can't we raise a nice error message when we get a socket, whereas the function does not support HANDLE? What is the current message on Windows when passing sock.fileno() to a function expecting a file descriptor? Would it be worse if Python implictly call .fileno() on a socket?
msg404586 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-10-21 11:57
Victor, do we want to support "with open("somefile") as f: os.chmod(f, 0o644)"? The feature request has been languishing for 8 years.
msg404691 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-21 22:26
I like calling .fileno() explicitly. It seems like it's not really needed to call it implicitly, since this issue doesn't get much activity. I suggest to reject it.
msg404692 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-10-21 22:27
Fine with me! Thanks for your feedback.
History
Date User Action Args
2022-04-11 14:57:47adminsetgithub: 62476
2021-10-21 22:27:49christian.heimessetstatus: open -> closed
resolution: rejected
messages: + msg404692

stage: needs patch -> resolved
2021-10-21 22:26:59vstinnersetstatus: pending -> open

messages: + msg404691
2021-10-21 11:57:32christian.heimessetstatus: open -> pending

messages: + msg404586
versions: + Python 3.11, - Python 3.4
2013-06-21 12:27:27vstinnersetmessages: + msg191574
2013-06-21 12:11:07christian.heimessetmessages: + msg191573
2013-06-21 12:08:07amaury.forgeotdarcsetmessages: + msg191572
2013-06-21 11:52:17christian.heimessetmessages: + msg191569
2013-06-21 11:29:57vstinnersetnosy: + vstinner
messages: + msg191565
2013-06-21 11:20:52amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg191564
2013-06-21 11:12:42christian.heimescreate