Skip to content

Commit d11799f

Browse files
authored
Merge pull request #176 from PerretB/py_container_from_null
Abort array/tensor construction if pyobject is not valid
2 parents 3ebbcc4 + 81ff7b0 commit d11799f

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

include/xtensor-python/pyarray.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,11 @@ namespace xt
744744
template <class T, layout_type L>
745745
inline void pyarray<T, L>::init_from_python()
746746
{
747+
if (!static_cast<bool>(*this))
748+
{
749+
return;
750+
}
751+
747752
m_shape = inner_shape_type(reinterpret_cast<size_type*>(PyArray_SHAPE(this->python_array())),
748753
static_cast<size_type>(PyArray_NDIM(this->python_array())));
749754
m_strides = inner_strides_type(reinterpret_cast<difference_type*>(PyArray_STRIDES(this->python_array())),

include/xtensor-python/pytensor.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@ namespace xt
442442
template <class T, std::size_t N, layout_type L>
443443
inline void pytensor<T, N, L>::init_from_python()
444444
{
445+
if (!static_cast<bool>(*this))
446+
{
447+
return;
448+
}
449+
445450
if (PyArray_NDIM(this->python_array()) != N)
446451
{
447452
throw std::runtime_error("NumPy: ndarray has incorrect number of dimensions");

0 commit comments

Comments
 (0)