Skip to content

bpo-40170: Add _PyObject_CheckBuffer() internal function #19541

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ _PyObject_IS_GC(PyObject *obj)
|| Py_TYPE(obj)->tp_is_gc(obj)));
}

// Fast inlined version of PyObject_CheckBuffer()
static inline int
_PyObject_CheckBuffer(PyObject *obj)
{
PyBufferProcs *tp_as_buffer = Py_TYPE(obj)->tp_as_buffer;
return (tp_as_buffer != NULL && tp_as_buffer->bf_getbuffer != NULL);
}

// Fast inlined version of PyType_IS_GC()
#define _PyType_IS_GC(t) _PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)

Expand Down
8 changes: 4 additions & 4 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
#include "pycore_object.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h" // _PyThreadState_GET()
#include <ctype.h>
Expand Down Expand Up @@ -283,8 +284,7 @@ PyObject_DelItemString(PyObject *o, const char *key)
int
PyObject_CheckBuffer(PyObject *obj)
{
PyBufferProcs *tp_as_buffer = Py_TYPE(obj)->tp_as_buffer;
return (tp_as_buffer != NULL && tp_as_buffer->bf_getbuffer != NULL);
return _PyObject_CheckBuffer(obj);
}


Expand Down Expand Up @@ -615,8 +615,8 @@ int PyObject_CopyData(PyObject *dest, PyObject *src)
Py_ssize_t *indices, elements;
char *dptr, *sptr;

if (!PyObject_CheckBuffer(dest) ||
!PyObject_CheckBuffer(src)) {
if (!_PyObject_CheckBuffer(dest) ||
!_PyObject_CheckBuffer(src)) {
PyErr_SetString(PyExc_TypeError,
"both destination and source must be "\
"bytes-like objects");
Expand Down
4 changes: 2 additions & 2 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
}

/* Use the buffer API */
if (PyObject_CheckBuffer(arg)) {
if (_PyObject_CheckBuffer(arg)) {
Py_ssize_t size;
Py_buffer view;
if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
Expand Down Expand Up @@ -1614,7 +1614,7 @@ bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
char *buf;

/* bytearray_setslice code only accepts something supporting PEP 3118. */
if (PyObject_CheckBuffer(iterable_of_ints)) {
if (_PyObject_CheckBuffer(iterable_of_ints)) {
if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1)
return NULL;

Expand Down
3 changes: 2 additions & 1 deletion Objects/bytes_methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_bytes_methods.h"
#include "pycore_object.h"

PyDoc_STRVAR_shared(_Py_isspace__doc__,
"B.isspace() -> bool\n\
Expand Down Expand Up @@ -462,7 +463,7 @@ parse_args_finds_byte(const char *function_name, PyObject *args,
start, end))
return 0;

if (PyObject_CheckBuffer(tmp_subobj)) {
if (_PyObject_CheckBuffer(tmp_subobj)) {
*subobj = tmp_subobj;
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen)
return result;
}
/* does it support buffer protocol? */
if (PyObject_CheckBuffer(v)) {
if (_PyObject_CheckBuffer(v)) {
/* maybe we can avoid making a copy of the buffer object here? */
result = _PyBytes_FromBuffer(v);
if (result == NULL)
Expand Down Expand Up @@ -2740,7 +2740,7 @@ PyBytes_FromObject(PyObject *x)
}

/* Use the modern buffer interface */
if (PyObject_CheckBuffer(x))
if (_PyObject_CheckBuffer(x))
return _PyBytes_FromBuffer(x);

if (PyList_CheckExact(x))
Expand Down
2 changes: 1 addition & 1 deletion Objects/memoryobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ PyMemoryView_FromObject(PyObject *v)
CHECK_RELEASED(mv);
return mbuf_add_view(mv->mbuf, &mv->view);
}
else if (PyObject_CheckBuffer(v)) {
else if (_PyObject_CheckBuffer(v)) {
PyObject *ret;
mbuf = (_PyManagedBufferObject *)_PyManagedBuffer_FromObject(v);
if (mbuf == NULL)
Expand Down
2 changes: 1 addition & 1 deletion Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3936,7 +3936,7 @@ PyUnicode_FSDecoder(PyObject* arg, void* addr)
return 1;
}

is_buffer = PyObject_CheckBuffer(arg);
is_buffer = _PyObject_CheckBuffer(arg);
if (!is_buffer) {
path = PyOS_FSPath(arg);
if (path == NULL) {
Expand Down
3 changes: 2 additions & 1 deletion PC/winreg.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "pycore_object.h"
#include "structmember.h" // PyMemberDef
#include <windows.h>

Expand Down Expand Up @@ -689,7 +690,7 @@ Py2Reg(PyObject *value, DWORD typ, BYTE **retDataBuf, DWORD *retDataSize)
else {
Py_buffer view;

if (!PyObject_CheckBuffer(value)) {
if (!_PyObject_CheckBuffer(value)) {
PyErr_Format(PyExc_TypeError,
"Objects of type '%s' can not "
"be used as binary registry values",
Expand Down
3 changes: 2 additions & 1 deletion Python/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define PY_SSIZE_T_CLEAN

#include "Python.h"
#include "pycore_object.h"
#include "longintrepr.h"
#include "code.h"
#include "marshal.h"
Expand Down Expand Up @@ -526,7 +527,7 @@ w_complex_object(PyObject *v, char flag, WFILE *p)
w_long(co->co_firstlineno, p);
w_object(co->co_lnotab, p);
}
else if (PyObject_CheckBuffer(v)) {
else if (_PyObject_CheckBuffer(v)) {
/* Write unknown bytes-like objects as a bytes object */
Py_buffer view;
if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) {
Expand Down