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