New submission from Ori Avtalion <o...@avtalion.name>:

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))

----------
components: Interpreter Core
messages: 336218
nosy: salty-horse
priority: normal
severity: normal
status: open
title: Add unified C API for accessing bytes and bytearray
type: enhancement
versions: Python 2.7, Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue36065>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to