On Windows, os.environ normalizes environment variables to uppercase. Our
current bytes-based environ substitution object is a simple dict, so we add
the normalization behavior.
This fixes test-http-peer.t on Windows.
( )
marmoute |
hg-reviewers |
On Windows, os.environ normalizes environment variables to uppercase. Our
current bytes-based environ substitution object is a simple dict, so we add
the normalization behavior.
This fixes test-http-peer.t on Windows.
No Linters Available |
No Unit Test Coverage |
Path | Packages | |||
---|---|---|---|---|
M | mercurial/encoding.py (118 lines) |
Commit | Parents | Author | Summary | Date |
---|---|---|---|---|
4b0a80029d16 | e22c8d7b2f2c | Raphaël Gomès | Jun 9 2021, 10:51 AM |
return s # pytype: disable=bad-return-type | return s # pytype: disable=bad-return-type | ||||
def strfromlocal(s): | def strfromlocal(s): | ||||
# type: (bytes) -> str | # type: (bytes) -> str | ||||
return s # pytype: disable=bad-return-type | return s # pytype: disable=bad-return-type | ||||
strmethod = pycompat.identity | strmethod = pycompat.identity | ||||
def lower(s): | |||||
# type: (bytes) -> bytes | |||||
"""best-effort encoding-aware case-folding of local string s""" | |||||
try: | |||||
return asciilower(s) | |||||
except UnicodeDecodeError: | |||||
pass | |||||
try: | |||||
if isinstance(s, localstr): | |||||
u = s._utf8.decode("utf-8") | |||||
else: | |||||
u = s.decode(_sysstr(encoding), _sysstr(encodingmode)) | |||||
lu = u.lower() | |||||
if u == lu: | |||||
return s # preserve localstring | |||||
return lu.encode(_sysstr(encoding)) | |||||
except UnicodeError: | |||||
return s.lower() # we don't know how to fold this except in ASCII | |||||
except LookupError as k: | |||||
raise error.Abort(k, hint=b"please check your locale settings") | |||||
def upper(s): | |||||
# type: (bytes) -> bytes | |||||
"""best-effort encoding-aware case-folding of local string s""" | |||||
try: | |||||
return asciiupper(s) | |||||
except UnicodeDecodeError: | |||||
return upperfallback(s) | |||||
def upperfallback(s): | |||||
# type: (Any) -> Any | |||||
try: | |||||
if isinstance(s, localstr): | |||||
u = s._utf8.decode("utf-8") | |||||
else: | |||||
u = s.decode(_sysstr(encoding), _sysstr(encodingmode)) | |||||
uu = u.upper() | |||||
if u == uu: | |||||
return s # preserve localstring | |||||
return uu.encode(_sysstr(encoding)) | |||||
except UnicodeError: | |||||
return s.upper() # we don't know how to fold this except in ASCII | |||||
except LookupError as k: | |||||
raise error.Abort(k, hint=b"please check your locale settings") | |||||
if not _nativeenviron: | if not _nativeenviron: | ||||
# now encoding and helper functions are available, recreate the environ | # now encoding and helper functions are available, recreate the environ | ||||
# dict to be exported to other modules | # dict to be exported to other modules | ||||
environ = { | if pycompat.iswindows and pycompat.ispy3: | ||||
tolocal(k.encode('utf-8')): tolocal(v.encode('utf-8')) | |||||
for k, v in os.environ.items() # re-exports | class WindowsEnviron(dict): | ||||
} | """`os.environ` normalizes environment variables to uppercase on windows""" | ||||
def get(self, key, default=None): | |||||
return super().get(upper(key), default) | |||||
environ = WindowsEnviron() | |||||
for k, v in os.environ.items(): # re-exports | |||||
environ[tolocal(k.encode('utf-8'))] = tolocal(v.encode('utf-8')) | |||||
if pycompat.ispy3: | if pycompat.ispy3: | ||||
# os.getcwd() on Python 3 returns string, but it has os.getcwdb() which | # os.getcwd() on Python 3 returns string, but it has os.getcwdb() which | ||||
# returns bytes. | # returns bytes. | ||||
if pycompat.iswindows: | if pycompat.iswindows: | ||||
# Python 3 on Windows issues a DeprecationWarning about using the bytes | # Python 3 on Windows issues a DeprecationWarning about using the bytes | ||||
# API when os.getcwdb() is called. | # API when os.getcwdb() is called. | ||||
# | # | ||||
concat = lambda s: s + ellipsis | concat = lambda s: s + ellipsis | ||||
for i in pycompat.xrange(1, len(u)): | for i in pycompat.xrange(1, len(u)): | ||||
usub = uslice(i) | usub = uslice(i) | ||||
if ucolwidth(usub) <= width: | if ucolwidth(usub) <= width: | ||||
return concat(usub.encode(_sysstr(encoding))) | return concat(usub.encode(_sysstr(encoding))) | ||||
return ellipsis # no enough room for multi-column characters | return ellipsis # no enough room for multi-column characters | ||||
def lower(s): | |||||
# type: (bytes) -> bytes | |||||
"""best-effort encoding-aware case-folding of local string s""" | |||||
try: | |||||
return asciilower(s) | |||||
except UnicodeDecodeError: | |||||
pass | |||||
try: | |||||
if isinstance(s, localstr): | |||||
u = s._utf8.decode("utf-8") | |||||
else: | |||||
u = s.decode(_sysstr(encoding), _sysstr(encodingmode)) | |||||
lu = u.lower() | |||||
if u == lu: | |||||
return s # preserve localstring | |||||
return lu.encode(_sysstr(encoding)) | |||||
except UnicodeError: | |||||
return s.lower() # we don't know how to fold this except in ASCII | |||||
except LookupError as k: | |||||
raise error.Abort(k, hint=b"please check your locale settings") | |||||
def upper(s): | |||||
# type: (bytes) -> bytes | |||||
"""best-effort encoding-aware case-folding of local string s""" | |||||
try: | |||||
return asciiupper(s) | |||||
except UnicodeDecodeError: | |||||
return upperfallback(s) | |||||
def upperfallback(s): | |||||
# type: (Any) -> Any | |||||
try: | |||||
if isinstance(s, localstr): | |||||
u = s._utf8.decode("utf-8") | |||||
else: | |||||
u = s.decode(_sysstr(encoding), _sysstr(encodingmode)) | |||||
uu = u.upper() | |||||
if u == uu: | |||||
return s # preserve localstring | |||||
return uu.encode(_sysstr(encoding)) | |||||
except UnicodeError: | |||||
return s.upper() # we don't know how to fold this except in ASCII | |||||
except LookupError as k: | |||||
raise error.Abort(k, hint=b"please check your locale settings") | |||||
class normcasespecs(object): | class normcasespecs(object): | ||||
"""what a platform's normcase does to ASCII strings | """what a platform's normcase does to ASCII strings | ||||
This is specified per platform, and should be consistent with what normcase | This is specified per platform, and should be consistent with what normcase | ||||
on that platform actually does. | on that platform actually does. | ||||
lower: normcase lowercases ASCII strings | lower: normcase lowercases ASCII strings | ||||
upper: normcase uppercases ASCII strings | upper: normcase uppercases ASCII strings |