mercurial/scmwindows.py
changeset 46094 224af78021de
parent 45843 9ac96b9fa76e
child 48966 6000f5b25c9b
--- a/mercurial/scmwindows.py	Wed Dec 09 15:50:59 2020 -0500
+++ b/mercurial/scmwindows.py	Wed Dec 09 18:21:16 2020 -0500
@@ -68,7 +68,7 @@
 
 def userrcpath():
     '''return os-specific hgrc search path to the user dir'''
-    home = os.path.expanduser(b'~')
+    home = _legacy_expanduser(b'~')
     path = [os.path.join(home, b'mercurial.ini'), os.path.join(home, b'.hgrc')]
     userprofile = encoding.environ.get(b'USERPROFILE')
     if userprofile and userprofile != home:
@@ -77,5 +77,37 @@
     return path
 
 
+def _legacy_expanduser(path):
+    """Expand ~ and ~user constructs in the pre 3.8 style"""
+
+    # Python 3.8+ changed the expansion of '~' from HOME to USERPROFILE.  See
+    # https://bugs.python.org/issue36264.  It also seems to capitalize the drive
+    # letter, as though it was processed through os.path.realpath().
+    if not path.startswith(b'~'):
+        return path
+
+    i, n = 1, len(path)
+    while i < n and path[i] not in b'\\/':
+        i += 1
+
+    if b'HOME' in encoding.environ:
+        userhome = encoding.environ[b'HOME']
+    elif b'USERPROFILE' in encoding.environ:
+        userhome = encoding.environ[b'USERPROFILE']
+    elif b'HOMEPATH' not in encoding.environ:
+        return path
+    else:
+        try:
+            drive = encoding.environ[b'HOMEDRIVE']
+        except KeyError:
+            drive = b''
+        userhome = os.path.join(drive, encoding.environ[b'HOMEPATH'])
+
+    if i != 1:  # ~user
+        userhome = os.path.join(os.path.dirname(userhome), path[1:i])
+
+    return userhome + path[i:]
+
+
 def termsize(ui):
     return win32.termsize()