Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify hocpickle_reduce #3233

Merged
merged 10 commits into from
Nov 21, 2024
40 changes: 9 additions & 31 deletions src/nrnpython/nrnpy_hoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2900,53 +2900,31 @@ static PyObject* hocpickle_reduce(PyObject* self, PyObject* args) {
PyHocObject* pho = (PyHocObject*) self;
if (!is_obj_type(pho->ho_, "Vector")) {
PyErr_SetString(PyExc_TypeError, "HocObject: Only Vector instance can be pickled");
return NULL;
return nullptr;
}
Vect* vec = (Vect*) pho->ho_->u.this_pointer;

// neuron module has a _pkl method that returns h.Vector(0)
auto mod = nb::steal(PyImport_ImportModule("neuron"));
if (!mod) {
alkino marked this conversation as resolved.
Show resolved Hide resolved
return nullptr;
}
auto obj = nb::steal(PyObject_GetAttrString(mod.ptr(), "_pkl"));

nb::module_ mod = nb::module_::import_("neuron");
nb::object obj = mod.attr("_pkl");
if (!obj) {
PyErr_SetString(PyExc_Exception, "neuron module has no _pkl method.");
return nullptr;
}

auto ret = nb::steal(PyTuple_New(3));
if (!ret) {
return nullptr;
}
PyTuple_SET_ITEM(ret.ptr(), 0, obj.release().ptr());
PyTuple_SET_ITEM(ret.ptr(), 1, Py_BuildValue("(N)", PyInt_FromLong(0)));
// see numpy implementation if more ret[1] stuff needed in case we
// pickle anything but a hoc Vector. I don't think ret[1] can be None.

// Fill object's state. Tuple with 4 args:
// pickle version, 2.0 bytes to determine if swapbytes needed,
alkino marked this conversation as resolved.
Show resolved Hide resolved
// vector size, string data
alkino marked this conversation as resolved.
Show resolved Hide resolved
auto state = nb::steal(PyTuple_New(4));
if (!state) {
return nullptr;
}
PyTuple_SET_ITEM(state.ptr(), 0, PyInt_FromLong(1));
double x = 2.0;
auto two = nb::steal(PyBytes_FromStringAndSize((const char*) (&x), sizeof(double)));
if (!two) {
return nullptr;
}
PyTuple_SET_ITEM(state.ptr(), 1, two.release().ptr());
PyTuple_SET_ITEM(state.ptr(), 2, PyInt_FromLong(vec->size()));
auto str = nb::steal(
PyBytes_FromStringAndSize((const char*) vector_vec(vec), vec->size() * sizeof(double)));
if (!str) {
return nullptr;
}
PyTuple_SET_ITEM(state.ptr(), 3, str.release().ptr());
PyTuple_SET_ITEM(ret.ptr(), 2, state.release().ptr());
return ret.release().ptr();
nb::bytes str((const void*) (&x), sizeof(double));
alkino marked this conversation as resolved.
Show resolved Hide resolved
nb::bytes str1(vec->data(), vec->size() * sizeof(double));
alkino marked this conversation as resolved.
Show resolved Hide resolved
nb::tuple state = nb::make_tuple(1, str, vec->size(), str1);

return nb::make_tuple(obj, nb::make_tuple(0), state).release().ptr();
}

static PyObject* hocpickle_reduce_safe(PyObject* self, PyObject* args) {
Expand Down
Loading