1 /* Example of embedding Python in another program */
8 long my_writehandler(char *buf
, long count
)
11 unsigned char mybuf
[255];
14 if (mycount
> 255 ) mycount
= 255;
15 mybuf
[0] = (unsigned char)mycount
;
16 strncpy((char *)mybuf
+1, buf
, mycount
);
25 /* So the user can set argc/argv to something interesting */
26 argc
= ccommand(&argv
);
27 /* Save a copy of argv0 */
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
);
35 if ( argc
> 1 && strcmp(argv
[1], "-d") == 0 ) {
36 PyMac_SetConsoleHandler(PyMac_DummyReadHandler
, my_writehandler
,
39 /* Initialize the Python interpreter. Required. */
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
);
48 /* Do some application specific code */
49 printf("Hello, brave new world\n\n");
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");
56 /* Note that you can call any public function of the Python
57 interpreter here, e.g. call_object(). */
59 /* Some more application specific code */
60 printf("\nGoodbye, cruel world\n");
61 /* Exit, cleaning up the interpreter */
66 /* This function is called by the interpreter to get its own name */