Skip to content

Commit

Permalink
HOC push_section(python_section_name) does not work. (#1070)
Browse files Browse the repository at this point in the history
* from neuron.expect_hocerr import expect_hocerr available for all tests.
  • Loading branch information
nrnhines authored and alexsavulescu committed Apr 13, 2021
1 parent 160c8ea commit e8fa58c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
40 changes: 40 additions & 0 deletions share/lib/python/neuron/expect_hocerr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sys
from io import StringIO
from neuron import h
pc = h.ParallelContext()
nhost = pc.nhost()

def expect_hocerr(callable, args, sec=None):
"""
Execute callable(args) and assert that it generated an error.
If sec is not None, executes callable(args, sec=sec)
Skips if nhost > 1 as all hoc_execerror end in MPI_ABORT
Does not work well with nrniv launch since hoc_execerror messages do not
pass through sys.stderr.
"""

if nhost > 1:
return

old_stderr = sys.stderr
sys.stderr = my_stderr = StringIO()
err = 0
try:
if sec:
callable(*args, sec=sec)
else:
callable(*args)
except:
err=1
errmes = my_stderr.getvalue()
sys.stderr = old_stderr
if errmes:
errmes = errmes.splitlines()[0]
errmes = errmes[(errmes.find(':')+2):]
print("expect_hocerr: %s" % errmes)
if err == 0:
print("expect_hocerr: no err for %s%s" % (str(callable), str(args)))
assert(err)


2 changes: 1 addition & 1 deletion src/nrnoc/cabcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -2382,7 +2382,7 @@ void push_section(void) {
sec = (Section*)0;
s = gargstr(1);
ForAllSections(sec1) /* I can't imagine a more inefficient way */
if (strcmp(s, secname(sec1)) == 0) {
if (strcmp(s, nrn_sec2pysecname(sec1)) == 0) {
sec = sec1;
break;
}
Expand Down
25 changes: 25 additions & 0 deletions test/pynrn/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from neuron.expect_hocerr import expect_hocerr

import numpy as np

Expand Down Expand Up @@ -113,3 +114,27 @@ def test_newobject_err_recover():
assert(err == 1)
h.finitialize() # succeeds without seg fault

def test_push_section():
h('''create hCable1, hCable2''')
h.push_section("hCable1")
assert(h.secname() == "hCable1")
h.pop_section()
h.push_section("hCable2")
assert(h.secname() == "hCable2")
h.pop_section()
h.delete_section(sec=h.hCable1)
h.delete_section(sec=h.hCable2)

sections = [h.Section(name="pCable%d"%i) for i in range(2)]
sections.append(h.Section()) # Anonymous in the past
for sec in sections:
name_in_hoc = h.secname(sec=sec)
h.push_section(name_in_hoc)
assert(h.secname() == name_in_hoc)
h.pop_section()
s = sections[-1]
h.delete_section(sec=s) # but not yet freed (though the name is now "")
# not [no longer] a section pointer
expect_hocerr(h.push_section, (int(s.hoc_internal_name().replace("__nrnsec_", ""), 0),))
# not a sectionname
expect_hocerr(h.push_section, ("not_a_sectionname",))

0 comments on commit e8fa58c

Please sign in to comment.