Skip to content

Commit

Permalink
Switch on "new" buffer API.
Browse files Browse the repository at this point in the history
"Old" buffer API will removed in Python 3.10. This is also fix #7930.
  • Loading branch information
shadchin committed Feb 24, 2021
1 parent a43ddfb commit b1981b6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
12 changes: 6 additions & 6 deletions python/google/protobuf/pyext/message.cc
Expand Up @@ -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;
}

Expand All @@ -1942,7 +1941,8 @@ static PyObject* MergeFromString(CMessage* self, PyObject* arg) {
const char* ptr;
internal::ParseContext ctx(
depth, false, &ptr,
StringPiece(static_cast<const char*>(data), data_length));
StringPiece(static_cast<const char*>(data.buf), data.len));
PyBuffer_Release(&data);
ctx.data().pool = factory->pool->pool;
ctx.data().factory = factory->message_factory;

Expand All @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions python/tox.ini
Expand Up @@ -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
Expand Down

0 comments on commit b1981b6

Please sign in to comment.