Patchwork py3: enable legacy fs encoding to fix filename compatibility on Windows

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 15, 2019, 2:07 p.m.
Message ID <f982758100f4a0de2e90.1571148475@mimosa>
Download mbox | patch
Permalink /patch/42358/
State New
Headers show

Comments

Yuya Nishihara - Oct. 15, 2019, 2:07 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1571147095 -32400
#      Tue Oct 15 22:44:55 2019 +0900
# Node ID f982758100f4a0de2e902b56a3a96ff924a6f32f
# Parent  0d609ed185ea3847bfd6a5ec89d1c8efa373fdbb
py3: enable legacy fs encoding to fix filename compatibility on Windows

This patch is untested. I just followed the instruction:

https://docs.python.org/3/whatsnew/3.6.html#pep-529-change-windows-filesystem-encoding-to-utf-8

Patch

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -92,6 +92,13 @@  if ispy3:
     import io
     import struct
 
+    if os.name == r'nt' and sys.version_info >= (3, 6):
+        # 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()
+
     fsencode = os.fsencode
     fsdecode = os.fsdecode
     oscurdir = os.curdir.encode('ascii')
@@ -137,8 +144,8 @@  if ispy3:
     #
     # https://hg.python.org/cpython/file/v3.5.1/Programs/python.c#l55
     #
-    # TODO: On Windows, the native argv is wchar_t, so we'll need a different
-    # workaround to simulate the Python 2 (i.e. ANSI Win32 API) behavior.
+    # On Windows, the native argv is unicode and is converted to MBCS bytes
+    # since we do enable the legacy filesystem encoding.
     if getattr(sys, 'argv', None) is not None:
         sysargv = list(map(os.fsencode, sys.argv))