From 56f9cfe30c4488aade888905bb6280ecb952a613 Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Fri, 5 Aug 2022 13:35:20 -0600 Subject: [lldb] Remove uses of six module (NFC) With lldb (& llvm) requiring Python 3.6+, use of the `six` module can be removed. Differential Revision: https://reviews.llvm.org/D131304 --- .../Python/lldbsuite/support/encoded_file.py | 27 ++-------- lldb/packages/Python/lldbsuite/support/seven.py | 58 ++++++++-------------- 2 files changed, 26 insertions(+), 59 deletions(-) (limited to 'lldb/packages/Python/lldbsuite/support') diff --git a/lldb/packages/Python/lldbsuite/support/encoded_file.py b/lldb/packages/Python/lldbsuite/support/encoded_file.py index c233e04..9de63fd 100644 --- a/lldb/packages/Python/lldbsuite/support/encoded_file.py +++ b/lldb/packages/Python/lldbsuite/support/encoded_file.py @@ -10,26 +10,12 @@ to see a description of the supported command line arguments. # Python modules: import io -# Third party modules -import six - - -def _encoded_read(old_read, encoding): - def impl(size): - result = old_read(size) - # If this is Python 2 then we need to convert the resulting `unicode` back - # into a `str` before returning - if six.PY2: - result = result.encode(encoding) - return result - return impl - def _encoded_write(old_write, encoding): def impl(s): - # If we were asked to write a `str` (in Py2) or a `bytes` (in Py3) decode it - # as unicode before attempting to write. - if isinstance(s, six.binary_type): + # If we were asked to write a `bytes` decode it as unicode before + # attempting to write. + if isinstance(s, bytes): s = s.decode(encoding, "replace") # Filter unreadable characters, Python 3 is stricter than python 2 about them. import re @@ -38,9 +24,8 @@ def _encoded_write(old_write, encoding): return impl ''' -Create a Text I/O file object that can be written to with either unicode strings or byte strings -under Python 2 and Python 3, and automatically encodes and decodes as necessary to return the -native string type for the current Python version +Create a Text I/O file object that can be written to with either unicode strings +or byte strings. ''' @@ -60,8 +45,6 @@ def open( errors=errors, newline=newline, closefd=closefd) - new_read = _encoded_read(getattr(wrapped_file, 'read'), encoding) new_write = _encoded_write(getattr(wrapped_file, 'write'), encoding) - setattr(wrapped_file, 'read', new_read) setattr(wrapped_file, 'write', new_write) return wrapped_file diff --git a/lldb/packages/Python/lldbsuite/support/seven.py b/lldb/packages/Python/lldbsuite/support/seven.py index 969b61d..e9ebf17 100644 --- a/lldb/packages/Python/lldbsuite/support/seven.py +++ b/lldb/packages/Python/lldbsuite/support/seven.py @@ -1,47 +1,31 @@ import binascii -import six import shlex - -if six.PY2: - import commands - get_command_output = commands.getoutput - get_command_status_output = commands.getstatusoutput - - cmp_ = cmp -else: - def get_command_status_output(command): - try: - import subprocess - return ( - 0, - subprocess.check_output( - command, - shell=True, - universal_newlines=True).rstrip()) - except subprocess.CalledProcessError as e: - return (e.returncode, e.output) - - def get_command_output(command): - return get_command_status_output(command)[1] - - cmp_ = lambda x, y: (x > y) - (x < y) - -def bitcast_to_string(b): +import subprocess + +def get_command_output(command): + try: + return subprocess.check_output( + command, + shell=True, + universal_newlines=True).rstrip() + except subprocess.CalledProcessError as e: + return e.output + +def bitcast_to_string(b: bytes) -> str: """ - Take a string(PY2) or a bytes(PY3) object and return a string. The returned - string contains the exact same bytes as the input object (latin1 <-> unicode - transformation is an identity operation for the first 256 code points). + Take a bytes object and return a string. The returned string contains the + exact same bytes as the input object. (latin1 <-> unicode transformation is + an identity operation for the first 256 code points). """ - return b if six.PY2 else b.decode("latin1") + return b.decode("latin1") -def bitcast_to_bytes(s): +def bitcast_to_bytes(s: str) -> bytes: """ - Take a string and return a string(PY2) or a bytes(PY3) object. The returned - object contains the exact same bytes as the input string. (latin1 <-> - unicode transformation is an identity operation for the first 256 code - points). + Take a string and return a bytes object. The returned object contains the + exact same bytes as the input string. (latin1 <-> unicode transformation isi + an identity operation for the first 256 code points). """ - return s if six.PY2 else s.encode("latin1") + return s.encode("latin1") def unhexlify(hexstr): """Hex-decode a string. The result is always a string.""" -- cgit v1.1