# Buggy code
def __init__(self, connection, theme="day", parent=None):
    super().__init__(parent)
    self.connection = connection
    self.setFont(Font().load())
    self.setAcceptRichText(False)
    self.setReadOnly(False)
    self.setUndoRedoEnabled(False)
    self.setContextMenuPolicy(Qt.CustomContextMenu)
    self.customContextMenuRequested.connect(self.context_menu)
    # The following variable maintains the position where we know
    # the device cursor is placed. It is initialized to the beginning
    # of the QTextEdit (i.e. equal to the Qt cursor position)
    self.device_cursor_position = self.textCursor().position()
    self.setObjectName("replpane")
    self.set_theme(theme)
    self.unprocessed_input = b""  # used by process_bytes
    self.decoder = codecs.getincrementaldecoder("utf8")()
    self.vt100_regex = re.compile(
        r"\x1B\[(?P<count>[\d]*)(;?[\d]*)*(?P<action>[A-Za-z])"
    )


# Traceback (UnicodeDecodeError)
Starting Mu 1.1.0.alpha.2
2020-10-01 08:53:24,342 - root:123(run) INFO: uname_result(system='Darwin', node='dybber', release='18.7.0', version='Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64', machine='x86_64', processor='i386')
2020-10-01 08:53:24,342 - root:124(run) INFO: Python path: ['/Users/dpr964/Development/micropython/mu', '/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python38.zip', '/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8', '/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload', '/Users/dpr964/.virtualenvs/mu-editor/lib/python3.8/site-packages', '/Users/dpr964/Development/micropython/mu', '/Users/dpr964/Library/Application Support/mu/site-packages', '/Users/dpr964/.virtualenvs/mu-editor/lib/python3.8/site-packages/IPython/extensions']
2020-10-01 08:53:24,342 - root:125(run) INFO: Language code: en_US
2020-10-01 08:53:24,990 - mu.logic:845(__init__) INFO: Setting up editor.
2020-10-01 08:53:24,992 - mu.logic:867(__init__) INFO: Settings path: /Users/dpr964/Library/Application Support/mu/settings.json
2020-10-01 08:53:24,992 - mu.logic:868(__init__) INFO: Session path: /Users/dpr964/Library/Application Support/mu/session.json
2020-10-01 08:53:24,992 - mu.logic:869(__init__) INFO: Log directory: /Users/dpr964/Library/Logs/mu
2020-10-01 08:53:24,992 - mu.logic:870(__init__) INFO: Data directory: /Users/dpr964/Library/Application Support/mu
2020-10-01 08:53:25,012 - mu.logic:884(setup) INFO: Available modes: python, circuitpython, microbit, esp, web, debugger
2020-10-01 08:53:25,771 - mu.logic:1611(change_mode) INFO: Workspace directory: /Users/dpr964/mu_code
2020-10-01 08:53:25,773 - mu.logic:969(restore_session) INFO: Restoring session from: /Users/dpr964/Library/Application Support/mu/session.json
2020-10-01 08:53:25,773 - mu.logic:970(restore_session) DEBUG: {'theme': 'day', 'mode': 'esp', 'paths': [], 'envars': [], 'minify': False, 'microbit_runtime': '', 'zoom_level': 2, 'window': {'x': 256, 'y': 144, 'w': 2048, 'h': 1152}}
2020-10-01 08:53:25,779 - mu.logic:992(restore_session) INFO: Loaded files.
2020-10-01 08:53:25,779 - mu.logic:995(restore_session) INFO: User defined environment variables: []
2020-10-01 08:53:25,779 - mu.logic:1001(restore_session) INFO: Minify scripts on micro:bit? False
2020-10-01 08:53:25,800 - mu.logic:1611(change_mode) INFO: Workspace directory: /Users/dpr964/mu_code
2020-10-01 08:53:25,953 - mu.logic:1038(restore_session) INFO: Starting with blank file.
2020-10-01 08:53:26,139 - mu.logic:824(check_usb) INFO: esp device connected on port: /dev/cu.usbserial-A9523A07B6(VID: 0x0403, PID: 0x6001, manufacturer: 'M5STACK Inc.')
2020-10-01 08:53:27,553 - mu.modes.base:123(open) INFO: Connecting to REPL on port: /dev/cu.usbserial-A9523A07B6
2020-10-01 08:53:27,566 - mu.modes.base:141(open) INFO: Connected to REPL on port: /dev/cu.usbserial-A9523A07B6
2020-10-01 08:53:27,578 - mu.modes.base:507(add_repl) INFO: Started REPL on port: /dev/cu.usbserial-A9523A07B6
2020-10-01 08:53:27,578 - mu.modes.base:478(toggle_repl) INFO: Toggle REPL on.
2020-10-01 08:53:27,599 - root:105(excepthook) ERROR: Unrecoverable error
Traceback (most recent call last):
File "/Users/dpr964/Development/micropython/mu/mu/interface/panes.py", line 364, in process_tty_data
data = self.decoder.decode(data)
File "/usr/local/opt/python@3.8/bin/../Frameworks/Python.framework/Versions/3.8/lib/python3.8/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd8 in position 4: invalid continuation byte
2020-10-01 08:53:38,324 - root:122(run) INFO:

# Fixed code
def __init__(self, connection, theme="day", parent=None):
    super().__init__(parent)
    self.connection = connection
    self.setFont(Font().load())
    self.setAcceptRichText(False)
    self.setReadOnly(False)
    self.setUndoRedoEnabled(False)
    self.setContextMenuPolicy(Qt.CustomContextMenu)
    self.customContextMenuRequested.connect(self.context_menu)
    # The following variable maintains the position where we know
    # the device cursor is placed. It is initialized to the beginning
    # of the QTextEdit (i.e. equal to the Qt cursor position)
    self.device_cursor_position = self.textCursor().position()
    self.setObjectName("replpane")
    self.set_theme(theme)
    self.unprocessed_input = b""  # used by process_bytes
    self.decoder = codecs.getincrementaldecoder("utf8")("replace")
    self.vt100_regex = re.compile(
        r"\x1B\[(?P<count>[\d]*)(;?[\d]*)*(?P<action>[A-Za-z])"
    )