|
| 1 | +/* Simpilfied and improved Python Error/Exception functions |
| 2 | + * |
| 3 | + * 2009 (C) David Sommerseth <davids@redhat.com> |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation; either version 2 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | + * |
| 19 | + * For the avoidance of doubt the "preferred form" of this code is one which |
| 20 | + * is in an open unpatent encumbered format. Where cryptographic key signing |
| 21 | + * forms part of the process of creating an executable the information |
| 22 | + * including keys needed to generate an equivalently functional executable |
| 23 | + * are deemed to be part of the source code. |
| 24 | + */ |
| 25 | + |
| 26 | +/** |
| 27 | + * @file dmierror.h |
| 28 | + * @brief Simpilfied and improved Python Error/Exception functions |
| 29 | + * @author David Sommerseth <davids@redhat.com> |
| 30 | + */ |
| 31 | + |
| 32 | +// #include <Python.h> |
| 33 | +#include <stdio.h> |
| 34 | +#include <stdlib.h> |
| 35 | +#include <stdarg.h> |
| 36 | +#include <string.h> |
| 37 | + |
| 38 | +#define PRINT_ERRORS 1 |
| 39 | + |
| 40 | +/** |
| 41 | + * A more flexible function for setting error messages. This function |
| 42 | + * is called via PyReturnError(...) macro which also returns NULL. |
| 43 | + * @author David Sommerseth <davids@redhat.com> |
| 44 | + * @param PyObject* A Python Exception object |
| 45 | + * @param const char* Error message to follow the exception, may be string formated |
| 46 | + * |
| 47 | + */ |
| 48 | +void _pyReturnError(void *exception, const char *fname, int line, const char *fmt, ...) |
| 49 | +{ |
| 50 | + va_list ap; |
| 51 | + char *buf = NULL; |
| 52 | + |
| 53 | + va_start(ap, fmt); |
| 54 | + buf = (char *) malloc(4098); |
| 55 | + memset(buf, 0, 4098); |
| 56 | + |
| 57 | + if( buf == NULL ) { |
| 58 | + // Backup routine if we can't get the needed memory |
| 59 | + fprintf(stderr, "\n\n** ERROR ALLOCATING ERROR MESSAGE BUFFER\n\n"); |
| 60 | + fprintf(stderr, "** ERROR: [%s:%i] ", fname, line); |
| 61 | + vfprintf(stderr, fmt, ap); |
| 62 | + fprintf(stderr, "\n"); |
| 63 | + va_end(ap); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // Set the error state and message |
| 68 | + snprintf(buf, 4096, "[%s:%i] %s", fname, line, fmt); |
| 69 | + /// PyErr_Format(exception, buf, ap); |
| 70 | + |
| 71 | +#ifdef PRINT_ERRORS |
| 72 | + fprintf(stderr, "\n**\n** ERROR: "); |
| 73 | + vfprintf(stderr, buf, ap); |
| 74 | + fprintf(stderr, "\n**\n\n"); |
| 75 | +#endif |
| 76 | + va_end(ap); |
| 77 | + free(buf); buf = NULL; |
| 78 | +} |
| 79 | + |
0 commit comments