python: numpy wrapper returns None in error cases
lammps.numpy wrapper functions will now return None in error cases instead of throwing exception with NULL pointer access errors. If nelem or dimension is zero it will return an empty numpy array with the correct shape.
This commit is contained in:
@ -422,45 +422,51 @@ class numpy_wrapper:
|
|||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
def iarray(self, c_int_type, raw_ptr, nelem, dim=1):
|
def iarray(self, c_int_type, raw_ptr, nelem, dim=1):
|
||||||
if raw_ptr is None:
|
if raw_ptr and nelem >= 0 and dim >= 0:
|
||||||
return None
|
import numpy as np
|
||||||
|
np_int_type = self._ctype_to_numpy_int(c_int_type)
|
||||||
|
ptr = None
|
||||||
|
|
||||||
import numpy as np
|
if dim == 1:
|
||||||
np_int_type = self._ctype_to_numpy_int(c_int_type)
|
ptr = cast(raw_ptr, POINTER(c_int_type * nelem))
|
||||||
|
elif raw_ptr[0]:
|
||||||
|
ptr = cast(raw_ptr[0], POINTER(c_int_type * nelem * dim))
|
||||||
|
|
||||||
if dim == 1:
|
if ptr:
|
||||||
ptr = cast(raw_ptr, POINTER(c_int_type * nelem))
|
a = np.frombuffer(ptr.contents, dtype=np_int_type)
|
||||||
else:
|
else:
|
||||||
ptr = cast(raw_ptr[0], POINTER(c_int_type * nelem * dim))
|
a = np.empty(0, dtype=np_int_type)
|
||||||
|
|
||||||
a = np.frombuffer(ptr.contents, dtype=np_int_type)
|
if dim > 1:
|
||||||
|
a.shape = (nelem, dim)
|
||||||
if dim > 1:
|
else:
|
||||||
a.shape = (nelem, dim)
|
a.shape = (nelem)
|
||||||
else:
|
return a
|
||||||
a.shape = (nelem)
|
return None
|
||||||
return a
|
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
def darray(self, raw_ptr, nelem, dim=1):
|
def darray(self, raw_ptr, nelem, dim=1):
|
||||||
if raw_ptr is None:
|
if raw_ptr and nelem >= 0 and dim >= 0:
|
||||||
return None
|
import numpy as np
|
||||||
|
ptr = None
|
||||||
|
|
||||||
import numpy as np
|
if dim == 1:
|
||||||
|
ptr = cast(raw_ptr, POINTER(c_double * nelem))
|
||||||
|
elif raw_ptr[0]:
|
||||||
|
ptr = cast(raw_ptr[0], POINTER(c_double * nelem * dim))
|
||||||
|
|
||||||
if dim == 1:
|
if ptr:
|
||||||
ptr = cast(raw_ptr, POINTER(c_double * nelem))
|
a = np.frombuffer(ptr.contents)
|
||||||
else:
|
else:
|
||||||
ptr = cast(raw_ptr[0], POINTER(c_double * nelem * dim))
|
a = np.empty(0, c_double)
|
||||||
|
|
||||||
a = np.frombuffer(ptr.contents)
|
if dim > 1:
|
||||||
|
a.shape = (nelem, dim)
|
||||||
if dim > 1:
|
else:
|
||||||
a.shape = (nelem, dim)
|
a.shape = (nelem)
|
||||||
else:
|
return a
|
||||||
a.shape = (nelem)
|
return None
|
||||||
return a
|
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user