Repository: python/cpython
Pull request: gh-127385: Add `F_DUPFD_QUERY` to `fcntl`
<!--
Thanks for your contribution!
Please read this comment in its entirety. It's quite important.

# Pull Request title

It should be in the following format:

```
gh-NNNNN: Summary of the changes made
```

Where: gh-NNNNN refers to the GitHub issue number.

Most PRs will require an issue number. Trivial changes, like fixing a typo, do not need an issue.

# Backport Pull Request title

If this is a backport PR (PR made against branches other than `main`),
please ensure that the PR title is in the following format:

```
[X.Y] <title from the original PR> (GH-NNNN)
```

Where: [X.Y] is the branch name, e.g. [3.6].

GH-NNNN refers to the PR number from `main`.

-->



<!-- gh-issue-number: gh-127385 -->
* Issue: gh-127385
<!-- /gh-issue-number -->


Problem statement:
Add `F_DUPFD_QUERY` to `fcntl`
# Feature or enhancement

### Proposal:

It's used to query and copy a target file descriptor. for example:
```
import fcntl

f = /* open a file */;
/* 'ret' must be greater equal than 5 */
ret = fcntl.fcntl(fd, F_DUPFD_QUERY, 5);
```


### Has this already been discussed elsewhere?

No response given

### Links to previous discussion of this feature:

_No response_

<!-- gh-linked-prs -->
### Linked PRs
* gh-127386
<!-- /gh-linked-prs -->

Hints from issue discussion:

Commit message: Add note

Patch to review:
diff --git a/Doc/library/fcntl.rst b/Doc/library/fcntl.rst
index 7bd64e43dd5bfe..975da9bbb9db3e 100644
--- a/Doc/library/fcntl.rst
+++ b/Doc/library/fcntl.rst
@@ -79,6 +79,10 @@ descriptor.
    On macOS and NetBSD, the :mod:`!fcntl` module exposes the ``F_GETNOSIGPIPE``
    and ``F_SETNOSIGPIPE`` constant.
 
+.. versionchanged:: 3.14
+   On Linux >= 6.1, the :mod:`!fcntl` module exposes the ``F_DUPFD_QUERY``
+   to query a file descriptor pointing to the same file.
+
 The module defines the following functions:
 
 
diff --git a/Misc/NEWS.d/next/Library/2024-11-29-13-06-52.gh-issue-127385.PErcyB.rst b/Misc/NEWS.d/next/Library/2024-11-29-13-06-52.gh-issue-127385.PErcyB.rst
new file mode 100644
index 00000000000000..809ec9d9a0c0ce
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-11-29-13-06-52.gh-issue-127385.PErcyB.rst
@@ -0,0 +1 @@
+Add the ``F_DUPFD_QUERY`` macro constants to the :mod:`fcntl`.
diff --git a/Modules/fcntlmodule.c b/Modules/fcntlmodule.c
index 90ebfd7e99a777..72cdc34a31d2f7 100644
--- a/Modules/fcntlmodule.c
+++ b/Modules/fcntlmodule.c
@@ -565,6 +565,9 @@ all_ins(PyObject* m)
 #ifdef F_NOTIFY
     if (PyModule_AddIntMacro(m, F_NOTIFY)) return -1;
 #endif
+#ifdef F_DUPFD_QUERY
+    if (PyModule_AddIntMacro(m, F_DUPFD_QUERY)) return -1;
+#endif
 /* Old BSD flock(). */
 #ifdef F_EXLCK
     if (PyModule_AddIntMacro(m, F_EXLCK)) return -1;


Code review comments:
diff_hunk: @@ -0,0 +1 @@
+Add the ``F_DUPFD_QUERY`` macro constants to the :mod:`fcntl`.
path: Misc/NEWS.d/next/Library/2024-11-29-13-06-52.gh-issue-127385.PErcyB.rst
text: @user1:
```suggestion
Add the ``F_DUPFD_QUERY`` constant to the :mod:`fcntl` module.
```