diff --git a/Include/internal/pycore_object.h b/Include/internal/pycore_object.h index 32e86d06db5b43..eeb926273d2d42 100644 --- a/Include/internal/pycore_object.h +++ b/Include/internal/pycore_object.h @@ -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) diff --git a/Objects/abstract.c b/Objects/abstract.c index 6e390dd92c3aef..0dcef4104ee24e 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -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 @@ -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); } @@ -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"); diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index b271e57abb6866..97e34995dd1343 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -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) @@ -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; diff --git a/Objects/bytes_methods.c b/Objects/bytes_methods.c index 72daa1fdd554e0..d9c72705fefcbf 100644 --- a/Objects/bytes_methods.c +++ b/Objects/bytes_methods.c @@ -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\ @@ -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; } diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index 06ead2b58f980f..d650d13104c1f3 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -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) @@ -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)) diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index 682bbe8a61e85e..67444859061bb5 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -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) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 51775df199d1ea..5ec9848f32220e 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -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) { diff --git a/PC/winreg.c b/PC/winreg.c index 3e13e75826f157..38ce25493438e2 100644 --- a/PC/winreg.c +++ b/PC/winreg.c @@ -14,6 +14,7 @@ #define PY_SSIZE_T_CLEAN #include "Python.h" +#include "pycore_object.h" #include "structmember.h" // PyMemberDef #include @@ -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", diff --git a/Python/marshal.c b/Python/marshal.c index b4429aea502d3f..eb53812adfc27b 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -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" @@ -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) {