Skip to content

Commit 01a8607

Browse files
committed
Fix Windows argv mojibake bug and add municode compiler flags
1 parent a64c8da commit 01a8607

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ Thumbs.db
6666
.env
6767
.env.*
6868
!.env.example
69+
test.py

py2c_windows.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,20 @@ def make_wrapper(mode: str, pyver: str, sitepackages_value: str, root_value: str
112112
_putenv_s("PYTHONTRACEMALLOC", "1");
113113
snprintf(path, sizeof(path), "{root_value}\\\\src;{sitepackages_value}");
114114
_putenv_s("PYTHONPATH", path);
115-
fprintf(stderr, "[py2c-debug] exe=%s\\n", argv[0]);
115+
fprintf(stderr, "[py2c-debug] exe=%ls\\n", argv[0]);
116116
fprintf(stderr, "[py2c-debug] PYTHONPATH=%s\\n", path);
117117
"""
118118
return f"""#include <stdio.h>
119119
#include <stdlib.h>
120120
#include <string.h>
121121
#include <windows.h>
122122
123-
int __real_main(int argc, char **argv);
123+
int __real_main(int argc, wchar_t **argv);
124124
125-
static void exe_dir(char *out, size_t size, char **argv) {{
125+
static void exe_dir(char *out, size_t size) {{
126126
DWORD n = GetModuleFileNameA(NULL, out, (DWORD)size - 1);
127127
if (n == 0) {{
128-
strncpy(out, argv[0], size - 1);
129-
out[size - 1] = '\\0';
128+
strcpy(out, ".");
130129
}} else {{
131130
out[n] = '\\0';
132131
}}
@@ -136,10 +135,10 @@ def make_wrapper(mode: str, pyver: str, sitepackages_value: str, root_value: str
136135
else strcpy(out, ".");
137136
}}
138137
139-
int main(int argc, char **argv) {{
138+
int wmain(int argc, wchar_t **argv) {{
140139
char dir[4096];
141140
char path[8192];
142-
exe_dir(dir, sizeof(dir), argv);
141+
exe_dir(dir, sizeof(dir));
143142
{runtime_setup}
144143
return __real_main(argc, argv);
145144
}}
@@ -163,6 +162,7 @@ def compile_binary(mode: str, verbose: bool) -> Path:
163162
cmd = [
164163
"cl",
165164
"/nologo",
165+
"/municode",
166166
*flags,
167167
str(main_c),
168168
str(wrapper_c),
@@ -176,6 +176,7 @@ def compile_binary(mode: str, verbose: bool) -> Path:
176176
flags = ["-O3", "-DNDEBUG"] if mode == "release" else ["-O0", "-g3", "-DDEBUG"]
177177
cmd = [
178178
"gcc",
179+
"-municode",
179180
str(main_c),
180181
str(wrapper_c),
181182
*flags,

0 commit comments

Comments
 (0)