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 unified C API for accessing bytes and bytearray #80246

Closed
salty-horse mannequin opened this issue Feb 21, 2019 · 7 comments
Closed

Add unified C API for accessing bytes and bytearray #80246

salty-horse mannequin opened this issue Feb 21, 2019 · 7 comments
Labels
3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@salty-horse
Copy link
Mannequin

salty-horse mannequin commented Feb 21, 2019

BPO 36065
Nosy @ronaldoussoren, @salty-horse, @serhiy-storchaka, @matrixise

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 2019-02-21.16:01:51.213>
created_at = <Date 2019-02-21.14:20:08.593>
labels = ['interpreter-core', 'type-feature', '3.8']
title = 'Add unified C API for accessing bytes and bytearray'
updated_at = <Date 2019-02-21.16:01:51.212>
user = 'https://github.com/salty-horse'

bugs.python.org fields:

activity = <Date 2019-02-21.16:01:51.212>
actor = 'serhiy.storchaka'
assignee = 'none'
closed = True
closed_date = <Date 2019-02-21.16:01:51.213>
closer = 'serhiy.storchaka'
components = ['Interpreter Core']
creation = <Date 2019-02-21.14:20:08.593>
creator = 'salty-horse'
dependencies = []
files = []
hgrepos = []
issue_num = 36065
keywords = []
message_count = 7.0
messages = ['336218', '336219', '336220', '336222', '336227', '336232', '336233']
nosy_count = 4.0
nosy_names = ['ronaldoussoren', 'salty-horse', 'serhiy.storchaka', 'matrixise']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue36065'
versions = ['Python 3.8']

@salty-horse
Copy link
Mannequin Author

salty-horse mannequin commented Feb 21, 2019

It would be useful to have a shared API for consuming bytes and bytearrays.

At present, I need to write very similar code twice. Some existing codebases only support bytes (perhaps forgetting bytearrays exist). Adding support for bytearray would be trivial if there was a shared API.

These are the functions/macros that can have "BytesOrByteArray" equivalents:

  • PyBytes_Check
  • PyBytes_CheckExact
  • PyBytes_Size
  • PyBytes_GET_SIZE
  • PyBytes_AsString
  • PyBytes_AS_STRING
  • PyBytes_AsStringAndSize

Here are some example implementations for the macros:

#define PyBytesOrByteArray_Check(ob) (PyBytes_Check(ob) || PyByteArray_Check(ob))
#define PyBytesOrByteArray_AS_STRING(ob) (PyBytes_Check(ob) ? PyBytes_AS_STRING(ob) : PyByteArray_AS_STRING(ob))
#define PyBytesOrByteArray_GET_SIZE(ob) #define PyByteArray_GET_SIZE(self) (assert(PyBytesOrByteArray_Check(self)), Py_SIZE(self))

@salty-horse salty-horse mannequin added 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Feb 21, 2019
@matrixise
Copy link
Member

I remove 2.7 because this branch is in bugfix mode, no new features.

@vstinner
Copy link
Member

Stéphane: Please don't add me to the nosy list of bugs.

@ronaldoussoren
Copy link
Contributor

What is your use case for this? Is that something that can use the buffer API instead of these low-level APIs?

@serhiy-storchaka
Copy link
Member

The unified C API already exists. It is called the buffer protocol.

https://docs.python.org/3/c-api/buffer.html#buffer-related-functions

@salty-horse
Copy link
Mannequin Author

salty-horse mannequin commented Feb 21, 2019

My use-case is modifying existing code that supports bytes to also support bytearray.

https://github.com/mongodb/mongo-python-driver/blob/9902d239b4e557c2a657e8c8110f7751864cec95/bson/_cbsonmodule.c#L1112

The buffer protocol, which I didn't know of, feels slightly complicated for my use-case. For now I opted to adding these macros myself, only changing 3 lines.

@serhiy-storchaka
Copy link
Member

If you need to support only bytes and bytearray, but not other bytes-like object, this is a too special case. It is easy to write your own macros or functions that wrap existing C API. Other option -- duplicate the code and replace PyBytes_ with PyByteArray_. In all cases be aware abot differences between bytes and bytearray: bytarray can change its content and size, saved values of PyByteArray_AS_STRING(ob) and PyByteArray_GET_SIZE(self) can not be used after executing arbitrary code in destructors or releasing GIT.

@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
3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants