change type keyword to return a pointer to static location for better portability

This commit is contained in:
Axel Kohlmeyer
2023-06-08 14:55:45 -04:00
parent b093f1aac1
commit 81854cd03e
2 changed files with 18 additions and 8 deletions

View File

@ -775,11 +775,10 @@ class lammps(object):
ptr = self.lib.lammps_last_thermo(self.lmp, c_char_p("keyword".encode()), i)
kw = cast(ptr, c_char_p).value.decode()
# temporarily switch return type since this stores an int in a pointer
self.lib.lammps_last_thermo.restype = c_int
with ExceptionCheck(self):
typ = self.lib.lammps_last_thermo(self.lmp, c_char_p("type".encode()), i)
self.lib.lammps_last_thermo.restype = c_void_p
ptr = self.lib.lammps_last_thermo(self.lmp, c_char_p("type".encode()), i)
typ = cast(ptr, POINTER(c_int)).contents.value
with ExceptionCheck(self):
ptr = self.lib.lammps_last_thermo(self.lmp, c_char_p("data".encode()), i)

View File

@ -765,6 +765,13 @@ of the last thermo output data and the corresponding keyword strings.
The how to handle the return value depends on the value of the *what*
argument string.
.. note::
The *type* property points to a static location that is reassigned
with every call, so the returned pointer should be recast,
dereferenced, and assigned immediately. Otherwise, its value may be
changed with the next invocation of the function.
.. list-table::
:header-rows: 1
:widths: auto
@ -787,7 +794,7 @@ argument string.
- yes
* - type
- data type of thermo output column; see :cpp:enum:`_LMP_DATATYPE_CONST`
- const int (**not** a pointer)
- pointer to static int
- yes
* - data
- actual field data for column
@ -808,6 +815,7 @@ void *lammps_last_thermo(void *handle, const char *what, int index)
Thermo *th = lmp->output->thermo;
if (!th) return nullptr;
const int nfield = *th->get_nfield();
static int datatype;
BEGIN_CAPTURE
{
@ -826,11 +834,14 @@ void *lammps_last_thermo(void *handle, const char *what, int index)
if ((index < 0) || (index >= nfield)) return nullptr;
const auto &field = th->get_fields()[index];
if (field.type == multitype::INT) {
val = (void *) LAMMPS_INT;
datatype = LAMMPS_INT;
val = (void *) &datatype;
} else if (field.type == multitype::BIGINT) {
val = (void *) LAMMPS_INT64;
datatype = LAMMPS_INT64;
val = (void *) &datatype;
} else if (field.type == multitype::DOUBLE) {
val = (void *) LAMMPS_DOUBLE;
datatype = LAMMPS_DOUBLE;
val = (void *) &datatype;
}
} else if (strcmp(what, "data") == 0) {