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

Add support for PyFile_FromFile #44

Open
javaprog opened this issue Jan 28, 2021 · 1 comment
Open

Add support for PyFile_FromFile #44

javaprog opened this issue Jan 28, 2021 · 1 comment

Comments

@javaprog
Copy link
Contributor

In Python 2 there is only PyFile_FromFile, which accepts a file pointer, whereas in Python 3 there is only PyFile_FromFd, which accepts a file descriptor. To avoid changing legacy code and introducing special cases for both Python versions, I would like to suggest an implementation of PyFile_FromFile for Python 3 that simply forwards to the existing function: with the help of fileno() the file descriptor for a file pointer can be obtained.

@encukou
Copy link
Owner

encukou commented Jan 29, 2021

To anyone looking for hints: In #45, implementing PyFile_FromFile in terms of PyFile_FromFd and fileno didn't work because it leaked FILE* structures.


Unfortunately, "file objects" in Python 3 are quite different than in Python 2.
As the py3 docs say,

The [PyFile_* functions] are convenience C wrappers over these new APIs, and meant mostly for internal error reporting in the interpreter; third-party code is advised to access the io APIs instead.

Given that io is available on Python 2.6+, I'd rather encourage using that. It is what PyFile_FromFd does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants