You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by we...@apache.org on 2018/07/08 14:13:06 UTC

[arrow] branch master updated: ARROW-2601: [Python] Prevent user from calling *MemoryPool constructors directly

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 0933b4f  ARROW-2601: [Python] Prevent user from calling *MemoryPool constructors directly
0933b4f is described below

commit 0933b4fca04d5dbccdbc7d70e88925a76bf06bdd
Author: Wes McKinney <we...@apache.org>
AuthorDate: Sun Jul 8 10:12:58 2018 -0400

    ARROW-2601: [Python] Prevent user from calling *MemoryPool constructors directly
    
    Note I opened ARROW-2808 as a follow up since unit testing around use of various memory pools is rather thin at the moment
    
    Author: Wes McKinney <we...@apache.org>
    
    Closes #2228 from wesm/ARROW-2601 and squashes the following commits:
    
    d7892800 <Wes McKinney> Prevent user from calling *MemoryPool constructors directly
---
 python/pyarrow/memory.pxi       | 33 +++++++++++++++++++++++++++------
 python/pyarrow/tests/test_io.py | 12 ++++++++++++
 2 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/python/pyarrow/memory.pxi b/python/pyarrow/memory.pxi
index 1a461ef..e99955c 100644
--- a/python/pyarrow/memory.pxi
+++ b/python/pyarrow/memory.pxi
@@ -21,6 +21,11 @@
 
 
 cdef class MemoryPool:
+
+    def __init__(self):
+        raise TypeError("Do not call {}'s constructor directly"
+                        .format(self.__class__.__name__))
+
     cdef void init(self, CMemoryPool* pool):
         self.pool = pool
 
@@ -39,6 +44,10 @@ cdef class LoggingMemoryPool(MemoryPool):
     cdef:
         unique_ptr[CLoggingMemoryPool] logging_pool
 
+    def __init__(self):
+        raise TypeError("Do not call {}'s constructor directly"
+                        .format(self.__class__.__name__))
+
     def __cinit__(self, MemoryPool pool):
         self.logging_pool.reset(new CLoggingMemoryPool(pool.pool))
         self.init(self.logging_pool.get())
@@ -52,25 +61,37 @@ cdef class ProxyMemoryPool(MemoryPool):
     cdef:
         unique_ptr[CProxyMemoryPool] proxy_pool
 
-    def __cinit__(self, MemoryPool pool):
-        self.proxy_pool.reset(new CProxyMemoryPool(pool.pool))
-        self.init(self.proxy_pool.get())
+    def __init__(self):
+        raise TypeError("Do not call {}'s constructor directly. "
+                        "Use pyarrow.proxy_memory_pool instead."
+                        .format(self.__class__.__name__))
 
 
 def default_memory_pool():
     cdef:
-        MemoryPool pool = MemoryPool()
+        MemoryPool pool = MemoryPool.__new__(MemoryPool)
     pool.init(c_get_memory_pool())
     return pool
 
 
+def proxy_memory_pool(MemoryPool parent):
+    """
+    Derived MemoryPool class that tracks the number of bytes and
+    maximum memory allocated through its direct calls.
+    """
+    cdef ProxyMemoryPool out = ProxyMemoryPool.__new__(ProxyMemoryPool)
+    out.proxy_pool.reset(new CProxyMemoryPool(parent.pool))
+    out.init(out.proxy_pool.get())
+    return out
+
+
 def set_memory_pool(MemoryPool pool):
     c_set_default_memory_pool(pool.pool)
 
 
 cdef MemoryPool _default_memory_pool = default_memory_pool()
-cdef LoggingMemoryPool _logging_memory_pool = (
-    LoggingMemoryPool(_default_memory_pool))
+cdef LoggingMemoryPool _logging_memory_pool = LoggingMemoryPool.__new__(
+    LoggingMemoryPool, _default_memory_pool)
 
 
 def log_memory_allocations(enable=True):
diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py
index 25008f8..7551cce 100644
--- a/python/pyarrow/tests/test_io.py
+++ b/python/pyarrow/tests/test_io.py
@@ -193,6 +193,18 @@ def test_python_file_closing():
 
 
 # ----------------------------------------------------------------------
+# MemoryPool
+
+
+def test_memory_pool_cannot_use_ctor():
+    with pytest.raises(TypeError):
+        pa.MemoryPool()
+
+    with pytest.raises(TypeError):
+        pa.ProxyMemoryPool()
+
+
+# ----------------------------------------------------------------------
 # Buffers