Merge branch 'fix-accents' of https://github.com/vincent-lg/evennia into vincent-lg-fix-accents

This commit is contained in:
Griatch 2019-01-15 20:33:31 +01:00
commit fbe536f00c
4 changed files with 40 additions and 5 deletions

View file

@ -638,7 +638,7 @@ class AMPLauncherProtocol(amp.AMP):
else:
status = pickle.loads(status)
callback(status)
return {b"status": b""}
return {"status": pickle.dumps(b"")}
def send_instruction(operation, arguments, callback=None, errback=None):

View file

@ -73,11 +73,11 @@ Content-Type: text/html
# Helper functions for pickling.
def dumps(data):
return to_str(pickle.dumps(to_str(data), pickle.HIGHEST_PROTOCOL))
return pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
def loads(data):
return pickle.loads(to_str(data))
return pickle.loads(data)
@wraps

View file

@ -243,7 +243,7 @@ class TelnetProtocol(Telnet, StatefulTelnetProtocol, Session):
line (str): Line to send.
"""
line = bytes(line, 'utf-8')
line = self.try_encode(line)
# escape IAC in line mode, and correctly add \r\n (the TELNET end-of-line)
line = line.replace(IAC, IAC + IAC)
line = line.replace(b'\n', b'\r\n')
@ -343,7 +343,7 @@ class TelnetProtocol(Telnet, StatefulTelnetProtocol, Session):
strip_ansi=nocolor, xterm256=xterm256)
if mxp:
prompt = mxp_parse(prompt)
prompt = prompt.encode()
prompt = self.try_encode(prompt)
prompt = prompt.replace(IAC, IAC + IAC).replace(b'\n', b'\r\n')
prompt += IAC + GA
self.transport.write(mccp_compress(self, prompt))

View file

@ -135,6 +135,41 @@ class Session(object):
if self.account:
self.protocol_flags.update(self.account.attributes.get("_saved_protocol_flags", {}))
# helpers
def try_encode(self, text):
"""
Try to encode the given text, following the session's protocol flag.
Args:
text (str or bytes): the text to encode to bytes.
Returns:
encoded_text (bytes): the encoded text following the session's
protocol flag. If the converting fails, log the error
and send the text with "?" in place of problematic
characters. If the specified encoding cannot be found,
the protocol flag is reset to utf-8.
In any case, returns bytes.
Note:
If the argument is bytes, return it as is.
"""
if isinstance(text, bytes):
return text
try:
encoded = text.encode(self.protocol_flags["ENCODING"])
except LookupError:
self.protocol_flags["ENCODING"] = 'utf-8'
encoded = text.encode('utf-8')
except UnicodeEncodeError:
print("An error occurred during string encoding to {encoding}. Will remove errors and try again.".format(encoding=self.protocol_flags["ENCODING"]))
encoded = text.encode(self.protocol_flags["ENCODING"], errors="replace")
return encoded
# access hooks
def disconnect(self, reason=None):