3 // SWIG file embedding the Python interpreter in something else.
4 // This file is based on Python-1.4.
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.
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.
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.
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).
32 %echo "embed.i : Using Python 1.4"
40 #include <pythonrun.h>
45 void SWIG_init(); /* Forward reference */
47 #define inittab python_inittab
49 /* Grab Python's inittab[] structure */
58 /* Now define our own version of it.
59 Hopefully someone does not have more than 1000 built-in modules */
61 struct _inittab inittab[1000];
63 static int swig_num_modules = 0;
65 /* Function for adding modules to Python */
68 static void swig_add_module(char *name, void (*initfunc)()) {
69 inittab[swig_num_modules].name = name;
70 inittab[swig_num_modules].initfunc = initfunc;
72 inittab[swig_num_modules].name = (char *) 0;
73 inittab[swig_num_modules].initfunc = 0;
76 /* Function to add all of Python's build in modules to our interpreter */
78 static void swig_add_builtin() {
80 while (python_inittab[i].name) {
81 swig_add_module(python_inittab[i].name, python_inittab[i].initfunc);
87 /* Add SWIG builtin function */
88 swig_add_module(SWIG_name, SWIG_init);
95 /* Interface to getopt(): */
100 extern "C" int getopt(int, char **, char *);
102 extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
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 */
110 /* Subroutines that live in their own file */
113 extern int isatty(int fd);
114 extern void PySys_SetArgv(int, char **);
116 extern char *Py_GetVersion();
117 extern char *Py_GetCopyright();
122 /* For getprogramname(); set by main() */
125 /* For getargcargv(); set by main() */
126 static char **orig_argv;
127 static int orig_argc;
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";
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\
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\
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\
157 main(int argc, char **argv) {
160 char *command = NULL;
161 char *filename = NULL;
167 swig_add_builtin(); /* Add SWIG built-in modules */
168 orig_argc = argc; /* For getargcargv() */
170 argv0 = argv[0]; /* For getprogramname() */
172 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
174 if ((p = getenv("PYTHONSUPPRESS")) && *p != '\0')
175 Py_SuppressPrintingFlag = 1;
176 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
178 if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
180 if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
183 while ((c = getopt(argc, argv, "c:disuv")) != EOF) {
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);
191 "not enough memory to copy -c argument");
192 strcpy(command, optarg);
193 strcat(command, "\n");
208 Py_SuppressPrintingFlag++;
219 /* This space reserved for other options */
222 fprintf(stderr, usage_line, argv[0]);
223 fprintf(stderr, usage_top);
224 fprintf(stderr, usage_bot);
233 setbuf(stdout, (char *)NULL);
234 setbuf(stderr, (char *)NULL);
236 /* On MPW (3.2) unbuffered seems to hang */
237 setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
238 setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
242 if (command == NULL && optind < argc &&
243 strcmp(argv[optind], "-") != 0)
244 filename = argv[optind];
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());
251 if (filename != NULL) {
252 if ((fp = fopen(filename, "r")) == NULL) {
253 fprintf(stderr, "%s: can't open file '%s'\n",
260 if (command != NULL) {
261 /* Backup optind and force sys.argv[0] = '-c' */
266 PySys_SetArgv(argc-optind, argv+optind);
269 sts = PyRun_SimpleString(command) != 0;
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");
277 (void) PyRun_SimpleFile(fp, startup);
284 fp, filename == NULL ? "<stdin>" : filename) != 0;
285 if (filename != NULL)
289 if (inspect && isatty((int)fileno(stdin)) &&
290 (filename != NULL || command != NULL))
291 sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
298 /* Return the program name -- some code out there needs this. */
311 /* Make the *original* argc/argv available to other modules.
312 This is rare, but it is needed by the secureware extension. */
318 getargcargv(int *argc,char ***argv)
324 /* Total Hack to get getpath.c to compile under C++ */
327 #define malloc (char *) malloc