diff options
author | Feng Xiao <xfxyjwf@gmail.com> | 2018-06-03 14:54:53 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-03 14:54:53 -0700 |
commit | a7252bf42df8f0841cf3a0c85fdbf1a5172adecb (patch) | |
tree | 1faaacdc957f50a0b2f0a8819ca47d93f102cae0 /python/google/protobuf/pyext/message.cc | |
parent | 17ab85deab32b09b751aa28ec9ad548236e7128a (diff) | |
parent | 7c6514245b6e256cc97dd31a6da1e2c72de487e6 (diff) | |
download | protobuf-a7252bf42df8f0841cf3a0c85fdbf1a5172adecb.tar.gz protobuf-a7252bf42df8f0841cf3a0c85fdbf1a5172adecb.tar.bz2 protobuf-a7252bf42df8f0841cf3a0c85fdbf1a5172adecb.zip |
Merge pull request #4698 from ThomasColthurst/proto_c_api
Introduce Proto C API; based on cl/198113115 by amauryfa
Diffstat (limited to 'python/google/protobuf/pyext/message.cc')
-rw-r--r-- | python/google/protobuf/pyext/message.cc | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/python/google/protobuf/pyext/message.cc b/python/google/protobuf/pyext/message.cc index 5893533a..53736b9c 100644 --- a/python/google/protobuf/pyext/message.cc +++ b/python/google/protobuf/pyext/message.cc @@ -2866,17 +2866,38 @@ const Message* (*GetCProtoInsidePyProtoPtr)(PyObject* msg); Message* (*MutableCProtoInsidePyProtoPtr)(PyObject* msg); static const Message* GetCProtoInsidePyProtoImpl(PyObject* msg) { + const Message* message = PyMessage_GetMessagePointer(msg); + if (message == NULL) { + PyErr_Clear(); + return NULL; + } + return message; +} + +static Message* MutableCProtoInsidePyProtoImpl(PyObject* msg) { + Message* message = PyMessage_GetMutableMessagePointer(msg); + if (message == NULL) { + PyErr_Clear(); + return NULL; + } + return message; +} + +const Message* PyMessage_GetMessagePointer(PyObject* msg) { if (!PyObject_TypeCheck(msg, &CMessage_Type)) { + PyErr_SetString(PyExc_TypeError, "Not a Message instance"); return NULL; } CMessage* cmsg = reinterpret_cast<CMessage*>(msg); return cmsg->message; } -static Message* MutableCProtoInsidePyProtoImpl(PyObject* msg) { +Message* PyMessage_GetMutableMessagePointer(PyObject* msg) { if (!PyObject_TypeCheck(msg, &CMessage_Type)) { + PyErr_SetString(PyExc_TypeError, "Not a Message instance"); return NULL; } + CMessage* cmsg = reinterpret_cast<CMessage*>(msg); if ((cmsg->composite_fields && PyDict_Size(cmsg->composite_fields) != 0) || (cmsg->extensions != NULL && @@ -2885,6 +2906,9 @@ static Message* MutableCProtoInsidePyProtoImpl(PyObject* msg) { // the underlying C++ message back to the CMessage (e.g. removed repeated // composite containers). We only allow direct mutation of the underlying // C++ message if there is no child data in the CMessage. + PyErr_SetString(PyExc_ValueError, + "Cannot reliably get a mutable pointer " + "to a message with extra references"); return NULL; } cmessage::AssureWritable(cmsg); |