Avoiding PyObject_AsCharBuffer from the Limited API

PyObject_AsCharBuffer does exactly what I want: It allows me to extract the data from a python array of bytes (from within C). The trouble is that it has been deprecated for years, and I am supposed to use the “new” buffer protocol. The trouble with that is that PyBuf is an opaque type when using the Limited API.

What is the recommended approach?

  • Add lots of preprocessor magic to switch off deprecation warnings with both GCC and Clang for the duration of the call?
  • Persuade the Python devs to not deprecate PyObject_AsCharBuffer when building the Limited API? (But that causes grief for source that wants to work both Limited and non-Limited, and doesn’t work when I am still building against Python 3.10 source)
  • Call this other obvious function? (This is my preferred solution, but I can’t find what the function is called.)

As of 3.11, the buffer protocol is actually now part of the Limited API. Doesn’t help for versions before that as much, though, I’m not sure about best practice there.

1 Like

Ah ha! That’s helpful.

I’m currently building against Python 3.10, so I could declare the buffer protocol functions and types myself (to match the real declaration), and then remove that as and when we move to 3.11 - but that feels really unpleasant. Or I could add in the preprocessor noise for the moment, and rip it out when we move to 3.11

I do still wonder if it would be sensible to undeprecate PyObject_AsCharBuffer, given that (as part of the Stable ABI) it can never be removed. (I suppose it could be removed from the external header files, but left as a symbol that an older extension can be linked against).