annotate mercurial/exewrapper.c @ 48301:9327ece2bc6f

relnotes: add 6.0 release notes Differential Revision: https://phab.mercurial-scm.org/D11736
author Rapha?l Gom?s <rgomes@octobus.net>
date Thu, 04 Nov 2021 12:34:51 +0100
parents 67d14d4e036c
children 2ec5fbe26659
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
1 /*
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
2 exewrapper.c - wrapper for calling a python script on Windows
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
3
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
4 Copyright 2012 Adrian Buehlmann <adrian@cadifra.com> and others
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
5
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
6 This software may be used and distributed according to the terms of the
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
7 GNU General Public License version 2 or any later version.
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
8 */
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
9
40989
ef7119cd4965 py3: enable legacy stdio mode in exewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents: 40407
diff changeset
10 #include <Python.h>
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
11 #include <stdio.h>
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
12 #include <tchar.h>
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
13 #include <windows.h>
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
14
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
15 #include "hgpythonlib.h"
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
16
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
17 #ifdef __GNUC__
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
18 int strcat_s(char *d, size_t n, const char *s)
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
19 {
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
20 return !strncat(d, s, n);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
21 }
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
22 int strcpy_s(char *d, size_t n, const char *s)
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
23 {
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
24 return !strncpy(d, s, n);
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
25 }
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
26
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
27 #define _tcscpy_s strcpy_s
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
28 #define _tcscat_s strcat_s
40407
36ba91e06948 exewrapper: apply clang-format to silence test-check-clang-format.t
Yuya Nishihara <yuya@tcha.org>
parents: 40396
diff changeset
29 #define _countof(array) (sizeof(array) / sizeof(array[0]))
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
30 #endif
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
31
48191
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
32 #if PY_MAJOR_VERSION >= 3
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
33
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
34 #pragma comment(lib, "Advapi32.lib")
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
35
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
36 /* python.org installations */
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
37 #define CORE_PATH L"SOFTWARE\\Python\\PythonCore"
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
38
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
39 /* Microsoft Store installations */
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
40 #define LOOKASIDE_PATH \
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
41 L"SOFTWARE\\Microsoft\\AppModel\\Lookaside\\user\\Software\\Python\\" \
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
42 L"PythonCore"
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
43
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
44 static wchar_t *_locate_python_for_key(HKEY root, LPCWSTR subkey, size_t *size)
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
45 {
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
46 wchar_t installPathKey[512];
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
47 wchar_t *executable = NULL;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
48 DWORD type;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
49 DWORD sz = 0;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
50 HKEY ip_key;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
51 LSTATUS status;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
52
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
53 _snwprintf_s(installPathKey, sizeof(installPathKey), _TRUNCATE,
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
54 L"%ls\\%d.%d\\InstallPath", subkey, PY_MAJOR_VERSION,
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
55 PY_MINOR_VERSION);
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
56
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
57 status =
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
58 RegOpenKeyExW(root, installPathKey, 0, KEY_QUERY_VALUE, &ip_key);
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
59
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
60 if (status != ERROR_SUCCESS)
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
61 return NULL;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
62
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
63 status = RegQueryValueExW(ip_key, L"ExecutablePath", NULL, &type,
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
64 (LPBYTE)executable, &sz);
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
65 if (status == ERROR_SUCCESS) {
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
66 /* Allocate extra space so path\to\python.exe can be converted
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
67 * to path\to\python39.dll + NUL.
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
68 */
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
69 *size = sz + sizeof(_T(HGPYTHONLIB ".dll")) + sizeof(wchar_t);
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
70 executable = malloc(*size);
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
71
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
72 if (executable) {
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
73 status =
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
74 RegQueryValueExW(ip_key, L"ExecutablePath", NULL,
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
75 &type, (LPBYTE)executable, &sz);
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
76
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
77 if (status != ERROR_SUCCESS) {
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
78 free(executable);
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
79 executable = NULL;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
80 } else {
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
81 /* Not all values are stored NUL terminated */
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
82 executable[sz] = L'\0';
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
83 }
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
84 }
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
85 }
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
86
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
87 RegCloseKey(ip_key);
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
88 return executable;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
89 }
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
90
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
91 static HMODULE load_system_py3(void)
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
92 {
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
93 wchar_t *subkeys[] = {CORE_PATH, LOOKASIDE_PATH};
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
94 HKEY roots[] = {HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER};
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
95
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
96 /* Give priority to python.org installs, because MS Store installs can
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
97 * break with user profile corruption, and also use some NTFS feature
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
98 * that MSYS doesn't understand.
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
99 */
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
100 for (int s = 0; s < _countof(subkeys); s++) {
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
101 for (int r = 0; r < _countof(roots); r++) {
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
102 size_t size = 0;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
103 wchar_t *py =
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
104 _locate_python_for_key(roots[r], subkeys[s], &size);
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
105 wchar_t *cut = NULL;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
106 HMODULE pydll;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
107
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
108 if (!py) {
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
109 continue;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
110 }
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
111
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
112 /* Cut off the python executable component */
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
113 cut = wcsrchr(py, L'\\');
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
114 if (cut == NULL) {
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
115 free(py);
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
116 continue;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
117 }
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
118 *cut = 0;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
119
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
120 wcscat_s(py, size, _T("\\" HGPYTHONLIB ".dll"));
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
121
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
122 pydll = LoadLibrary(py);
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
123
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
124 /* Also load python3.dll, so we don't pick up a random
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
125 * one on PATH. We don't search {sys.prefix}\DLLs like
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
126 * python.exe because this is commented as "not been a
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
127 * normal install layout for a while", and don't search
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
128 * LOAD_LIBRARY_SEARCH_APPLICATION_DIR because it's not
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
129 * clear what the use case is.
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
130 */
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
131 if (pydll != NULL) {
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
132 HANDLE py3dll = NULL;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
133
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
134 *cut = 0;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
135 wcscat_s(py, size, L"\\python3.dll");
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
136
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
137 py3dll = LoadLibrary(py);
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
138 if (py3dll == NULL) {
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
139 const char *err;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
140 err = "failed to load python3.dll "
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
141 "for " HGPYTHONLIB ".dll";
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
142 fprintf(stderr, "abort: %s (0x%X)\n",
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
143 err, GetLastError());
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
144 exit(255);
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
145 }
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
146 }
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
147
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
148 free(py);
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
149
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
150 if (pydll != NULL) {
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
151 return pydll;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
152 }
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
153 }
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
154 }
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
155
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
156 return NULL;
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
157 }
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
158 #endif
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
159
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
160 static TCHAR pyscript[MAX_PATH + 10];
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
161 static TCHAR pyhome[MAX_PATH + 10];
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
162 static TCHAR pydllfile[MAX_PATH + 10];
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
163
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
164 int _tmain(int argc, TCHAR *argv[])
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
165 {
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
166 TCHAR *p;
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
167 int ret;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
168 int i;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
169 int n;
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
170 TCHAR **pyargv;
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
171 WIN32_FIND_DATA fdata;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
172 HANDLE hfind;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
173 const char *err;
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
174 HMODULE pydll;
40407
36ba91e06948 exewrapper: apply clang-format to silence test-check-clang-format.t
Yuya Nishihara <yuya@tcha.org>
parents: 40396
diff changeset
175 void(__cdecl * Py_SetPythonHome)(TCHAR * home);
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
176 int(__cdecl * Py_Main)(int argc, TCHAR *argv[]);
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
177
40989
ef7119cd4965 py3: enable legacy stdio mode in exewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents: 40407
diff changeset
178 #if PY_MAJOR_VERSION >= 3
47326
825d5a5907b4 exewrapper: avoid directly linking against python3X.dll
Matt Harbison <matt_harbison@yahoo.com>
parents: 40989
diff changeset
179 _wputenv(L"PYTHONLEGACYWINDOWSSTDIO=1");
40989
ef7119cd4965 py3: enable legacy stdio mode in exewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents: 40407
diff changeset
180 #endif
ef7119cd4965 py3: enable legacy stdio mode in exewrapper
Matt Harbison <matt_harbison@yahoo.com>
parents: 40407
diff changeset
181
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
182 if (GetModuleFileName(NULL, pyscript, _countof(pyscript)) == 0) {
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
183 err = "GetModuleFileName failed";
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
184 goto bail;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
185 }
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
186
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
187 p = _tcsrchr(pyscript, '.');
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
188 if (p == NULL) {
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
189 err = "malformed module filename";
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
190 goto bail;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
191 }
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
192 *p = 0; /* cut trailing ".exe" */
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
193 _tcscpy_s(pyhome, _countof(pyhome), pyscript);
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
194
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
195 hfind = FindFirstFile(pyscript, &fdata);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
196 if (hfind != INVALID_HANDLE_VALUE) {
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
197 /* pyscript exists, close handle */
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
198 FindClose(hfind);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
199 } else {
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
200 /* file pyscript isn't there, take <pyscript>exe.py */
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
201 _tcscat_s(pyscript, _countof(pyscript), _T("exe.py"));
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
202 }
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
203
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
204 pydll = NULL;
31452
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
205
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
206 p = _tcsrchr(pyhome, _T('\\'));
31452
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
207 if (p == NULL) {
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
208 err = "can't find backslash in module filename";
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
209 goto bail;
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
210 }
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
211 *p = 0; /* cut at directory */
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
212
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
213 /* check for private Python of HackableMercurial */
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
214 _tcscat_s(pyhome, _countof(pyhome), _T("\\hg-python"));
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
215
31452
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
216 hfind = FindFirstFile(pyhome, &fdata);
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
217 if (hfind != INVALID_HANDLE_VALUE) {
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
218 /* Path .\hg-python exists. We are probably in HackableMercurial
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
219 scenario, so let's load python dll from this dir. */
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
220 FindClose(hfind);
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
221 _tcscpy_s(pydllfile, _countof(pydllfile), pyhome);
40407
36ba91e06948 exewrapper: apply clang-format to silence test-check-clang-format.t
Yuya Nishihara <yuya@tcha.org>
parents: 40396
diff changeset
222 _tcscat_s(pydllfile, _countof(pydllfile),
36ba91e06948 exewrapper: apply clang-format to silence test-check-clang-format.t
Yuya Nishihara <yuya@tcha.org>
parents: 40396
diff changeset
223 _T("\\") _T(HGPYTHONLIB) _T(".dll"));
31452
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
224 pydll = LoadLibrary(pydllfile);
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
225 if (pydll == NULL) {
40407
36ba91e06948 exewrapper: apply clang-format to silence test-check-clang-format.t
Yuya Nishihara <yuya@tcha.org>
parents: 40396
diff changeset
226 err = "failed to load private Python DLL " HGPYTHONLIB
36ba91e06948 exewrapper: apply clang-format to silence test-check-clang-format.t
Yuya Nishihara <yuya@tcha.org>
parents: 40396
diff changeset
227 ".dll";
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
228 goto bail;
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
229 }
34636
31c6c4d27be7 exewrapper: format with clang-format
Augie Fackler <augie@google.com>
parents: 31452
diff changeset
230 Py_SetPythonHome =
31c6c4d27be7 exewrapper: format with clang-format
Augie Fackler <augie@google.com>
parents: 31452
diff changeset
231 (void *)GetProcAddress(pydll, "Py_SetPythonHome");
31452
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
232 if (Py_SetPythonHome == NULL) {
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
233 err = "failed to get Py_SetPythonHome";
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
234 goto bail;
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
235 }
31452
0241dd94ed38 exewrapper: prefer HackableMercurial python if availbale
Kostia Balytskyi <ikostia@fb.com>
parents: 29019
diff changeset
236 Py_SetPythonHome(pyhome);
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
237 }
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
238
48191
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
239 #if PY_MAJOR_VERSION >= 3
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
240 if (pydll == NULL) {
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
241 pydll = load_system_py3();
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
242 }
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
243 #endif
67d14d4e036c exewrapper: find the proper python3X.dll in the registry
Matt Harbison <matt_harbison@yahoo.com>
parents: 47326
diff changeset
244
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
245 if (pydll == NULL) {
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
246 pydll = LoadLibrary(_T(HGPYTHONLIB) _T(".dll"));
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
247 if (pydll == NULL) {
26662
d215def59c3b exewrapper: report name of failed DLL in error message
Adrian Buehlmann <adrian@cadifra.com>
parents: 17732
diff changeset
248 err = "failed to load Python DLL " HGPYTHONLIB ".dll";
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
249 goto bail;
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
250 }
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
251 }
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
252
34636
31c6c4d27be7 exewrapper: format with clang-format
Augie Fackler <augie@google.com>
parents: 31452
diff changeset
253 Py_Main = (void *)GetProcAddress(pydll, "Py_Main");
17732
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
254 if (Py_Main == NULL) {
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
255 err = "failed to get Py_Main";
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
256 goto bail;
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
257 }
93d97a212559 exewrapper: adapt for legacy HackableMercurial
Adrian Buehlmann <adrian@cadifra.com>
parents: 17063
diff changeset
258
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
259 /*
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
260 Only add the pyscript to the args, if it's not already there. It may
17063
3fbc6e3abdbd exewrapper: use generic term script
Adrian Buehlmann <adrian@cadifra.com>
parents: 17058
diff changeset
261 already be there, if the script spawned a child process of itself, in
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
262 the same way as it got called, that is, with the pyscript already in
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
263 place. So we optionally accept the pyscript as the first argument
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
264 (argv[1]), letting our exe taking the role of the python interpreter.
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
265 */
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
266 if (argc >= 2 && _tcscmp(argv[1], pyscript) == 0) {
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
267 /*
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
268 pyscript is already in the args, so there is no need to copy
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
269 the args and we can directly call the python interpreter with
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
270 the original args.
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
271 */
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
272 return Py_Main(argc, argv);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
273 }
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
274
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
275 /*
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
276 Start assembling the args for the Python interpreter call. We put the
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
277 name of our exe (argv[0]) in the position where the python.exe
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
278 canonically is, and insert the pyscript next.
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
279 */
40396
973ff03d9bc0 exewrapper: convert to _tcsxxx functions for Unicode compatability
Matt Harbison <matt_harbison@yahoo.com>
parents: 40395
diff changeset
280 pyargv = malloc((argc + 5) * sizeof(TCHAR *));
17058
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
281 if (pyargv == NULL) {
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
282 err = "not enough memory";
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
283 goto bail;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
284 }
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
285 n = 0;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
286 pyargv[n++] = argv[0];
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
287 pyargv[n++] = pyscript;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
288
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
289 /* copy remaining args from the command line */
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
290 for (i = 1; i < argc; i++)
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
291 pyargv[n++] = argv[i];
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
292 /* argv[argc] is guaranteed to be NULL, so we forward that guarantee */
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
293 pyargv[n] = NULL;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
294
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
295 ret = Py_Main(n, pyargv); /* The Python interpreter call */
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
296
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
297 free(pyargv);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
298 return ret;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
299
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
300 bail:
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
301 fprintf(stderr, "abort: %s\n", err);
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
302 return 255;
d5422faf648c exewrapper: adding new exewrapper.c
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff changeset
303 }