From 3fb4d66b584beb3fc91fc22708cd83b125b567d9 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 16 Jan 2021 10:49:06 +0100 Subject: helper: get rid of decode_string() It just adds unnecessary type confusion. In most places where it is used, the types are always str, so it does nothing. In the few others, the encoding/decoding is better handled explicitly. --- alot/commands/__init__.py | 3 +-- alot/commands/envelope.py | 5 ++--- alot/helper.py | 32 ++------------------------------ alot/settings/manager.py | 7 +++---- alot/widgets/globals.py | 3 --- 5 files changed, 8 insertions(+), 42 deletions(-) diff --git a/alot/commands/__init__.py b/alot/commands/__init__.py index 1e16b4d4..61ff7f21 100644 --- a/alot/commands/__init__.py +++ b/alot/commands/__init__.py @@ -8,7 +8,7 @@ import os import re from ..settings.const import settings -from ..helper import split_commandstring, string_decode +from ..helper import split_commandstring class Command: @@ -167,7 +167,6 @@ def commandfactory(cmdline, mode='global'): args = split_commandstring(cmdline) except ValueError as e: raise CommandParseError(str(e)) - args = [string_decode(x, 'utf-8') for x in args] logging.debug('ARGS: %s', args) cmdname = args[0] args = args[1:] diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py index 914829eb..1f806581 100644 --- a/alot/commands/envelope.py +++ b/alot/commands/envelope.py @@ -23,7 +23,6 @@ from .. import crypto from ..account import SendingMailFailed, StoreMailError from ..db.errors import DatabaseError from ..errors import GPGProblem -from ..helper import string_decode from ..settings.const import settings from ..settings.errors import NoMatchingAccount from ..utils import argparse as cargparse @@ -357,8 +356,8 @@ class EditCommand(Command): # get input # tempfile will be removed on buffer cleanup enc = settings.get('editor_writes_encoding') - with open(self.envelope.tmpfile.name) as f: - template = string_decode(f.read(), enc) + with open(self.envelope.tmpfile.name, 'rb') as f: + template = f.read().decode(enc) # call post-edit translate hook translate = settings.get_hook('post_edit_translate') diff --git a/alot/helper.py b/alot/helper.py index 5800841d..27652988 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -46,31 +46,6 @@ def split_commandstring(cmdstring): assert isinstance(cmdstring, str) return shlex.split(cmdstring) - -def string_decode(string, enc='ascii'): - """ - safely decodes string to unicode bytestring, respecting `enc` as a hint. - - :param string: the string to decode - :type string: str or unicode - :param enc: a hint what encoding is used in string ('ascii', 'utf-8', ...) - :type enc: str - :returns: the unicode decoded input string - :rtype: unicode - - """ - - if enc is None: - enc = 'ascii' - try: - string = str(string, enc, errors='replace') - except LookupError: # malformed enc string - string = string.decode('ascii', errors='replace') - except TypeError: # already str - pass - return string - - def shorten(string, maxlen): """shortens string if longer than maxlen, appending ellipsis""" if 1 < maxlen < len(string): @@ -173,26 +148,23 @@ def call_cmd(cmdlist, stdin=None): :rtype: str, str, int """ termenc = urwid.util.detected_encoding - if isinstance(stdin, str): - stdin = stdin.encode(termenc) try: logging.debug("Calling %s" % cmdlist) proc = subprocess.Popen( cmdlist, + encoding = termenc, errors = 'backslashreplace', stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE if stdin is not None else None) except OSError as e: - out = b'' + out = '' err = e.strerror ret = e.errno else: out, err = proc.communicate(stdin) ret = proc.returncode - out = string_decode(out, termenc) - err = string_decode(err, termenc) return out, err, ret diff --git a/alot/settings/manager.py b/alot/settings/manager.py index ab5e70fb..e5dcc4b3 100644 --- a/alot/settings/manager.py +++ b/alot/settings/manager.py @@ -16,7 +16,7 @@ from configobj import ConfigObj, Section from ..account import SendmailAccount from ..addressbook.abook import AbookAddressBook from ..addressbook.external import ExternalAddressbook -from ..helper import string_decode, get_xdg_env +from ..helper import get_xdg_env from ..utils import configobj as checks from .errors import ConfigError, NoMatchingAccount @@ -422,7 +422,6 @@ class SettingsManager: fallback_focus) translated = cfg['tags'][sec]['translated'] - translated = string_decode(translated, 'UTF-8') if translated is None: translated = tag translation = cfg['tags'][sec]['translation'] @@ -585,11 +584,11 @@ class SettingsManager: fixed_format = self.get('timestamp_format') if fixed_format: - rep = string_decode(d.strftime(fixed_format), 'UTF-8') + rep = d.strftime(fixed_format) else: format_hook = self.get_hook('timestamp_format') if format_hook: - rep = string_decode(format_hook(d), 'UTF-8') + rep = format_hook(d) else: rep = _pretty_datetime(d) return rep diff --git a/alot/widgets/globals.py b/alot/widgets/globals.py index 6f7531d2..387797af 100644 --- a/alot/widgets/globals.py +++ b/alot/widgets/globals.py @@ -9,7 +9,6 @@ import re import operator import urwid -from ..helper import string_decode from ..settings.const import settings from ..db.attachment import Attachment from ..errors import CompletionError @@ -127,8 +126,6 @@ class CompleteEdit(urwid.Edit): self.historypos = None self.focus_in_clist = 0 - if not isinstance(edit_text, str): - edit_text = string_decode(edit_text) self.start_completion_pos = len(edit_text) self.completions = None urwid.Edit.__init__(self, edit_text=edit_text, **kwargs) -- cgit v1.2.3