Files
RustPython/Lib/test
Richard Hozák 31fdb204d8 Allow fcntl functions to accept objects with fileno() function (#4144)
As specified in Python official documentation
(https://docs.python.org/3.10/library/fcntl.html)

> All functions in this module take a file descriptor fd as their first
> argument. This can be an integer file descriptor, such as returned by
> sys.stdin.fileno(), or an io.IOBase object, such as sys.stdin itself,
> which provides a fileno() that returns a genuine file descriptor.

And clarified more in fcntl.fcntl function:

> Perform the operation cmd on file descriptor fd (file objects
> providing a fileno() method are accepted as well).

All function in fcntl modules should accept either int fd or object with
fileno() function which returns int fd.

This was already implemented with for `fcntl.fcntl` function with
`io::Fildes` newtype helper which extracted either int fd or called
fileno() function, and it was also implemented with duplicated ad-hoc
code for `fcntl.ioctl`.

This commit replaces the ad-hoc implementation with `io::Fildes` and
adds it to missing functions: `fcntl.flock` and `fcntl.lockf`.

For more information that this is implemented in the same way as
CPython, you can check the corresponding CPython module:
https://github.com/python/cpython/blob/3.10/Modules/clinic/fcntlmodule.c.h

The functions are: `fcntl_fcntl`, `fcntl_ioctl`, `fcntl_flock` and
`fcntl_lockf`, all of these functions use
`_PyLong_FileDescriptor_Converter` which does the same thing as
RustPython's `io::Fildes`, meaning it either extracts the argument as
int fd or calls fileno() function on passed object that returns the int
fd.

Here is the implementation for `_PyLong_FileDescriptor_Converter`:
https://github.com/python/cpython/blob/3.10/Objects/fileobject.c#L227
which in turns calls `PyObject_AsFileDescriptor` which is located here
https://github.com/python/cpython/blob/3.10/Objects/fileobject.c#L180 in
the same file and we can see that it tries to convert it to int or call
fileno() function.

Note regarding the python unit tests `test_flock`,
`test_lockf_exclusive`, `test_lockf_shared`:

The tests no longer fail with `TypeError: 'BufferedRandom' object cannot
be interpreted as an integer` which makes `test_flock` pass the test,
but `test_lockf_exclusive` and `test_lockf_exclusive` still fail but not
on fcntl calls, they fail on `Process()` constructor with
`AttributeError: module 'os' has no attribute 'fork'` which seems
unrelated with this change.

This unrelated error was probably never detected as fcntl calls failed
earlier, before `Process()` could even be called.
2022-09-02 02:39:02 +09:00
..
2022-08-18 03:22:20 +09:00
2020-01-04 16:30:02 +02:00
2022-04-23 08:06:17 +09:00
2022-07-14 00:31:40 +09:00
2022-07-14 00:31:40 +09:00
2021-02-19 13:38:57 -05:00
2021-02-19 13:38:57 -05:00
2021-02-19 13:38:57 -05:00
2021-02-20 20:37:25 -05:00
2021-02-20 20:37:25 -05:00
2022-02-13 17:56:08 -05:00
2020-11-12 11:30:33 +09:00
2021-04-18 14:44:36 -04:00
2022-02-13 17:56:08 -05:00
2021-02-25 22:50:48 -05:00
2021-02-25 22:50:48 -05:00
2021-02-25 22:50:48 -05:00
2021-04-21 11:17:00 +02:00
2019-12-30 21:31:36 +02:00
2022-02-12 20:48:57 -05:00
2022-01-25 23:31:23 +09:00
2022-04-21 09:49:17 +02:00
2022-02-13 17:56:08 -05:00
2021-10-10 19:11:23 -04:00
2022-07-18 04:07:35 +09:00
2021-11-28 20:51:32 +08:00
2021-11-28 20:51:32 +08:00
2021-11-28 20:51:32 +08:00
2021-10-10 19:11:23 -04:00
2021-11-28 20:51:32 +08:00
2021-11-28 20:51:32 +08:00
2022-07-18 04:07:35 +09:00
2022-08-13 08:39:58 +09:00
2021-11-29 21:03:02 +08:00
2021-11-29 21:03:02 +08:00
2022-07-24 19:47:31 +09:00
2020-01-26 01:18:51 +09:00
2022-03-22 02:22:40 +09:00
2022-04-27 22:56:24 -04:00
2022-01-29 20:17:36 -05:00
2021-11-28 20:51:32 +08:00
2021-11-28 20:51:32 +08:00
2021-11-29 21:03:02 +08:00
2022-07-16 21:28:01 +09:00
2022-07-18 22:52:26 +09:00
2021-02-16 21:57:50 -05:00
2022-07-25 04:10:59 +09:00
2022-07-16 13:42:53 +09:00
2021-11-20 20:56:26 +09:00
2022-08-11 07:14:20 +09:00
2021-11-28 20:51:32 +08:00
2022-08-15 05:25:42 +09:00
2022-07-13 17:02:17 +09:00
2022-08-04 15:44:40 +09:00
2022-08-15 05:25:42 +09:00
2021-02-21 18:47:55 -05:00
2022-05-30 07:36:07 +09:00
2022-06-29 18:02:57 +08:00
2022-08-15 04:24:58 +09:00
2021-08-07 20:24:41 +09:00
2022-05-04 07:35:44 +09:00
2022-08-07 04:15:21 +09:00
2021-05-13 19:46:17 -05:00
2021-11-28 20:51:32 +08:00
2022-08-16 10:08:03 +09:00
2022-08-15 02:36:46 +09:00
2021-04-25 12:59:12 -04:00
2022-08-15 02:36:46 +09:00
2021-01-26 10:30:09 -06:00
2022-08-15 02:36:46 +09:00
2022-07-27 14:43:47 +09:00
2022-07-25 22:07:26 +02:00
2021-11-28 23:25:17 +08:00
2022-08-15 01:08:58 +09:00
2022-06-21 22:00:08 +09:00
2021-11-29 21:03:02 +08:00
2022-08-15 04:24:50 +09:00
2022-08-15 01:08:58 +09:00
2022-07-24 15:50:21 +09:00
2021-11-28 20:51:32 +08:00
2021-11-28 20:51:32 +08:00
2021-11-20 18:59:59 +09:00
2022-08-15 01:08:58 +09:00
2022-08-15 01:08:58 +09:00
2022-02-12 23:51:47 -05:00
2022-04-16 00:00:31 +09:00
2021-05-13 20:09:23 -04:00
2022-08-13 08:39:58 +09:00
2021-05-16 13:50:34 -04:00
2022-04-26 22:26:38 -04:00
2022-08-20 22:58:58 +09:00
2021-11-29 21:03:02 +08:00
2022-08-15 00:34:06 +09:00
2022-08-14 23:08:08 +09:00
2022-08-15 00:34:06 +09:00
2022-07-25 04:10:59 +09:00
2021-08-31 23:53:05 +09:00
2022-07-25 04:10:59 +09:00
2021-11-28 20:51:33 +08:00
2022-03-12 21:39:42 +08:00
2022-07-19 01:35:32 +09:00
2022-07-25 04:10:59 +09:00
2021-07-12 19:51:20 -04:00
2021-07-12 20:02:14 -04:00
2022-08-09 03:53:17 +09:00
2021-05-21 19:19:34 -04:00
2021-05-21 19:19:34 -04:00