Skip to content

Commit e5055e5

Browse files
author
David Sommerseth
committed
Added new error handling code, to simplify error handling
1 parent 8e3054a commit e5055e5

2 files changed

Lines changed: 133 additions & 0 deletions

File tree

src/dmierror.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+

src/dmierror.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
33+
#ifndef DMIERROR_H
34+
#define DMIERROR_H
35+
36+
#include <stdarg.h>
37+
38+
void _pyReturnError(PyObject *exception, const char *fname, int line, const char *msgfmt, ...);
39+
40+
/**
41+
* A more flexible function for setting error messages.
42+
* This macro is the one which is supposed to be used in programs, as it will
43+
* also exit the calling function with NULL.
44+
* @author David Sommerseth <davids@redhat.com>
45+
* @param PyObject* A Python Exception object
46+
* @param const char* Error message to follow the exception, may be string formated
47+
*/
48+
#define PyReturnError(Exception, msg...) { \
49+
_pyReturnError(Exception, __FILE__, __LINE__,## msg); \
50+
return NULL; \
51+
}
52+
53+
54+
#endif

0 commit comments

Comments
 (0)