From 8c5da708238a18ac20a6201390a9099d15304a48 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 12 Oct 2020 06:16:28 -0400 Subject: [PATCH] handle the case where the variable type is invalid and thus a null pointer is returned --- python/lammps.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/python/lammps.py b/python/lammps.py index 19883e266a..b11ab6a3a2 100644 --- a/python/lammps.py +++ b/python/lammps.py @@ -1114,7 +1114,7 @@ class lammps(object): after the data is copied to a Python variable or list. The variable must be either an equal-style (or equivalent) variable or an atom-style variable. The variable type has to - provided as ``vartype`` parameter which may be two constants: + provided as ``vartype`` parameter which may be one of two constants: ``LMP_VAR_EQUAL`` or ``LMP_VAR_STRING``; it defaults to equal-style variables. The group parameter is only used for atom-style variables and @@ -1135,7 +1135,8 @@ class lammps(object): if vartype == LMP_VAR_EQUAL: self.lib.lammps_extract_variable.restype = POINTER(c_double) ptr = self.lib.lammps_extract_variable(self.lmp,name,group) - result = ptr[0] + if ptr: result = ptr[0] + else: return None self.lib.lammps_free(ptr) return result elif vartype == LMP_VAR_ATOM: @@ -1143,8 +1144,10 @@ class lammps(object): result = (c_double*nlocal)() self.lib.lammps_extract_variable.restype = POINTER(c_double) ptr = self.lib.lammps_extract_variable(self.lmp,name,group) - for i in range(nlocal): result[i] = ptr[i] - self.lib.lammps_free(ptr) + if ptr: + for i in range(nlocal): result[i] = ptr[i] + self.lib.lammps_free(ptr) + else: return None return result return None