Skip to content

Commit

Permalink
If python is launched use nrnpy_pr instead of printf to print to stdout.
Browse files Browse the repository at this point in the history
This allows h.printf, h.topology, and h.psection to print to
the Jupyter notebook.
  • Loading branch information
nrnhines committed May 16, 2017
1 parent 5a78380 commit ef4da5d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 8 deletions.
33 changes: 27 additions & 6 deletions share/lib/python/neuron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,14 @@ def nrn_dll(printpath=False):
base_path = os.path.join(neuron_home, 'lib' , 'python', 'neuron', 'hoc')
for extension in ['', '.dll', '.so', '.dylib']:
dlls = glob.glob(base_path + '*' + extension)
try:
the_dll = ctypes.cdll[dlls[0]]
if printpath : print(dlls[0])
success = True
except:
pass
for dll in dlls:
try:
the_dll = ctypes.cdll[dll]
if printpath : print(dll)
success = True
except:
pass
if success: break
if success: break
else:
raise Exception('unable to connect to the NEURON library')
Expand Down Expand Up @@ -530,3 +532,22 @@ def _has_scipy():
def _pkl(arg):
#print 'neuron._pkl arg is ', arg
return h.Vector(0)

def nrnpy_pr(s):
import sys
sys.stdout.write(s.decode())
return 0

try:
# callback in place of hoc printf
# ensures consistent with python stdout even with jupyter notebook.
nrnpy_pr_proto = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_char_p)
nrnpy_set_pr = nrn_dll_sym('nrnpy_set_pr')
nrnpy_set_pr.argtypes = [nrnpy_pr_proto]

nrnpy_pr_callback = nrnpy_pr_proto(nrnpy_pr)
nrnpy_set_pr(nrnpy_pr_callback)
except:
print("Failed to setup nrnpy_pr")
pass

2 changes: 1 addition & 1 deletion src/ivoc/ivocmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ extern "C" {
extern int nrn_global_argc;
extern const char** nrn_global_argv;
int always_false;
int nrn_is_python_extension;
extern int nrn_is_python_extension;
}

// some things are defined in libraries earlier than they are used so...
Expand Down
50 changes: 50 additions & 0 deletions src/oc/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include "hoc.h"
#include "ocmisc.h"
Expand Down Expand Up @@ -866,3 +867,52 @@ void hoc_Chdir(void) {
ret();
pushx((double)i);
}

int nrn_is_python_extension;
int (*nrnpy_pr_callback)(char*);

void nrnpy_set_pr(int (*cb)(char*)) {
if (nrn_is_python_extension) {
nrnpy_pr_callback = cb;
}
}

int nrnpy_pr(const char *fmt, ...) {
int size = 0;
char *p = NULL;
va_list ap;

if (!nrnpy_pr_callback) {
va_start(ap, fmt);
size = vprintf(fmt, ap);
return size;
}

/* Determine required size */

va_start(ap, fmt);
size = vsnprintf(p, size, fmt, ap);
va_end(ap);

if (size < 0)
return 0;

size++; /* For '\0' */
p = malloc(size);
if (p == NULL)
return 0;

va_start(ap, fmt);
size = vsnprintf(p, size, fmt, ap);
if (size < 0) {
free(p);
return 0;
}
va_end(ap);

(*nrnpy_pr_callback)(p);

free(p);
return size;
}

2 changes: 1 addition & 1 deletion src/oc/hocdec.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ int ilint;
#define Strcpy strcpy
#define Strncpy strncpy
#define Sprintf sprintf
#define Printf printf
#define Printf nrnpy_pr
#define Fprintf fprintf
#endif

Expand Down
1 change: 1 addition & 0 deletions src/oc/oc_ansi.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ extern int hoc_pid(void);
extern int hoc_ired(const char*, int, int, int);
extern double hoc_xred(const char*, double, double, double);
extern int hoc_sred(const char*, char*, char*);
extern int nrnpy_pr(const char* fmt, ...);

#if defined (__cplusplus)
extern void hoc_free_allobjects(cTemplate*, Symlist*, Objectdata*);
Expand Down
4 changes: 4 additions & 0 deletions src/oc/plot.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ void plprint(const char* s)
}else
#endif
{
#if 0
IGNORE(fprintf(stdout, "%s", s));
#else
nrnpy_pr("%s", s);
#endif
}
}
if (hardplot && hpdev && text && strlen(s)) {
Expand Down

0 comments on commit ef4da5d

Please sign in to comment.