aboutsummaryrefslogtreecommitdiffhomepage
path: root/python/google/protobuf/pyext/scoped_pyobject_ptr.h
diff options
context:
space:
mode:
Diffstat (limited to 'python/google/protobuf/pyext/scoped_pyobject_ptr.h')
-rw-r--r--python/google/protobuf/pyext/scoped_pyobject_ptr.h16
1 files changed, 11 insertions, 5 deletions
diff --git a/python/google/protobuf/pyext/scoped_pyobject_ptr.h b/python/google/protobuf/pyext/scoped_pyobject_ptr.h
index 18ddd5cd..9979b83b 100644
--- a/python/google/protobuf/pyext/scoped_pyobject_ptr.h
+++ b/python/google/protobuf/pyext/scoped_pyobject_ptr.h
@@ -51,16 +51,22 @@ class ScopedPyObjectPtr {
// Reset. Deletes the current owned object, if any.
// Then takes ownership of a new object, if given.
- // this->reset(this->get()) works.
+ // This function must be called with a reference that you own.
+ // this->reset(this->get()) is wrong!
+ // this->reset(this->release()) is OK.
PyObject* reset(PyObject* p = NULL) {
- if (p != ptr_) {
- Py_XDECREF(ptr_);
- ptr_ = p;
- }
+ Py_XDECREF(ptr_);
+ ptr_ = p;
return ptr_;
}
+ // ScopedPyObjectPtr should not be copied.
+ // We explicitly list and delete this overload to avoid automatic conversion
+ // to PyObject*, which is wrong in this case.
+ PyObject* reset(const ScopedPyObjectPtr& other) = delete;
+
// Releases ownership of the object.
+ // The caller now owns the returned reference.
PyObject* release() {
PyObject* p = ptr_;
ptr_ = NULL;