Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/PC/launcher.c
12 views
1
/*
2
* Copyright (C) 2011-2013 Vinay Sajip.
3
* Licensed to PSF under a contributor agreement.
4
*
5
* Based on the work of:
6
*
7
* Mark Hammond (original author of Python version)
8
* Curt Hagenlocher (job management)
9
*/
10
11
#include <windows.h>
12
#include <shlobj.h>
13
#include <stdio.h>
14
#include <tchar.h>
15
16
#define BUFSIZE 256
17
#define MSGSIZE 1024
18
19
/* Build options. */
20
#define SKIP_PREFIX
21
#define SEARCH_PATH
22
23
/* Error codes */
24
25
#define RC_NO_STD_HANDLES 100
26
#define RC_CREATE_PROCESS 101
27
#define RC_BAD_VIRTUAL_PATH 102
28
#define RC_NO_PYTHON 103
29
#define RC_NO_MEMORY 104
30
/*
31
* SCRIPT_WRAPPER is used to choose one of the variants of an executable built
32
* from this source file. If not defined, the PEP 397 Python launcher is built;
33
* if defined, a script launcher of the type used by setuptools is built, which
34
* looks for a script name related to the executable name and runs that script
35
* with the appropriate Python interpreter.
36
*
37
* SCRIPT_WRAPPER should be undefined in the source, and defined in a VS project
38
* which builds the setuptools-style launcher.
39
*/
40
#if defined(SCRIPT_WRAPPER)
41
#define RC_NO_SCRIPT 105
42
#endif
43
/*
44
* VENV_REDIRECT is used to choose the variant that looks for an adjacent or
45
* one-level-higher pyvenv.cfg, and uses its "home" property to locate and
46
* launch the original python.exe.
47
*/
48
#if defined(VENV_REDIRECT)
49
#define RC_NO_VENV_CFG 106
50
#define RC_BAD_VENV_CFG 107
51
#endif
52
53
/* Just for now - static definition */
54
55
static FILE * log_fp = NULL;
56
57
static wchar_t *
58
skip_whitespace(wchar_t * p)
59
{
60
while (*p && isspace(*p))
61
++p;
62
return p;
63
}
64
65
static void
66
debug(wchar_t * format, ...)
67
{
68
va_list va;
69
70
if (log_fp != NULL) {
71
va_start(va, format);
72
vfwprintf_s(log_fp, format, va);
73
va_end(va);
74
}
75
}
76
77
static void
78
winerror(int rc, wchar_t * message, int size)
79
{
80
FormatMessageW(
81
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
82
NULL, rc, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
83
message, size, NULL);
84
}
85
86
static void
87
error(int rc, wchar_t * format, ... )
88
{
89
va_list va;
90
wchar_t message[MSGSIZE];
91
wchar_t win_message[MSGSIZE];
92
int len;
93
94
va_start(va, format);
95
len = _vsnwprintf_s(message, MSGSIZE, _TRUNCATE, format, va);
96
va_end(va);
97
98
if (rc == 0) { /* a Windows error */
99
winerror(GetLastError(), win_message, MSGSIZE);
100
if (len >= 0) {
101
_snwprintf_s(&message[len], MSGSIZE - len, _TRUNCATE, L": %ls",
102
win_message);
103
}
104
}
105
106
#if !defined(_WINDOWS)
107
fwprintf(stderr, L"%ls\n", message);
108
#else
109
MessageBoxW(NULL, message, L"Python Launcher is sorry to say ...",
110
MB_OK);
111
#endif
112
exit(rc);
113
}
114
115
/*
116
* This function is here to simplify memory management
117
* and to treat blank values as if they are absent.
118
*/
119
static wchar_t * get_env(wchar_t * key)
120
{
121
/* This is not thread-safe, just like getenv */
122
static wchar_t buf[BUFSIZE];
123
DWORD result = GetEnvironmentVariableW(key, buf, BUFSIZE);
124
125
if (result >= BUFSIZE) {
126
/* Large environment variable. Accept some leakage */
127
wchar_t *buf2 = (wchar_t*)malloc(sizeof(wchar_t) * (result+1));
128
if (buf2 == NULL) {
129
error(RC_NO_MEMORY, L"Could not allocate environment buffer");
130
}
131
GetEnvironmentVariableW(key, buf2, result);
132
return buf2;
133
}
134
135
if (result == 0)
136
/* Either some error, e.g. ERROR_ENVVAR_NOT_FOUND,
137
or an empty environment variable. */
138
return NULL;
139
140
return buf;
141
}
142
143
#if defined(_DEBUG)
144
/* Do not define EXECUTABLEPATH_VALUE in debug builds as it'll
145
never point to the debug build. */
146
#if defined(_WINDOWS)
147
148
#define PYTHON_EXECUTABLE L"pythonw_d.exe"
149
150
#else
151
152
#define PYTHON_EXECUTABLE L"python_d.exe"
153
154
#endif
155
#else
156
#if defined(_WINDOWS)
157
158
#define PYTHON_EXECUTABLE L"pythonw.exe"
159
#define EXECUTABLEPATH_VALUE L"WindowedExecutablePath"
160
161
#else
162
163
#define PYTHON_EXECUTABLE L"python.exe"
164
#define EXECUTABLEPATH_VALUE L"ExecutablePath"
165
166
#endif
167
#endif
168
169
#define MAX_VERSION_SIZE 8
170
171
typedef struct {
172
wchar_t version[MAX_VERSION_SIZE]; /* m.n */
173
int bits; /* 32 or 64 */
174
wchar_t executable[MAX_PATH];
175
wchar_t exe_display[MAX_PATH];
176
} INSTALLED_PYTHON;
177
178
/*
179
* To avoid messing about with heap allocations, just assume we can allocate
180
* statically and never have to deal with more versions than this.
181
*/
182
#define MAX_INSTALLED_PYTHONS 100
183
184
static INSTALLED_PYTHON installed_pythons[MAX_INSTALLED_PYTHONS];
185
186
static size_t num_installed_pythons = 0;
187
188
/*
189
* To hold SOFTWARE\Python\PythonCore\X.Y...\InstallPath
190
* The version name can be longer than MAX_VERSION_SIZE, but will be
191
* truncated to just X.Y for comparisons.
192
*/
193
#define IP_BASE_SIZE 80
194
#define IP_VERSION_SIZE 8
195
#define IP_SIZE (IP_BASE_SIZE + IP_VERSION_SIZE)
196
#define CORE_PATH L"SOFTWARE\\Python\\PythonCore"
197
/*
198
* Installations from the Microsoft Store will set the same registry keys,
199
* but because of a limitation in Windows they cannot be enumerated normally
200
* (unless you have no other Python installations... which is probably false
201
* because that's the most likely way to get this launcher!)
202
* This key is under HKEY_LOCAL_MACHINE
203
*/
204
#define LOOKASIDE_PATH L"SOFTWARE\\Microsoft\\AppModel\\Lookaside\\user\\Software\\Python\\PythonCore"
205
206
static wchar_t * location_checks[] = {
207
L"\\",
208
L"\\PCbuild\\win32\\",
209
L"\\PCbuild\\amd64\\",
210
/* To support early 32bit versions of Python that stuck the build binaries
211
* directly in PCbuild... */
212
L"\\PCbuild\\",
213
NULL
214
};
215
216
static INSTALLED_PYTHON *
217
find_existing_python(const wchar_t * path)
218
{
219
INSTALLED_PYTHON * result = NULL;
220
size_t i;
221
INSTALLED_PYTHON * ip;
222
223
for (i = 0, ip = installed_pythons; i < num_installed_pythons; i++, ip++) {
224
if (_wcsicmp(path, ip->executable) == 0) {
225
result = ip;
226
break;
227
}
228
}
229
return result;
230
}
231
232
static INSTALLED_PYTHON *
233
find_existing_python2(int bits, const wchar_t * version)
234
{
235
INSTALLED_PYTHON * result = NULL;
236
size_t i;
237
INSTALLED_PYTHON * ip;
238
239
for (i = 0, ip = installed_pythons; i < num_installed_pythons; i++, ip++) {
240
if (bits == ip->bits && _wcsicmp(version, ip->version) == 0) {
241
result = ip;
242
break;
243
}
244
}
245
return result;
246
}
247
248
static void
249
_locate_pythons_for_key(HKEY root, LPCWSTR subkey, REGSAM flags, int bits,
250
int display_name_only)
251
{
252
HKEY core_root, ip_key;
253
LSTATUS status = RegOpenKeyExW(root, subkey, 0, flags, &core_root);
254
wchar_t message[MSGSIZE];
255
DWORD i;
256
size_t n;
257
BOOL ok, append_name;
258
DWORD type, data_size, attrs;
259
INSTALLED_PYTHON * ip, * pip;
260
wchar_t ip_version[IP_VERSION_SIZE];
261
wchar_t ip_path[IP_SIZE];
262
wchar_t * check;
263
wchar_t ** checkp;
264
wchar_t *key_name = (root == HKEY_LOCAL_MACHINE) ? L"HKLM" : L"HKCU";
265
266
if (status != ERROR_SUCCESS)
267
debug(L"locate_pythons_for_key: unable to open PythonCore key in %ls\n",
268
key_name);
269
else {
270
ip = &installed_pythons[num_installed_pythons];
271
for (i = 0; num_installed_pythons < MAX_INSTALLED_PYTHONS; i++) {
272
status = RegEnumKeyW(core_root, i, ip_version, IP_VERSION_SIZE);
273
if (status != ERROR_SUCCESS) {
274
if (status != ERROR_NO_MORE_ITEMS) {
275
/* unexpected error */
276
winerror(status, message, MSGSIZE);
277
debug(L"Can't enumerate registry key for version %ls: %ls\n",
278
ip_version, message);
279
}
280
break;
281
}
282
else {
283
wcsncpy_s(ip->version, MAX_VERSION_SIZE, ip_version,
284
MAX_VERSION_SIZE-1);
285
/* Still treating version as "x.y" rather than sys.winver
286
* When PEP 514 tags are properly used, we shouldn't need
287
* to strip this off here.
288
*/
289
check = wcsrchr(ip->version, L'-');
290
if (check && !wcscmp(check, L"-32")) {
291
*check = L'\0';
292
}
293
_snwprintf_s(ip_path, IP_SIZE, _TRUNCATE,
294
L"%ls\\%ls\\InstallPath", subkey, ip_version);
295
status = RegOpenKeyExW(root, ip_path, 0, flags, &ip_key);
296
if (status != ERROR_SUCCESS) {
297
winerror(status, message, MSGSIZE);
298
/* Note: 'message' already has a trailing \n*/
299
debug(L"%ls\\%ls: %ls", key_name, ip_path, message);
300
continue;
301
}
302
data_size = sizeof(ip->executable) - 1;
303
append_name = FALSE;
304
#ifdef EXECUTABLEPATH_VALUE
305
status = RegQueryValueExW(ip_key, EXECUTABLEPATH_VALUE, NULL, &type,
306
(LPBYTE)ip->executable, &data_size);
307
#else
308
status = ERROR_FILE_NOT_FOUND; /* actual error doesn't matter */
309
#endif
310
if (status != ERROR_SUCCESS || type != REG_SZ || !data_size) {
311
append_name = TRUE;
312
data_size = sizeof(ip->executable) - 1;
313
status = RegQueryValueExW(ip_key, NULL, NULL, &type,
314
(LPBYTE)ip->executable, &data_size);
315
if (status != ERROR_SUCCESS) {
316
winerror(status, message, MSGSIZE);
317
debug(L"%ls\\%ls: %ls\n", key_name, ip_path, message);
318
RegCloseKey(ip_key);
319
continue;
320
}
321
}
322
RegCloseKey(ip_key);
323
if (type != REG_SZ) {
324
continue;
325
}
326
327
data_size = data_size / sizeof(wchar_t) - 1; /* for NUL */
328
if (ip->executable[data_size - 1] == L'\\')
329
--data_size; /* reg value ended in a backslash */
330
/* ip->executable is data_size long */
331
for (checkp = location_checks; *checkp; ++checkp) {
332
check = *checkp;
333
if (append_name) {
334
_snwprintf_s(&ip->executable[data_size],
335
MAX_PATH - data_size,
336
MAX_PATH - data_size,
337
L"%ls%ls", check, PYTHON_EXECUTABLE);
338
}
339
attrs = GetFileAttributesW(ip->executable);
340
if (attrs == INVALID_FILE_ATTRIBUTES) {
341
winerror(GetLastError(), message, MSGSIZE);
342
debug(L"locate_pythons_for_key: %ls: %ls",
343
ip->executable, message);
344
}
345
else if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
346
debug(L"locate_pythons_for_key: '%ls' is a directory\n",
347
ip->executable);
348
}
349
else if (find_existing_python(ip->executable)) {
350
debug(L"locate_pythons_for_key: %ls: already found\n",
351
ip->executable);
352
}
353
else {
354
/* check the executable type. */
355
if (bits) {
356
ip->bits = bits;
357
} else {
358
ok = GetBinaryTypeW(ip->executable, &attrs);
359
if (!ok) {
360
debug(L"Failure getting binary type: %ls\n",
361
ip->executable);
362
}
363
else {
364
if (attrs == SCS_64BIT_BINARY)
365
ip->bits = 64;
366
else if (attrs == SCS_32BIT_BINARY)
367
ip->bits = 32;
368
else
369
ip->bits = 0;
370
}
371
}
372
if (ip->bits == 0) {
373
debug(L"locate_pythons_for_key: %ls: \
374
invalid binary type: %X\n",
375
ip->executable, attrs);
376
}
377
else {
378
if (display_name_only) {
379
/* display just the executable name. This is
380
* primarily for the Store installs */
381
const wchar_t *name = wcsrchr(ip->executable, L'\\');
382
if (name) {
383
wcscpy_s(ip->exe_display, MAX_PATH, name+1);
384
}
385
}
386
if (wcschr(ip->executable, L' ') != NULL) {
387
/* has spaces, so quote, and set original as
388
* the display name */
389
if (!ip->exe_display[0]) {
390
wcscpy_s(ip->exe_display, MAX_PATH, ip->executable);
391
}
392
n = wcslen(ip->executable);
393
memmove(&ip->executable[1],
394
ip->executable, n * sizeof(wchar_t));
395
ip->executable[0] = L'\"';
396
ip->executable[n + 1] = L'\"';
397
ip->executable[n + 2] = L'\0';
398
}
399
debug(L"locate_pythons_for_key: %ls \
400
is a %dbit executable\n",
401
ip->executable, ip->bits);
402
if (find_existing_python2(ip->bits, ip->version)) {
403
debug(L"locate_pythons_for_key: %ls-%i: already \
404
found\n", ip->version, ip->bits);
405
}
406
else {
407
++num_installed_pythons;
408
pip = ip++;
409
if (num_installed_pythons >=
410
MAX_INSTALLED_PYTHONS)
411
break;
412
}
413
}
414
}
415
}
416
}
417
}
418
RegCloseKey(core_root);
419
}
420
}
421
422
static int
423
compare_pythons(const void * p1, const void * p2)
424
{
425
INSTALLED_PYTHON * ip1 = (INSTALLED_PYTHON *) p1;
426
INSTALLED_PYTHON * ip2 = (INSTALLED_PYTHON *) p2;
427
/* note reverse sorting on version */
428
int result = CompareStringW(LOCALE_INVARIANT, SORT_DIGITSASNUMBERS,
429
ip2->version, -1, ip1->version, -1);
430
switch (result) {
431
case 0:
432
error(0, L"CompareStringW failed");
433
return 0;
434
case CSTR_LESS_THAN:
435
return -1;
436
case CSTR_EQUAL:
437
return ip2->bits - ip1->bits; /* 64 before 32 */
438
case CSTR_GREATER_THAN:
439
return 1;
440
default:
441
return 0; // This should never be reached.
442
}
443
}
444
445
static void
446
locate_pythons_for_key(HKEY root, REGSAM flags)
447
{
448
_locate_pythons_for_key(root, CORE_PATH, flags, 0, FALSE);
449
}
450
451
static void
452
locate_store_pythons(void)
453
{
454
#if defined(_M_X64)
455
/* 64bit process, so look in native registry */
456
_locate_pythons_for_key(HKEY_LOCAL_MACHINE, LOOKASIDE_PATH,
457
KEY_READ, 64, TRUE);
458
#else
459
/* 32bit process, so check that we're on 64bit OS */
460
BOOL f64 = FALSE;
461
if (IsWow64Process(GetCurrentProcess(), &f64) && f64) {
462
_locate_pythons_for_key(HKEY_LOCAL_MACHINE, LOOKASIDE_PATH,
463
KEY_READ | KEY_WOW64_64KEY, 64, TRUE);
464
}
465
#endif
466
}
467
468
static void
469
locate_venv_python(void)
470
{
471
static wchar_t venv_python[MAX_PATH];
472
INSTALLED_PYTHON * ip;
473
wchar_t *virtual_env = get_env(L"VIRTUAL_ENV");
474
DWORD attrs;
475
476
/* Check for VIRTUAL_ENV environment variable */
477
if (virtual_env == NULL || virtual_env[0] == L'\0') {
478
return;
479
}
480
481
/* Check for a python executable in the venv */
482
debug(L"Checking for Python executable in virtual env '%ls'\n", virtual_env);
483
_snwprintf_s(venv_python, MAX_PATH, _TRUNCATE,
484
L"%ls\\Scripts\\%ls", virtual_env, PYTHON_EXECUTABLE);
485
attrs = GetFileAttributesW(venv_python);
486
if (attrs == INVALID_FILE_ATTRIBUTES) {
487
debug(L"Python executable %ls missing from virtual env\n", venv_python);
488
return;
489
}
490
491
ip = &installed_pythons[num_installed_pythons++];
492
wcscpy_s(ip->executable, MAX_PATH, venv_python);
493
ip->bits = 0;
494
wcscpy_s(ip->version, MAX_VERSION_SIZE, L"venv");
495
}
496
497
static void
498
locate_all_pythons(void)
499
{
500
/* venv Python is highest priority */
501
locate_venv_python();
502
#if defined(_M_X64)
503
/* If we are a 64bit process, first hit the 32bit keys. */
504
debug(L"locating Pythons in 32bit registry\n");
505
locate_pythons_for_key(HKEY_CURRENT_USER, KEY_READ | KEY_WOW64_32KEY);
506
locate_pythons_for_key(HKEY_LOCAL_MACHINE, KEY_READ | KEY_WOW64_32KEY);
507
#else
508
/* If we are a 32bit process on a 64bit Windows, first hit the 64bit keys.*/
509
BOOL f64 = FALSE;
510
if (IsWow64Process(GetCurrentProcess(), &f64) && f64) {
511
debug(L"locating Pythons in 64bit registry\n");
512
locate_pythons_for_key(HKEY_CURRENT_USER, KEY_READ | KEY_WOW64_64KEY);
513
locate_pythons_for_key(HKEY_LOCAL_MACHINE, KEY_READ | KEY_WOW64_64KEY);
514
}
515
#endif
516
/* now hit the "native" key for this process bittedness. */
517
debug(L"locating Pythons in native registry\n");
518
locate_pythons_for_key(HKEY_CURRENT_USER, KEY_READ);
519
locate_pythons_for_key(HKEY_LOCAL_MACHINE, KEY_READ);
520
/* Store-installed Python is lowest priority */
521
locate_store_pythons();
522
qsort(installed_pythons, num_installed_pythons, sizeof(INSTALLED_PYTHON),
523
compare_pythons);
524
}
525
526
static INSTALLED_PYTHON *
527
find_python_by_version(wchar_t const * wanted_ver)
528
{
529
INSTALLED_PYTHON * result = NULL;
530
INSTALLED_PYTHON * ip = installed_pythons;
531
size_t i, n;
532
size_t wlen = wcslen(wanted_ver);
533
int bits = 0;
534
535
if (wcsstr(wanted_ver, L"-32")) {
536
bits = 32;
537
wlen -= wcslen(L"-32");
538
}
539
else if (wcsstr(wanted_ver, L"-64")) { /* Added option to select 64 bit explicitly */
540
bits = 64;
541
wlen -= wcslen(L"-64");
542
}
543
for (i = 0; i < num_installed_pythons; i++, ip++) {
544
n = wcslen(ip->version);
545
/*
546
* If wlen is greater than 1, we're probably trying to find a specific
547
* version and thus want an exact match: 3.1 != 3.10. Otherwise, we
548
* just want a prefix match.
549
*/
550
if ((wlen > 1) && (n != wlen)) {
551
continue;
552
}
553
if (n > wlen) {
554
n = wlen;
555
}
556
if ((wcsncmp(ip->version, wanted_ver, n) == 0) &&
557
/* bits == 0 => don't care */
558
((bits == 0) || (ip->bits == bits))) {
559
result = ip;
560
break;
561
}
562
}
563
return result;
564
}
565
566
567
static wchar_t appdata_ini_path[MAX_PATH];
568
static wchar_t launcher_ini_path[MAX_PATH];
569
570
/*
571
* Get a value either from the environment or a configuration file.
572
* The key passed in will either be "python", "python2" or "python3".
573
*/
574
static wchar_t *
575
get_configured_value(wchar_t * key)
576
{
577
/*
578
* Note: this static value is used to return a configured value
579
* obtained either from the environment or configuration file.
580
* This should be OK since there wouldn't be any concurrent calls.
581
*/
582
static wchar_t configured_value[MSGSIZE];
583
wchar_t * result = NULL;
584
wchar_t * found_in = L"environment";
585
DWORD size;
586
587
/* First, search the environment. */
588
_snwprintf_s(configured_value, MSGSIZE, _TRUNCATE, L"py_%ls", key);
589
result = get_env(configured_value);
590
if (result == NULL && appdata_ini_path[0]) {
591
/* Not in environment: check local configuration. */
592
size = GetPrivateProfileStringW(L"defaults", key, NULL,
593
configured_value, MSGSIZE,
594
appdata_ini_path);
595
if (size > 0) {
596
result = configured_value;
597
found_in = appdata_ini_path;
598
}
599
}
600
if (result == NULL && launcher_ini_path[0]) {
601
/* Not in environment or local: check global configuration. */
602
size = GetPrivateProfileStringW(L"defaults", key, NULL,
603
configured_value, MSGSIZE,
604
launcher_ini_path);
605
if (size > 0) {
606
result = configured_value;
607
found_in = launcher_ini_path;
608
}
609
}
610
if (result) {
611
debug(L"found configured value '%ls=%ls' in %ls\n",
612
key, result, found_in ? found_in : L"(unknown)");
613
} else {
614
debug(L"found no configured value for '%ls'\n", key);
615
}
616
return result;
617
}
618
619
static INSTALLED_PYTHON *
620
locate_python(wchar_t * wanted_ver, BOOL from_shebang)
621
{
622
static wchar_t config_key [] = { L"pythonX" };
623
static wchar_t * last_char = &config_key[sizeof(config_key) /
624
sizeof(wchar_t) - 2];
625
INSTALLED_PYTHON * result = NULL;
626
size_t n = wcslen(wanted_ver);
627
wchar_t * configured_value;
628
629
if (num_installed_pythons == 0)
630
locate_all_pythons();
631
632
if (n == 1) { /* just major version specified */
633
*last_char = *wanted_ver;
634
configured_value = get_configured_value(config_key);
635
if (configured_value != NULL)
636
wanted_ver = configured_value;
637
}
638
if (*wanted_ver) {
639
result = find_python_by_version(wanted_ver);
640
debug(L"search for Python version '%ls' found ", wanted_ver);
641
if (result) {
642
debug(L"'%ls'\n", result->executable);
643
} else {
644
debug(L"no interpreter\n");
645
}
646
}
647
else {
648
*last_char = L'\0'; /* look for an overall default */
649
result = find_python_by_version(L"venv");
650
if (result == NULL) {
651
configured_value = get_configured_value(config_key);
652
if (configured_value)
653
result = find_python_by_version(configured_value);
654
}
655
/* Not found a value yet - try by major version.
656
* If we're looking for an interpreter specified in a shebang line,
657
* we want to try Python 2 first, then Python 3 (for Unix and backward
658
* compatibility). If we're being called interactively, assume the user
659
* wants the latest version available, so try Python 3 first, then
660
* Python 2.
661
*/
662
if (result == NULL)
663
result = find_python_by_version(from_shebang ? L"2" : L"3");
664
if (result == NULL)
665
result = find_python_by_version(from_shebang ? L"3" : L"2");
666
debug(L"search for default Python found ");
667
if (result) {
668
debug(L"version %ls at '%ls'\n",
669
result->version, result->executable);
670
} else {
671
debug(L"no interpreter\n");
672
}
673
}
674
return result;
675
}
676
677
#if defined(SCRIPT_WRAPPER)
678
/*
679
* Check for a script located alongside the executable
680
*/
681
682
#if defined(_WINDOWS)
683
#define SCRIPT_SUFFIX L"-script.pyw"
684
#else
685
#define SCRIPT_SUFFIX L"-script.py"
686
#endif
687
688
static wchar_t wrapped_script_path[MAX_PATH];
689
690
/* Locate the script being wrapped.
691
*
692
* This code should store the name of the wrapped script in
693
* wrapped_script_path, or terminate the program with an error if there is no
694
* valid wrapped script file.
695
*/
696
static void
697
locate_wrapped_script(void)
698
{
699
wchar_t * p;
700
size_t plen;
701
DWORD attrs;
702
703
plen = GetModuleFileNameW(NULL, wrapped_script_path, MAX_PATH);
704
p = wcsrchr(wrapped_script_path, L'.');
705
if (p == NULL) {
706
debug(L"GetModuleFileNameW returned value has no extension: %ls\n",
707
wrapped_script_path);
708
error(RC_NO_SCRIPT, L"Wrapper name '%ls' is not valid.", wrapped_script_path);
709
}
710
711
wcsncpy_s(p, MAX_PATH - (p - wrapped_script_path) + 1, SCRIPT_SUFFIX, _TRUNCATE);
712
attrs = GetFileAttributesW(wrapped_script_path);
713
if (attrs == INVALID_FILE_ATTRIBUTES) {
714
debug(L"File '%ls' non-existent\n", wrapped_script_path);
715
error(RC_NO_SCRIPT, L"Script file '%ls' is not present.", wrapped_script_path);
716
}
717
718
debug(L"Using wrapped script file '%ls'\n", wrapped_script_path);
719
}
720
#endif
721
722
/*
723
* Process creation code
724
*/
725
726
static BOOL
727
safe_duplicate_handle(HANDLE in, HANDLE * pout)
728
{
729
BOOL ok;
730
HANDLE process = GetCurrentProcess();
731
DWORD rc;
732
733
*pout = NULL;
734
ok = DuplicateHandle(process, in, process, pout, 0, TRUE,
735
DUPLICATE_SAME_ACCESS);
736
if (!ok) {
737
rc = GetLastError();
738
if (rc == ERROR_INVALID_HANDLE) {
739
debug(L"DuplicateHandle returned ERROR_INVALID_HANDLE\n");
740
ok = TRUE;
741
}
742
else {
743
debug(L"DuplicateHandle returned %d\n", rc);
744
}
745
}
746
return ok;
747
}
748
749
static BOOL WINAPI
750
ctrl_c_handler(DWORD code)
751
{
752
return TRUE; /* We just ignore all control events. */
753
}
754
755
static void
756
run_child(wchar_t * cmdline)
757
{
758
HANDLE job;
759
JOBOBJECT_EXTENDED_LIMIT_INFORMATION info;
760
DWORD rc;
761
BOOL ok;
762
STARTUPINFOW si;
763
PROCESS_INFORMATION pi;
764
765
#if defined(_WINDOWS)
766
/*
767
When explorer launches a Windows (GUI) application, it displays
768
the "app starting" (the "pointer + hourglass") cursor for a number
769
of seconds, or until the app does something UI-ish (eg, creating a
770
window, or fetching a message). As this launcher doesn't do this
771
directly, that cursor remains even after the child process does these
772
things. We avoid that by doing a simple post+get message.
773
See http://bugs.python.org/issue17290
774
*/
775
MSG msg;
776
777
PostMessage(0, 0, 0, 0);
778
GetMessage(&msg, 0, 0, 0);
779
#endif
780
781
debug(L"run_child: about to run '%ls'\n", cmdline);
782
job = CreateJobObject(NULL, NULL);
783
ok = QueryInformationJobObject(job, JobObjectExtendedLimitInformation,
784
&info, sizeof(info), &rc);
785
if (!ok || (rc != sizeof(info)) || !job)
786
error(RC_CREATE_PROCESS, L"Job information querying failed");
787
info.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE |
788
JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK;
789
ok = SetInformationJobObject(job, JobObjectExtendedLimitInformation, &info,
790
sizeof(info));
791
if (!ok)
792
error(RC_CREATE_PROCESS, L"Job information setting failed");
793
memset(&si, 0, sizeof(si));
794
GetStartupInfoW(&si);
795
ok = safe_duplicate_handle(GetStdHandle(STD_INPUT_HANDLE), &si.hStdInput);
796
if (!ok)
797
error(RC_NO_STD_HANDLES, L"stdin duplication failed");
798
ok = safe_duplicate_handle(GetStdHandle(STD_OUTPUT_HANDLE), &si.hStdOutput);
799
if (!ok)
800
error(RC_NO_STD_HANDLES, L"stdout duplication failed");
801
ok = safe_duplicate_handle(GetStdHandle(STD_ERROR_HANDLE), &si.hStdError);
802
if (!ok)
803
error(RC_NO_STD_HANDLES, L"stderr duplication failed");
804
805
ok = SetConsoleCtrlHandler(ctrl_c_handler, TRUE);
806
if (!ok)
807
error(RC_CREATE_PROCESS, L"control handler setting failed");
808
809
si.dwFlags = STARTF_USESTDHANDLES;
810
ok = CreateProcessW(NULL, cmdline, NULL, NULL, TRUE,
811
0, NULL, NULL, &si, &pi);
812
if (!ok)
813
error(RC_CREATE_PROCESS, L"Unable to create process using '%ls'", cmdline);
814
AssignProcessToJobObject(job, pi.hProcess);
815
CloseHandle(pi.hThread);
816
WaitForSingleObjectEx(pi.hProcess, INFINITE, FALSE);
817
ok = GetExitCodeProcess(pi.hProcess, &rc);
818
if (!ok)
819
error(RC_CREATE_PROCESS, L"Failed to get exit code of process");
820
debug(L"child process exit code: %d\n", rc);
821
exit(rc);
822
}
823
824
static void
825
invoke_child(wchar_t * executable, wchar_t * suffix, wchar_t * cmdline)
826
{
827
wchar_t * child_command;
828
size_t child_command_size;
829
BOOL no_suffix = (suffix == NULL) || (*suffix == L'\0');
830
BOOL no_cmdline = (*cmdline == L'\0');
831
832
if (no_suffix && no_cmdline)
833
run_child(executable);
834
else {
835
if (no_suffix) {
836
/* add 2 for space separator + terminating NUL. */
837
child_command_size = wcslen(executable) + wcslen(cmdline) + 2;
838
}
839
else {
840
/* add 3 for 2 space separators + terminating NUL. */
841
child_command_size = wcslen(executable) + wcslen(suffix) +
842
wcslen(cmdline) + 3;
843
}
844
child_command = calloc(child_command_size, sizeof(wchar_t));
845
if (child_command == NULL)
846
error(RC_CREATE_PROCESS, L"unable to allocate %zd bytes for child command.",
847
child_command_size);
848
if (no_suffix)
849
_snwprintf_s(child_command, child_command_size,
850
child_command_size - 1, L"%ls %ls",
851
executable, cmdline);
852
else
853
_snwprintf_s(child_command, child_command_size,
854
child_command_size - 1, L"%ls %ls %ls",
855
executable, suffix, cmdline);
856
run_child(child_command);
857
free(child_command);
858
}
859
}
860
861
typedef struct {
862
wchar_t *shebang;
863
BOOL search;
864
} SHEBANG;
865
866
static SHEBANG builtin_virtual_paths [] = {
867
{ L"/usr/bin/env python", TRUE },
868
{ L"/usr/bin/python", FALSE },
869
{ L"/usr/local/bin/python", FALSE },
870
{ L"python", FALSE },
871
{ NULL, FALSE },
872
};
873
874
/* For now, a static array of commands. */
875
876
#define MAX_COMMANDS 100
877
878
typedef struct {
879
wchar_t key[MAX_PATH];
880
wchar_t value[MSGSIZE];
881
} COMMAND;
882
883
static COMMAND commands[MAX_COMMANDS];
884
static int num_commands = 0;
885
886
#if defined(SKIP_PREFIX)
887
888
static wchar_t * builtin_prefixes [] = {
889
/* These must be in an order that the longest matches should be found,
890
* i.e. if the prefix is "/usr/bin/env ", it should match that entry
891
* *before* matching "/usr/bin/".
892
*/
893
L"/usr/bin/env ",
894
L"/usr/bin/",
895
L"/usr/local/bin/",
896
NULL
897
};
898
899
static wchar_t * skip_prefix(wchar_t * name)
900
{
901
wchar_t ** pp = builtin_prefixes;
902
wchar_t * result = name;
903
wchar_t * p;
904
size_t n;
905
906
for (; p = *pp; pp++) {
907
n = wcslen(p);
908
if (_wcsnicmp(p, name, n) == 0) {
909
result += n; /* skip the prefix */
910
if (p[n - 1] == L' ') /* No empty strings in table, so n > 1 */
911
result = skip_whitespace(result);
912
break;
913
}
914
}
915
return result;
916
}
917
918
#endif
919
920
#if defined(SEARCH_PATH)
921
922
static COMMAND path_command;
923
924
static COMMAND * find_on_path(wchar_t * name)
925
{
926
wchar_t * pathext;
927
size_t varsize;
928
wchar_t * context = NULL;
929
wchar_t * extension;
930
COMMAND * result = NULL;
931
DWORD len;
932
errno_t rc;
933
934
wcscpy_s(path_command.key, MAX_PATH, name);
935
if (wcschr(name, L'.') != NULL) {
936
/* assume it has an extension. */
937
len = SearchPathW(NULL, name, NULL, MSGSIZE, path_command.value, NULL);
938
if (len) {
939
result = &path_command;
940
}
941
}
942
else {
943
/* No extension - search using registered extensions. */
944
rc = _wdupenv_s(&pathext, &varsize, L"PATHEXT");
945
if (rc == 0) {
946
extension = wcstok_s(pathext, L";", &context);
947
while (extension) {
948
len = SearchPathW(NULL, name, extension, MSGSIZE, path_command.value, NULL);
949
if (len) {
950
result = &path_command;
951
break;
952
}
953
extension = wcstok_s(NULL, L";", &context);
954
}
955
free(pathext);
956
}
957
}
958
return result;
959
}
960
961
#endif
962
963
static COMMAND * find_command(wchar_t * name)
964
{
965
COMMAND * result = NULL;
966
COMMAND * cp = commands;
967
int i;
968
969
for (i = 0; i < num_commands; i++, cp++) {
970
if (_wcsicmp(cp->key, name) == 0) {
971
result = cp;
972
break;
973
}
974
}
975
#if defined(SEARCH_PATH)
976
if (result == NULL)
977
result = find_on_path(name);
978
#endif
979
return result;
980
}
981
982
static void
983
update_command(COMMAND * cp, wchar_t * name, wchar_t * cmdline)
984
{
985
wcsncpy_s(cp->key, MAX_PATH, name, _TRUNCATE);
986
wcsncpy_s(cp->value, MSGSIZE, cmdline, _TRUNCATE);
987
}
988
989
static void
990
add_command(wchar_t * name, wchar_t * cmdline)
991
{
992
if (num_commands >= MAX_COMMANDS) {
993
debug(L"can't add %ls = '%ls': no room\n", name, cmdline);
994
}
995
else {
996
COMMAND * cp = &commands[num_commands++];
997
998
update_command(cp, name, cmdline);
999
}
1000
}
1001
1002
static void
1003
read_config_file(wchar_t * config_path)
1004
{
1005
wchar_t keynames[MSGSIZE];
1006
wchar_t value[MSGSIZE];
1007
DWORD read;
1008
wchar_t * key;
1009
COMMAND * cp;
1010
wchar_t * cmdp;
1011
1012
read = GetPrivateProfileStringW(L"commands", NULL, NULL, keynames, MSGSIZE,
1013
config_path);
1014
if (read == MSGSIZE - 1) {
1015
debug(L"read_commands: %ls: not enough space for names\n", config_path);
1016
}
1017
key = keynames;
1018
while (*key) {
1019
read = GetPrivateProfileStringW(L"commands", key, NULL, value, MSGSIZE,
1020
config_path);
1021
if (read == MSGSIZE - 1) {
1022
debug(L"read_commands: %ls: not enough space for %ls\n",
1023
config_path, key);
1024
}
1025
cmdp = skip_whitespace(value);
1026
if (*cmdp) {
1027
cp = find_command(key);
1028
if (cp == NULL)
1029
add_command(key, value);
1030
else
1031
update_command(cp, key, value);
1032
}
1033
key += wcslen(key) + 1;
1034
}
1035
}
1036
1037
static void read_commands(void)
1038
{
1039
if (launcher_ini_path[0])
1040
read_config_file(launcher_ini_path);
1041
if (appdata_ini_path[0])
1042
read_config_file(appdata_ini_path);
1043
}
1044
1045
static BOOL
1046
parse_shebang(wchar_t * shebang_line, int nchars, wchar_t ** command,
1047
wchar_t ** suffix, BOOL *search)
1048
{
1049
BOOL rc = FALSE;
1050
SHEBANG * vpp;
1051
size_t plen;
1052
wchar_t * p;
1053
wchar_t zapped;
1054
wchar_t * endp = shebang_line + nchars - 1;
1055
COMMAND * cp;
1056
wchar_t * skipped;
1057
1058
*command = NULL; /* failure return */
1059
*suffix = NULL;
1060
*search = FALSE;
1061
1062
if ((*shebang_line++ == L'#') && (*shebang_line++ == L'!')) {
1063
shebang_line = skip_whitespace(shebang_line);
1064
if (*shebang_line) {
1065
*command = shebang_line;
1066
for (vpp = builtin_virtual_paths; vpp->shebang; ++vpp) {
1067
plen = wcslen(vpp->shebang);
1068
if (wcsncmp(shebang_line, vpp->shebang, plen) == 0) {
1069
rc = TRUE;
1070
*search = vpp->search;
1071
/* We can do this because all builtin commands contain
1072
* "python".
1073
*/
1074
*command = wcsstr(shebang_line, L"python");
1075
break;
1076
}
1077
}
1078
if (vpp->shebang == NULL) {
1079
/*
1080
* Not found in builtins - look in customized commands.
1081
*
1082
* We can't permanently modify the shebang line in case
1083
* it's not a customized command, but we can temporarily
1084
* stick a NUL after the command while searching for it,
1085
* then put back the char we zapped.
1086
*/
1087
#if defined(SKIP_PREFIX)
1088
skipped = skip_prefix(shebang_line);
1089
#else
1090
skipped = shebang_line;
1091
#endif
1092
p = wcspbrk(skipped, L" \t\r\n");
1093
if (p != NULL) {
1094
zapped = *p;
1095
*p = L'\0';
1096
}
1097
cp = find_command(skipped);
1098
if (p != NULL)
1099
*p = zapped;
1100
if (cp != NULL) {
1101
*command = cp->value;
1102
if (p != NULL)
1103
*suffix = skip_whitespace(p);
1104
}
1105
}
1106
/* remove trailing whitespace */
1107
while ((endp > shebang_line) && isspace(*endp))
1108
--endp;
1109
if (endp > shebang_line)
1110
endp[1] = L'\0';
1111
}
1112
}
1113
return rc;
1114
}
1115
1116
/* #define CP_UTF8 65001 defined in winnls.h */
1117
#define CP_UTF16LE 1200
1118
#define CP_UTF16BE 1201
1119
#define CP_UTF32LE 12000
1120
#define CP_UTF32BE 12001
1121
1122
typedef struct {
1123
int length;
1124
char sequence[4];
1125
UINT code_page;
1126
} BOM;
1127
1128
/*
1129
* Strictly, we don't need to handle UTF-16 and UTF-32, since Python itself
1130
* doesn't. Never mind, one day it might - there's no harm leaving it in.
1131
*/
1132
static BOM BOMs[] = {
1133
{ 3, { 0xEF, 0xBB, 0xBF }, CP_UTF8 }, /* UTF-8 - keep first */
1134
/* Test UTF-32LE before UTF-16LE since UTF-16LE BOM is a prefix
1135
* of UTF-32LE BOM. */
1136
{ 4, { 0xFF, 0xFE, 0x00, 0x00 }, CP_UTF32LE }, /* UTF-32LE */
1137
{ 4, { 0x00, 0x00, 0xFE, 0xFF }, CP_UTF32BE }, /* UTF-32BE */
1138
{ 2, { 0xFF, 0xFE }, CP_UTF16LE }, /* UTF-16LE */
1139
{ 2, { 0xFE, 0xFF }, CP_UTF16BE }, /* UTF-16BE */
1140
{ 0 } /* sentinel */
1141
};
1142
1143
static BOM *
1144
find_BOM(char * buffer)
1145
{
1146
/*
1147
* Look for a BOM in the input and return a pointer to the
1148
* corresponding structure, or NULL if not found.
1149
*/
1150
BOM * result = NULL;
1151
BOM *bom;
1152
1153
for (bom = BOMs; bom->length; bom++) {
1154
if (strncmp(bom->sequence, buffer, bom->length) == 0) {
1155
result = bom;
1156
break;
1157
}
1158
}
1159
return result;
1160
}
1161
1162
static char *
1163
find_terminator(char * buffer, int len, BOM *bom)
1164
{
1165
char * result = NULL;
1166
char * end = buffer + len;
1167
char * p;
1168
char c;
1169
int cp;
1170
1171
for (p = buffer; p < end; p++) {
1172
c = *p;
1173
if (c == '\r') {
1174
result = p;
1175
break;
1176
}
1177
if (c == '\n') {
1178
result = p;
1179
break;
1180
}
1181
}
1182
if (result != NULL) {
1183
cp = bom->code_page;
1184
1185
/* adjustments to include all bytes of the char */
1186
/* no adjustment needed for UTF-8 or big endian */
1187
if (cp == CP_UTF16LE)
1188
++result;
1189
else if (cp == CP_UTF32LE)
1190
result += 3;
1191
++result; /* point just past terminator */
1192
}
1193
return result;
1194
}
1195
1196
static BOOL
1197
validate_version(wchar_t * p)
1198
{
1199
/*
1200
Version information should start with the major version,
1201
Optionally followed by a period and a minor version,
1202
Optionally followed by a minus and one of 32 or 64.
1203
Valid examples:
1204
2
1205
3
1206
2.7
1207
3.6
1208
2.7-32
1209
The intent is to add to the valid patterns:
1210
3.10
1211
3-32
1212
3.6-64
1213
3-64
1214
*/
1215
BOOL result = (p != NULL); /* Default to False if null pointer. */
1216
1217
result = result && iswdigit(*p); /* Result = False if first string element is not a digit. */
1218
1219
while (result && iswdigit(*p)) /* Require a major version */
1220
++p; /* Skip all leading digit(s) */
1221
if (result && (*p == L'.')) /* Allow . for major minor separator.*/
1222
{
1223
result = iswdigit(*++p); /* Must be at least one digit */
1224
while (result && iswdigit(*++p)) ; /* Skip any more Digits */
1225
}
1226
if (result && (*p == L'-')) { /* Allow - for Bits Separator */
1227
switch(*++p){
1228
case L'3': /* 3 is OK */
1229
result = (*++p == L'2') && !*++p; /* only if followed by 2 and ended.*/
1230
break;
1231
case L'6': /* 6 is OK */
1232
result = (*++p == L'4') && !*++p; /* only if followed by 4 and ended.*/
1233
break;
1234
default:
1235
result = FALSE;
1236
break;
1237
}
1238
}
1239
result = result && !*p; /* Must have reached EOS */
1240
return result;
1241
1242
}
1243
1244
typedef struct {
1245
unsigned short min;
1246
unsigned short max;
1247
wchar_t version[MAX_VERSION_SIZE];
1248
} PYC_MAGIC;
1249
1250
static PYC_MAGIC magic_values[] = {
1251
{ 50823, 50823, L"2.0" },
1252
{ 60202, 60202, L"2.1" },
1253
{ 60717, 60717, L"2.2" },
1254
{ 62011, 62021, L"2.3" },
1255
{ 62041, 62061, L"2.4" },
1256
{ 62071, 62131, L"2.5" },
1257
{ 62151, 62161, L"2.6" },
1258
{ 62171, 62211, L"2.7" },
1259
{ 3000, 3131, L"3.0" },
1260
{ 3141, 3151, L"3.1" },
1261
{ 3160, 3180, L"3.2" },
1262
{ 3190, 3230, L"3.3" },
1263
{ 3250, 3310, L"3.4" },
1264
{ 3320, 3351, L"3.5" },
1265
{ 3360, 3379, L"3.6" },
1266
{ 3390, 3399, L"3.7" },
1267
{ 3400, 3419, L"3.8" },
1268
{ 3420, 3429, L"3.9" },
1269
{ 3430, 3449, L"3.10" },
1270
/* Allow 50 magic numbers per version from here on */
1271
{ 3450, 3499, L"3.11" },
1272
{ 3500, 3549, L"3.12" },
1273
{ 3550, 3599, L"3.13" },
1274
{ 0 }
1275
};
1276
1277
static INSTALLED_PYTHON *
1278
find_by_magic(unsigned short magic)
1279
{
1280
INSTALLED_PYTHON * result = NULL;
1281
PYC_MAGIC * mp;
1282
1283
for (mp = magic_values; mp->min; mp++) {
1284
if ((magic >= mp->min) && (magic <= mp->max)) {
1285
result = locate_python(mp->version, FALSE);
1286
if (result != NULL)
1287
break;
1288
}
1289
}
1290
return result;
1291
}
1292
1293
static void
1294
maybe_handle_shebang(wchar_t ** argv, wchar_t * cmdline)
1295
{
1296
/*
1297
* Look for a shebang line in the first argument. If found
1298
* and we spawn a child process, this never returns. If it
1299
* does return then we process the args "normally".
1300
*
1301
* argv[0] might be a filename with a shebang.
1302
*/
1303
FILE * fp;
1304
errno_t rc = _wfopen_s(&fp, *argv, L"rb");
1305
char buffer[BUFSIZE];
1306
wchar_t shebang_line[BUFSIZE + 1];
1307
size_t read;
1308
char *p;
1309
char * start;
1310
char * shebang_alias = (char *) shebang_line;
1311
BOM* bom;
1312
int i, j, nchars = 0;
1313
int header_len;
1314
BOOL is_virt;
1315
BOOL search;
1316
wchar_t * command;
1317
wchar_t * suffix;
1318
COMMAND *cmd = NULL;
1319
INSTALLED_PYTHON * ip;
1320
1321
if (rc == 0) {
1322
read = fread(buffer, sizeof(char), BUFSIZE, fp);
1323
debug(L"maybe_handle_shebang: read %zd bytes\n", read);
1324
fclose(fp);
1325
1326
if ((read >= 4) && (buffer[3] == '\n') && (buffer[2] == '\r')) {
1327
ip = find_by_magic((((unsigned char)buffer[1]) << 8 |
1328
(unsigned char)buffer[0]) & 0xFFFF);
1329
if (ip != NULL) {
1330
debug(L"script file is compiled against Python %ls\n",
1331
ip->version);
1332
invoke_child(ip->executable, NULL, cmdline);
1333
}
1334
}
1335
/* Look for BOM */
1336
bom = find_BOM(buffer);
1337
if (bom == NULL) {
1338
start = buffer;
1339
debug(L"maybe_handle_shebang: BOM not found, using UTF-8\n");
1340
bom = BOMs; /* points to UTF-8 entry - the default */
1341
}
1342
else {
1343
debug(L"maybe_handle_shebang: BOM found, code page %u\n",
1344
bom->code_page);
1345
start = &buffer[bom->length];
1346
}
1347
p = find_terminator(start, BUFSIZE, bom);
1348
/*
1349
* If no CR or LF was found in the heading,
1350
* we assume it's not a shebang file.
1351
*/
1352
if (p == NULL) {
1353
debug(L"maybe_handle_shebang: No line terminator found\n");
1354
}
1355
else {
1356
/*
1357
* Found line terminator - parse the shebang.
1358
*
1359
* Strictly, we don't need to handle UTF-16 anf UTF-32,
1360
* since Python itself doesn't.
1361
* Never mind, one day it might.
1362
*/
1363
header_len = (int) (p - start);
1364
switch(bom->code_page) {
1365
case CP_UTF8:
1366
nchars = MultiByteToWideChar(bom->code_page,
1367
0,
1368
start, header_len, shebang_line,
1369
BUFSIZE);
1370
break;
1371
case CP_UTF16BE:
1372
if (header_len % 2 != 0) {
1373
debug(L"maybe_handle_shebang: UTF-16BE, but an odd number \
1374
of bytes: %d\n", header_len);
1375
/* nchars = 0; Not needed - initialised to 0. */
1376
}
1377
else {
1378
for (i = header_len; i > 0; i -= 2) {
1379
shebang_alias[i - 1] = start[i - 2];
1380
shebang_alias[i - 2] = start[i - 1];
1381
}
1382
nchars = header_len / sizeof(wchar_t);
1383
}
1384
break;
1385
case CP_UTF16LE:
1386
if ((header_len % 2) != 0) {
1387
debug(L"UTF-16LE, but an odd number of bytes: %d\n",
1388
header_len);
1389
/* nchars = 0; Not needed - initialised to 0. */
1390
}
1391
else {
1392
/* no actual conversion needed. */
1393
memcpy(shebang_line, start, header_len);
1394
nchars = header_len / sizeof(wchar_t);
1395
}
1396
break;
1397
case CP_UTF32BE:
1398
if (header_len % 4 != 0) {
1399
debug(L"UTF-32BE, but not divisible by 4: %d\n",
1400
header_len);
1401
/* nchars = 0; Not needed - initialised to 0. */
1402
}
1403
else {
1404
for (i = header_len, j = header_len / 2; i > 0; i -= 4,
1405
j -= 2) {
1406
shebang_alias[j - 1] = start[i - 2];
1407
shebang_alias[j - 2] = start[i - 1];
1408
}
1409
nchars = header_len / sizeof(wchar_t);
1410
}
1411
break;
1412
case CP_UTF32LE:
1413
if (header_len % 4 != 0) {
1414
debug(L"UTF-32LE, but not divisible by 4: %d\n",
1415
header_len);
1416
/* nchars = 0; Not needed - initialised to 0. */
1417
}
1418
else {
1419
for (i = header_len, j = header_len / 2; i > 0; i -= 4,
1420
j -= 2) {
1421
shebang_alias[j - 1] = start[i - 3];
1422
shebang_alias[j - 2] = start[i - 4];
1423
}
1424
nchars = header_len / sizeof(wchar_t);
1425
}
1426
break;
1427
}
1428
if (nchars > 0) {
1429
shebang_line[--nchars] = L'\0';
1430
is_virt = parse_shebang(shebang_line, nchars, &command,
1431
&suffix, &search);
1432
if (command != NULL) {
1433
debug(L"parse_shebang: found command: %ls\n", command);
1434
if (!is_virt) {
1435
invoke_child(command, suffix, cmdline);
1436
}
1437
else {
1438
suffix = wcschr(command, L' ');
1439
if (suffix != NULL) {
1440
*suffix++ = L'\0';
1441
suffix = skip_whitespace(suffix);
1442
}
1443
if (wcsncmp(command, L"python", 6))
1444
error(RC_BAD_VIRTUAL_PATH, L"Unknown virtual \
1445
path '%ls'", command);
1446
command += 6; /* skip past "python" */
1447
if (search && ((*command == L'\0') || isspace(*command))) {
1448
/* Command is eligible for path search, and there
1449
* is no version specification.
1450
*/
1451
debug(L"searching PATH for python executable\n");
1452
cmd = find_on_path(PYTHON_EXECUTABLE);
1453
debug(L"Python on path: %ls\n", cmd ? cmd->value : L"<not found>");
1454
if (cmd) {
1455
debug(L"located python on PATH: %ls\n", cmd->value);
1456
invoke_child(cmd->value, suffix, cmdline);
1457
/* Exit here, as we have found the command */
1458
return;
1459
}
1460
/* FALL THROUGH: No python found on PATH, so fall
1461
* back to locating the correct installed python.
1462
*/
1463
}
1464
if (*command && !validate_version(command))
1465
error(RC_BAD_VIRTUAL_PATH, L"Invalid version \
1466
specification: '%ls'.\nIn the first line of the script, 'python' needs to be \
1467
followed by a valid version specifier.\nPlease check the documentation.",
1468
command);
1469
/* TODO could call validate_version(command) */
1470
ip = locate_python(command, TRUE);
1471
if (ip == NULL) {
1472
error(RC_NO_PYTHON, L"Requested Python version \
1473
(%ls) is not installed", command);
1474
}
1475
else {
1476
invoke_child(ip->executable, suffix, cmdline);
1477
}
1478
}
1479
}
1480
}
1481
}
1482
}
1483
}
1484
1485
static wchar_t *
1486
skip_me(wchar_t * cmdline)
1487
{
1488
BOOL quoted;
1489
wchar_t c;
1490
wchar_t * result = cmdline;
1491
1492
quoted = cmdline[0] == L'\"';
1493
if (!quoted)
1494
c = L' ';
1495
else {
1496
c = L'\"';
1497
++result;
1498
}
1499
result = wcschr(result, c);
1500
if (result == NULL) /* when, for example, just exe name on command line */
1501
result = L"";
1502
else {
1503
++result; /* skip past space or closing quote */
1504
result = skip_whitespace(result);
1505
}
1506
return result;
1507
}
1508
1509
static DWORD version_high = 0;
1510
static DWORD version_low = 0;
1511
1512
static void
1513
get_version_info(wchar_t * version_text, size_t size)
1514
{
1515
WORD maj, min, rel, bld;
1516
1517
if (!version_high && !version_low)
1518
wcsncpy_s(version_text, size, L"0.1", _TRUNCATE); /* fallback */
1519
else {
1520
maj = HIWORD(version_high);
1521
min = LOWORD(version_high);
1522
rel = HIWORD(version_low);
1523
bld = LOWORD(version_low);
1524
_snwprintf_s(version_text, size, _TRUNCATE, L"%d.%d.%d.%d", maj,
1525
min, rel, bld);
1526
}
1527
}
1528
1529
static void
1530
show_help_text(wchar_t ** argv)
1531
{
1532
wchar_t version_text [MAX_PATH];
1533
#if defined(_M_X64)
1534
BOOL canDo64bit = TRUE;
1535
#else
1536
/* If we are a 32bit process on a 64bit Windows, first hit the 64bit keys. */
1537
BOOL canDo64bit = FALSE;
1538
IsWow64Process(GetCurrentProcess(), &canDo64bit);
1539
#endif
1540
1541
get_version_info(version_text, MAX_PATH);
1542
fwprintf(stdout, L"\
1543
Python Launcher for Windows Version %ls\n\n", version_text);
1544
fwprintf(stdout, L"\
1545
usage:\n\
1546
%ls [launcher-args] [python-args] [script [script-args]]\n\n", argv[0]);
1547
fputws(L"\
1548
Launcher arguments:\n\n\
1549
-2 : Launch the latest Python 2.x version\n\
1550
-3 : Launch the latest Python 3.x version\n\
1551
-X.Y : Launch the specified Python version\n", stdout);
1552
if (canDo64bit) {
1553
fputws(L"\
1554
The above all default to 64 bit if a matching 64 bit python is present.\n\
1555
-X.Y-32: Launch the specified 32bit Python version\n\
1556
-X-32 : Launch the latest 32bit Python X version\n\
1557
-X.Y-64: Launch the specified 64bit Python version\n\
1558
-X-64 : Launch the latest 64bit Python X version", stdout);
1559
}
1560
fputws(L"\n-0 --list : List the available pythons", stdout);
1561
fputws(L"\n-0p --list-paths : List with paths", stdout);
1562
fputws(L"\n\n If no script is specified the specified interpreter is opened.", stdout);
1563
fputws(L"\nIf an exact version is not given, using the latest version can be overridden by", stdout);
1564
fputws(L"\nany of the following, (in priority order):", stdout);
1565
fputws(L"\n An active virtual environment", stdout);
1566
fputws(L"\n A shebang line in the script (if present)", stdout);
1567
fputws(L"\n With -2 or -3 flag a matching PY_PYTHON2 or PY_PYTHON3 Environment variable", stdout);
1568
fputws(L"\n A PY_PYTHON Environment variable", stdout);
1569
fputws(L"\n From [defaults] in py.ini in your %LOCALAPPDATA%\\py.ini", stdout);
1570
fputws(L"\n From [defaults] in py.ini beside py.exe (use `where py` to locate)", stdout);
1571
fputws(L"\n\nThe following help text is from Python:\n\n", stdout);
1572
fflush(stdout);
1573
}
1574
1575
static BOOL
1576
show_python_list(wchar_t ** argv)
1577
{
1578
/*
1579
* Display options -0
1580
*/
1581
INSTALLED_PYTHON * result = NULL;
1582
INSTALLED_PYTHON * ip = installed_pythons; /* List of installed pythons */
1583
INSTALLED_PYTHON * defpy = locate_python(L"", FALSE);
1584
size_t i = 0;
1585
wchar_t *p = argv[1];
1586
wchar_t *ver_fmt = L"-%ls-%d";
1587
wchar_t *fmt = L"\n %ls";
1588
wchar_t *defind = L" *"; /* Default indicator */
1589
1590
/*
1591
* Output informational messages to stderr to keep output
1592
* clean for use in pipes, etc.
1593
*/
1594
fwprintf(stderr,
1595
L"Installed Pythons found by %s Launcher for Windows", argv[0]);
1596
if (!_wcsicmp(p, L"-0p") || !_wcsicmp(p, L"--list-paths"))
1597
fmt = L"\n %-15ls%ls"; /* include path */
1598
1599
if (num_installed_pythons == 0) /* We have somehow got here without searching for pythons */
1600
locate_all_pythons(); /* Find them, Populates installed_pythons */
1601
1602
if (num_installed_pythons == 0) /* No pythons found */
1603
fwprintf(stderr, L"\nNo Installed Pythons Found!");
1604
else
1605
{
1606
for (i = 0; i < num_installed_pythons; i++, ip++) {
1607
wchar_t version[BUFSIZ];
1608
if (wcscmp(ip->version, L"venv") == 0) {
1609
wcscpy_s(version, BUFSIZ, L"(venv)");
1610
}
1611
else {
1612
swprintf_s(version, BUFSIZ, ver_fmt, ip->version, ip->bits);
1613
}
1614
1615
if (ip->exe_display[0]) {
1616
fwprintf(stdout, fmt, version, ip->exe_display);
1617
}
1618
else {
1619
fwprintf(stdout, fmt, version, ip->executable);
1620
}
1621
/* If there is a default indicate it */
1622
if (defpy == ip)
1623
fwprintf(stderr, defind);
1624
}
1625
}
1626
1627
if ((defpy == NULL) && (num_installed_pythons > 0))
1628
/* We have pythons but none is the default */
1629
fwprintf(stderr, L"\n\nCan't find a Default Python.\n\n");
1630
else
1631
fwprintf(stderr, L"\n\n"); /* End with a blank line */
1632
return FALSE; /* If this has been called we cannot continue */
1633
}
1634
1635
#if defined(VENV_REDIRECT)
1636
1637
static int
1638
find_home_value(const char *buffer, const char **start, DWORD *length)
1639
{
1640
for (const char *s = strstr(buffer, "home"); s; s = strstr(s + 1, "\nhome")) {
1641
if (*s == '\n') {
1642
++s;
1643
}
1644
for (int i = 4; i > 0 && *s; --i, ++s);
1645
1646
while (*s && iswspace(*s)) {
1647
++s;
1648
}
1649
if (*s != L'=') {
1650
continue;
1651
}
1652
1653
do {
1654
++s;
1655
} while (*s && iswspace(*s));
1656
1657
*start = s;
1658
char *nl = strchr(s, '\n');
1659
if (nl) {
1660
*length = (DWORD)((ptrdiff_t)nl - (ptrdiff_t)s);
1661
} else {
1662
*length = (DWORD)strlen(s);
1663
}
1664
return 1;
1665
}
1666
return 0;
1667
}
1668
#endif
1669
1670
static wchar_t *
1671
wcsdup_pad(const wchar_t *s, int padding, int *newlen)
1672
{
1673
size_t len = wcslen(s);
1674
len += 1 + padding;
1675
wchar_t *r = (wchar_t *)malloc(len * sizeof(wchar_t));
1676
if (!r) {
1677
return NULL;
1678
}
1679
if (wcscpy_s(r, len, s)) {
1680
free(r);
1681
return NULL;
1682
}
1683
*newlen = len < MAXINT ? (int)len : MAXINT;
1684
return r;
1685
}
1686
1687
static wchar_t *
1688
get_process_name(void)
1689
{
1690
DWORD bufferLen = MAX_PATH;
1691
DWORD len = bufferLen;
1692
wchar_t *r = NULL;
1693
1694
while (!r) {
1695
r = (wchar_t *)malloc(bufferLen * sizeof(wchar_t));
1696
if (!r) {
1697
error(RC_NO_MEMORY, L"out of memory");
1698
return NULL;
1699
}
1700
len = GetModuleFileNameW(NULL, r, bufferLen);
1701
if (len == 0) {
1702
free(r);
1703
error(0, L"Failed to get module name");
1704
return NULL;
1705
} else if (len == bufferLen &&
1706
GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
1707
free(r);
1708
r = NULL;
1709
bufferLen *= 2;
1710
}
1711
}
1712
1713
return r;
1714
}
1715
1716
static int
1717
process(int argc, wchar_t ** argv)
1718
{
1719
wchar_t * wp;
1720
wchar_t * command;
1721
wchar_t * executable;
1722
wchar_t * p;
1723
wchar_t * argv0;
1724
int rc = 0;
1725
INSTALLED_PYTHON * ip;
1726
BOOL valid;
1727
DWORD size, attrs;
1728
wchar_t message[MSGSIZE];
1729
void * version_data;
1730
VS_FIXEDFILEINFO * file_info;
1731
UINT block_size;
1732
#if defined(VENV_REDIRECT)
1733
wchar_t * venv_cfg_path;
1734
int newlen;
1735
#elif defined(SCRIPT_WRAPPER)
1736
wchar_t * newcommand;
1737
wchar_t * av[2];
1738
int newlen;
1739
HRESULT hr;
1740
int index;
1741
#else
1742
HRESULT hr;
1743
int index;
1744
#endif
1745
1746
setvbuf(stderr, (char *)NULL, _IONBF, 0);
1747
wp = get_env(L"PYLAUNCH_DEBUG");
1748
if ((wp != NULL) && (*wp != L'\0'))
1749
log_fp = stderr;
1750
1751
#if defined(_M_X64)
1752
debug(L"launcher build: 64bit\n");
1753
#else
1754
debug(L"launcher build: 32bit\n");
1755
#endif
1756
#if defined(_WINDOWS)
1757
debug(L"launcher executable: Windows\n");
1758
#else
1759
debug(L"launcher executable: Console\n");
1760
#endif
1761
#if !defined(VENV_REDIRECT)
1762
/* Get the local appdata folder (non-roaming) */
1763
hr = SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA,
1764
NULL, 0, appdata_ini_path);
1765
if (hr != S_OK) {
1766
debug(L"SHGetFolderPath failed: %X\n", hr);
1767
appdata_ini_path[0] = L'\0';
1768
}
1769
else {
1770
wcsncat_s(appdata_ini_path, MAX_PATH, L"\\py.ini", _TRUNCATE);
1771
attrs = GetFileAttributesW(appdata_ini_path);
1772
if (attrs == INVALID_FILE_ATTRIBUTES) {
1773
debug(L"File '%ls' non-existent\n", appdata_ini_path);
1774
appdata_ini_path[0] = L'\0';
1775
} else {
1776
debug(L"Using local configuration file '%ls'\n", appdata_ini_path);
1777
}
1778
}
1779
#endif
1780
argv0 = get_process_name();
1781
size = GetFileVersionInfoSizeW(argv0, &size);
1782
if (size == 0) {
1783
winerror(GetLastError(), message, MSGSIZE);
1784
debug(L"GetFileVersionInfoSize failed: %ls\n", message);
1785
}
1786
else {
1787
version_data = malloc(size);
1788
if (version_data) {
1789
valid = GetFileVersionInfoW(argv0, 0, size,
1790
version_data);
1791
if (!valid)
1792
debug(L"GetFileVersionInfo failed: %X\n", GetLastError());
1793
else {
1794
valid = VerQueryValueW(version_data, L"\\",
1795
(LPVOID *) &file_info, &block_size);
1796
if (!valid)
1797
debug(L"VerQueryValue failed: %X\n", GetLastError());
1798
else {
1799
version_high = file_info->dwFileVersionMS;
1800
version_low = file_info->dwFileVersionLS;
1801
}
1802
}
1803
free(version_data);
1804
}
1805
}
1806
1807
#if defined(VENV_REDIRECT)
1808
/* Allocate some extra space for new filenames */
1809
venv_cfg_path = wcsdup_pad(argv0, 32, &newlen);
1810
if (!venv_cfg_path) {
1811
error(RC_NO_MEMORY, L"Failed to copy module name");
1812
}
1813
p = wcsrchr(venv_cfg_path, L'\\');
1814
1815
if (p == NULL) {
1816
error(RC_NO_VENV_CFG, L"No pyvenv.cfg file");
1817
}
1818
p[0] = L'\0';
1819
wcscat_s(venv_cfg_path, newlen, L"\\pyvenv.cfg");
1820
attrs = GetFileAttributesW(venv_cfg_path);
1821
if (attrs == INVALID_FILE_ATTRIBUTES) {
1822
debug(L"File '%ls' non-existent\n", venv_cfg_path);
1823
p[0] = '\0';
1824
p = wcsrchr(venv_cfg_path, L'\\');
1825
if (p != NULL) {
1826
p[0] = '\0';
1827
wcscat_s(venv_cfg_path, newlen, L"\\pyvenv.cfg");
1828
attrs = GetFileAttributesW(venv_cfg_path);
1829
if (attrs == INVALID_FILE_ATTRIBUTES) {
1830
debug(L"File '%ls' non-existent\n", venv_cfg_path);
1831
error(RC_NO_VENV_CFG, L"No pyvenv.cfg file");
1832
}
1833
}
1834
}
1835
debug(L"Using venv configuration file '%ls'\n", venv_cfg_path);
1836
#else
1837
/* Allocate some extra space for new filenames */
1838
if (wcscpy_s(launcher_ini_path, MAX_PATH, argv0)) {
1839
error(RC_NO_MEMORY, L"Failed to copy module name");
1840
}
1841
p = wcsrchr(launcher_ini_path, L'\\');
1842
1843
if (p == NULL) {
1844
debug(L"GetModuleFileNameW returned value has no backslash: %ls\n",
1845
launcher_ini_path);
1846
launcher_ini_path[0] = L'\0';
1847
}
1848
else {
1849
p[0] = L'\0';
1850
wcscat_s(launcher_ini_path, MAX_PATH, L"\\py.ini");
1851
attrs = GetFileAttributesW(launcher_ini_path);
1852
if (attrs == INVALID_FILE_ATTRIBUTES) {
1853
debug(L"File '%ls' non-existent\n", launcher_ini_path);
1854
launcher_ini_path[0] = L'\0';
1855
} else {
1856
debug(L"Using global configuration file '%ls'\n", launcher_ini_path);
1857
}
1858
}
1859
#endif
1860
1861
command = skip_me(GetCommandLineW());
1862
debug(L"Called with command line: %ls\n", command);
1863
1864
#if !defined(VENV_REDIRECT)
1865
/* bpo-35811: The __PYVENV_LAUNCHER__ variable is used to
1866
* override sys.executable and locate the original prefix path.
1867
* However, if it is silently inherited by a non-venv Python
1868
* process, that process will believe it is running in the venv
1869
* still. This is the only place where *we* can clear it (that is,
1870
* when py.exe is being used to launch Python), so we do.
1871
*/
1872
SetEnvironmentVariableW(L"__PYVENV_LAUNCHER__", NULL);
1873
#endif
1874
1875
#if defined(SCRIPT_WRAPPER)
1876
/* The launcher is being used in "script wrapper" mode.
1877
* There should therefore be a Python script named <exename>-script.py in
1878
* the same directory as the launcher executable.
1879
* Put the script name into argv as the first (script name) argument.
1880
*/
1881
1882
/* Get the wrapped script name - if the script is not present, this will
1883
* terminate the program with an error.
1884
*/
1885
locate_wrapped_script();
1886
1887
/* Add the wrapped script to the start of command */
1888
newlen = wcslen(wrapped_script_path) + wcslen(command) + 2; /* ' ' + NUL */
1889
newcommand = malloc(sizeof(wchar_t) * newlen);
1890
if (!newcommand) {
1891
error(RC_NO_MEMORY, L"Could not allocate new command line");
1892
}
1893
else {
1894
wcscpy_s(newcommand, newlen, wrapped_script_path);
1895
wcscat_s(newcommand, newlen, L" ");
1896
wcscat_s(newcommand, newlen, command);
1897
debug(L"Running wrapped script with command line '%ls'\n", newcommand);
1898
read_commands();
1899
av[0] = wrapped_script_path;
1900
av[1] = NULL;
1901
maybe_handle_shebang(av, newcommand);
1902
/* Returns if no shebang line - pass to default processing */
1903
command = newcommand;
1904
valid = FALSE;
1905
}
1906
#elif defined(VENV_REDIRECT)
1907
{
1908
FILE *f;
1909
char buffer[4096]; /* 4KB should be enough for anybody */
1910
char *start;
1911
DWORD len, cch, cch_actual;
1912
size_t cb;
1913
if (_wfopen_s(&f, venv_cfg_path, L"r")) {
1914
error(RC_BAD_VENV_CFG, L"Cannot read '%ls'", venv_cfg_path);
1915
}
1916
cb = fread_s(buffer, sizeof(buffer), sizeof(buffer[0]),
1917
sizeof(buffer) / sizeof(buffer[0]), f);
1918
fclose(f);
1919
1920
if (!find_home_value(buffer, &start, &len)) {
1921
error(RC_BAD_VENV_CFG, L"Cannot find home in '%ls'",
1922
venv_cfg_path);
1923
}
1924
1925
cch = MultiByteToWideChar(CP_UTF8, 0, start, len, NULL, 0);
1926
if (!cch) {
1927
error(0, L"Cannot determine memory for home path");
1928
}
1929
cch += (DWORD)wcslen(PYTHON_EXECUTABLE) + 4; /* include sep, null and quotes */
1930
executable = (wchar_t *)malloc(cch * sizeof(wchar_t));
1931
if (executable == NULL) {
1932
error(RC_NO_MEMORY, L"A memory allocation failed");
1933
}
1934
/* start with a quote - we'll skip this ahead, but want it for the final string */
1935
executable[0] = L'"';
1936
cch_actual = MultiByteToWideChar(CP_UTF8, 0, start, len, &executable[1], cch - 1);
1937
if (!cch_actual) {
1938
error(RC_BAD_VENV_CFG, L"Cannot decode home path in '%ls'",
1939
venv_cfg_path);
1940
}
1941
cch_actual += 1; /* account for the first quote */
1942
executable[cch_actual] = L'\0';
1943
if (executable[cch_actual - 1] != L'\\') {
1944
executable[cch_actual++] = L'\\';
1945
executable[cch_actual] = L'\0';
1946
}
1947
if (wcscat_s(&executable[1], cch - 1, PYTHON_EXECUTABLE)) {
1948
error(RC_BAD_VENV_CFG, L"Cannot create executable path from '%ls'",
1949
venv_cfg_path);
1950
}
1951
/* there's no trailing quote, so we only have to skip one character for the test */
1952
if (GetFileAttributesW(&executable[1]) == INVALID_FILE_ATTRIBUTES) {
1953
error(RC_NO_PYTHON, L"No Python at '%ls'", executable);
1954
}
1955
/* now append the final quote */
1956
wcscat_s(executable, cch, L"\"");
1957
/* smuggle our original path through */
1958
if (!SetEnvironmentVariableW(L"__PYVENV_LAUNCHER__", argv0)) {
1959
error(0, L"Failed to set launcher environment");
1960
}
1961
valid = 1;
1962
}
1963
#else
1964
if (argc <= 1) {
1965
valid = FALSE;
1966
p = NULL;
1967
}
1968
else {
1969
p = argv[1];
1970
if ((argc == 2) && // list version args
1971
(!wcsncmp(p, L"-0", wcslen(L"-0")) ||
1972
!wcsncmp(p, L"--list", wcslen(L"--list"))))
1973
{
1974
show_python_list(argv);
1975
return rc;
1976
}
1977
valid = valid && (*p == L'-') && validate_version(&p[1]);
1978
if (valid) {
1979
ip = locate_python(&p[1], FALSE);
1980
if (ip == NULL)
1981
{
1982
fwprintf(stdout, \
1983
L"Python %ls not found!\n", &p[1]);
1984
valid = show_python_list(argv);
1985
error(RC_NO_PYTHON, L"Requested Python version (%ls) not \
1986
installed, use -0 for available pythons", &p[1]);
1987
}
1988
executable = ip->executable;
1989
command += wcslen(p);
1990
command = skip_whitespace(command);
1991
}
1992
else {
1993
for (index = 1; index < argc; ++index) {
1994
if (*argv[index] != L'-')
1995
break;
1996
}
1997
if (index < argc) {
1998
read_commands();
1999
maybe_handle_shebang(&argv[index], command);
2000
}
2001
}
2002
}
2003
#endif
2004
2005
if (!valid) {
2006
if ((argc == 2) && (!_wcsicmp(p, L"-h") || !_wcsicmp(p, L"--help")))
2007
show_help_text(argv);
2008
if ((argc == 2) &&
2009
(!_wcsicmp(p, L"-0") || !_wcsicmp(p, L"--list") ||
2010
!_wcsicmp(p, L"-0p") || !_wcsicmp(p, L"--list-paths")))
2011
{
2012
executable = NULL; /* Info call only */
2013
}
2014
else {
2015
/* look for the default Python */
2016
ip = locate_python(L"", FALSE);
2017
if (ip == NULL)
2018
error(RC_NO_PYTHON, L"Can't find a default Python.");
2019
executable = ip->executable;
2020
}
2021
}
2022
if (executable != NULL)
2023
invoke_child(executable, NULL, command);
2024
else
2025
rc = RC_NO_PYTHON;
2026
return rc;
2027
}
2028
2029
#if defined(_WINDOWS)
2030
2031
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
2032
LPWSTR lpstrCmd, int nShow)
2033
{
2034
return process(__argc, __wargv);
2035
}
2036
2037
#else
2038
2039
int cdecl wmain(int argc, wchar_t ** argv)
2040
{
2041
return process(argc, argv);
2042
}
2043
2044
#endif
2045
2046