Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 869bad9

Browse files
committed
Issue #3717: Fix Demo/embed/demo.c.
Reviewed by Benjamin Peterson.
1 parent c820eaf commit 869bad9

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

Demo/embed/demo.c

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,38 @@
22

33
#include "Python.h"
44

5-
void initxyzzy(void); /* Forward */
5+
PyObject* PyInit_xyzzy(void); /* Forward */
66

77
main(int argc, char **argv)
88
{
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+
913
/* 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);
1118

1219
/* Initialize the Python interpreter. Required. */
1320
Py_Initialize();
1421

15-
/* Add a static module */
16-
initxyzzy();
17-
1822
/* Define sys.argv. It is up to the application if you
1923
want this; you can also let it undefined (since the Python
2024
code is generally not a main program it has no business
2125
touching sys.argv...) */
22-
PySys_SetArgv(argc, argv);
26+
PySys_SetArgv(2, args);
2327

2428
/* Do some application specific code */
2529
printf("Hello, brave new world\n\n");
2630

2731
/* Execute some Python statements (in module __main__) */
2832
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");
3337

3438
/* Note that you can call any public function of the Python
3539
interpreter here, e.g. call_object(). */
@@ -57,9 +61,20 @@ static PyMethodDef xyzzy_methods[] = {
5761
{NULL, NULL} /* sentinel */
5862
};
5963

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)
6278
{
63-
PyImport_AddModule("xyzzy");
64-
Py_InitModule("xyzzy", xyzzy_methods);
79+
return PyModule_Create(&xyzzymodule);
6580
}

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Build
7474
Tools/Demos
7575
-----------
7676

77+
- Issue #3717: Fix Demo/embed/demo.c.
78+
7779
- Issue #4072: Add a distutils demo for build_py_2to3.
7880

7981

0 commit comments

Comments
 (0)