Skip to content

Commit

Permalink
nanobind: use loads and dumps as callable (#3043)
Browse files Browse the repository at this point in the history
  • Loading branch information
alkino authored and mgeplf committed Aug 19, 2024
1 parent c2918ce commit 65e55c2
Showing 1 changed file with 12 additions and 35 deletions.
47 changes: 12 additions & 35 deletions src/nrnpython/nrnpy_p2h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,19 +635,12 @@ static void setpickle() {

// note that *size includes the null terminating character if it exists
static std::vector<char> pickle(PyObject* p) {
PyObject* arg = PyTuple_Pack(1, p);
PyObject* r = nrnpy_pyCallObject(dumps.ptr(), arg);
Py_XDECREF(arg);
auto r = nb::borrow<nb::bytes>(dumps(nb::borrow(p)));
if (!r && PyErr_Occurred()) {
PyErr_Print();
}
assert(r);
assert(PyBytes_Check(r));
std::size_t size = PyBytes_Size(r);
char* buf = PyBytes_AsString(r);
std::vector<char> ret(buf, buf + size);
Py_XDECREF(r);
return ret;
return std::vector<char>(r.c_str(), r.c_str() + r.size());
}

static std::vector<char> po2pickle(Object* ho) {
Expand All @@ -661,10 +654,7 @@ static std::vector<char> po2pickle(Object* ho) {
}

static nb::object unpickle(const char* s, std::size_t len) {
nb::bytes string(s, len);
nb::list args;
args.append(string);
return loads(*args);
return loads(nb::bytes(s, len));
}

static nb::object unpickle(const std::vector<char>& s) {
Expand Down Expand Up @@ -743,43 +733,30 @@ std::vector<char> call_picklef(const std::vector<char>& fname, int narg) {
// fname is a pickled callable, narg is the number of args on the
// hoc stack with types double, char*, hoc Vector, and PythonObject
// callable return must be pickleable.
PyObject* args = 0;
PyObject* result = 0;
PyObject* callable;

setpickle();
PyObject* ps = PyBytes_FromStringAndSize(fname.data(), fname.size());
args = PyTuple_Pack(1, ps);
callable = nrnpy_pyCallObject(loads.ptr(), args);
nb::bytes ps(fname.data(), fname.size());

auto callable = nb::borrow<nb::callable>(loads(ps));
assert(callable);
Py_XDECREF(args);
Py_XDECREF(ps);

args = PyTuple_New(narg);
nb::list args{};
for (int i = 0; i < narg; ++i) {
PyObject* arg = nrnpy_hoc_pop("call_picklef");
if (PyTuple_SetItem(args, narg - 1 - i, arg)) {
assert(0);
}
// Py_XDECREF(arg);
nb::object arg = nb::steal(nrnpy_hoc_pop("call_picklef"));
args.append(arg);
}
result = nrnpy_pyCallObject(callable, args);
Py_DECREF(callable);
Py_DECREF(args);
nb::object result = callable(*args);
if (!result) {
char* mes = nrnpyerr_str();
if (mes) {
Fprintf(stderr, "%s\n", mes);
std::cerr << mes << std::endl;
free(mes);
hoc_execerror("PyObject method call failed:", NULL);
}
if (PyErr_Occurred()) {
PyErr_Print();
}
}
auto rs = pickle(result);
Py_XDECREF(result);
return rs;
return pickle(result.ptr());
}

#include "nrnmpi.h"
Expand Down

0 comments on commit 65e55c2

Please sign in to comment.