Jack Jansen | 6ad8d13 | 1997-01-15 16:53:37 +0000 | [diff] [blame] | 1 | /* Example of embedding Python in another program */ |
| 2 | |
| 3 | #include "Python.h" |
Jack Jansen | 6ad8d13 | 1997-01-15 16:53:37 +0000 | [diff] [blame] | 4 | #include "macglue.h" |
Jack Jansen | 6ad8d13 | 1997-01-15 16:53:37 +0000 | [diff] [blame] | 5 | |
| 6 | static char *argv0; |
| 7 | |
Jack Jansen | 893b0ab | 2001-10-09 23:09:00 +0000 | [diff] [blame] | 8 | long 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 Jansen | 6ad8d13 | 1997-01-15 16:53:37 +0000 | [diff] [blame] | 21 | main(argc, argv) |
| 22 | int argc; |
| 23 | char **argv; |
| 24 | { |
Jack Jansen | 6ad8d13 | 1997-01-15 16:53:37 +0000 | [diff] [blame] | 25 | /* So the user can set argc/argv to something interesting */ |
| 26 | argc = ccommand(&argv); |
Jack Jansen | 6ad8d13 | 1997-01-15 16:53:37 +0000 | [diff] [blame] | 27 | /* Save a copy of argv0 */ |
| 28 | argv0 = argv[0]; |
| 29 | |
Jack Jansen | 509ad42 | 2001-10-08 15:32:17 +0000 | [diff] [blame] | 30 | /* 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 Jansen | 893b0ab | 2001-10-09 23:09:00 +0000 | [diff] [blame] | 34 | } 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 Jansen | 6ad8d13 | 1997-01-15 16:53:37 +0000 | [diff] [blame] | 40 | PyMac_Initialize(); |
Jack Jansen | 6ad8d13 | 1997-01-15 16:53:37 +0000 | [diff] [blame] | 41 | |
| 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 Jansen | 6ad8d13 | 1997-01-15 16:53:37 +0000 | [diff] [blame] | 61 | /* 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 */ |
| 67 | char * |
| 68 | getprogramname() |
| 69 | { |
| 70 | return argv0; |
| 71 | } |