示例#1
0
/* Fetches and raises an error
 */
void VARARGS(
      pyexe_error_fetch_and_raise,
      PyObject *exception_object,
      const char *,
      format_string )
{
	va_list argument_list;

	char error_string[ PYEXE_ERROR_STRING_SIZE ];

	PyObject *exception_traceback = NULL;
	PyObject *exception_type      = NULL;
	PyObject *exception_value     = NULL;
	PyObject *string_object       = NULL;
	static char *function         = "pyexe_error_fetch_and_raise";
	char *exception_string        = NULL;
	size_t error_string_length    = 0;
	int print_count               = 0;

#if PY_MAJOR_VERSION >= 3
	PyObject *utf8_string_object  = NULL;
#endif

	if( format_string == NULL )
	{
		PyErr_Format(
		 PyExc_ValueError,
		 "%s: missing format string.",
		 function );

		return;
	}
	VASTART(
	 argument_list,
	 const char *,
	 format_string );

	print_count = PyOS_vsnprintf(
	               error_string,
	               PYEXE_ERROR_STRING_SIZE,
	               format_string,
	               argument_list );

	VAEND(
	 argument_list );

	if( print_count < 0 )
	{
		PyErr_Format(
		 PyExc_ValueError,
		 "%s: unable to format exception string.",
		 function );

		return;
	}
	error_string_length = libcstring_narrow_string_length(
	                       error_string );

	if( ( error_string_length >= 1 )
	 && ( error_string[ error_string_length - 1 ] == '.' ) )
	{
		error_string[ error_string_length - 1 ] = 0;
	}
	PyErr_Fetch(
	 &exception_type,
	 &exception_value,
	 &exception_traceback );

	string_object = PyObject_Repr(
			    exception_value );

#if PY_MAJOR_VERSION >= 3
	utf8_string_object = PyUnicode_AsUTF8String(
	                      string_object );

	if( utf8_string_object != NULL )
	{
		exception_string = PyBytes_AsString(
		                    utf8_string_object );
	}
#else
	exception_string = PyString_AsString(
	                    string_object );
#endif
	if( exception_string != NULL )
	{
		PyErr_Format(
		 exception_object,
		 "%s with error: %s.",
		 error_string,
		 exception_string );
	}
	else
	{
		PyErr_Format(
		 exception_object,
		 "%s.",
		 error_string );
	}
	Py_DecRef(
	 string_object );

	return;
}
示例#2
0
void sf_error(const char *func_name, sf_error_t code, const char *fmt, ...)
{
    PyGILState_STATE save;
    PyObject *scipy_special = NULL;
    char msg[2048], info[1024];
    static PyObject *py_SpecialFunctionWarning = NULL;
    sf_action_t action;
    va_list ap;

    if ((int)code < 0 || (int)code >= 10) {
	code = SF_ERROR_OTHER;
    }
    action = sf_error_get_action(code);
    if (action == SF_ERROR_IGNORE) {
        return;
    }

    if (func_name == NULL) {
        func_name = "?";
    }

    if (fmt != NULL && fmt[0] != '\0') {
        va_start(ap, fmt);
        PyOS_vsnprintf(info, 1024, fmt, ap);
        va_end(ap);
        PyOS_snprintf(msg, 2048, "scipy.special/%s: (%s) %s",
                      func_name, sf_error_messages[(int)code], info);
    }
    else {
        PyOS_snprintf(msg, 2048, "scipy.special/%s: %s",
                      func_name, sf_error_messages[(int)code]);
    }

#ifdef WITH_THREAD
    save = PyGILState_Ensure();
#endif

    if (PyErr_Occurred()) {
      goto skip_warn;
    }

    scipy_special = PyImport_ImportModule("scipy.special");
    if (scipy_special == NULL) {
	PyErr_Clear();
	goto skip_warn;
    }

    if (action == SF_ERROR_WARN) {
	py_SpecialFunctionWarning =
	    PyObject_GetAttrString(scipy_special, "SpecialFunctionWarning");
    }
    else if (action == SF_ERROR_RAISE) {
	py_SpecialFunctionWarning =
	    PyObject_GetAttrString(scipy_special, "SpecialFunctionError");
    }
    else {
	/* Sentinel, should never get here */
	py_SpecialFunctionWarning = NULL;
    }
    if (py_SpecialFunctionWarning == NULL) {
	PyErr_Clear();
	goto skip_warn;
    }

    if (action == SF_ERROR_WARN) {
	PyErr_WarnEx(py_SpecialFunctionWarning, msg, 1);
	/*
	 * For ufuncs the return value is ignored! We rely on the fact
	 * that the Ufunc loop will call PyErr_Occurred() later on.
	 */
    }
    else if (action == SF_ERROR_RAISE) {
	PyErr_SetString(py_SpecialFunctionWarning, msg);
    }
    else {
	goto skip_warn;
    }

    skip_warn:
#ifdef WITH_THREAD
        PyGILState_Release(save);
#else
	;
#endif
}
示例#3
0
/* Raises an error
 */
void VARARGS(
    pymsiecf_error_raise,
    libcerror_error_t *error,
    PyObject *exception_object,
    const char *,
    format_string )
{
    va_list argument_list;

    char error_string[ PYMSIECF_ERROR_STRING_SIZE ];
    char exception_string[ PYMSIECF_ERROR_STRING_SIZE ];

    static char *function     = "pymsiecf_error_raise";
    size_t error_string_index = 0;
    int print_count           = 0;

    if( format_string == NULL )
    {
        PyErr_Format(
            PyExc_ValueError,
            "%s: missing format string.",
            function );

        return;
    }
    VASTART(
        argument_list,
        const char *,
        format_string );

    print_count = PyOS_vsnprintf(
                      exception_string,
                      PYMSIECF_ERROR_STRING_SIZE,
                      format_string,
                      argument_list );

    VAEND(
        argument_list );

    if( print_count < 0 )
    {
        PyErr_Format(
            PyExc_ValueError,
            "%s: unable to format exception string.",
            function );

        return;
    }
    if( error != NULL )
    {
        if( libcerror_error_backtrace_sprint(
                    error,
                    error_string,
                    PYMSIECF_ERROR_STRING_SIZE ) != -1 )
        {
            while( error_string_index < PYMSIECF_ERROR_STRING_SIZE )
            {
                if( error_string[ error_string_index ] == 0 )
                {
                    break;
                }
                if( error_string[ error_string_index ] == '\n' )
                {
                    error_string[ error_string_index ] = ' ';
                }
                error_string_index++;
            }
            if( error_string_index >= PYMSIECF_ERROR_STRING_SIZE )
            {
                error_string[ PYMSIECF_ERROR_STRING_SIZE - 1 ] = 0;
            }
            PyErr_Format(
                exception_object,
                "%s %s",
                exception_string,
                error_string );

            return;
        }
    }
    PyErr_Format(
        exception_object,
        "%s",
        exception_string );

    return;
}
示例#4
0
void sf_error(char *func_name, sf_error_t code, char *fmt, ...)
{
    char msg[2048], info[1024];
    static PyObject *py_SpecialFunctionWarning = NULL;
    va_list ap;

    if (!print_error_messages) {
        return;
    }

    if (func_name == NULL) {
        func_name = "?";
    }

    if ((int)code < 0 || (int)code >= 10) {
        code = SF_ERROR_OTHER;
    }

    if (fmt != NULL && fmt[0] != '\0') {
        va_start(ap, fmt);
        PyOS_vsnprintf(info, 1024, fmt, ap);
        va_end(ap);
        PyOS_snprintf(msg, 2048, "scipy.special/%s: (%s) %s",
                      func_name, sf_error_messages[(int)code], info);
    }
    else {
        PyOS_snprintf(msg, 2048, "scipy.special/%s: %s",
                      func_name, sf_error_messages[(int)code]);
    }

    {
#ifdef WITH_THREAD
        PyGILState_STATE save = PyGILState_Ensure();
#endif

        if (PyErr_Occurred())
            goto skip_warn;

        if (py_SpecialFunctionWarning == NULL) {
            PyObject *scipy_special = NULL;

            scipy_special = PyImport_ImportModule("scipy.special");
            if (scipy_special == NULL) {
                PyErr_Clear();
                goto skip_warn;
            }

            py_SpecialFunctionWarning = PyObject_GetAttrString(
                scipy_special, "SpecialFunctionWarning");
            if (py_SpecialFunctionWarning == NULL) {
                PyErr_Clear();
                goto skip_warn;
            }
        }

        if (py_SpecialFunctionWarning != NULL) {
            PyErr_WarnEx(py_SpecialFunctionWarning, msg, 1);

            /*
             * The return value is ignored! We rely on the fact that the
             * Ufunc loop will call PyErr_Occurred() later on.
             */
        }

    skip_warn:
#ifdef WITH_THREAD
        PyGILState_Release(save);
#endif
    }
}