From a3b131a3123035b53df08e3b1220bca3c13521e0 Mon Sep 17 00:00:00 2001 From: arch1t3cht Date: Tue, 1 Nov 2022 20:16:56 +0100 Subject: [PATCH] vapoursynth: Reset locale after initializing On Windows, Python changes the application's locale upon being called, which will break wxwidgets, causing various assertion error dialogs or even crashes (for example when interacting with any sort of float edit control). Saving the locale and restoring it afterwards seems to be the only really possible way to remedy this. --- src/vapoursynth_common.cpp | 6 ++++-- src/vapoursynth_wrap.cpp | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/vapoursynth_common.cpp b/src/vapoursynth_common.cpp index 617849360..52614528c 100644 --- a/src/vapoursynth_common.cpp +++ b/src/vapoursynth_common.cpp @@ -23,15 +23,17 @@ #include int OpenScriptOrVideo(const VSSCRIPTAPI *api, VSScript *script, agi::fs::path const& filename, std::string default_script) { + int result; if (agi::fs::HasExtension(filename, "py") || agi::fs::HasExtension(filename, "vpy")) { - return api->evaluateFile(script, filename.string().c_str()); + result = api->evaluateFile(script, filename.string().c_str()); } else { std::string fname = filename.string(); boost::replace_all(fname, "\\", "\\\\"); boost::replace_all(fname, "'", "\\'"); std::string vscript = "filename = '" + fname + "'\n" + default_script; - return api->evaluateBuffer(script, vscript.c_str(), "aegisub"); + result = api->evaluateBuffer(script, vscript.c_str(), "aegisub"); } + return result; } #endif // WITH_VAPOURSYNTH diff --git a/src/vapoursynth_wrap.cpp b/src/vapoursynth_wrap.cpp index d0d0fbd8b..ac2b87dff 100644 --- a/src/vapoursynth_wrap.cpp +++ b/src/vapoursynth_wrap.cpp @@ -78,7 +78,12 @@ VapourSynthWrapper::VapourSynthWrapper() { if (!getVSScriptAPI) throw VapoursynthError("Failed to get address of getVSScriptAPI from " VSSCRIPT_SO); + // Python will set the program's locale to the user's default locale, which will break + // half of wxwidgets on some operating systems due to locale mismatches. There's not really anything + // we can do to fix it except for saving it and setting it back to its original value afterwards. + std::string oldlocale(setlocale(LC_ALL, NULL)); scriptapi = getVSScriptAPI(VSSCRIPT_API_VERSION); + setlocale(LC_ALL, oldlocale.c_str()); if (!scriptapi) throw VapoursynthError("Failed to get Vapoursynth ScriptAPI");