|
2 | 2 |
|
3 | 3 | #include "Python.h" |
4 | 4 |
|
5 | | -void initxyzzy(void); /* Forward */ |
| 5 | +PyObject* PyInit_xyzzy(void); /* Forward */ |
6 | 6 |
|
7 | 7 | main(int argc, char **argv) |
8 | 8 | { |
| 9 | + /* Ignore passed-in argc/argv. If desired, conversion |
| 10 | + should use mbstowcs to convert them. */ |
| 11 | + wchar_t *args[] = {L"embed", L"hello", 0}; |
| 12 | + |
9 | 13 | /* Pass argv[0] to the Python interpreter */ |
10 | | - Py_SetProgramName(argv[0]); |
| 14 | + Py_SetProgramName(args[0]); |
| 15 | + |
| 16 | + /* Add a static module */ |
| 17 | + PyImport_AppendInittab("xyzzy", PyInit_xyzzy); |
11 | 18 |
|
12 | 19 | /* Initialize the Python interpreter. Required. */ |
13 | 20 | Py_Initialize(); |
14 | 21 |
|
15 | | - /* Add a static module */ |
16 | | - initxyzzy(); |
17 | | - |
18 | 22 | /* Define sys.argv. It is up to the application if you |
19 | 23 | want this; you can also let it undefined (since the Python |
20 | 24 | code is generally not a main program it has no business |
21 | 25 | touching sys.argv...) */ |
22 | | - PySys_SetArgv(argc, argv); |
| 26 | + PySys_SetArgv(2, args); |
23 | 27 |
|
24 | 28 | /* Do some application specific code */ |
25 | 29 | printf("Hello, brave new world\n\n"); |
26 | 30 |
|
27 | 31 | /* Execute some Python statements (in module __main__) */ |
28 | 32 | PyRun_SimpleString("import sys\n"); |
29 | | - PyRun_SimpleString("print sys.builtin_module_names\n"); |
30 | | - PyRun_SimpleString("print sys.modules.keys()\n"); |
31 | | - PyRun_SimpleString("print sys.executable\n"); |
32 | | - PyRun_SimpleString("print sys.argv\n"); |
| 33 | + PyRun_SimpleString("print(sys.builtin_module_names)\n"); |
| 34 | + PyRun_SimpleString("print(sys.modules.keys())\n"); |
| 35 | + PyRun_SimpleString("print(sys.executable)\n"); |
| 36 | + PyRun_SimpleString("print(sys.argv)\n"); |
33 | 37 |
|
34 | 38 | /* Note that you can call any public function of the Python |
35 | 39 | interpreter here, e.g. call_object(). */ |
@@ -57,9 +61,20 @@ static PyMethodDef xyzzy_methods[] = { |
57 | 61 | {NULL, NULL} /* sentinel */ |
58 | 62 | }; |
59 | 63 |
|
60 | | -void |
61 | | -initxyzzy(void) |
| 64 | +static struct PyModuleDef xyzzymodule = { |
| 65 | + {}, /* m_base */ |
| 66 | + "xyzzy", /* m_name */ |
| 67 | + 0, /* m_doc */ |
| 68 | + 0, /* m_size */ |
| 69 | + xyzzy_methods, /* m_methods */ |
| 70 | + 0, /* m_reload */ |
| 71 | + 0, /* m_traverse */ |
| 72 | + 0, /* m_clear */ |
| 73 | + 0, /* m_free */ |
| 74 | +}; |
| 75 | + |
| 76 | +PyObject* |
| 77 | +PyInit_xyzzy(void) |
62 | 78 | { |
63 | | - PyImport_AddModule("xyzzy"); |
64 | | - Py_InitModule("xyzzy", xyzzy_methods); |
| 79 | + return PyModule_Create(&xyzzymodule); |
65 | 80 | } |
0 commit comments