]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxSWIG/swig_lib/python/embed13.i
Since I have made several changes to SWIG over the years to accomodate
[wxWidgets.git] / wxPython / wxSWIG / swig_lib / python / embed13.i
1 //
2 // embed.i
3 // SWIG file embedding the Python interpreter in something else.
4 // This file is based on Python-1.3, but it might work with
5 // later versions.
6 //
7 // This file makes it possible to extend Python and all of its
8 // built-in functions without having to hack it's setup script.
9 //
10
11 #ifdef AUTODOC
12 %subsection "embed13.i"
13 %text %{
14 This module provides support for building a new version of the
15 Python 1.3 executable. This will be necessary on systems that do
16 not support shared libraries and may be necessary with C++
17 extensions. This file contains everything you need to build
18 a new version of Python from include files and libraries normally
19 installed with the Python language.
20
21 This module is functionally equivalent to the embed.i library,
22 but has a number of changes needed to work with older versions
23 of Python.
24 %}
25 #else
26 %echo "embed.i : Using Python 1.3"
27 #endif
28
29
30
31
32 %wrapper %{
33
34 #ifndef NEED_GETOPT
35 #include <unistd.h>
36 #endif
37
38 #include <pythonrun.h>
39 typedef struct SWIGPyTab {
40 char *name;
41 void (*initfunc)();
42 } SWIGPyTab;
43
44 #ifdef __cplusplus
45 extern "C"
46 #endif
47 void SWIG_init(void); /* Forward reference */
48
49 #define inittab python_inittab
50
51 /* Grab Python's inittab[] structure */
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 #include <config.c>
57
58 #undef inittab
59
60
61 /* Now define our own version of it.
62 God forbid someone have more than 1000 built-in modules! */
63
64 SWIGPyTab inittab[1000];
65
66 static int swig_num_modules = 0;
67
68 /* Function for adding modules to Python */
69
70 static void swig_add_module(char *name, void (*initfunc)()) {
71 inittab[swig_num_modules].name = name;
72 inittab[swig_num_modules].initfunc = initfunc;
73 swig_num_modules++;
74 inittab[swig_num_modules].name = (char *) 0;
75 inittab[swig_num_modules].initfunc = (void (*)()) 0;
76 }
77
78 /* Function to add all of Python's build in modules to our interpreter */
79
80 static void swig_add_builtin() {
81 int i = 0;
82 while (python_inittab[i].name) {
83 swig_add_module(python_inittab[i].name, python_inittab[i].initfunc);
84 i++;
85 }
86
87 /* Add SWIG builtin function */
88 swig_add_module(SWIG_name, SWIG_init);
89 #ifdef SWIGMODINIT
90 SWIGMODINIT
91 #endif
92 }
93 #ifdef __cplusplus
94 }
95 #endif
96
97 /* Interface to getopt(): */
98 extern int optind;
99 extern char *optarg;
100
101 #ifdef NEED_GETOPT
102 #ifdef __cplusplus
103 extern "C" int getopt(int, char **, char *);
104 #else
105 extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
106 #endif
107 #endif
108
109 extern int Py_DebugFlag; /* For parser.c, declared in pythonrun.c */
110 extern int Py_VerboseFlag; /* For import.c, declared in pythonrun.c */
111 extern int Py_SuppressPrintingFlag; /* For ceval.c, declared in pythonrun.c */
112
113 /* Subroutines that live in their own file */
114 #ifdef __cplusplus
115 extern "C" {
116 extern int isatty(int fd);
117 extern int PySys_SetArgv(int, char **);
118 #endif
119 extern char *getversion();
120 extern char *getcopyright();
121 #ifdef __cplusplus
122 }
123 #endif
124
125 /* For getprogramname(); set by main() */
126 static char *argv0;
127
128 /* For getargcargv(); set by main() */
129 static char **orig_argv;
130 static int orig_argc;
131
132 /* Short usage message (with %s for argv0) */
133 static char *usage_line =
134 "usage: %s [-d] [-i] [-s] [-u ] [-v] [-c cmd | file | -] [arg] ...\n";
135
136 /* Long usage message, split into parts < 512 bytes */
137 static char *usage_top = "\n\
138 Options and arguments (and corresponding environment variables):\n\
139 -d : debug output from parser (also PYTHONDEBUG=x)\n\
140 -i : inspect interactively after running script (also PYTHONINSPECT=x)\n\
141 -s : suppress printing of top level expressions (also PYTHONSUPPRESS=x)\n\
142 -u : unbuffered stdout and stderr (also PYTHONUNBUFFERED=x)\n\
143 -v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
144 -c cmd : program passed in as string (terminates option list)\n\
145 ";
146 static char *usage_bot = "\
147 file : program read from script file\n\
148 - : program read from stdin (default; interactive mode if a tty)\n\
149 arg ...: arguments passed to program in sys.argv[1:]\n\
150 \n\
151 Other environment variables:\n\
152 PYTHONSTARTUP: file executed on interactive startup (no default)\n\
153 PYTHONPATH : colon-separated list of directories prefixed to the\n\
154 default module search path. The result is sys.path.\n\
155 ";
156
157 /* Main program */
158
159 int
160 main(int argc, char **argv) {
161 int c;
162 int sts;
163 char *command = NULL;
164 char *filename = NULL;
165 FILE *fp = stdin;
166 char *p;
167 int inspect = 0;
168 int unbuffered = 0;
169
170 swig_add_builtin(); /* Add SWIG built-in modules */
171 orig_argc = argc; /* For getargcargv() */
172 orig_argv = argv;
173 argv0 = argv[0]; /* For getprogramname() */
174
175 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
176 Py_DebugFlag = 1;
177 if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
178 Py_SuppressPrintingFlag = 1;
179 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
180 Py_VerboseFlag = 1;
181 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
182 inspect = 1;
183 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
184 unbuffered = 1;
185
186 while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
187 if (c == 'c') {
188 /* -c is the last option; following arguments
189 that look like options are left for the
190 the command to interpret. */
191 command = (char *) malloc(strlen(optarg) + 2);
192 if (command == NULL)
193 Py_FatalError(
194 "not enough memory to copy -c argument");
195 strcpy(command, optarg);
196 strcat(command, "\n");
197 break;
198 }
199
200 switch (c) {
201
202 case 'd':
203 Py_DebugFlag++;
204 break;
205
206 case 'i':
207 inspect++;
208 break;
209
210 case 's':
211 Py_SuppressPrintingFlag++;
212 break;
213
214 case 'u':
215 unbuffered++;
216 break;
217
218 case 'v':
219 Py_VerboseFlag++;
220 break;
221
222 /* This space reserved for other options */
223
224 default:
225 fprintf(stderr, usage_line, argv[0]);
226 fprintf(stderr, usage_top);
227 fprintf(stderr, usage_bot);
228 exit(2);
229 /*NOTREACHED*/
230
231 }
232 }
233
234 if (unbuffered) {
235 #ifndef MPW
236 setbuf(stdout, (char *)NULL);
237 setbuf(stderr, (char *)NULL);
238 #else
239 /* On MPW (3.2) unbuffered seems to hang */
240 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
241 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
242 #endif
243 }
244
245 if (command == NULL && optind < argc &&
246 strcmp(argv[optind], "-") != 0)
247 filename = argv[optind];
248
249 if (Py_VerboseFlag ||
250 command == NULL && filename == NULL && isatty((int)fileno(fp)))
251 fprintf(stderr, "Python %s\n%s\n",
252 getversion(), getcopyright());
253
254 if (filename != NULL) {
255 if ((fp = fopen(filename, "r")) == NULL) {
256 fprintf(stderr, "%s: can't open file '%s'\n",
257 argv[0], filename);
258 exit(2);
259 }
260 }
261
262 Py_Initialize();
263 if (command != NULL) {
264 /* Backup optind and force sys.argv[0] = '-c' */
265 optind--;
266 argv[optind] = "-c";
267 }
268
269 PySys_SetArgv(argc-optind, argv+optind);
270
271 if (command) {
272 sts = PyRun_SimpleString(command) != 0;
273 }
274 else {
275 if (filename == NULL && isatty((int)fileno(fp))) {
276 char *startup = getenv("PYTHONSTARTUP");
277 if (startup != NULL && startup[0] != '\0') {
278 FILE *fp = fopen(startup, "r");
279 if (fp != NULL) {
280 (void) PyRun_SimpleFile(fp, startup);
281 PyErr_Clear();
282 fclose(fp);
283 }
284 }
285 }
286 sts = PyRun_AnyFile(
287 fp, filename == NULL ? "<stdin>" : filename) != 0;
288 if (filename != NULL)
289 fclose(fp);
290 }
291
292 if (inspect && isatty((int)fileno(stdin)) &&
293 (filename != NULL || command != NULL))
294 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
295
296 Py_Exit(sts);
297 /*NOTREACHED*/
298 }
299
300
301 /* Return the program name -- some code out there needs this. */
302
303 #ifdef __cplusplus
304 extern "C"
305 #endif
306
307 char *
308 getprogramname()
309 {
310 return argv0;
311 }
312
313
314 /* Make the *original* argc/argv available to other modules.
315 This is rare, but it is needed by the secureware extension. */
316
317 #ifdef __cplusplus
318 extern "C"
319 #endif
320 void
321 getargcargv(int *argc,char ***argv)
322 {
323 *argc = orig_argc;
324 *argv = orig_argv;
325 }
326
327 /* Total Hack to get getpath.c to compile under C++ */
328
329 #ifdef __cplusplus
330 #define malloc (char *) malloc
331 extern "C" {
332 #endif
333 #include <getpath.c>
334 #ifdef __cplusplus
335 }
336 #undef malloc
337 #endif
338
339 %}
340
341
342