Skip to content

Commit

Permalink
char2pylist return a nb::list
Browse files Browse the repository at this point in the history
  • Loading branch information
alkino committed Nov 19, 2024
1 parent fecdbd7 commit d949451
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions src/nrnpython/nrnpy_p2h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ static void hpoasgn(Object* o, int type) {
Symbol* sym;
nb::object poright;
if (type == NUMBER) {
poright = nb::steal(PyFloat_FromDouble(hoc_xpop()));
poright = nb::float_(hoc_xpop());
} else if (type == STRING) {
poright = nb::steal(Py_BuildValue("s", *hoc_strpop()));
poright = nb::str(*hoc_strpop());
} else if (type == OBJECTVAR || type == OBJECTTMP) {
Object** po2 = hoc_objpop();
poright = nb::steal(nrnpy_ho2po(*po2));
Expand All @@ -279,7 +279,7 @@ static void hpoasgn(Object* o, int type) {
nindex = hoc_ipop();
// printf("hpoasgn %s %s %d\n", hoc_object_name(o), sym->name, nindex);
if (nindex == 0) {
err = PyObject_SetAttrString(poleft.ptr(), sym->name, poright.ptr());
poleft.attr(sym->name) = poright;
} else if (nindex == 1) {
int ndim = hoc_pop_ndim();
assert(ndim == 1);
Expand All @@ -288,10 +288,10 @@ static void hpoasgn(Object* o, int type) {
if (strcmp(sym->name, "_") == 0) {
a = nb::borrow(poleft);
} else {
a = nb::steal(PyObject_GetAttrString(poleft.ptr(), sym->name));
a = poleft.attr(sym->name);
}
if (a) {
err = PyObject_SetItem(a.ptr(), key.ptr(), poright.ptr());
a[key] = poright;
} else {
err = -1;
}
Expand Down Expand Up @@ -331,7 +331,7 @@ static double praxis_efun(Object* ho, Object* v) {

auto pc = nb::steal(nrnpy_ho2po(ho));
auto pv = nb::steal(nrnpy_ho2po(v));
auto po = nb::steal(Py_BuildValue("(OO)", pc.ptr(), pv.ptr()));
auto po = nb::make_tuple(pc, pv);
nb::object r = hoccommand_exec_help1(po);
if (!r.is_valid()) {
char* mes = nrnpyerr_str();
Expand Down Expand Up @@ -428,9 +428,7 @@ static Object* callable_with_args(Object* ho, int narg) {
}
}

auto r = nb::steal(PyTuple_New(2));
PyTuple_SetItem(r.ptr(), 1, args.release().ptr());
PyTuple_SetItem(r.ptr(), 0, po.release().ptr());
nb::tuple r = nb::make_tuple(po, args);

Object* hr = nrnpy_po2ho(r.release().ptr());

Expand Down Expand Up @@ -672,17 +670,13 @@ int* mk_displ(int* cnts) {
return displ;
}

static PyObject* char2pylist(char* buf, int np, int* cnt, int* displ) {
PyObject* plist = PyList_New(np);
assert(plist != NULL);
static nb::list char2pylist(char* buf, int np, int* cnt, int* displ) {
nb::list plist{};
for (int i = 0; i < np; ++i) {
if (cnt[i] == 0) {
Py_INCREF(Py_None); // 'Fatal Python error: deallocating None' eventually
PyList_SetItem(plist, i, Py_None);
plist.append(nb::none());
} else {
nb::object po = unpickle(buf + displ[i], cnt[i]);
PyObject* p = po.release().ptr();
PyList_SetItem(plist, i, p);
plist.append(unpickle(buf + displ[i], cnt[i]));
}
}
return plist;
Expand All @@ -701,11 +695,11 @@ static PyObject* py_allgather(PyObject* psrc) {

nrnmpi_char_allgatherv(sbuf.data(), rbuf, rcnt, rdispl);

PyObject* pdest = char2pylist(rbuf, np, rcnt, rdispl);
nb::list pdest = char2pylist(rbuf, np, rcnt, rdispl);
delete[] rbuf;
delete[] rcnt;
delete[] rdispl;
return pdest;
return pdest.release().ptr();
}

static PyObject* py_gather(PyObject* psrc, int root) {
Expand All @@ -727,16 +721,14 @@ static PyObject* py_gather(PyObject* psrc, int root) {

nrnmpi_char_gatherv(sbuf.data(), scnt, rbuf, rcnt, rdispl, root);

PyObject* pdest = Py_None;
nb::list pdest = nb::none();
if (root == nrnmpi_myid) {
pdest = char2pylist(rbuf, np, rcnt, rdispl);
delete[] rbuf;
delete[] rcnt;
delete[] rdispl;
} else {
Py_INCREF(pdest);
}
return pdest;
return pdest.release().ptr();
}

static PyObject* py_broadcast(PyObject* psrc, int root) {
Expand Down Expand Up @@ -896,7 +888,7 @@ static Object* py_alltoall_type(int size, int type) {
nrnmpi_char_alltoallv(s.data(), scnt.data(), sdispl, r, rcnt, rdispl);
delete[] sdispl;

pdest = char2pylist(r, np, rcnt, rdispl);
pdest = char2pylist(r, np, rcnt, rdispl).release().ptr();

delete[] r;
delete[] rcnt;
Expand Down

0 comments on commit d949451

Please sign in to comment.