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

Inadequate C-API to Python 3 I/O objects #54047

Closed
pv mannequin opened this issue Sep 12, 2010 · 3 comments
Closed

Inadequate C-API to Python 3 I/O objects #54047

pv mannequin opened this issue Sep 12, 2010 · 3 comments
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@pv
Copy link
Mannequin

pv mannequin commented Sep 12, 2010

BPO 9838
Nosy @pitrou, @tiran, @pv

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2013-07-08.15:40:30.543>
created_at = <Date 2010-09-12.13:23:45.418>
labels = ['extension-modules', 'type-feature']
title = 'Inadequate C-API to Python 3 I/O objects'
updated_at = <Date 2013-07-08.15:40:30.541>
user = 'https://github.com/pv'

bugs.python.org fields:

activity = <Date 2013-07-08.15:40:30.541>
actor = 'christian.heimes'
assignee = 'none'
closed = True
closed_date = <Date 2013-07-08.15:40:30.543>
closer = 'christian.heimes'
components = ['Extension Modules']
creation = <Date 2010-09-12.13:23:45.418>
creator = 'pv'
dependencies = []
files = []
hgrepos = []
issue_num = 9838
keywords = []
message_count = 3.0
messages = ['116188', '116192', '192662']
nosy_count = 3.0
nosy_names = ['pitrou', 'christian.heimes', 'pv']
pr_nums = []
priority = 'normal'
resolution = 'postponed'
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue9838'
versions = ['Python 3.2']

@pv
Copy link
Mannequin Author

pv mannequin commented Sep 12, 2010

The C-API exposed by the io module on Python 3.1/3.2 is very limited, and makes interfacing with Python file objects in extension modules difficult.

In more detail:

  1. Because the Python layer has buffering etc., the file handle returned by PyObject_AsFileDescriptor is not usable as-is. It requires flush and seek before use, every time there is a chance that the file object has been accessed on the Python side.

  2. There are no C-API functions such as the minimal set of PyFile_Write(buf, length), PyFile_Read(buf, length), PyFile_Seek(pos, whence), PyFile_Tell().

Instead, every call must go through PyObject_CallMethod, and the file objects only handle PyBytes and PyByteArray which are cumbersome and inefficient to use in extension modules.

@pv pv mannequin added extension-modules C modules in the Modules dir type-feature A feature request or enhancement labels Sep 12, 2010
@pitrou
Copy link
Member

pitrou commented Sep 12, 2010

Instead, every call must go through PyObject_CallMethod, and the file
objects only handle PyBytes and PyByteArray which are cumbersome
and inefficient to use in extension modules.

Because of the generic nature of the 3.x I/O stack, even shortcuts such as the proposed PyFile_Write will still have to use PyObject_CallMethod(obj, "write", ...) under the hood.

As for the types handled by file objects, you should be able to use a PyMemoryViewObject, which allows you to create a memory buffer without copying it (if that's what you're after).
You can also pass your own objects provided they support the new buffer API: http://docs.python.org/dev/c-api/buffer.html#bufferobjects
(I agree this documentation is not very well written, though)

So, bottom line, we could create a set of PyFile_* wrappers (or, rather, PyStream_*), but they wouldn't be much more efficient that what you can write by hand. Do you still think it's worth it? If so, I think you should float the idea on python-dev (the mailing-list).

@tiran
Copy link
Member

tiran commented Jul 8, 2013

I agree with Antoine. If you are still interested to improve the situation then please start a discussion on the python-ideas mailing list.

@tiran tiran closed this as completed Jul 8, 2013
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants