Skip to content

Add public Py_fopen() and Py_fclose() functions, and remove private _Py_fopen_obj() function #51

@vstinner

Description

@vstinner

Hi,

Python 3.13 removed the private function _Py_wfopen(). A possible replacement is the private _Py_fopen_obj() function. Instead, I propose adding a clean public tested and documented Py_fopen() function, with a companion Py_fclose() function.

API:

FILE* Py_fopen(PyObject *path, const char *mode)
int Py_fclose(FILE *file)

Py_fopen() is similar to the fopen() function, but the path argument is a Python object (instead of char*), and an exception is set on error.

On Windows, files opened by Py_fopen() in the Python DLL must be closed by the Python DLL to use the same C runtime version. Otherwise, calling fclose() directly can cause undefined behavior. So I also propose adding a Py_close() function to avoid this problem.

Finally, I propose to remove the untested and undocumented _Py_fopen_obj() function, to promote the usage of the public tested and documented Py_fopen() function.

Py_fopen() and Py_fclose() are needed by the long list of C API functions which expect a FILE* argument.

Add Py_fopen() and Py_close():

Remove _Py_fopen_obj():

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions