Skip to content

Instantly share code, notes, and snippets.

@methane
Created June 28, 2023 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save methane/2ff94584229de30eb4691d76d13df135 to your computer and use it in GitHub Desktop.
Save methane/2ff94584229de30eb4691d76d13df135 to your computer and use it in GitHub Desktop.
diff -r ffdfb1066ac6 contrib/win32/hg.bat
--- a/contrib/win32/hg.bat Thu Mar 02 15:34:45 2023 +0100
+++ b/contrib/win32/hg.bat Wed Jun 28 18:53:44 2023 +0900
@@ -5,6 +5,7 @@
set HG=%~f0
set PYTHONLEGACYWINDOWSSTDIO=1
+set PYTHONLEGACYWINDOWSFSENCODING=1
rem Use a full path to Python (relative to this script) if it exists,
rem as the standard Python install does not put python.exe on the PATH...
diff -r ffdfb1066ac6 mercurial/exewrapper.c
--- a/mercurial/exewrapper.c Thu Mar 02 15:34:45 2023 +0100
+++ b/mercurial/exewrapper.c Wed Jun 28 18:53:44 2023 +0900
@@ -49,6 +49,7 @@
#if PY_MAJOR_VERSION >= 3
_wputenv(L"PYTHONLEGACYWINDOWSSTDIO=1");
+ _wputenv(L"PYTHONLEGACYWINDOWSFSENCODING=1");
#endif
if (GetModuleFileName(NULL, pyscript, _countof(pyscript)) == 0) {
diff -r ffdfb1066ac6 mercurial/pycompat.py
--- a/mercurial/pycompat.py Thu Mar 02 15:34:45 2023 +0100
+++ b/mercurial/pycompat.py Wed Jun 28 18:53:44 2023 +0900
@@ -110,13 +110,6 @@
return _rapply(f, xs)
-if os.name == r'nt':
- # MBCS (or ANSI) filesystem encoding must be used as before.
- # Otherwise non-ASCII filenames in existing repositories would be
- # corrupted.
- # This must be set once prior to any fsencode/fsdecode calls.
- sys._enablelegacywindowsfsencoding() # pytype: disable=module-attr
-
fsencode = os.fsencode
fsdecode = os.fsdecode
oscurdir: bytes = os.curdir.encode('ascii')
@@ -171,17 +164,11 @@
#
# On Windows, the wchar_t **argv is passed into the interpreter as-is.
# Like POSIX, we need to emulate what Py_EncodeLocale() would do. But
- # there's an additional wrinkle. What we really want to access is the
- # ANSI codepage representation of the arguments, as this is what
- # `int main()` would receive if Python 3 didn't define `int wmain()`
- # (this is how Python 2 worked). To get that, we encode with the mbcs
- # encoding, which will pass CP_ACP to the underlying Windows API to
- # produce bytes.
- sysargv: List[bytes] = []
- if os.name == r'nt':
- sysargv = [a.encode("mbcs", "ignore") for a in sys.argv]
- else:
- sysargv = [fsencode(a) for a in sys.argv]
+ # When PYTHONLEGACYWINDOWSFSENCODING is set, fsencde/fsdecode uses
+ # ANSI codepage. This is backward compatible.
+ # Otherwise, Python uses UTF-8 on Windows too. This is good for hg-git
+ # and new repositories.
+ sysargv: List[bytes] = [fsencode(a) for a in sys.argv]
bytechr = struct.Struct('>B').pack
byterepr = b'%r'.__mod__
diff -r ffdfb1066ac6 tests/run-tests.py
--- a/tests/run-tests.py Thu Mar 02 15:34:45 2023 +0100
+++ b/tests/run-tests.py Wed Jun 28 18:53:44 2023 +0900
@@ -1462,6 +1462,7 @@
# dummysmtpd.py, and dumbhttp.py.
if WINDOWS:
env['PYTHONLEGACYWINDOWSSTDIO'] = '1'
+ env['PYTHONLEGACYWINDOWSFSENCODING'] = '1'
# Modified HOME in test environment can confuse Rust tools. So set
# CARGO_HOME and RUSTUP_HOME automatically if a Rust toolchain is
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment