-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add public Py_fopen() and Py_fclose() functions, and remove private _Py_fopen_obj() function #51
Comments
I think the new functions are good, and essential, so we should add them. I'd prefer to keep the old name around for another version or two, even if it's only an alias for the new name, so that users have a bit more overlap in which to change the name (i.e. if they care about 3.13 and 3.14, they can keep using the old name for now). |
For the backward compatibility, I plan to add these functions to pythoncapi-compat, so they will be usable on Python 3.6+. |
Ideally everyone would use Python file objects instead of
How will |
I guess that it will just call fclose(). I don't know if it would work as expected or not on Windows. |
Yeah, we went through the "ideally" on the bug already, and there are just too many public/"stable" APIs taking At least being able to keep the
I imagine it would break for the user who reported the issue in the first place, since they were using static linking.1 But there really isn't a way around that as far as I can tell. And also no way to detect it other than trying to do it and hopefully failing (as opposed to crashing or corrupting data). Footnotes
|
Could you please give a reference? This looks like a bad news. Some of the highlevel C API can close the input file. If |
Unfortunately, I'm not aware of any direct reference. The team behind the runtimes consider mixing runtime implementations out of their scope, so they don't document the things that may happen. To derive the problem from first principles, you have to recognise that distinct DLLs have their own global variables, that static linking essentially treats the "host" process/DLL as the global variable scope, that the debug UCRT is a different DLL from the standard one (and both can be loaded simultaneously), that open files in the UCRT are stored in a global variable1, and that the Thus, multiple instances of the UCRT will have multiple instances of the array and are allocating file objects with the same index but different addresses. The
You can if you compile with normal settings. If you statically link the C runtime, or insist on using a debug CRT with a release CPython (using the release CRT), then you may well encounter issues. But when all the libraries use the same runtime, which is the usual case, there's no problem. Footnotes
|
The Since removing |
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 documentedPy_fopen()
function, with a companionPy_fclose()
function.API:
Py_fopen()
is similar to thefopen()
function, but the path argument is a Python object (instead ofchar*
), 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, callingfclose()
directly can cause undefined behavior. So I also propose adding aPy_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 documentedPy_fopen()
function.Py_fopen()
andPy_fclose()
are needed by the long list of C API functions which expect aFILE*
argument.Add Py_fopen() and Py_close():
Remove
_Py_fopen_obj()
:The text was updated successfully, but these errors were encountered: