Remove a Python 2-ism and improve type hints. (#11699)
On Python 2, indexing a byte-string gives back a byte-string, while on Python 3 it gives back the ASCII equivalent as an int.pull/11707/head
parent
70ce9aea71
commit
201c48c8de
|
@ -0,0 +1 @@
|
||||||
|
Remove fallback code for Python 2.
|
|
@ -21,6 +21,7 @@ from typing import (
|
||||||
ClassVar,
|
ClassVar,
|
||||||
Dict,
|
Dict,
|
||||||
Mapping,
|
Mapping,
|
||||||
|
Match,
|
||||||
MutableMapping,
|
MutableMapping,
|
||||||
Optional,
|
Optional,
|
||||||
Tuple,
|
Tuple,
|
||||||
|
@ -380,7 +381,7 @@ def map_username_to_mxid_localpart(
|
||||||
onto different mxids
|
onto different mxids
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
unicode: string suitable for a mxid localpart
|
string suitable for a mxid localpart
|
||||||
"""
|
"""
|
||||||
if not isinstance(username, bytes):
|
if not isinstance(username, bytes):
|
||||||
username = username.encode("utf-8")
|
username = username.encode("utf-8")
|
||||||
|
@ -388,29 +389,23 @@ def map_username_to_mxid_localpart(
|
||||||
# first we sort out upper-case characters
|
# first we sort out upper-case characters
|
||||||
if case_sensitive:
|
if case_sensitive:
|
||||||
|
|
||||||
def f1(m):
|
def f1(m: Match[bytes]) -> bytes:
|
||||||
return b"_" + m.group().lower()
|
return b"_" + m.group().lower()
|
||||||
|
|
||||||
username = UPPER_CASE_PATTERN.sub(f1, username)
|
username = UPPER_CASE_PATTERN.sub(f1, username)
|
||||||
else:
|
else:
|
||||||
username = username.lower()
|
username = username.lower()
|
||||||
|
|
||||||
# then we sort out non-ascii characters
|
# then we sort out non-ascii characters by converting to the hex equivalent.
|
||||||
def f2(m):
|
def f2(m: Match[bytes]) -> bytes:
|
||||||
g = m.group()[0]
|
return b"=%02x" % (m.group()[0],)
|
||||||
if isinstance(g, str):
|
|
||||||
# on python 2, we need to do a ord(). On python 3, the
|
|
||||||
# byte itself will do.
|
|
||||||
g = ord(g)
|
|
||||||
return b"=%02x" % (g,)
|
|
||||||
|
|
||||||
username = NON_MXID_CHARACTER_PATTERN.sub(f2, username)
|
username = NON_MXID_CHARACTER_PATTERN.sub(f2, username)
|
||||||
|
|
||||||
# we also do the =-escaping to mxids starting with an underscore.
|
# we also do the =-escaping to mxids starting with an underscore.
|
||||||
username = re.sub(b"^_", b"=5f", username)
|
username = re.sub(b"^_", b"=5f", username)
|
||||||
|
|
||||||
# we should now only have ascii bytes left, so can decode back to a
|
# we should now only have ascii bytes left, so can decode back to a string.
|
||||||
# unicode.
|
|
||||||
return username.decode("ascii")
|
return username.decode("ascii")
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue