示例#1
0
文件: errors.c 项目: irov/Mengine
/* Call when an exception has occurred but there is no way for Python
   to handle it.  Examples: exception in __del__ or during GC. */
void
PyErr_WriteUnraisable(PyObject *obj)
{
    PyObject *f, *t, *v, *tb;
    PyErr_Fetch(&t, &v, &tb);
    f = PySys_GetObject("stderr");
    if (f != NULL) {
        PyFile_WriteString("Exception ", f);
        if (t) {
            PyObject* moduleName;
            char* className;
            assert(PyExceptionClass_Check(t));
            className = PyExceptionClass_Name(t);
            if (className != NULL) {
                char *dot = strrchr(className, '.');
                if (dot != NULL)
                    className = dot+1;
            }

            moduleName = PyObject_GetAttrString(t, "__module__");
            if (moduleName == NULL)
                PyFile_WriteString("<unknown>", f);
            else {
                char* modstr = PyString_AsString(moduleName);
                if (modstr &&
                    strcmp(modstr, "exceptions") != 0)
                {
                    PyFile_WriteString(modstr, f);
                    PyFile_WriteString(".", f);
                }
            }
            if (className == NULL)
                PyFile_WriteString("<unknown>", f);
            else
                PyFile_WriteString(className, f);
            if (v && v != Py_None) {
                PyFile_WriteString(": ", f);
                if (PyFile_WriteObject(v, f, 0) < 0) {
                    PyErr_Clear();
                    PyFile_WriteString("<exception repr() failed>", f);
                }
            }
            Py_XDECREF(moduleName);
        }
        PyFile_WriteString(" in ", f);
        if (PyFile_WriteObject(obj, f, 0) < 0) {
            PyErr_Clear();
            PyFile_WriteString("<object repr() failed>", f);
        }
        PyFile_WriteString(" ignored\n", f);
        PyErr_Clear(); /* Just in case */
    }
    Py_XDECREF(t);
    Py_XDECREF(v);
    Py_XDECREF(tb);
}
示例#2
0
文件: pennpy.c 项目: tkrajcar/pypenn
static void
cu5_pennpy_exception(char *buff, char **bp)
{
    const char *ex_name;
    PyObject *ex_type;

    /*
     * Report exception.  We don't return specifics; if you need them,
     * handle that in the Python code and return a string.
     */
    ex_name = "PYTHON"; /* catch-all exception "name" */

    ex_type = PyErr_Occurred();
    if (PyExceptionClass_Check(ex_type)) {
        /* Exception type is a valid exception class. */
        ex_name = PyExceptionClass_Name(ex_type);
        if (ex_name) {
            /* Exception class has a name. */
            char *ex_short_name = strrchr(ex_name, '.');
            if (ex_short_name) {
                /* blah.blah.ExceptionName */
                ex_name = ex_short_name + 1;
            }
        } else {
            /* Shouldn't happen, but extra cautious. */
        }
    } else {
        /*
         * This must be an old-style string exception, but those are
         * deprecated and now too rare for us to bother identifying.
         */
    }

    safe_format(buff, bp, T("#-1 %s EXCEPTION"), ex_name);

    /* Dump the stack to stderr and clear the error indicator. */
    do_rawlog(LT_ERR, "PennPy: Python exception:");
    PyErr_Print();
}
示例#3
0
文件: errors.c 项目: cpcloud/cpython
/* Call when an exception has occurred but there is no way for Python
   to handle it.  Examples: exception in __del__ or during GC. */
void
PyErr_WriteUnraisable(PyObject *obj)
{
    _Py_IDENTIFIER(__module__);
    PyObject *f, *t, *v, *tb;
    PyObject *moduleName = NULL;
    char* className;

    PyErr_Fetch(&t, &v, &tb);

    f = _PySys_GetObjectId(&PyId_stderr);
    if (f == NULL || f == Py_None)
        goto done;

    if (obj) {
        if (PyFile_WriteString("Exception ignored in: ", f) < 0)
            goto done;
        if (PyFile_WriteObject(obj, f, 0) < 0) {
            PyErr_Clear();
            if (PyFile_WriteString("<object repr() failed>", f) < 0) {
                goto done;
            }
        }
        if (PyFile_WriteString("\n", f) < 0)
            goto done;
    }

    if (PyTraceBack_Print(tb, f) < 0)
        goto done;

    if (!t)
        goto done;

    assert(PyExceptionClass_Check(t));
    className = PyExceptionClass_Name(t);
    if (className != NULL) {
        char *dot = strrchr(className, '.');
        if (dot != NULL)
            className = dot+1;
    }

    moduleName = _PyObject_GetAttrId(t, &PyId___module__);
    if (moduleName == NULL) {
        PyErr_Clear();
        if (PyFile_WriteString("<unknown>", f) < 0)
            goto done;
    }
    else {
        if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins)) {
            if (PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) < 0)
                goto done;
            if (PyFile_WriteString(".", f) < 0)
                goto done;
        }
    }
    if (className == NULL) {
        if (PyFile_WriteString("<unknown>", f) < 0)
            goto done;
    }
    else {
        if (PyFile_WriteString(className, f) < 0)
            goto done;
    }

    if (v && v != Py_None) {
        if (PyFile_WriteString(": ", f) < 0)
            goto done;
        if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0) {
            PyErr_Clear();
            if (PyFile_WriteString("<exception str() failed>", f) < 0) {
                goto done;
            }
        }
    }
    if (PyFile_WriteString("\n", f) < 0)
        goto done;

done:
    Py_XDECREF(moduleName);
    Py_XDECREF(t);
    Py_XDECREF(v);
    Py_XDECREF(tb);
    PyErr_Clear(); /* Just in case */
}
示例#4
0
文件: pythonrun.c 项目: tiran/cpython
static void
print_exception(PyObject *f, PyObject *value)
{
    int err = 0;
    PyObject *type, *tb;
    _Py_IDENTIFIER(print_file_and_line);

    if (!PyExceptionInstance_Check(value)) {
        err = PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
        err += PyFile_WriteString(Py_TYPE(value)->tp_name, f);
        err += PyFile_WriteString(" found\n", f);
        if (err)
            PyErr_Clear();
        return;
    }

    Py_INCREF(value);
    fflush(stdout);
    type = (PyObject *) Py_TYPE(value);
    tb = PyException_GetTraceback(value);
    if (tb && tb != Py_None)
        err = PyTraceBack_Print(tb, f);
    if (err == 0 &&
        _PyObject_HasAttrId(value, &PyId_print_file_and_line))
    {
        PyObject *message, *filename, *text;
        int lineno, offset;
        if (!parse_syntax_error(value, &message, &filename,
                                &lineno, &offset, &text))
            PyErr_Clear();
        else {
            PyObject *line;

            Py_DECREF(value);
            value = message;

            line = PyUnicode_FromFormat("  File \"%U\", line %d\n",
                                          filename, lineno);
            Py_DECREF(filename);
            if (line != NULL) {
                PyFile_WriteObject(line, f, Py_PRINT_RAW);
                Py_DECREF(line);
            }

            if (text != NULL) {
                print_error_text(f, offset, text);
                Py_DECREF(text);
            }

            /* Can't be bothered to check all those
               PyFile_WriteString() calls */
            if (PyErr_Occurred())
                err = -1;
        }
    }
    if (err) {
        /* Don't do anything else */
    }
    else {
        PyObject* moduleName;
        const char *className;
        _Py_IDENTIFIER(__module__);
        assert(PyExceptionClass_Check(type));
        className = PyExceptionClass_Name(type);
        if (className != NULL) {
            const char *dot = strrchr(className, '.');
            if (dot != NULL)
                className = dot+1;
        }

        moduleName = _PyObject_GetAttrId(type, &PyId___module__);
        if (moduleName == NULL || !PyUnicode_Check(moduleName))
        {
            Py_XDECREF(moduleName);
            err = PyFile_WriteString("<unknown>", f);
        }
        else {
            if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins))
            {
                err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW);
                err += PyFile_WriteString(".", f);
            }
            Py_DECREF(moduleName);
        }
        if (err == 0) {
            if (className == NULL)
                      err = PyFile_WriteString("<unknown>", f);
            else
                      err = PyFile_WriteString(className, f);
        }
    }
    if (err == 0 && (value != Py_None)) {
        PyObject *s = PyObject_Str(value);
        /* only print colon if the str() of the
           object is not the empty string
        */
        if (s == NULL) {
            PyErr_Clear();
            err = -1;
            PyFile_WriteString(": <exception str() failed>", f);
        }
        else if (!PyUnicode_Check(s) ||
            PyUnicode_GetLength(s) != 0)
            err = PyFile_WriteString(": ", f);
        if (err == 0)
          err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
        Py_XDECREF(s);
    }
    /* try to write a newline in any case */
    if (err < 0) {
        PyErr_Clear();
    }
    err += PyFile_WriteString("\n", f);
    Py_XDECREF(tb);
    Py_DECREF(value);
    /* If an error happened here, don't show it.
       XXX This is wrong, but too many callers rely on this behavior. */
    if (err != 0)
        PyErr_Clear();
}
示例#5
0
void
PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
{
	int err = 0;
	PyObject *f = PySys_GetObject("stderr");
	Py_INCREF(value);
	if (f == NULL)
		fprintf(stderr, "lost sys.stderr\n");
	else {
		if (Py_FlushLine())
			PyErr_Clear();
		fflush(stdout);
		if (tb && tb != Py_None)
			err = PyTraceBack_Print(tb, f);
		if (err == 0 &&
		    PyObject_HasAttrString(value, "print_file_and_line"))
		{
			PyObject *message;
			const char *filename, *text;
			int lineno, offset;
			if (!parse_syntax_error(value, &message, &filename,
						&lineno, &offset, &text))
				PyErr_Clear();
			else {
				char buf[10];
				PyFile_WriteString("  File \"", f);
				if (filename == NULL)
					PyFile_WriteString("<string>", f);
				else
					PyFile_WriteString(filename, f);
				PyFile_WriteString("\", line ", f);
				PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
				PyFile_WriteString(buf, f);
				PyFile_WriteString("\n", f);
				if (text != NULL)
					print_error_text(f, offset, text);
				Py_DECREF(value);
				value = message;
				/* Can't be bothered to check all those
				   PyFile_WriteString() calls */
				if (PyErr_Occurred())
					err = -1;
			}
		}
		if (err) {
			/* Don't do anything else */
		}
		else if (PyExceptionClass_Check(exception)) {
			PyObject* moduleName;
			char* className = PyExceptionClass_Name(exception);
			if (className != NULL) {
				char *dot = strrchr(className, '.');
				if (dot != NULL)
					className = dot+1;
			}

			moduleName = PyObject_GetAttrString(exception, "__module__");
			if (moduleName == NULL)
				err = PyFile_WriteString("<unknown>", f);
			else {
				char* modstr = PyString_AsString(moduleName);
				if (modstr && strcmp(modstr, "exceptions"))
				{
					err = PyFile_WriteString(modstr, f);
					err += PyFile_WriteString(".", f);
				}
				Py_DECREF(moduleName);
			}
			if (err == 0) {
				if (className == NULL)
				      err = PyFile_WriteString("<unknown>", f);
				else
				      err = PyFile_WriteString(className, f);
			}
		}
		else
			err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
		if (err == 0 && (value != Py_None)) {
			PyObject *s = PyObject_Str(value);
			/* only print colon if the str() of the
			   object is not the empty string
			*/
			if (s == NULL)
				err = -1;
			else if (!PyString_Check(s) ||
				 PyString_GET_SIZE(s) != 0)
				err = PyFile_WriteString(": ", f);
			if (err == 0)
			  err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
			Py_XDECREF(s);
		}
		if (err == 0)
			err = PyFile_WriteString("\n", f);
	}
	Py_DECREF(value);
	/* If an error happened here, don't show it.
	   XXX This is wrong, but too many callers rely on this behavior. */
	if (err != 0)
		PyErr_Clear();
}
示例#6
0
PyObject* NS(ErrorSet)(
  PyObject  *self
)
{
  SETUP_context
  PyObject *typeO = NULL, *valueO=NULL, *tbO = NULL, *retO = Py_None;

  PyThreadState *tstate = PyThreadState_Get();

  if (tstate->curexc_type == NULL || tstate->curexc_type == Py_None) {
    typeO = tstate->exc_type;
    valueO = tstate->exc_value;
    tbO = tstate->exc_traceback;
  } else {
    typeO = tstate->curexc_type;
    valueO = tstate->curexc_value;
    tbO = tstate->curexc_traceback;
  }
  Py_XINCREF(typeO);
  Py_XINCREF(valueO);
  Py_XINCREF(tbO);
  PyErr_Clear();

  {
    PyObject *valO = NULL ;

    if (typeO == Py_None || typeO == NULL) {
      MqErrorC (context, __func__, -1, "No active exception to reraise");

    } else if (PyErr_GivenExceptionMatches(typeO,NS(MqSException))) {

      MqErrorSet (context, 
	(MQ_INT) PyLong_AsLong(PyObject_GetAttrString(typeO,"num")),
	(enum MqErrorE) PyLong_AsLong(PyObject_GetAttrString(typeO,"code")),
	(MQ_CST const) PyBytes_AsString(PyUnicode_AsUTF8String(PyObject_GetAttrString(typeO,"text"))),
	NULL
      );

    } else if (valueO) {
      PyObject *tmod = NULL, *strO = NULL, *lstO = NULL;
      PyErrorCheckNT(end1,  tmod = PyImport_ImportModule("traceback"));
      if (tbO) {
	PyErr_NormalizeException(&typeO, &valueO, &tbO);
	PyErrorCheckNT(end1,  lstO = PyObject_CallMethod(tmod, "format_exception", "OOO", typeO, valueO, tbO));
      } else {
	PyErrorCheckNT(end1,  lstO = PyObject_CallMethod(tmod, "format_exception_only", "OO", typeO, valueO));
      }
      PyErrorCheckNT(end1,  strO = PyC2O(""));
      PyErrorCheckNT(end1,  valO = PyObject_CallMethod(strO, "join", "O", lstO));
end1:
      Py_XDECREF(tmod);
      Py_XDECREF(lstO);
      Py_XDECREF(strO);
      if (valO != NULL) {
	PyObject *strO=NULL, *utf8=NULL;

	PyErrorCheckNT(end2, strO = PyObject_Str(valO));
	PyErrorCheckNT(end2, utf8 = PyUnicode_AsUTF8String(strO));
	MqErrorC(context, __func__, -1, PyBytes_AsString(utf8));
end2:
	Py_XDECREF(utf8);
	Py_XDECREF(strO);
      }
      if (MqErrorGetCodeI(context) != MQ_ERROR)
	MqErrorV(context, __func__, -1, "%s: python error", PyExceptionClass_Name(typeO));
    }
  }

  Py_XDECREF(typeO);
  Py_XDECREF(valueO);
  Py_XDECREF(tbO);
  Py_XINCREF(retO);

  return retO;
}