1 /* -----------------------------------------------------------------------------
4 * Dynamically loadable python module for wad.
6 * Author(s) : David Beazley (beazley@cs.uchicago.edu)
8 * Copyright (C) 2000. The University of Chicago.
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * See the file COPYING for a complete copy of the LGPL.
25 * ----------------------------------------------------------------------------- */
30 static char cvs[] = "$Id: python.c 10001 2007-10-17 21:33:57Z wsfulton $";
32 /* These are the python exception objects we will add
33 SegFault, BusError, AbortError */
35 static PyObject *segfault_exc = 0;
36 static PyObject *buserror_exc = 0;
37 static PyObject *abort_exc = 0;
38 static PyObject *illegal_exc = 0;
40 extern PyObject *new_wadobject(WadFrame *f,int);
42 /* Function return points and values */
44 static WadReturnFunc retpts[] = {
46 {"_PyImport_LoadDynamicModule", 0},
47 {"PyEval_EvalCode", 0},
48 {"PyObject_GetAttrString", 0},
49 {"PyObject_SetAttrString", -1},
51 {"PyObject_Print", -1},
52 {"PyObject_CallFunction", 0},
53 {"PyObject_CallMethod", 0},
54 {"PyObject_CallObject", 0},
56 {"PyObject_Compare", -1},
57 {"PyObject_DelAttrString",-1},
58 {"PyObject_DelItem",-1},
59 {"PyObject_GetItem",0},
60 {"PyObject_SetItem",-1},
61 {"PyObject_HasAttrString",-1},
63 {"PyObject_Length",-1},
67 {"PyNumber_Absolute", 0},
70 {"PyNumber_Coerce",0},
71 {"PyNumber_Divide",0},
72 {"PyNumber_Divmod",0},
75 {"PyNumber_Invert",0},
77 {"PyNumber_Lshift",0},
78 {"PyNumber_Multiply", 0},
79 {"PyNumber_Negative", 0},
81 {"PyNumber_Positive", 0},
83 {"PyNumber_Remainder",0},
84 {"PyNumber_Rshift",0},
85 {"PyNumber_Subtract",0},
88 {"PySequence_Concat",0},
89 {"PySequence_Count",-1},
90 {"PySequence_Delitem",-1},
91 {"PySequence_DelSlice",-1},
92 {"PySequence_Getitem",0},
93 {"PySequence_GetSlice",0},
95 {"PySequence_Index",-1},
96 {"PySequence_Repeat",0},
97 {"PySequence_SetItem",-1},
98 {"PySequence_SetSlice",-1},
99 {"PySequence_Tuple",0},
101 {"PyMapping_Clear",-1},
102 {"PyMapping_DelItem",-1},
103 {"PyMapping_DelItemString",-1},
104 {"PyMapping_GetItemString",0},
105 {"PyMapping_HasKey",-1},
106 {"PyMapping_HasKeyString",-1},
107 {"PyMapping_Items",0},
108 {"PyMapping_Keys",0},
109 {"PyMapping_Length", -1},
110 {"PyMapping_SetItemString", -1},
111 {"PyMapping_Values", 0},
114 /* Handler function */
115 static void handler(int signo, WadFrame *frame, char *ret) {
116 static char message[65536];
117 static char temp[1024];
125 /* printf("python handler.\n"); */
127 wad_default_callback(signo, frame, ret);
131 strcpy(message,"[ C stack trace ]\n\n");
143 type = PyExc_FloatingPointError;
149 type = PyExc_RuntimeError;
155 /* Find the last exception frame */
159 /* Now work backwards */
162 strcat(message, f->debug_str);
163 if (f->debug_srcstr) srcstr = f->debug_srcstr;
167 strcat(message,"\n");
168 strcat(message, srcstr);
169 strcat(message,"\n");
173 if (wad_heap_overflow) {
174 write(2, "WAD: Heap overflow detected.\n", 30);
175 wad_default_callback(signo, frame, ret);
178 /* Note: if the heap is blown, there is a very good chance that this
179 function will not succeed and we'll dump core. However, the check
180 above should dump a stack trace to stderr just in case we don't make it
184 PyErr_SetString(type, message);
186 PyErr_SetObject(type, new_wadobject(frame,0));
192 m = PyImport_ImportModule((char *)"__builtin__");
193 d = PyModule_GetDict(m);
194 printf("WAD Enabled\n");
196 segfault_exc = PyErr_NewException((char *)"exceptions.SegFault", NULL, NULL);
197 PyDict_SetItemString(d,(char *)"SegFault",segfault_exc);
199 buserror_exc = PyErr_NewException((char *)"exceptions.BusError", NULL, NULL);
200 PyDict_SetItemString(d,(char *)"BusError",buserror_exc);
202 abort_exc = PyErr_NewException((char*)"exceptions.AbortError", NULL, NULL);
203 PyDict_SetItemString(d,(char *)"AbortError",abort_exc);
205 illegal_exc = PyErr_NewException((char *)"exceptions.IllegalInstruction", NULL, NULL);
206 PyDict_SetItemString(d,(char *)"IllegalInstruction",illegal_exc);
209 wad_set_callback(handler);
210 wad_set_returns(retpts);
213 static PyMethodDef wadmethods[] = {
217 void initlibwadpy() {
218 Py_InitModule((char *)"libwadpy",wadmethods);