Skip to content

Commit

Permalink
Rely on 'apply(bool)' and 'suppress' decorator in is_decodable.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Oct 31, 2021
1 parent d708962 commit 1098494
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions jaraco/text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
import itertools
import textwrap
import functools
import contextlib

try:
from importlib.resources import files # type: ignore
except ImportError: # pragma: nocover
from importlib_resources import files # type: ignore

from jaraco.functools import compose, method_cache
from jaraco.functools import compose, method_cache, apply


# copied from jaraco.context:
class _suppress(contextlib.suppress, contextlib.ContextDecorator):
pass


def substitution(old, new):
Expand Down Expand Up @@ -128,6 +134,8 @@ def split(self, splitter=' ', maxsplit=0):
return pattern.split(self, maxsplit)


@apply(bool)
@_suppress(UnicodeDecodeError)
def is_decodable(value):
r"""
Return True if the supplied value is decodable (using the default
Expand All @@ -138,13 +146,7 @@ def is_decodable(value):
>>> is_decodable(b'\x32')
True
"""
# TODO: This code could be expressed more consisely and directly
# with a jaraco.context.ExceptionTrap, but that adds an unfortunate
# long dependency tree, so for now, use boolean literals.
try:
value.decode()
except UnicodeDecodeError:
return False
value.decode()
return True


Expand Down

0 comments on commit 1098494

Please sign in to comment.