blob: a022f94fa83026e0d9beb85dafee0f51c2d01cf8 [file] [log] [blame]
Jack Jansen6ad8d131997-01-15 16:53:37 +00001/* Example of embedding Python in another program */
2
3#include "Python.h"
Jack Jansen6ad8d131997-01-15 16:53:37 +00004#include "macglue.h"
Jack Jansen6ad8d131997-01-15 16:53:37 +00005
6static char *argv0;
7
Jack Jansen893b0ab2001-10-09 23:09:00 +00008long my_writehandler(char *buf, long count)
9{
10 long mycount;
11 unsigned char mybuf[255];
12
13 mycount = count;
14 if (mycount > 255 ) mycount = 255;
15 mybuf[0] = (unsigned char)mycount;
16 strncpy((char *)mybuf+1, buf, mycount);
17 DebugStr(mybuf);
18 return count;
19}
20
Jack Jansen6ad8d131997-01-15 16:53:37 +000021main(argc, argv)
22 int argc;
23 char **argv;
24{
Jack Jansen6ad8d131997-01-15 16:53:37 +000025 /* So the user can set argc/argv to something interesting */
26 argc = ccommand(&argv);
Jack Jansen6ad8d131997-01-15 16:53:37 +000027 /* Save a copy of argv0 */
28 argv0 = argv[0];
29
Jack Jansen509ad422001-10-08 15:32:17 +000030 /* If the first option is "-q" we don't open a console */
31 if ( argc > 1 && strcmp(argv[1], "-q") == 0 ) {
32 PyMac_SetConsoleHandler(PyMac_DummyReadHandler, PyMac_DummyWriteHandler,
33 PyMac_DummyWriteHandler);
Jack Jansen893b0ab2001-10-09 23:09:00 +000034 } else
35 if ( argc > 1 && strcmp(argv[1], "-d") == 0 ) {
36 PyMac_SetConsoleHandler(PyMac_DummyReadHandler, my_writehandler,
37 my_writehandler);
38 }
39 /* Initialize the Python interpreter. Required. */
Jack Jansen6ad8d131997-01-15 16:53:37 +000040 PyMac_Initialize();
Jack Jansen6ad8d131997-01-15 16:53:37 +000041
42 /* Define sys.argv. It is up to the application if you
43 want this; you can also let it undefined (since the Python
44 code is generally not a main program it has no business
45 touching sys.argv...) */
46 PySys_SetArgv(argc, argv);
47
48 /* Do some application specific code */
49 printf("Hello, brave new world\n\n");
50
51 /* Execute some Python statements (in module __main__) */
52 PyRun_SimpleString("import sys\n");
53 PyRun_SimpleString("print sys.builtin_module_names\n");
54 PyRun_SimpleString("print sys.argv\n");
55
56 /* Note that you can call any public function of the Python
57 interpreter here, e.g. call_object(). */
58
59 /* Some more application specific code */
60 printf("\nGoodbye, cruel world\n");
Jack Jansen6ad8d131997-01-15 16:53:37 +000061 /* Exit, cleaning up the interpreter */
62 Py_Exit(0);
63 /*NOTREACHED*/
64}
65
66/* This function is called by the interpreter to get its own name */
67char *
68getprogramname()
69{
70 return argv0;
71}