This is an automated email from the ASF dual-hosted git repository.
uwe 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 99899d6 ARROW-2232: [Python] pyarrow.Tensor constructor segfaults
99899d6 is described below
commit 99899d6e3c761b2cdcb843dc0da7fd08aa3db06b
Author: Phillip Cloud <[email protected]>
AuthorDate: Fri Mar 2 15:16:40 2018 +0100
ARROW-2232: [Python] pyarrow.Tensor constructor segfaults
Author: Phillip Cloud <[email protected]>
Closes #1682 from cpcloud/ARROW-2232 and squashes the following commits:
1837b02 <Phillip Cloud> ARROW-2232: pyarrow.Tensor constructor segfaults
---
python/pyarrow/array.pxi | 87 ++++++++++++++++++++------------------
python/pyarrow/tests/test_array.py | 11 +++++
2 files changed, 56 insertions(+), 42 deletions(-)
diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi
index 5b8621f..80e15f2 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 197dac0..c1131a0 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()
--
To stop receiving notification emails like this one, please contact
[email protected].