Skip to content

gh-132742: Refactor fcntl.fcntl() and fcntl.ioctl() #132768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 24, 2025

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Apr 21, 2025

  • Support arbitrary bytes-like objects, not only bytes, in fcntl().
  • The fcntl() buffer argument is now null-terminated.
  • Automatically retry an ioctl() system calls failing with EINTR.
  • Release the GIL for an ioctl() system call even for large bytes-like object.
  • Do not silence arbitrary errors whet try to get a buffer.
  • Optimize argument parsing, check the argument type before trying to get a buffer or convert it to integer.
  • Fix some error messages.

📚 Documentation preview 📚: https://cpython-previews--132768.org.readthedocs.build/

@serhiy-storchaka serhiy-storchaka changed the title WIP: gh-132742: Refactor fcntl.fcntl() and fcntl.ioctl() gh-132742: Refactor fcntl.fcntl() and fcntl.ioctl() Apr 23, 2025
@serhiy-storchaka serhiy-storchaka marked this pull request as ready for review April 23, 2025 11:52
@serhiy-storchaka
Copy link
Member Author

@vstinner, could you please take a look?

Other changes are planned for other PRs.

char buf[IOCTL_BUFSZ+1]; /* argument plus NUL byte */
if (mutate_arg && !PyBytes_Check(arg) && !PyUnicode_Check(arg)) {
if (PyObject_GetBuffer(arg, &view, PyBUF_WRITABLE) == 0) {
if (view.len <= IOCTL_BUFSZ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many indentation levels (nested if), did you consider to split this code into sub-functions?

Co-authored-by: Victor Stinner <vstinner@python.org>
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@serhiy-storchaka
Copy link
Member Author

Thank you for your review, @vstinner.

I just tried to split the code on smaller functions. This adds many duplications: declarations of local variables ret, async_err, buf, functions parameters (there is a subtle difference for fcntl and ioct, it is easy to make an error). While there is a benefit if such refactoring (the conditions for types can be simpler), but there is also an additional risk of introducing errors, and the code will be larger. So I am not sure that the net effect is positive.

@serhiy-storchaka serhiy-storchaka merged commit 9f5994b into python:main Apr 24, 2025
42 checks passed
@serhiy-storchaka serhiy-storchaka deleted the fcntl-rewrite branch April 24, 2025 16:17
@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 Windows Server 2022 NoGIL 3.x (tier-1) has failed when building commit 9f5994b.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1241/builds/5397) and take a look at the build logs.
  4. Check if the failure is related to this commit (9f5994b) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/1241/builds/5397

Failed tests:

  • test_opcache

Summary of the results of the build (if available):

==

Click to see traceback logs
remote: Enumerating objects: 26, done.        
remote: Counting objects:   3% (1/26)        
remote: Counting objects:   7% (2/26)        
remote: Counting objects:  11% (3/26)        
remote: Counting objects:  15% (4/26)        
remote: Counting objects:  19% (5/26)        
remote: Counting objects:  23% (6/26)        
remote: Counting objects:  26% (7/26)        
remote: Counting objects:  30% (8/26)        
remote: Counting objects:  34% (9/26)        
remote: Counting objects:  38% (10/26)        
remote: Counting objects:  42% (11/26)        
remote: Counting objects:  46% (12/26)        
remote: Counting objects:  50% (13/26)        
remote: Counting objects:  53% (14/26)        
remote: Counting objects:  57% (15/26)        
remote: Counting objects:  61% (16/26)        
remote: Counting objects:  65% (17/26)        
remote: Counting objects:  69% (18/26)        
remote: Counting objects:  73% (19/26)        
remote: Counting objects:  76% (20/26)        
remote: Counting objects:  80% (21/26)        
remote: Counting objects:  84% (22/26)        
remote: Counting objects:  88% (23/26)        
remote: Counting objects:  92% (24/26)        
remote: Counting objects:  96% (25/26)        
remote: Counting objects: 100% (26/26)        
remote: Counting objects: 100% (26/26), done.        
remote: Compressing objects:   7% (1/14)        
remote: Compressing objects:  14% (2/14)        
remote: Compressing objects:  21% (3/14)        
remote: Compressing objects:  28% (4/14)        
remote: Compressing objects:  35% (5/14)        
remote: Compressing objects:  42% (6/14)        
remote: Compressing objects:  50% (7/14)        
remote: Compressing objects:  57% (8/14)        
remote: Compressing objects:  64% (9/14)        
remote: Compressing objects:  71% (10/14)        
remote: Compressing objects:  78% (11/14)        
remote: Compressing objects:  85% (12/14)        
remote: Compressing objects:  92% (13/14)        
remote: Compressing objects: 100% (14/14)        
remote: Compressing objects: 100% (14/14), done.        
remote: Total 14 (delta 12), reused 2 (delta 0), pack-reused 0 (from 0)        
From https://github.com/python/cpython
 * branch                    main       -> FETCH_HEAD
Note: switching to '9f5994b94cb6b8526bcb8fa29a99656dc403e25e'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 9f5994b94cb gh-132742: Refactor fcntl.fcntl() and fcntl.ioctl()  (GH-132768)
Switched to and reset branch 'main'

Could Not Find C:\Users\Administrator\buildarea\3.x.itamaro-win64-srv-22-aws.x64.nogil\build\Lib\*.pyc
The system cannot find the file specified.
Could Not Find C:\Users\Administrator\buildarea\3.x.itamaro-win64-srv-22-aws.x64.nogil\build\PCbuild\python*.zip

Could Not Find C:\Users\Administrator\buildarea\3.x.itamaro-win64-srv-22-aws.x64.nogil\build\PCbuild\python*.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants