append(): Fixing the test for convertability after consultation with
[python/dscho.git] / Mac / Demo / embed / demo.c
bloba022f94fa83026e0d9beb85dafee0f51c2d01cf8
1 /* Example of embedding Python in another program */
3 #include "Python.h"
4 #include "macglue.h"
6 static char *argv0;
8 long my_writehandler(char *buf, long count)
10 long mycount;
11 unsigned char mybuf[255];
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;
21 main(argc, argv)
22 int argc;
23 char **argv;
25 /* So the user can set argc/argv to something interesting */
26 argc = ccommand(&argv);
27 /* Save a copy of argv0 */
28 argv0 = argv[0];
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);
34 } else
35 if ( argc > 1 && strcmp(argv[1], "-d") == 0 ) {
36 PyMac_SetConsoleHandler(PyMac_DummyReadHandler, my_writehandler,
37 my_writehandler);
39 /* Initialize the Python interpreter. Required. */
40 PyMac_Initialize();
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 */
62 Py_Exit(0);
63 /*NOTREACHED*/
66 /* This function is called by the interpreter to get its own name */
67 char *
68 getprogramname()
70 return argv0;