This commit is contained in:
恍兮惚兮 2024-05-06 19:37:31 +08:00
parent f0773fd5be
commit a237c5e0d5

View File

@ -266,7 +266,19 @@ const auto init_script =
L" ctypes.windll.user32.MessageBoxW(None, str(msg), str(info), 0)\n"
L" return 0\n"
L"os.MessageBox = MessageBox\n"
#ifndef PYSTAND_CONSOLE
L"try:\n"
L" fd = os.open('CONOUT$', os.O_RDWR | os.O_BINARY, encoding='utf8')\n"
L" fp = os.fdopen(fd, 'w', encoding='utf8')\n"
L" sys.stdout = fp\n"
L" sys.stderr = fp\n"
L" attached = True\n"
L"except Exception as e:\n"
L" fp = open(os.devnull, 'w', encoding='utf8')\n"
L" sys.stdout = fp\n"
L" sys.stderr = fp\n"
L" attached = False\n"
#endif
L"sys.argv = [os.environ['LUNA_EXE_NAME'] ,sys.argv[0], PYSTAND_SCRIPT] + sys.argv[1:]\n"
L"text = open(PYSTAND_SCRIPT, 'rb').read()\n"
L"environ = {'__file__': PYSTAND_SCRIPT, '__name__': '__main__'}\n"
@ -276,6 +288,8 @@ const auto init_script =
L" code = compile(text, PYSTAND_SCRIPT, 'exec')\n"
L" exec(code, environ)\n"
L"except Exception:\n"
L" if attached:\n"
L" raise\n"
L" import traceback, io\n"
L" sio = io.StringIO()\n"
L" traceback.print_exc(file = sio)\n"