summaryrefslogtreecommitdiffstats
path: root/PC/python_uwp.cpp
blob: 5c8caa6666c4e093bb4c57b85eaa3f16fdde427c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* Main program when embedded in a UWP application on Windows */

#include "Python.h"
#include <string.h>

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <shellapi.h>

#include <winrt\Windows.ApplicationModel.h>
#include <winrt\Windows.Storage.h>

#ifdef PYTHONW
#ifdef _DEBUG
const wchar_t *PROGNAME = L"pythonw_d.exe";
#else
const wchar_t *PROGNAME = L"pythonw.exe";
#endif
#else
#ifdef _DEBUG
const wchar_t *PROGNAME = L"python_d.exe";
#else
const wchar_t *PROGNAME = L"python.exe";
#endif
#endif

static void
set_user_base()
{
    wchar_t envBuffer[2048];
    try {
        const auto appData = winrt::Windows::Storage::ApplicationData::Current();
        if (appData) {
            const auto localCache = appData.LocalCacheFolder();
            if (localCache) {
                auto path = localCache.Path();
                if (!path.empty() &&
                    !wcscpy_s(envBuffer, path.c_str()) &&
                    !wcscat_s(envBuffer, L"\\local-packages")
                ) {
                    _wputenv_s(L"PYTHONUSERBASE", envBuffer);
                }
            }
        }
    } catch (...) {
    }
}

static const wchar_t *
get_argv0(const wchar_t *argv0)
{
    winrt::hstring installPath;
    const wchar_t *launcherPath;
    wchar_t *buffer;
    size_t len;

    launcherPath = _wgetenv(L"__PYVENV_LAUNCHER__");
    if (launcherPath && launcherPath[0]) {
        len = wcslen(launcherPath) + 1;
        buffer = (wchar_t *)malloc(sizeof(wchar_t) * len);
        if (!buffer) {
            Py_FatalError("out of memory");
            return NULL;
        }
        if (wcscpy_s(buffer, len, launcherPath)) {
            Py_FatalError("failed to copy to buffer");
            return NULL;
        }
        return buffer;
    }

    try {
        const auto package = winrt::Windows::ApplicationModel::Package::Current();
        if (package) {
            const auto install = package.InstalledLocation();
            if (install) {
                installPath = install.Path();
            }
        }
    }
    catch (...) {
    }

    if (!installPath.empty()) {
        len = installPath.size() + wcslen(PROGNAME) + 2;
    } else {
        len = wcslen(argv0) + wcslen(PROGNAME) + 1;
    }

    buffer = (wchar_t *)malloc(sizeof(wchar_t) * len);
    if (!buffer) {
        Py_FatalError("out of memory");
        return NULL;
    }

    if (!installPath.empty()) {
        if (wcscpy_s(buffer, len, installPath.c_str())) {
            Py_FatalError("failed to copy to buffer");
            return NULL;
        }
        if (wcscat_s(buffer, len, L"\\")) {
            Py_FatalError("failed to concatenate backslash");
            return NULL;
        }
    } else {
        if (wcscpy_s(buffer, len, argv0)) {
            Py_FatalError("failed to copy argv[0]");
            return NULL;
        }

        wchar_t *name = wcsrchr(buffer, L'\\');
        if (name) {
            name[1] = L'\0';
        } else {
            buffer[0] = L'\0';
        }
    }

    if (wcscat_s(buffer, len, PROGNAME)) {
        Py_FatalError("failed to concatenate program name");
        return NULL;
    }

    return buffer;
}

static wchar_t *
get_process_name()
{
    DWORD bufferLen = MAX_PATH;
    DWORD len = bufferLen;
    wchar_t *r = NULL;

    while (!r) {
        r = (wchar_t *)malloc(bufferLen * sizeof(wchar_t));
        if (!r) {
            Py_FatalError("out of memory");
            return NULL;
        }
        len = GetModuleFileNameW(NULL, r, bufferLen);
        if (len == 0) {
            free((void *)r);
            return NULL;
        } else if (len == bufferLen &&
                   GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
            free(r);
            r = NULL;
            bufferLen *= 2;
        }
    }

    return r;
}

int
wmain(int argc, wchar_t **argv)
{
    const wchar_t **new_argv;
    int new_argc;
    const wchar_t *exeName;

    new_argc = argc;
    new_argv = (const wchar_t**)malloc(sizeof(wchar_t *) * (argc + 2));
    if (new_argv == NULL) {
        Py_FatalError("out of memory");
        return -1;
    }

    exeName = get_process_name();

    new_argv[0] = get_argv0(exeName ? exeName : argv[0]);
    for (int i = 1; i < argc; ++i) {
        new_argv[i] = argv[i];
    }

    set_user_base();

    if (exeName) {
        const wchar_t *p = wcsrchr(exeName, L'\\');
        if (p) {
            const wchar_t *moduleName = NULL;
            if (*p++ == L'\\') {
                if (wcsnicmp(p, L"pip", 3) == 0) {
                    moduleName = L"pip";
                    _wputenv_s(L"PIP_USER", L"true");
                }
                else if (wcsnicmp(p, L"idle", 4) == 0) {
                    moduleName = L"idlelib";
                }
            }

            if (moduleName) {
                new_argc += 2;
                for (int i = argc; i >= 1; --i) {
                    new_argv[i + 2] = new_argv[i];
                }
                new_argv[1] = L"-m";
                new_argv[2] = moduleName;
            }
        }
    }

    /* Override program_full_path from here so that
       sys.executable is set correctly. */
    _Py_SetProgramFullPath(new_argv[0]);

    int result = Py_Main(new_argc, (wchar_t **)new_argv);

    free((void *)exeName);
    free((void *)new_argv);

    return result;
}

#ifdef PYTHONW

int WINAPI wWinMain(
    HINSTANCE hInstance,      /* handle to current instance */
    HINSTANCE hPrevInstance,  /* handle to previous instance */
    LPWSTR lpCmdLine,         /* pointer to command line */
    int nCmdShow              /* show state of window */
)
{
    return wmain(__argc, __wargv);
}

#endif