jupyter/terminado

Terminal log output generates unnecessary log error `TypeError: expected string or bytes-like object`

devmonkey22 opened this issue · 1 comments

The new log terminal output code added in #112 is causing unnecessary log errors on anything but stdout messages.

The error is not able to serialize: expected string or bytes-like object with setup and disconnect messages like ['setup', {}] and ['disconnect', 1]

Traceback (most recent call last):
  File ".../handlers.py", line 62, in send_json_message
    if pattern.search(content[1]):
TypeError: expected string or bytes-like object

I assume we'd really want a check like:

def send_json_message(self, content):
        json_msg = json.dumps(content)
        pattern = re.compile(r'^(\w|\d)+')
        try:
            if isinstance(content[1], (str, bytes)) and pattern.search(content[1]):
                self.log_terminal_output(f'STDOUT: {content[1]}')
        except TypeError as e:
            self._logger.error(f'not able to serialize: {e}')
        self.write_message(json_msg)

This would avoid the type error for well-expected content values.

As a side note, it would be ideal to get LOG_TERMINAL_OUTPUT env variable in the constructor, then check it before building potentially big f-strings from STDOUT. I haven't confirmed this really causes any significant performance issues, so only wanted to note this as a potential.

Both suggestions sound reasonable to me, mind making a PR?