tensorflow/python/lib/core/py_util.cc
// Returns a PyObject containing a string, or null
Returns a PyObject containing a string, or null
Returns a PyObject containing a string or null
void TryAppendTraceback(PyObject* ptype, PyObject* pvalue, PyObject* ptraceback,
                        string* out) {
  PyObject* tb_module = PyImport_AddModule("traceback");
  if (!tb_module) {
    return;
  }
  PyObject* format_exception =
      PyObject_GetAttrString(tb_module, "format_exception");
  if (!format_exception) {
    return;
  }
  if (!PyCallable_Check(format_exception)) {
    Py_DECREF(format_exception);
    return;
  }
  PyObject* ret_val = PyObject_CallFunctionObjArgs(format_exception, ptype,
                                                   pvalue, ptraceback, nullptr);
  Py_DECREF(format_exception);
  if (!ret_val) {
    return;
  }
  if (!PyList_Check(ret_val)) {
    Py_DECREF(ret_val);
    return;
  }
  Py_ssize_t n = PyList_GET_SIZE(ret_val);
  for (Py_ssize_t i = 0; i < n; ++i) {
    PyObject* v = PyList_GET_ITEM(ret_val, i);
#if PY_MAJOR_VERSION < 3
    strings::StrAppend(out, PyString_AS_STRING(v), "\n");
#else
    strings::StrAppend(out, PyUnicode_AsUTF8(v), "\n");
#endif
  }
  Py_DECREF(ret_val);
}
void TryAppendTraceback ( PyObject * ptype , PyObject * pvalue , PyObject * ptraceback , string * out ) { PyObject * tb_module = PyImport_AddModule ( " " ) ; if ( ! tb_module ) { return ; } PyObject * format_exception = PyObject_GetAttrString ( tb_module , " " ) ; if ( ! format_exception ) { return ; } if ( ! PyCallable_Check ( format_exception ) ) { Py_DECREF ( format_exception ) ; return ; } PyObject * ret_val = PyObject_CallFunctionObjArgs ( format_exception , ptype , pvalue , ptraceback , nullptr ) ; Py_DECREF ( format_exception ) ; if ( ! ret_val ) { return ; } if ( ! PyList_Check ( ret_val ) ) { Py_DECREF ( ret_val ) ; return ; } Py_ssize_t n = PyList_GET_SIZE ( ret_val ) ; for ( Py_ssize_t i = 0 ; i < n ; ++ i ) { PyObject * v = PyList_GET_ITEM ( ret_val , i ) ; #if PY_MAJOR_VERSION < 3 strings :: StrAppend ( out , PyString_AS_STRING ( v ) , " \n " ) ; #else strings :: StrAppend ( out , PyUnicode_AsUTF8 ( v ) , " \n " ) ; #endif } Py_DECREF ( ret_val ) ; }
Returns a PyObject containing a string, or null
Returns a PyObject containing a string or null
// The "traceback" module is assumed to be imported already by script_ops.py. // Iterate through ret_val.