python & hoc strings
Posted: Tue Jun 26, 2012 1:39 pm
I am trying to modify a hoc string from within a mod file (strtest.mod code below) and want to be able to do this using both hoc and python. When I call the code in test.py there's no error when running using NEURON h(command) syntax, but when using h.variablename syntax, I get an error. Although the type of h.tstr (from the test.py code below) is a python string (<type 'str'>) and therefore immutable, is there a way to get access to the hoc strdef via h.variablename syntax without h.variablename making it a python string?
Thanks for any tips...
strtest.mod (compile with nrnivmodl):
test.py (run special -python test.py, or import test):
NEURON source code in src/oc/code.c where the crash occurs:
Thanks for any tips...
strtest.mod (compile with nrnivmodl):
Code: Select all
NEURON {
SUFFIX nothing
}
PARAMETER {
}
ASSIGNED {
}
PROCEDURE strtest () {
VERBATIM
char **pname, str[100];
char** hoc_pgargstr();
pname = hoc_pgargstr(1);
sprintf(str,"goodbye");
printf("first pname is %s\n",*pname);
hoc_assign_str(pname,str);
printf("now pname is %s\n",*pname);
return;
ENDVERBATIM
}
Code: Select all
from neuron import h
print "------------------------------------------------"
print " STARTING TEST"
print "------------------------------------------------"
h("strdef tstr") // declare the string in hoc
print "first, from hoc:"
h("strtest(tstr)") // run the test in hoc -- works properly
print type(h.tstr) # <type 'str'>
print "next, from python:"
h.strtest(h.tstr) # run the same thing from python -> causes a crash
Code: Select all
hoc_assign_str(cpp, buf)
char** cpp, *buf;
{
char* s = *cpp;
*cpp = (char *)emalloc((unsigned)(strlen(buf) + 1));
Strcpy(*cpp, buf);
if (s) {
hoc_free_string(s); // this line causes the crash
}
}