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

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

xhochy closed pull request #1682: ARROW-2232: [Python] pyarrow.Tensor 
constructor segfaults
URL: https://github.com/apache/arrow/pull/1682
 
 
   

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/array.pxi b/python/pyarrow/array.pxi
index 5b8621f13..80e15f2bd 100644
--- a/python/pyarrow/array.pxi
+++ b/python/pyarrow/array.pxi
@@ -496,11 +496,19 @@ cdef class Tensor:
         self.tp = sp_tensor.get()
         self.type = pyarrow_wrap_data_type(self.tp.type())
 
+    def _validate(self):
+        if self.tp is NULL:
+            raise TypeError(
+                'pyarrow.Tensor has not been initialized correctly Please use '
+                'pyarrow.Tensor.from_numpy to construct a pyarrow.Tensor')
+
     def __repr__(self):
+        if self.tp is NULL:
+            return '<invalid pyarrow.Tensor>'
         return """<pyarrow.Tensor>
-type: {0}
-shape: {1}
-strides: {2}""".format(self.type, self.shape, self.strides)
+type: {0.type}
+shape: {0.shape}
+strides: {0.strides}""".format(self)
 
     @staticmethod
     def from_numpy(obj):
@@ -514,8 +522,9 @@ strides: {2}""".format(self.type, self.shape, self.strides)
         """
         Convert arrow::Tensor to numpy.ndarray with zero copy
         """
-        cdef:
-            PyObject* out
+        self._validate()
+
+        cdef PyObject* out
 
         with nogil:
             check_status(TensorToNdarray(self.sp_tensor, self, &out))
@@ -525,45 +534,39 @@ strides: {2}""".format(self.type, self.shape, 
self.strides)
         """
         Return true if the tensors contains exactly equal data
         """
+        self._validate()
         return self.tp.Equals(deref(other.tp))
 
-    property is_mutable:
-
-        def __get__(self):
-            return self.tp.is_mutable()
-
-    property is_contiguous:
-
-        def __get__(self):
-            return self.tp.is_contiguous()
-
-    property ndim:
-
-        def __get__(self):
-            return self.tp.ndim()
-
-    property size:
-
-        def __get__(self):
-            return self.tp.size()
-
-    property shape:
-
-        def __get__(self):
-            cdef size_t i
-            py_shape = []
-            for i in range(self.tp.shape().size()):
-                py_shape.append(self.tp.shape()[i])
-            return py_shape
-
-    property strides:
-
-        def __get__(self):
-            cdef size_t i
-            py_strides = []
-            for i in range(self.tp.strides().size()):
-                py_strides.append(self.tp.strides()[i])
-            return py_strides
+    @property
+    def is_mutable(self):
+        self._validate()
+        return self.tp.is_mutable()
+
+    @property
+    def is_contiguous(self):
+        self._validate()
+        return self.tp.is_contiguous()
+
+    @property
+    def ndim(self):
+        self._validate()
+        return self.tp.ndim()
+
+    @property
+    def size(self):
+        self._validate()
+        return self.tp.size()
+
+    @property
+    def shape(self):
+        # Cython knows how to convert a vector[T] to a Python list
+        self._validate()
+        return self.tp.shape()
+
+    @property
+    def strides(self):
+        self._validate()
+        return self.tp.strides()
 
 
 cdef wrap_array_output(PyObject* output):
diff --git a/python/pyarrow/tests/test_array.py 
b/python/pyarrow/tests/test_array.py
index 197dac0d8..c1131a002 100644
--- a/python/pyarrow/tests/test_array.py
+++ b/python/pyarrow/tests/test_array.py
@@ -653,3 +653,14 @@ def test_buffers_nested():
     assert bytearray(null_bitmap)[0] == 0b00000100
     values = buffers[4].to_pybytes()
     assert struct.unpack('4xh', values) == (43,)
+
+
+def test_invalid_tensor_constructor_repr():
+    t = pa.Tensor([1])
+    assert repr(t) == '<invalid pyarrow.Tensor>'
+
+
+def test_invalid_tensor_operation():
+    t = pa.Tensor()
+    with pytest.raises(TypeError):
+        t.to_numpy()


 

----------------------------------------------------------------
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:
us...@infra.apache.org


> [Python] pyarrow.Tensor constructor segfaults
> ---------------------------------------------
>
>                 Key: ARROW-2232
>                 URL: https://issues.apache.org/jira/browse/ARROW-2232
>             Project: Apache Arrow
>          Issue Type: Bug
>          Components: Python
>    Affects Versions: 0.8.0
>            Reporter: Phillip Cloud
>            Assignee: Phillip Cloud
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 0.9.0
>
>
> {{pa.Tensor()}}, {{pa.Tensor([])}}, and {{pa.Tensor([1.0])}} all crash the 
> interpreter.



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

Reply via email to