Fix string issues related to switch to python3

safe_str is no longer relevant after unicode strings are no longer explicit string types.
Popen takes bytes by default now, not strings, which is fixed by setting an encoding
sys.stdout.write should work with strings on linux at least. We only use it for the buy menu timeout, which already only works on linux, so it shouldn't be a problem.
This commit is contained in:
Robert Maikher 2018-08-24 13:43:35 +02:00
parent 0ea9042820
commit 1675f26080
3 changed files with 10 additions and 15 deletions

View File

@ -113,14 +113,6 @@ def argmax(d, all=False, value=None):
return [k for k in list(d.keys()) if d[k] == d[maxarg]] return [k for k in list(d.keys()) if d[k] == d[maxarg]]
return maxarg return maxarg
def safe_str(obj):
'''
Ugly hack to avoid Python complaining about encodings.
Call this on any object to turn it into a string which is
(hopefully) safe for printing.
'''
return obj
def less(string): def less(string):
''' '''
@ -131,6 +123,6 @@ def less(string):
int_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) int_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
env = dict(os.environ) env = dict(os.environ)
env['LESSSECURE'] = '1' env['LESSSECURE'] = '1'
proc = subprocess.Popen('less', env=env, stdin=subprocess.PIPE) proc = subprocess.Popen('less', env=env, encoding='utf-8', stdin=subprocess.PIPE)
proc.communicate(safe_str(string)) proc.communicate(string)
signal.signal(signal.SIGINT, int_handler) signal.signal(signal.SIGINT, int_handler)

View File

@ -160,6 +160,10 @@ When finished, write an empty line to confirm the purchase.\n'''
print(f'USER {t.user.name} HAS LOWER CREDIT THAN {conf.low_credit_warning_limit:d},', print(f'USER {t.user.name} HAS LOWER CREDIT THAN {conf.low_credit_warning_limit:d},',
'AND SHOULD CONSIDER PUTTING SOME MONEY IN THE BOX.') 'AND SHOULD CONSIDER PUTTING SOME MONEY IN THE BOX.')
# Superfast mode skips a linebreak for some reason. This looks ugly.
# TODO: Figure out why this happens, and fix it in a less hacky way.
if self.superfast_mode:
print("")
return True return True
def complete_input(self): def complete_input(self):

View File

@ -9,7 +9,7 @@ from select import select
import conf import conf
from db import User, Session from db import User, Session
from helpers import search_user, search_product, safe_str, guess_data_type, argmax from helpers import search_user, search_product, guess_data_type, argmax
from text_interface import context_commands, local_help_commands, help_commands, \ from text_interface import context_commands, local_help_commands, help_commands, \
exit_commands exit_commands
@ -118,11 +118,9 @@ class Menu(object):
return result return result
while True: while True:
try: try:
# result = None
# It is replaced either way
if timeout: if timeout:
# assuming line buffering # assuming line buffering
sys.stdout.write(safe_str(prompt)) sys.stdout.write(prompt)
sys.stdout.flush() sys.stdout.flush()
rlist, _, _ = select([sys.stdin], [], [], timeout) rlist, _, _ = select([sys.stdin], [], [], timeout)
if not rlist: if not rlist:
@ -131,7 +129,7 @@ class Menu(object):
else: else:
result = input().strip() result = input().strip()
else: else:
result = input(safe_str(prompt)).strip() result = input(prompt).strip()
except EOFError: except EOFError:
print('quit') print('quit')
self.exit_menu() self.exit_menu()
@ -207,6 +205,7 @@ class Menu(object):
print('Please enter a valid choice.') print('Please enter a valid choice.')
def input_int(self, prompt=None, allowed_range=(None, None), null_allowed=False, default=None): def input_int(self, prompt=None, allowed_range=(None, None), null_allowed=False, default=None):
# TODO: Proper default handling
if prompt is None: if prompt is None:
prompt = self.prompt prompt = self.prompt
while True: while True: