-
Notifications
You must be signed in to change notification settings - Fork 15.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "PythonMessageMutator GetClearedMutableMessage(PyObject *);" in p…
…roto_api which works with cpp extension, upb and pure python. PiperOrigin-RevId: 688818180
- Loading branch information
1 parent
50689dc
commit 5706140
Showing
5 changed files
with
295 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "google/protobuf/proto_api.h" | ||
|
||
#include <string> | ||
|
||
#include "absl/log/absl_check.h" | ||
#include "google/protobuf/message.h" | ||
namespace google { | ||
namespace protobuf { | ||
namespace python { | ||
|
||
PythonMessageMutator::PythonMessageMutator(Message* owned_msg, Message* message, | ||
PyObject* py_msg) | ||
: owned_msg_(owned_msg), message_(message), py_msg_(py_msg) { | ||
ABSL_DCHECK(py_msg != nullptr); | ||
ABSL_DCHECK(message != nullptr); | ||
Py_INCREF(py_msg_); | ||
} | ||
|
||
PythonMessageMutator::PythonMessageMutator(PythonMessageMutator&& other) | ||
: owned_msg_(other.owned_msg_ == nullptr ? nullptr | ||
: other.owned_msg_.release()), | ||
message_(other.message_), | ||
py_msg_(other.py_msg_) { | ||
other.message_ = nullptr; | ||
other.py_msg_ = nullptr; | ||
} | ||
|
||
PythonMessageMutator::~PythonMessageMutator() { | ||
if (py_msg_ == nullptr) { | ||
return; | ||
} | ||
|
||
// PyErr_Occurred check is required because PyObject_CallMethod need this | ||
// check. | ||
if (!PyErr_Occurred() && owned_msg_ != nullptr) { | ||
std::string wire; | ||
message_->SerializeToString(&wire); | ||
PyObject* py_wire = PyBytes_FromStringAndSize( | ||
wire.data(), static_cast<Py_ssize_t>(wire.size())); | ||
PyObject* parse = | ||
PyObject_CallMethod(py_msg_, "ParseFromString", "O", py_wire); | ||
Py_DECREF(py_wire); | ||
if (parse != nullptr) { | ||
Py_DECREF(parse); | ||
} | ||
} | ||
Py_DECREF(py_msg_); | ||
} | ||
|
||
PythonMessageMutator PyProto_API::CreatePythonMessageMutator( | ||
Message* owned_msg, Message* msg, PyObject* py_msg) const { | ||
return PythonMessageMutator(owned_msg, msg, py_msg); | ||
} | ||
|
||
} // namespace python | ||
} // namespace protobuf | ||
} // namespace google |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters