]>
Commit | Line | Data |
---|---|---|
1 | /*********************************************************************** | |
2 | * $Header$ | |
3 | * swig_lib/python/python.cfg | |
4 | * | |
5 | * This file contains coded needed to add variable linking to the | |
6 | * Python interpreter. C variables are added as a new kind of Python | |
7 | * datatype. | |
8 | * | |
9 | * Also contains supporting code for building python under Windows | |
10 | * and things like that. | |
11 | * | |
12 | * $Log$ | |
13 | * Revision 1.1 1999/08/04 05:25:58 RD | |
14 | * New Makefile/Setup files supporting multiple dynamic extension modules | |
15 | * for unix systems. | |
16 | * | |
17 | * Fixes for the wxGLCanvas demo to work around a strange bug in gtk. | |
18 | * | |
19 | * SWIG support routines now compiled separately instead of being bundled | |
20 | * in wx.cpp. | |
21 | * | |
22 | ************************************************************************/ | |
23 | ||
24 | #ifdef __cplusplus | |
25 | extern "C" { | |
26 | #endif | |
27 | #include "Python.h" | |
28 | #ifdef __cplusplus | |
29 | } | |
30 | #endif | |
31 | ||
32 | /* Definitions for Windows/Unix exporting */ | |
33 | #if defined(__WIN32__) | |
34 | # if defined(_MSC_VER) | |
35 | # define SWIGEXPORT(a,b) __declspec(dllexport) a b | |
36 | # else | |
37 | # if defined(__BORLANDC__) | |
38 | # define SWIGEXPORT(a,b) a _export b | |
39 | # else | |
40 | # define SWIGEXPORT(a,b) a b | |
41 | # endif | |
42 | # endif | |
43 | #else | |
44 | # define SWIGEXPORT(a,b) a b | |
45 | #endif | |
46 | ||
47 | #ifdef SWIG_GLOBAL | |
48 | #ifdef __cplusplus | |
49 | #define SWIGSTATIC extern "C" | |
50 | #else | |
51 | #define SWIGSTATIC | |
52 | #endif | |
53 | #endif | |
54 | ||
55 | #ifndef SWIGSTATIC | |
56 | #define SWIGSTATIC static | |
57 | #endif | |
58 | ||
59 | typedef struct { | |
60 | char *name; | |
61 | PyObject *(*get_attr)(void); | |
62 | int (*set_attr)(PyObject *); | |
63 | } swig_globalvar; | |
64 | ||
65 | typedef struct swig_varlinkobject { | |
66 | PyObject_HEAD | |
67 | swig_globalvar **vars; | |
68 | int nvars; | |
69 | int maxvars; | |
70 | } swig_varlinkobject; | |
71 | ||
72 | /* ---------------------------------------------------------------------- | |
73 | swig_varlink_repr() | |
74 | ||
75 | Function for python repr method | |
76 | ---------------------------------------------------------------------- */ | |
77 | ||
78 | static PyObject * | |
79 | swig_varlink_repr(swig_varlinkobject *v) | |
80 | { | |
81 | v = v; | |
82 | return PyString_FromString("<Global variables>"); | |
83 | } | |
84 | ||
85 | /* --------------------------------------------------------------------- | |
86 | swig_varlink_print() | |
87 | ||
88 | Print out all of the global variable names | |
89 | --------------------------------------------------------------------- */ | |
90 | ||
91 | static int | |
92 | swig_varlink_print(swig_varlinkobject *v, FILE *fp, int flags) | |
93 | { | |
94 | ||
95 | int i = 0; | |
96 | flags = flags; | |
97 | fprintf(fp,"Global variables { "); | |
98 | while (v->vars[i]) { | |
99 | fprintf(fp,"%s", v->vars[i]->name); | |
100 | i++; | |
101 | if (v->vars[i]) fprintf(fp,", "); | |
102 | } | |
103 | fprintf(fp," }\n"); | |
104 | return 0; | |
105 | } | |
106 | ||
107 | /* -------------------------------------------------------------------- | |
108 | swig_varlink_getattr | |
109 | ||
110 | This function gets the value of a variable and returns it as a | |
111 | PyObject. In our case, we'll be looking at the datatype and | |
112 | converting into a number or string | |
113 | -------------------------------------------------------------------- */ | |
114 | ||
115 | static PyObject * | |
116 | swig_varlink_getattr(swig_varlinkobject *v, char *n) | |
117 | { | |
118 | int i = 0; | |
119 | char temp[128]; | |
120 | ||
121 | while (v->vars[i]) { | |
122 | if (strcmp(v->vars[i]->name,n) == 0) { | |
123 | return (*v->vars[i]->get_attr)(); | |
124 | } | |
125 | i++; | |
126 | } | |
127 | sprintf(temp,"C global variable %s not found.", n); | |
128 | PyErr_SetString(PyExc_NameError,temp); | |
129 | return NULL; | |
130 | } | |
131 | ||
132 | /* ------------------------------------------------------------------- | |
133 | swig_varlink_setattr() | |
134 | ||
135 | This function sets the value of a variable. | |
136 | ------------------------------------------------------------------- */ | |
137 | ||
138 | static int | |
139 | swig_varlink_setattr(swig_varlinkobject *v, char *n, PyObject *p) | |
140 | { | |
141 | char temp[128]; | |
142 | int i = 0; | |
143 | while (v->vars[i]) { | |
144 | if (strcmp(v->vars[i]->name,n) == 0) { | |
145 | return (*v->vars[i]->set_attr)(p); | |
146 | } | |
147 | i++; | |
148 | } | |
149 | sprintf(temp,"C global variable %s not found.", n); | |
150 | PyErr_SetString(PyExc_NameError,temp); | |
151 | return 1; | |
152 | } | |
153 | ||
154 | statichere PyTypeObject varlinktype = { | |
155 | /* PyObject_HEAD_INIT(&PyType_Type) Note : This doesn't work on some machines */ | |
156 | PyObject_HEAD_INIT(0) | |
157 | 0, | |
158 | "varlink", /* Type name */ | |
159 | sizeof(swig_varlinkobject), /* Basic size */ | |
160 | 0, /* Itemsize */ | |
161 | 0, /* Deallocator */ | |
162 | (printfunc) swig_varlink_print, /* Print */ | |
163 | (getattrfunc) swig_varlink_getattr, /* get attr */ | |
164 | (setattrfunc) swig_varlink_setattr, /* Set attr */ | |
165 | 0, /* tp_compare */ | |
166 | (reprfunc) swig_varlink_repr, /* tp_repr */ | |
167 | 0, /* tp_as_number */ | |
168 | 0, /* tp_as_mapping*/ | |
169 | 0, /* tp_hash */ | |
170 | }; | |
171 | ||
172 | /* Create a variable linking object for use later */ | |
173 | ||
174 | SWIGSTATIC PyObject * | |
175 | SWIG_newvarlink(void) | |
176 | { | |
177 | swig_varlinkobject *result = 0; | |
178 | result = PyMem_NEW(swig_varlinkobject,1); | |
179 | varlinktype.ob_type = &PyType_Type; /* Patch varlinktype into a PyType */ | |
180 | result->ob_type = &varlinktype; | |
181 | /* _Py_NewReference(result); Does not seem to be necessary */ | |
182 | result->nvars = 0; | |
183 | result->maxvars = 64; | |
184 | result->vars = (swig_globalvar **) malloc(64*sizeof(swig_globalvar *)); | |
185 | result->vars[0] = 0; | |
186 | result->ob_refcnt = 0; | |
187 | Py_XINCREF((PyObject *) result); | |
188 | return ((PyObject*) result); | |
189 | } | |
190 | ||
191 | SWIGSTATIC void | |
192 | SWIG_addvarlink(PyObject *p, char *name, | |
193 | PyObject *(*get_attr)(void), int (*set_attr)(PyObject *p)) | |
194 | { | |
195 | swig_varlinkobject *v; | |
196 | v= (swig_varlinkobject *) p; | |
197 | ||
198 | if (v->nvars >= v->maxvars -1) { | |
199 | v->maxvars = 2*v->maxvars; | |
200 | v->vars = (swig_globalvar **) realloc(v->vars,v->maxvars*sizeof(swig_globalvar *)); | |
201 | if (v->vars == NULL) { | |
202 | fprintf(stderr,"SWIG : Fatal error in initializing Python module.\n"); | |
203 | exit(1); | |
204 | } | |
205 | } | |
206 | v->vars[v->nvars] = (swig_globalvar *) malloc(sizeof(swig_globalvar)); | |
207 | v->vars[v->nvars]->name = (char *) malloc(strlen(name)+1); | |
208 | strcpy(v->vars[v->nvars]->name,name); | |
209 | v->vars[v->nvars]->get_attr = get_attr; | |
210 | v->vars[v->nvars]->set_attr = set_attr; | |
211 | v->nvars++; | |
212 | v->vars[v->nvars] = 0; | |
213 | } | |
214 | ||
215 | ||
216 |