ยปCore Development>Code coverage>PC/os2emx/pythonpm.c

Python code coverage for PC/os2emx/pythonpm.c

#countcontent
1n/a/* OS/2 PM main program - creates a hidden window, and starts Python
2n/a * interpreter in a separate thread, so that Python scripts can be
3n/a * run in PM process space without a console Window. The interpreter
4n/a * is incorporated by linking in the Python DLL.
5n/a *
6n/a * As it stands, I don't think this is adequate for supporting Python
7n/a * GUI modules, as the Python thread doesn't have its own message
8n/a * queue - which is required of threads that want to create/use
9n/a * PM windows.
10n/a *
11n/a * This code owes a lot to "OS/2 Presentation Manager Programming", by
12n/a * Charles Petzold.
13n/a *
14n/a * Andrew MacIntyre <andymac@bullseye.apana.org.au>, August 2001.
15n/a * Released under the terms of the Python 2.1.1 licence - see the LICENCE
16n/a * file in the Python v2.1.1 (or later) source distribution.
17n/a * Copyright assigned to the Python Software Foundation, 2001.
18n/a */
19n/a
20n/a#define INCL_DOS
21n/a#define INCL_WIN
22n/a#include <os2.h>
23n/a#include <process.h>
24n/a
25n/a#include "Python.h"
26n/a
27n/a/* use structure to pass command line to Python thread */
28n/atypedef struct
29n/a{
30n/a int argc;
31n/a char **argv;
32n/a HWND Frame;
33n/a int running;
34n/a} arglist;
35n/a
36n/a/* make this a global to simplify access.
37n/a * it should only be set from the Python thread, or by the code that
38n/a * initiates the Python thread when the thread cannot be created.
39n/a */
40n/aint PythonRC;
41n/a
42n/aextern DL_EXPORT(int) Py_Main(int, char **);
43n/avoid PythonThread(void *);
44n/a
45n/aint
46n/amain(int argc, char **argv)
47n/a{
48n/a ULONG FrameFlags = FCF_TITLEBAR |
49n/a FCF_SYSMENU |
50n/a FCF_SIZEBORDER |
51n/a FCF_HIDEBUTTON |
52n/a FCF_SHELLPOSITION |
53n/a FCF_TASKLIST;
54n/a HAB hab;
55n/a HMQ hmq;
56n/a HWND Client;
57n/a QMSG qmsg;
58n/a arglist args;
59n/a int python_tid;
60n/a
61n/a /* init PM and create message queue */
62n/a hab = WinInitialize(0);
63n/a hmq = WinCreateMsgQueue(hab, 0);
64n/a
65n/a /* create a (hidden) Window to house the window procedure */
66n/a args.Frame = WinCreateStdWindow(HWND_DESKTOP,
67n/a 0,
68n/a &FrameFlags,
69n/a NULL,
70n/a "PythonPM",
71n/a 0L,
72n/a 0,
73n/a 0,
74n/a &Client);
75n/a
76n/a /* run Python interpreter in a thread */
77n/a args.argc = argc;
78n/a args.argv = argv;
79n/a args.running = 0;
80n/a if (-1 == (python_tid = _beginthread(PythonThread, NULL, 1024 * 1024, &args)))
81n/a {
82n/a /* couldn't start thread */
83n/a WinAlarm(HWND_DESKTOP, WA_ERROR);
84n/a PythonRC = 1;
85n/a }
86n/a else
87n/a {
88n/a /* process PM messages, until Python exits */
89n/a while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
90n/a WinDispatchMsg(hab, &qmsg);
91n/a if (args.running > 0)
92n/a DosKillThread(python_tid);
93n/a }
94n/a
95n/a /* destroy window, shutdown message queue and PM */
96n/a WinDestroyWindow(args.Frame);
97n/a WinDestroyMsgQueue(hmq);
98n/a WinTerminate(hab);
99n/a
100n/a return PythonRC;
101n/a}
102n/a
103n/avoid PythonThread(void *argl)
104n/a{
105n/a HAB hab;
106n/a arglist *args;
107n/a
108n/a /* PM initialisation */
109n/a hab = WinInitialize(0);
110n/a
111n/a /* start Python */
112n/a args = (arglist *)argl;
113n/a args->running = 1;
114n/a PythonRC = Py_Main(args->argc, args->argv);
115n/a
116n/a /* enter a critical section and send the termination message */
117n/a DosEnterCritSec();
118n/a args->running = 0;
119n/a WinPostMsg(args->Frame, WM_QUIT, NULL, NULL);
120n/a
121n/a /* shutdown PM and terminate thread */
122n/a WinTerminate(hab);
123n/a _endthread();
124n/a}