[ 
https://issues.apache.org/jira/browse/ARROW-2252?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16385592#comment-16385592
 ] 

ASF GitHub Bot commented on ARROW-2252:
---------------------------------------

wesm closed pull request #1693: ARROW-2252: [Python] Create buffer from 
address, size and base
URL: https://github.com/apache/arrow/pull/1693
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/python/pyarrow/__init__.py b/python/pyarrow/__init__.py
index 15a37ca10..8cb4b3b9b 100644
--- a/python/pyarrow/__init__.py
+++ b/python/pyarrow/__init__.py
@@ -72,8 +72,8 @@
 from pyarrow.lib import TimestampType
 
 # Buffers, allocation
-from pyarrow.lib import (Buffer, ResizableBuffer, compress, decompress,
-                         allocate_buffer, frombuffer)
+from pyarrow.lib import (Buffer, ForeignBuffer, ResizableBuffer, compress,
+                         decompress, allocate_buffer, frombuffer)
 
 from pyarrow.lib import (MemoryPool, total_allocated_bytes,
                          set_memory_pool, default_memory_pool,
diff --git a/python/pyarrow/io.pxi b/python/pyarrow/io.pxi
index 325c5827f..5c8411be4 100644
--- a/python/pyarrow/io.pxi
+++ b/python/pyarrow/io.pxi
@@ -720,6 +720,18 @@ cdef class Buffer:
         return self.size
 
 
+cdef class ForeignBuffer(Buffer):
+
+    def __init__(self, addr, size, base):
+        cdef:
+            intptr_t c_addr = addr
+            int64_t c_size = size
+        self.base = base
+        cdef shared_ptr[CBuffer] buffer = make_shared[CBuffer](
+            <uint8_t*>c_addr, c_size)
+        self.init(<shared_ptr[CBuffer]> buffer)
+
+
 cdef class ResizableBuffer(Buffer):
 
     cdef void init_rz(self, const shared_ptr[CResizableBuffer]& buffer):
diff --git a/python/pyarrow/lib.pxd b/python/pyarrow/lib.pxd
index e4d574f18..c37bc2beb 100644
--- a/python/pyarrow/lib.pxd
+++ b/python/pyarrow/lib.pxd
@@ -324,6 +324,11 @@ cdef class Buffer:
     cdef int _check_nullptr(self) except -1
 
 
+cdef class ForeignBuffer(Buffer):
+    cdef:
+        object base
+
+
 cdef class ResizableBuffer(Buffer):
 
     cdef void init_rz(self, const shared_ptr[CResizableBuffer]& buffer)
diff --git a/python/pyarrow/tests/test_io.py b/python/pyarrow/tests/test_io.py
index d269ad0e7..17aca4333 100644
--- a/python/pyarrow/tests/test_io.py
+++ b/python/pyarrow/tests/test_io.py
@@ -24,6 +24,7 @@
 import weakref
 
 import numpy as np
+import numpy.testing as npt
 
 import pandas as pd
 
@@ -253,6 +254,14 @@ def test_buffer_equals():
     assert buf2.equals(buf5)
 
 
+def test_foreign_buffer():
+    n = np.array([1, 2])
+    addr = n.__array_interface__["data"][0]
+    size = n.nbytes
+    fb = pa.ForeignBuffer(addr, size, n)
+    npt.assert_array_equal(np.asarray(fb), n.view(dtype=np.int8))
+
+
 def test_allocate_buffer():
     buf = pa.allocate_buffer(100)
     assert buf.size == 100


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> [Python] Create buffer from address, size and base
> --------------------------------------------------
>
>                 Key: ARROW-2252
>                 URL: https://issues.apache.org/jira/browse/ARROW-2252
>             Project: Apache Arrow
>          Issue Type: New Feature
>          Components: Python
>            Reporter: Uwe L. Korn
>            Assignee: Uwe L. Korn
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 0.9.0
>
>
> Given a memory address and a size, we should be able to construct an Arrow 
> buffer from this. The additional base object will be used to hold a reference 
> to the underlying, original buffer so that it does not go out of scope before 
> the Arrow buffer.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to