Ejemplo n.º 1
0
static int mjt_ntop6(const void *_src, char *dst, int size) {
  unsigned i;
  unsigned short w[8];
  unsigned bs = 0, cs = 0;
  unsigned bl = 0, cl = 0;
  char *p;
  const unsigned char *s = _src;

  if (size < 40)	/* for simplicity, disallow non-max-size buffer */
    return 0;

  for(i = 0; i < 8; ++i, s += 2) {
    w[i] = (((unsigned short)(s[0])) << 8) | s[1];
    if (!w[i]) {
      if (!cl++) cs = i;
    }
    else {
      if (cl > bl) bl = cl, bs = cs;
    }
  }
  if (cl > bl) bl = cl, bs = cs;
  p = dst;
  if (bl == 1)
    bl = 0;
  if (bl) {
    for(i = 0; i < bs; ++i) {
      if (i) *p++ = ':';
      p = hexc(p, w[i]);
    }
    *p++ = ':';
    i += bl;
    if (i == 8)
      *p++ = ':';
  }
  else
    i = 0;
  for(; i < 8; ++i) {
    if (i) *p++ = ':';
    if (i == 6 && !bs && (bl == 6 || (bl == 5 && w[5] == 0xffff)))
      return mjt_ntop4(s - 4, p, size - (p - dst));
    p = hexc(p, w[i]);
  }
  *p = '\0';
  return 1;
}
Ejemplo n.º 2
0
/**
 * Extract the exception error string from the python interpreter.
 *
 * This function assumes the gil is acquired. See \ref python_thread_guard.
 *
 * Code adapted from http://stackoverflow.com/questions/1418015/how-to-get-python-exception-text
 */
std::string parse_python_error() {
  PyObject *exc,*val,*tb;
  PyErr_Fetch(&exc,&val,&tb);
  PyErr_NormalizeException(&exc,&val,&tb);
  python::handle<> hexc(exc),hval(python::allow_null(val)),htb(python::allow_null(tb));
  if(!hval) {
    return python::extract<std::string>(python::str(hexc));
  }
  else {
    python::object traceback(python::import("traceback"));
    python::object format_exception(traceback.attr("format_exception"));
    python::object formatted_list(format_exception(hexc,hval,htb));
    python::object formatted(python::str("").join(formatted_list));
    return python::extract<std::string>(formatted);
  }
}
Ejemplo n.º 3
0
/// Adapted from http://stackoverflow.com/a/6576177/1689220
std::string pyerr_to_string(void)
{
    PyObject *exc, *val, *tb;
    bp::object formatted_list, formatted;
    PyErr_Fetch(&exc, &val, &tb);
    // wrap exception, value, traceback with bp::handle for auto memory management
    bp::handle<> hexc(exc), hval(bp::allow_null(val)), htb(bp::allow_null(tb));
    // import "traceback" module
    bp::object traceback(bp::import("traceback"));
    if (!tb) {
        bp::object format_exception_only(traceback.attr("format_exception_only"));
        formatted_list = format_exception_only(hexc, hval);
    } else {
        bp::object format_exception(traceback.attr("format_exception"));
        formatted_list = format_exception(hexc, hval, htb);
    }
    formatted = bp::str("\n").join(formatted_list);
    return bp::extract<std::string>(formatted);
}