summaryrefslogtreecommitdiff
path: root/PC/launcher.c
diff options
context:
space:
mode:
Diffstat (limited to 'PC/launcher.c')
-rw-r--r--PC/launcher.c33
1 files changed, 21 insertions, 12 deletions
diff --git a/PC/launcher.c b/PC/launcher.c
index 40d4cb5474..d11df437b9 100644
--- a/PC/launcher.c
+++ b/PC/launcher.c
@@ -98,7 +98,7 @@ error(int rc, wchar_t * format, ... )
MessageBox(NULL, message, TEXT("Python Launcher is sorry to say ..."),
MB_OK);
#endif
- ExitProcess(rc);
+ exit(rc);
}
/*
@@ -465,7 +465,7 @@ get_configured_value(wchar_t * key)
}
static INSTALLED_PYTHON *
-locate_python(wchar_t * wanted_ver)
+locate_python(wchar_t * wanted_ver, BOOL from_shebang)
{
static wchar_t config_key [] = { L"pythonX" };
static wchar_t * last_char = &config_key[sizeof(config_key) /
@@ -497,10 +497,17 @@ locate_python(wchar_t * wanted_ver)
configured_value = get_configured_value(config_key);
if (configured_value)
result = find_python_by_version(configured_value);
+ /* Not found a value yet - try by major version.
+ * If we're looking for an interpreter specified in a shebang line,
+ * we want to try Python 2 first, then Python 3 (for Unix and backward
+ * compatibility). If we're being called interactively, assume the user
+ * wants the latest version available, so try Python 3 first, then
+ * Python 2.
+ */
if (result == NULL)
- result = find_python_by_version(L"2");
+ result = find_python_by_version(from_shebang ? L"2" : L"3");
if (result == NULL)
- result = find_python_by_version(L"3");
+ result = find_python_by_version(from_shebang ? L"3" : L"2");
debug(L"search for default Python found ");
if (result) {
debug(L"version %ls at '%ls'\n",
@@ -655,7 +662,7 @@ run_child(wchar_t * cmdline)
if (!ok)
error(RC_CREATE_PROCESS, L"Failed to get exit code of process");
debug(L"child process exit code: %d\n", rc);
- ExitProcess(rc);
+ exit(rc);
}
static void
@@ -1082,7 +1089,7 @@ static PYC_MAGIC magic_values[] = {
{ 3190, 3230, L"3.3" },
{ 3250, 3310, L"3.4" },
{ 3320, 3351, L"3.5" },
- { 3360, 3361, L"3.6" },
+ { 3360, 3379, L"3.6" },
{ 0 }
};
@@ -1094,7 +1101,7 @@ find_by_magic(unsigned short magic)
for (mp = magic_values; mp->min; mp++) {
if ((magic >= mp->min) && (magic <= mp->max)) {
- result = locate_python(mp->version);
+ result = locate_python(mp->version, FALSE);
if (result != NULL)
break;
}
@@ -1114,7 +1121,7 @@ maybe_handle_shebang(wchar_t ** argv, wchar_t * cmdline)
*/
FILE * fp;
errno_t rc = _wfopen_s(&fp, *argv, L"rb");
- unsigned char buffer[BUFSIZE];
+ char buffer[BUFSIZE];
wchar_t shebang_line[BUFSIZE + 1];
size_t read;
char *p;
@@ -1136,7 +1143,8 @@ maybe_handle_shebang(wchar_t ** argv, wchar_t * cmdline)
fclose(fp);
if ((read >= 4) && (buffer[3] == '\n') && (buffer[2] == '\r')) {
- ip = find_by_magic((buffer[1] << 8 | buffer[0]) & 0xFFFF);
+ ip = find_by_magic((((unsigned char)buffer[1]) << 8 |
+ (unsigned char)buffer[0]) & 0xFFFF);
if (ip != NULL) {
debug(L"script file is compiled against Python %ls\n",
ip->version);
@@ -1278,7 +1286,7 @@ specification: '%ls'.\nIn the first line of the script, 'python' needs to be \
followed by a valid version specifier.\nPlease check the documentation.",
command);
/* TODO could call validate_version(command) */
- ip = locate_python(command);
+ ip = locate_python(command, TRUE);
if (ip == NULL) {
error(RC_NO_PYTHON, L"Requested Python version \
(%ls) is not installed", command);
@@ -1362,6 +1370,7 @@ process(int argc, wchar_t ** argv)
wchar_t * av[2];
#endif
+ setvbuf(stderr, (char *)NULL, _IONBF, 0);
wp = get_env(L"PYLAUNCH_DEBUG");
if ((wp != NULL) && (*wp != L'\0'))
log_fp = stderr;
@@ -1483,7 +1492,7 @@ process(int argc, wchar_t ** argv)
plen = wcslen(p);
valid = (*p == L'-') && validate_version(&p[1]);
if (valid) {
- ip = locate_python(&p[1]);
+ ip = locate_python(&p[1], FALSE);
if (ip == NULL)
error(RC_NO_PYTHON, L"Requested Python version (%ls) not \
installed", &p[1]);
@@ -1510,7 +1519,7 @@ installed", &p[1]);
/* If we didn't find one, look for the default Python */
if (executable == NULL) {
- ip = locate_python(L"");
+ ip = locate_python(L"", FALSE);
if (ip == NULL)
error(RC_NO_PYTHON, L"Can't find a default Python.");
executable = ip->executable;