blob: 7d450f165f365189fcd042ce3e82bea398c406d0 [file] [log] [blame]
Jack Jansen6ad8d131997-01-15 16:53:37 +00001/* Example of embedding Python in another program */
2
3#include "Python.h"
4#ifdef macintosh
5#include "macglue.h"
Jack Jansen6ad8d131997-01-15 16:53:37 +00006#endif /* macintosh */
7
8static char *argv0;
9
10main(argc, argv)
11 int argc;
12 char **argv;
13{
14#ifdef macintosh
15 /* So the user can set argc/argv to something interesting */
16 argc = ccommand(&argv);
17#endif
18 /* Save a copy of argv0 */
19 argv0 = argv[0];
20
21 /* Initialize the Python interpreter. Required. */
22#ifdef macintosh
Jack Jansen509ad422001-10-08 15:32:17 +000023 /* If the first option is "-q" we don't open a console */
24 if ( argc > 1 && strcmp(argv[1], "-q") == 0 ) {
25 PyMac_SetConsoleHandler(PyMac_DummyReadHandler, PyMac_DummyWriteHandler,
26 PyMac_DummyWriteHandler);
27/* freopen("demo output", "w", stdout); */
28 }
Jack Jansen6ad8d131997-01-15 16:53:37 +000029 PyMac_Initialize();
30#else
31 Py_Initialize();
32#endif
33
34 /* Define sys.argv. It is up to the application if you
35 want this; you can also let it undefined (since the Python
36 code is generally not a main program it has no business
37 touching sys.argv...) */
38 PySys_SetArgv(argc, argv);
39
40 /* Do some application specific code */
41 printf("Hello, brave new world\n\n");
42
43 /* Execute some Python statements (in module __main__) */
44 PyRun_SimpleString("import sys\n");
45 PyRun_SimpleString("print sys.builtin_module_names\n");
46 PyRun_SimpleString("print sys.argv\n");
47
48 /* Note that you can call any public function of the Python
49 interpreter here, e.g. call_object(). */
50
51 /* Some more application specific code */
52 printf("\nGoodbye, cruel world\n");
Jack Jansen6ad8d131997-01-15 16:53:37 +000053 /* Exit, cleaning up the interpreter */
54 Py_Exit(0);
55 /*NOTREACHED*/
56}
57
58/* This function is called by the interpreter to get its own name */
59char *
60getprogramname()
61{
62 return argv0;
63}