Skip to content

Commit d7ee9cf

Browse files
author
skywind3000
committed
fixed: python local environment issues
1 parent 963ea74 commit d7ee9cf

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

PyStand.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ bool PyStand::CheckEnviron(const wchar_t *rtp)
9999

100100
// init: _pystand (full path of PyStand.exe)
101101
GetModuleFileNameW(NULL, path, MAX_PATH + 1);
102+
#if 0
103+
wsprintf(path, L"e:\\github\\tools\\pystand\\pystand.exe");
104+
#endif
102105
_pystand = path;
103106

104107
// init: _home
@@ -201,23 +204,23 @@ int PyStand::RunString(const wchar_t *script)
201204
}
202205
int hr = 0;
203206
int i;
204-
std::vector<std::wstring> argv;
207+
_py_argv.resize(0);
205208
// init arguments
206-
argv.push_back(_argv[0]);
207-
argv.push_back(L"-I");
208-
argv.push_back(L"-s");
209-
argv.push_back(L"-S");
210-
argv.push_back(L"-c");
211-
argv.push_back(script);
209+
_py_argv.push_back(_argv[0]);
210+
_py_argv.push_back(L"-I");
211+
_py_argv.push_back(L"-s");
212+
_py_argv.push_back(L"-S");
213+
_py_argv.push_back(L"-c");
214+
_py_argv.push_back(script);
212215
for (i = 1; i < (int)_argv.size(); i++) {
213-
argv.push_back(_argv[i]);
216+
_py_argv.push_back(_argv[i]);
214217
}
215218
// finalize arguments
216-
std::vector<wchar_t*> real_argv;
217-
for (i = 0; i < (int)argv.size(); i++) {
218-
real_argv.push_back((wchar_t*)argv[i].c_str());
219+
_py_args.resize(0);
220+
for (i = 0; i < (int)_py_argv.size(); i++) {
221+
_py_args.push_back((wchar_t*)_py_argv[i].c_str());
219222
}
220-
hr = _Py_Main((int)argv.size(), &real_argv[0]);
223+
hr = _Py_Main((int)_py_args.size(), &_py_args[0]);
221224
return hr;
222225
}
223226

@@ -300,12 +303,9 @@ const char *init_script =
300303
"sys.path.append(os.path.abspath(PYSTAND_HOME))\n"
301304
"sys.argv = [PYSTAND_SCRIPT] + sys.argv[1:]\n"
302305
"text = open(PYSTAND_SCRIPT).read()\n"
303-
"environ = {'__file__': PYSTAND_SCRIPT}\n"
304-
"saveloc = copy.copy(locals())\n"
305-
"for k in saveloc:\n"
306-
" if k.startswith('__'):\n"
307-
" environ[k] = saveloc[k]\n"
308-
"exec(text, globals(), environ)\n"
306+
"environ = {'__file__': PYSTAND_SCRIPT, '__name__': '__main__'}\n"
307+
"environ['__package__'] = None\n"
308+
"exec(text, environ)\n"
309309
"";
310310

311311

@@ -317,14 +317,15 @@ const char *init_script =
317317
//! src:
318318
//! mode: win
319319
//! int: objs
320-
int APIENTRY
320+
int WINAPI
321321
WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int show)
322322
{
323323
PyStand ps("runtime");
324324
if (ps.DetectScript() != 0) {
325325
return 3;
326326
}
327327
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
328+
#if 0
328329
freopen("CONOUT$", "w", stdout);
329330
freopen("CONOUT$", "w", stderr);
330331
freopen("CONIN$", "r", stdin);
@@ -338,6 +339,7 @@ WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int show)
338339
std::string fn = std::to_string(fd);
339340
SetEnvironmentVariableA("PYSTAND_STDIN", fn.c_str());
340341
}
342+
#endif
341343
}
342344
int hr = ps.RunString(init_script);
343345
// printf("finalize\n");

PyStand.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class PyStand
5252
std::wstring _home; // home directory of PyStand.exe
5353
std::wstring _script; // init script like PyStand.int or PyStand.py
5454
std::vector<std::wstring> _argv;
55+
std::vector<std::wstring> _py_argv;
56+
std::vector<wchar_t*> _py_args;
5557
};
5658

5759

0 commit comments

Comments
 (0)