Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ Python 3.14

See `PyLong_FromUInt64() documentation <https://docs.python.org/dev/c-api/long.html#c.PyLong_FromUInt64>`__.

.. c:function:: FILE* Py_fopen(PyObject *path, const char *mode)

See `Py_fopen() documentation <https://docs.python.org/dev/c-api/sys.html#c.Py_fopen>`__.

.. c:function:: int Py_fclose(FILE *file)

See `Py_fclose() documentation <https://docs.python.org/dev/c-api/sys.html#c.Py_fclose>`__.


Not supported:

Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Changelog
=========

* 2025-01-06: Add ``Py_fopen()`` and ``Py_fclose()`` functions.
* 2024-12-16: Add ``structmember.h`` constants:

* ``Py_T_BOOL``
Expand Down
41 changes: 41 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,47 @@ PyLongWriter_Finish(PyLongWriter *writer)
#endif


// gh-127350 added Py_fopen() and Py_fclose() to Python 3.14a4
#if PY_VERSION_HEX < 0x030E00A4
static inline FILE* Py_fopen(PyObject *path, const char *mode)
{
#if 0x030400A2 <= PY_VERSION_HEX && !defined(PYPY_VERSION)
extern FILE* _Py_fopen_obj(PyObject *path, const char *mode);
return _Py_fopen_obj(path, mode);
#else
FILE *f;
PyObject *bytes;
#if PY_VERSION_HEX >= 0x03000000
if (!PyUnicode_FSConverter(path, &bytes)) {
return NULL;
}
#else
if (!PyString_Check(path)) {
PyErr_SetString(PyExc_TypeError, "except str");
return NULL;
}
bytes = Py_NewRef(path);
#endif
const char *path_bytes = PyBytes_AS_STRING(bytes);

f = fopen(path_bytes, mode);
Py_DECREF(bytes);

if (f == NULL) {
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
return NULL;
}
return f;
#endif
}

int Py_fclose(FILE *file)
{
return fclose(file);
}
#endif


#ifdef __cplusplus
}
#endif
Expand Down
16 changes: 16 additions & 0 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -2097,6 +2097,21 @@ test_structmember(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
}


static PyObject *
test_file(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
const char *filename = __FILE__;
PyObject *path = create_string(filename);

FILE *fp = Py_fopen(path, "rb");
Py_DECREF(path);
assert(fp != NULL);
Py_fclose(fp);

Py_RETURN_NONE;
}


static struct PyMethodDef methods[] = {
{"test_object", test_object, METH_NOARGS, _Py_NULL},
{"test_py_is", test_py_is, METH_NOARGS, _Py_NULL},
Expand Down Expand Up @@ -2144,6 +2159,7 @@ static struct PyMethodDef methods[] = {
{"test_iter", test_iter, METH_NOARGS, _Py_NULL},
{"test_long_stdint", test_long_stdint, METH_NOARGS, _Py_NULL},
{"test_structmember", test_structmember, METH_NOARGS, _Py_NULL},
{"test_file", test_file, METH_NOARGS, _Py_NULL},
{_Py_NULL, _Py_NULL, 0, _Py_NULL}
};

Expand Down
Loading