import source from 1.3.40
[external/swig.git] / Tools / WAD / Python / python.c
1 /* ----------------------------------------------------------------------------- 
2  * python.c
3  *
4  *     Dynamically loadable python module for wad.
5  * 
6  * Author(s) : David Beazley (beazley@cs.uchicago.edu)
7  *
8  * Copyright (C) 2000.  The University of Chicago. 
9  *
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.
14  *
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.
19  *
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
23  * 
24  * See the file COPYING for a complete copy of the LGPL.
25  * ----------------------------------------------------------------------------- */
26
27 #include "Python.h"
28 #include "wad.h"
29
30 static char cvs[] = "$Id: python.c 10001 2007-10-17 21:33:57Z wsfulton $";
31
32 /* These are the python exception objects we will add
33    SegFault, BusError, AbortError */
34
35 static PyObject *segfault_exc = 0;
36 static PyObject *buserror_exc = 0;
37 static PyObject *abort_exc = 0;
38 static PyObject *illegal_exc = 0;
39
40 extern PyObject *new_wadobject(WadFrame *f,int);
41
42 /* Function return points and values */
43
44 static WadReturnFunc retpts[] = {
45   {"call_builtin", 0},
46   {"_PyImport_LoadDynamicModule", 0},
47   {"PyEval_EvalCode", 0},
48   {"PyObject_GetAttrString", 0},
49   {"PyObject_SetAttrString", -1},
50   {"PyObject_Repr", 0},
51   {"PyObject_Print", -1},
52   {"PyObject_CallFunction", 0},
53   {"PyObject_CallMethod", 0},
54   {"PyObject_CallObject", 0},
55   {"PyObject_Cmp", -1},
56   {"PyObject_Compare", -1},
57   {"PyObject_DelAttrString",-1},
58   {"PyObject_DelItem",-1},
59   {"PyObject_GetItem",0},
60   {"PyObject_SetItem",-1},
61   {"PyObject_HasAttrString",-1},
62   {"PyObject_Hash",-1},
63   {"PyObject_Length",-1},
64   {"PyObject_Str",0},
65   {"PyObject_Type", 0},
66
67   {"PyNumber_Absolute", 0},
68   {"PyNumber_Add",0},
69   {"PyNumber_And",0},
70   {"PyNumber_Coerce",0},
71   {"PyNumber_Divide",0},
72   {"PyNumber_Divmod",0},
73   {"PyNumber_Float",0},
74   {"PyNumber_Int",0},
75   {"PyNumber_Invert",0},
76   {"PyNumber_Long",0},
77   {"PyNumber_Lshift",0},
78   {"PyNumber_Multiply", 0},
79   {"PyNumber_Negative", 0},
80   {"PyNumber_Or",0},
81   {"PyNumber_Positive", 0},
82   {"PyNumber_Power",0},
83   {"PyNumber_Remainder",0},
84   {"PyNumber_Rshift",0},
85   {"PyNumber_Subtract",0},
86   {"PyNumber_Xor",0},
87
88   {"PySequence_Concat",0},
89   {"PySequence_Count",-1},
90   {"PySequence_Delitem",-1},
91   {"PySequence_DelSlice",-1},
92   {"PySequence_Getitem",0},
93   {"PySequence_GetSlice",0},
94   {"PySequence_In",-1},
95   {"PySequence_Index",-1},
96   {"PySequence_Repeat",0},
97   {"PySequence_SetItem",-1},
98   {"PySequence_SetSlice",-1},
99   {"PySequence_Tuple",0},
100
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},
112   {"",0}};
113
114 /* Handler function */  
115 static void handler(int signo, WadFrame *frame, char *ret) {
116   static char message[65536];
117   static char temp[1024];
118   int  len = 0;
119   PyObject *type;
120   char *name;
121   WadFrame *f;
122   WadFrame *fline = 0;
123   char     *srcstr = 0;
124
125   /*  printf("python handler.\n"); */
126   if (!ret) {
127     wad_default_callback(signo, frame, ret);
128     return;
129   }
130
131   strcpy(message,"[ C stack trace ]\n\n");
132   switch(signo) {
133   case SIGSEGV:
134     type = segfault_exc;
135     break;
136   case SIGBUS:
137     type = buserror_exc;
138     break;
139   case SIGABRT:
140     type = abort_exc;
141     break;
142   case SIGFPE:
143     type = PyExc_FloatingPointError;
144     break;
145   case SIGILL:
146     type = illegal_exc;
147     break;
148   default:
149     type = PyExc_RuntimeError;
150     break;
151   }
152
153 #ifdef OLD
154   f = frame;
155   /* Find the last exception frame */
156   while (!f->last) {
157     f= f->next;
158   }
159   /* Now work backwards */
160   f = f->prev;
161   while (f) {
162     strcat(message, f->debug_str);
163     if (f->debug_srcstr) srcstr = f->debug_srcstr;
164     f = f->prev;
165   }
166   if (srcstr) {
167     strcat(message,"\n");
168     strcat(message, srcstr);
169     strcat(message,"\n");
170   }
171 #endif
172
173   if (wad_heap_overflow) {
174     write(2, "WAD: Heap overflow detected.\n", 30);
175     wad_default_callback(signo, frame, ret);
176   }
177
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
181   back. */
182
183 #ifdef OLD
184   PyErr_SetString(type, message);
185 #endif
186   PyErr_SetObject(type, new_wadobject(frame,0));
187
188 }
189
190 void pywadinit() {
191   PyObject *d, *m;
192   m = PyImport_ImportModule((char *)"__builtin__");
193   d = PyModule_GetDict(m);
194   printf("WAD Enabled\n");
195
196   segfault_exc = PyErr_NewException((char *)"exceptions.SegFault", NULL, NULL);
197   PyDict_SetItemString(d,(char *)"SegFault",segfault_exc);
198
199   buserror_exc = PyErr_NewException((char *)"exceptions.BusError", NULL, NULL);
200   PyDict_SetItemString(d,(char *)"BusError",buserror_exc);
201
202   abort_exc = PyErr_NewException((char*)"exceptions.AbortError", NULL, NULL);
203   PyDict_SetItemString(d,(char *)"AbortError",abort_exc);
204
205   illegal_exc = PyErr_NewException((char *)"exceptions.IllegalInstruction", NULL, NULL);
206   PyDict_SetItemString(d,(char *)"IllegalInstruction",illegal_exc);  
207
208   wad_init();
209   wad_set_callback(handler);
210   wad_set_returns(retpts);
211 }
212
213 static PyMethodDef wadmethods[] = {
214   {0,0},
215 };
216
217 void initlibwadpy() {
218   Py_InitModule((char *)"libwadpy",wadmethods);
219 }
220
221