@@ -170,90 +170,91 @@ The driver application
170170This is the only part of your application that has to be written in C. The
171171application code should look like the following::
172172
173- /* Include the Python headers */
174- #include <Python.h>
175-
176- #define PYTHON_LOCATION L"interp"
177- #define APP_MODULE "MyAwesomePythonApp"
178- #define APP_FUNCTION "main"
179-
180- /* Finding the Python interpreter */
181- #include <windows.h>
182- #include <pathcch.h>
183-
184- /* Tell the Visual Studio linker what libraries we need */
185- #pragma comment(lib, "delayimp")
186- #pragma comment(lib, "pathcch")
187-
188- int dll_dir(wchar_t *path) {
189- wchar_t interp_dir[PATHCCH_MAX_CCH];
190- if (GetModuleFileNameW(NULL, interp_dir, PATHCCH_MAX_CCH) &&
191- SUCCEEDED(PathCchRemoveFileSpec(interp_dir, PATHCCH_MAX_CCH)) &&
192- SUCCEEDED(PathCchCombineEx(interp_dir, PATHCCH_MAX_CCH, interp_dir, path, PATHCCH_ALLOW_LONG_PATHS)) &&
193- SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS) &&
194- AddDllDirectory(interp_dir) != 0) {
195- return 1;
196- }
197- return 0;
173+ ```c
174+ /* Include the Python headers */
175+ #include <Python.h>
176+
177+ #define PYTHON_LOCATION L"interp"
178+ #define APP_MODULE "MyAwesomePythonApp"
179+ #define APP_FUNCTION "main"
180+
181+ /* Finding the Python interpreter */
182+ #include <windows.h>
183+ #include <pathcch.h>
184+
185+ /* Tell the Visual Studio linker what libraries we need */
186+ #pragma comment(lib, "delayimp")
187+ #pragma comment(lib, "pathcch")
188+
189+ int dll_dir(wchar_t *path) {
190+ wchar_t interp_dir[PATHCCH_MAX_CCH];
191+ if (GetModuleFileNameW(NULL, interp_dir, PATHCCH_MAX_CCH) &&
192+ SUCCEEDED(PathCchRemoveFileSpec(interp_dir, PATHCCH_MAX_CCH)) &&
193+ SUCCEEDED(PathCchCombineEx(interp_dir, PATHCCH_MAX_CCH, interp_dir, path, PATHCCH_ALLOW_LONG_PATHS)) &&
194+ SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS) &&
195+ AddDllDirectory(interp_dir) != 0) {
196+ return 1;
198197 }
199-
200- /* Your application main program */
201- int wmain(int argc, wchar_t **argv)
202- {
203- PyStatus status;
204- PyConfig config;
205-
206- /* Tell the loader where to find the Python interpreter.
207- * This is the name, relative to the directory containing
208- * the application executable, of the directory where you
209- * placed the embeddable Python distribution.
210- *
211- * This MUST be called before any functions from the Python
212- * runtime are called.
213- */
214- if (!dll_dir(PYTHON_LOCATION))
215- return -1;
216-
217- /* Initialise the Python configuration */
218- PyConfig_InitIsolatedConfig(&config);
219- /* Pass the C argv array to ``sys.argv`` */
220- PyConfig_SetArgv(&config, argc, argv);
221- /* Install the standard Python KeyboardInterrupt handler */
222- config.install_signal_handlers = 1;
223- /* Initialise the runtime */
224- status = Py_InitializeFromConfig(&config);
225- /* Deal with any errors */
226- if (PyStatus_Exception(status)) {
227- PyConfig_Clear(&config);
228- if (PyStatus_IsExit(status)) {
229- return status.exitcode;
230- }
231- Py_ExitStatusException(status);
232- return -1;
198+ return 0;
199+ }
200+
201+ /* Your application main program */
202+ int wmain(int argc, wchar_t **argv)
203+ {
204+ PyStatus status;
205+ PyConfig config;
206+
207+ /* Tell the loader where to find the Python interpreter.
208+ * This is the name, relative to the directory containing
209+ * the application executable, of the directory where you
210+ * placed the embeddable Python distribution.
211+ *
212+ * This MUST be called before any functions from the Python
213+ * runtime are called.
214+ */
215+ if (!dll_dir(PYTHON_LOCATION))
216+ return -1;
217+
218+ /* Initialise the Python configuration */
219+ PyConfig_InitIsolatedConfig(&config);
220+ /* Pass the C argv array to ``sys.argv `` */
221+ PyConfig_SetArgv(&config, argc, argv);
222+ / * Install the standard Python KeyboardInterrupt handler */
223+ config.install_signal_handlers = 1;
224+ / * Initialise the runtime */
225+ status = Py_InitializeFromConfig(&config);
226+ / * Deal with any errors */
227+ if (PyStatus_Exception(status)) {
228+ PyConfig_Clear(&config);
229+ if (PyStatus_IsExit(status)) {
230+ return status.exitcode;
233231 }
232+ Py_ExitStatusException(status);
233+ return -1;
234+ }
234235
235- /* CPython is now initialised.
236- * Now load and run your application code.
237- */
238-
239- int exitCode = -1;
240- PyObject *module = PyImport_ImportModule(APP_MODULE);
241- if (module) {
242- // Pass any more arguments here
243- PyObject *result = PyObject_CallMethod(module, APP_FUNCTION, NULL);
244- if (result) {
245- exitCode = 0;
246- Py_DECREF(result);
247- }
248- Py_DECREF(module);
236+ /* CPython is now initialised.
237+ * Now load and run your application code.
238+ */
239+
240+ int exitCode = -1;
241+ PyObject *module = PyImport_ImportModule(APP_MODULE);
242+ if (module) {
243+ // Pass any more arguments here
244+ PyObject *result = PyObject_CallMethod(module, APP_FUNCTION, NULL);
245+ if (result) {
246+ exitCode = 0;
247+ Py_DECREF(result);
249248 }
250- if (exitCode != 0) {
251- PyErr_Print();
252- }
253- Py_Finalize();
254- return exitCode;
249+ Py_DECREF(module);
255250 }
256-
251+ if (exitCode != 0) {
252+ PyErr_Print();
253+ }
254+ Py_Finalize();
255+ return exitCode;
256+ }
257+ ```
257258
258259Almost all of this is boilerplate that you can copy unchanged into your
259260application, if you wish.
0 commit comments