From 1f8f5cf80cc7ab1912c69fcc4a429cae8f4694d2 Mon Sep 17 00:00:00 2001 From: Alexander Shadchin Date: Wed, 24 Feb 2021 04:03:15 +0300 Subject: [PATCH] Switch on "new" buffer API. "Old" buffer API will removed in Python 3.10. This is also fix #7930. --- python/google/protobuf/pyext/message.cc | 12 ++++++------ python/tox.ini | 5 +---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/python/google/protobuf/pyext/message.cc b/python/google/protobuf/pyext/message.cc index 4e74386e2d1b..580b0d1c570d 100644 --- a/python/google/protobuf/pyext/message.cc +++ b/python/google/protobuf/pyext/message.cc @@ -1927,9 +1927,8 @@ PyObject* SetAllowOversizeProtos(PyObject* m, PyObject* arg) { } static PyObject* MergeFromString(CMessage* self, PyObject* arg) { - const void* data; - Py_ssize_t data_length; - if (PyObject_AsReadBuffer(arg, &data, &data_length) < 0) { + Py_buffer data; + if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) < 0) { return NULL; } @@ -1942,7 +1941,8 @@ static PyObject* MergeFromString(CMessage* self, PyObject* arg) { const char* ptr; internal::ParseContext ctx( depth, false, &ptr, - StringPiece(static_cast(data), data_length)); + StringPiece(static_cast(data.buf), data.len)); + PyBuffer_Release(&data); ctx.data().pool = factory->pool->pool; ctx.data().factory = factory->message_factory; @@ -1968,9 +1968,9 @@ static PyObject* MergeFromString(CMessage* self, PyObject* arg) { // TODO(jieluo): Raise error and return NULL instead. // b/27494216 PyErr_Warn(nullptr, "Unexpected end-group tag: Not all data was converted"); - return PyInt_FromLong(data_length - ctx.BytesUntilLimit(ptr)); + return PyInt_FromLong(data.len - ctx.BytesUntilLimit(ptr)); } - return PyInt_FromLong(data_length); + return PyInt_FromLong(data.len); } static PyObject* ParseFromString(CMessage* self, PyObject* arg) { diff --git a/python/tox.ini b/python/tox.ini index 9fabb6ddbb25..c3f24e2e2131 100644 --- a/python/tox.ini +++ b/python/tox.ini @@ -14,10 +14,7 @@ setenv = commands = python setup.py -q build_py python: python setup.py -q build - # --warnings_as_errors disabled until we update the Python C extension. See: - # https://github.com/protocolbuffers/protobuf/issues/7930 - # cpp: python setup.py -q build --cpp_implementation --warnings_as_errors --compile_static_extension - cpp: python setup.py -q build --cpp_implementation --compile_static_extension + cpp: python setup.py -q build --cpp_implementation --warnings_as_errors --compile_static_extension python: python setup.py -q test -q cpp: python setup.py -q test -q --cpp_implementation python: python setup.py -q test_conformance