Skip to content
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

gh-116646: Add PyLong_FileDescriptor_Converter() function #116655

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions Doc/c-api/long.rst
Expand Up @@ -464,3 +464,10 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.

Otherwise, the return value is undefined.

.. c:function:: int PyLong_FileDescriptor_Converter(PyObject *p, void *ptr)

Convert a Python object to a file descript (C ``int`` type).
Set `*ptr` and return ``1`` on success. Set an exception and return ``0`` on error.
vstinner marked this conversation as resolved.
Show resolved Hide resolved

It can be used with the ``O&`` format when :ref:`parsing arguments
<arg-parsing>`.
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.13.rst
Expand Up @@ -1645,6 +1645,10 @@ New Features
between native integer types and Python :class:`int` objects.
(Contributed by Steve Dower in :gh:`111140`.)

* Add :c:func:`PyLong_FileDescriptor_Converter` function: convert a Python
object to a file descriptor. Function added the limited C API.
(Contributed by Victor Stinner in :gh:`116646`.)


Porting to Python 3.13
----------------------
Expand Down
6 changes: 6 additions & 0 deletions Include/fileutils.h
Expand Up @@ -50,6 +50,12 @@ PyAPI_FUNC(char*) Py_EncodeLocale(
size_t *error_pos);
#endif

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
// PyArg converter to convert a Python int objet to a C int.
// Return 1 on success. Set an exception and return 0 on error.
PyAPI_FUNC(int) PyLong_FileDescriptor_Converter(PyObject *, void *);
#endif

#ifndef Py_LIMITED_API
# define Py_CPYTHON_FILEUTILS_H
# include "cpython/fileutils.h"
Expand Down
3 changes: 0 additions & 3 deletions Include/internal/pycore_fileutils.h
Expand Up @@ -312,9 +312,6 @@ extern HRESULT PathCchSkipRoot(const wchar_t *pszPath, const wchar_t **ppszRootE
# define _Py_END_SUPPRESS_IPH
#endif /* _MSC_VER >= 1900 */

// Export for 'select' shared extension (Argument Clinic code)
PyAPI_FUNC(int) _PyLong_FileDescriptor_Converter(PyObject *, void *);

// Export for test_peg_generator
PyAPI_FUNC(char*) _Py_UniversalNewlineFgetsWithSize(char *, int, FILE*, PyObject *, size_t*);

Expand Down
@@ -0,0 +1,2 @@
Add PyLong_FileDescriptor_Converter function: convert a Python object to a
file descriptor. Function added the limited C API. Patch by Victor Stinner.
11 changes: 5 additions & 6 deletions Modules/clinic/fcntlmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 16 additions & 17 deletions Modules/clinic/posixmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 10 additions & 11 deletions Modules/clinic/selectmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.