@@ -191,13 +191,13 @@ static Object* pysec_cell(Section* sec) {
191
191
}
192
192
193
193
static int NpySObj_contains (PyObject* s, PyObject* obj, const char * string) {
194
- /* Checks is provided PyObject* s contains obj */
195
- if (!PyObject_HasAttrString (obj, string)) {
194
+ /* Checks is provided PyObject* s matches obj.<string> */
195
+ auto pyobj = nb::borrow (obj); // keep refcount+1 during use
196
+ if (!nb::hasattr (pyobj, string)) {
196
197
return 0 ;
197
198
}
198
- auto _pyobj = nb::borrow (obj); // keep refcount+1 during use
199
- auto obj_seg = nb::steal (PyObject_GetAttrString (obj, string));
200
- return PyObject_RichCompareBool (s, obj_seg.ptr (), Py_EQ);
199
+ auto obj_seg = pyobj.attr (string);
200
+ return nb::handle{s}.equal (obj_seg);
201
201
}
202
202
203
203
static int NPySecObj_contains (PyObject* sec, PyObject* obj) {
@@ -1493,29 +1493,27 @@ static PyObject* NPySecObj_connect_safe(NPySecObj* self, PyObject* args) {
1493
1493
static PyObject* NPySecObj_insert (NPySecObj* self, PyObject* args) {
1494
1494
CHECK_SEC_INVALID (self->sec_ );
1495
1495
char * tname;
1496
- PyObject *tpyobj, *tpyobj2;
1497
1496
if (!PyArg_ParseTuple (args, " s" , &tname)) {
1498
1497
PyErr_Clear ();
1499
1498
// if called with an object that has an insert method, use that
1499
+ PyObject* tpyobj;
1500
1500
if (PyArg_ParseTuple (args, " O" , &tpyobj)) {
1501
- Py_INCREF (tpyobj);
1502
- Py_INCREF ((PyObject*) self);
1503
- tpyobj2 = PyObject_CallMethod (tpyobj, " insert" , " O" , (PyObject*) self);
1504
- Py_DECREF (tpyobj);
1505
- if (tpyobj2 == NULL ) {
1506
- Py_DECREF ((PyObject*) self);
1501
+ auto _tpyobj_tracker = nb::borrow (tpyobj);
1502
+ // Returned object to be discarded
1503
+ auto out_o = nb::steal (PyObject_CallMethod (tpyobj, " insert" , " O" , (PyObject*) self));
1504
+ if (!out_o.is_valid ()) {
1507
1505
PyErr_Clear ();
1508
1506
PyErr_SetString (
1509
1507
PyExc_TypeError,
1510
1508
" insert argument must be either a string or an object with an insert method" );
1511
- return NULL ;
1509
+ return nullptr ;
1512
1510
}
1513
- Py_DECREF (tpyobj2 );
1511
+ Py_INCREF (self );
1514
1512
return (PyObject*) self;
1515
1513
}
1516
1514
PyErr_Clear ();
1517
1515
PyErr_SetString (PyExc_TypeError, " insert takes a single positional argument" );
1518
- return NULL ;
1516
+ return nullptr ;
1519
1517
}
1520
1518
PyObject* otype = PyDict_GetItemString (pmech_types, tname);
1521
1519
if (!otype) {
@@ -1693,16 +1691,14 @@ static PyObject* seg_point_processes(NPySegObj* self) {
1693
1691
Section* sec = self->pysec_ ->sec_ ;
1694
1692
CHECK_SEC_INVALID (sec);
1695
1693
Node* nd = node_exact (sec, self->x_ );
1696
- PyObject* result = PyList_New ( 0 ) ;
1694
+ nb::list result{} ;
1697
1695
for (Prop* p = nd->prop ; p; p = p->next ) {
1698
1696
if (memb_func[p->_type ].is_point ) {
1699
1697
auto * pp = p->dparam [1 ].get <Point_process*>();
1700
- auto item = nb::steal (nrnpy_ho2po (pp->ob ));
1701
- int err = PyList_Append (result, item.ptr ());
1702
- assert (err == 0 );
1698
+ result.append (nb::steal (nrnpy_ho2po (pp->ob )));
1703
1699
}
1704
1700
}
1705
- return result;
1701
+ return result. release (). ptr () ;
1706
1702
}
1707
1703
1708
1704
static PyObject* seg_point_processes_safe (NPySegObj* self) {
0 commit comments