This is an automated email from the ASF dual-hosted git repository.

wesm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new f8445a3  ARROW-2171: [C++/Python] Make OwnedRef safer
f8445a3 is described below

commit f8445a3a4398187c2ea7dc07bb5ee1cca4b89a35
Author: Antoine Pitrou <anto...@python.org>
AuthorDate: Mon Feb 19 12:52:46 2018 -0500

    ARROW-2171: [C++/Python] Make OwnedRef safer
    
    Disable copy constructor and assignment operator.  Define a move 
constructor for proper ownership transfer.  Also inherit the constructors in 
OwnedRefNoGIL.
    
    Author: Antoine Pitrou <anto...@python.org>
    
    Closes #1626 from pitrou/ARROW-2171-copy-constructor and squashes the 
following commits:
    
    6591b55a [Antoine Pitrou] ARROW-2171: [C++/Python] Make OwnedRef safer
---
 cpp/src/arrow/python/common.h       |  8 +++++++-
 cpp/src/arrow/python/python-test.cc | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/cpp/src/arrow/python/common.h b/cpp/src/arrow/python/common.h
index 269385c..b2844b1 100644
--- a/cpp/src/arrow/python/common.h
+++ b/cpp/src/arrow/python/common.h
@@ -67,7 +67,7 @@ class ARROW_EXPORT PyAcquireGIL {
 class ARROW_EXPORT OwnedRef {
  public:
   OwnedRef() : obj_(NULLPTR) {}
-
+  OwnedRef(OwnedRef&& other) : OwnedRef(other.detach()) {}
   explicit OwnedRef(PyObject* obj) : obj_(obj) {}
 
   ~OwnedRef() { reset(); }
@@ -90,6 +90,8 @@ class ARROW_EXPORT OwnedRef {
   PyObject** ref() { return &obj_; }
 
  private:
+  ARROW_DISALLOW_COPY_AND_ASSIGN(OwnedRef);
+
   PyObject* obj_;
 };
 
@@ -98,6 +100,10 @@ class ARROW_EXPORT OwnedRef {
 // (e.g. if it is released in the middle of a function for performance reasons)
 class ARROW_EXPORT OwnedRefNoGIL : public OwnedRef {
  public:
+  OwnedRefNoGIL() : OwnedRef() {}
+  OwnedRefNoGIL(OwnedRefNoGIL&& other) : OwnedRef(other.detach()) {}
+  explicit OwnedRefNoGIL(PyObject* obj) : OwnedRef(obj) {}
+
   ~OwnedRefNoGIL() {
     PyAcquireGIL lock;
     reset();
diff --git a/cpp/src/arrow/python/python-test.cc 
b/cpp/src/arrow/python/python-test.cc
index bcf89a4..a2b832b 100644
--- a/cpp/src/arrow/python/python-test.cc
+++ b/cpp/src/arrow/python/python-test.cc
@@ -42,6 +42,40 @@ TEST(PyBuffer, InvalidInputObject) {
   ASSERT_EQ(old_refcnt, Py_REFCNT(input));
 }
 
+TEST(OwnedRef, TestMoves) {
+  PyAcquireGIL lock;
+  std::vector<OwnedRef> vec;
+  PyObject *u, *v;
+  u = PyList_New(0);
+  v = PyList_New(0);
+  {
+    OwnedRef ref(u);
+    vec.push_back(std::move(ref));
+    ASSERT_EQ(ref.obj(), nullptr);
+  }
+  vec.emplace_back(v);
+  ASSERT_EQ(Py_REFCNT(u), 1);
+  ASSERT_EQ(Py_REFCNT(v), 1);
+}
+
+TEST(OwnedRefNoGIL, TestMoves) {
+  std::vector<OwnedRefNoGIL> vec;
+  PyObject *u, *v;
+  {
+    PyAcquireGIL lock;
+    u = PyList_New(0);
+    v = PyList_New(0);
+  }
+  {
+    OwnedRefNoGIL ref(u);
+    vec.push_back(std::move(ref));
+    ASSERT_EQ(ref.obj(), nullptr);
+  }
+  vec.emplace_back(v);
+  ASSERT_EQ(Py_REFCNT(u), 1);
+  ASSERT_EQ(Py_REFCNT(v), 1);
+}
+
 class DecimalTest : public ::testing::Test {
  public:
   DecimalTest() : lock_(), decimal_module_(), decimal_constructor_() {

-- 
To stop receiving notification emails like this one, please contact
w...@apache.org.

Reply via email to