jquast / telnetlib3

Python Telnet server and client Protocol library using asyncio
Other
147 stars 32 forks source link

__repr__ logging problem #50

Closed JPHutchins closed 3 years ago

JPHutchins commented 3 years ago

I am using this excellent library as a dependency and the parent library seems to be calling the __repr__ of BaseClient defined in client_base.py: https://github.com/jquast/telnetlib3/blob/97b5705774753ba801bf1d7867c846bb849aa631/telnetlib3/client_base.py#L201-L207 The trouble is that it seems to be calling for this __repr__ after disconnect and self._transport has been redefined as None: https://github.com/jquast/telnetlib3/blob/97b5705774753ba801bf1d7867c846bb849aa631/telnetlib3/client_base.py#L99-L100 Which causes an AttributeError when trying to get _extra from _transport:

--- Logging error ---
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/logging/__init__.py", line 1081, in emit
    msg = self.format(record)
  File "/usr/local/lib/python3.8/logging/__init__.py", line 925, in format
    return fmt.format(record)
  File "/usr/local/lib/python3.8/site-packages/colorlog/colorlog.py", line 123, in format
    message = super(ColoredFormatter, self).format(record)
  File "/usr/local/lib/python3.8/logging/__init__.py", line 664, in format
    record.message = record.getMessage()
  File "/usr/local/lib/python3.8/logging/__init__.py", line 369, in getMessage
    msg = msg % self.args
  File "/usr/local/lib/python3.8/site-packages/telnetlib3/client_base.py", line 202, in __repr__
    hostport = self.get_extra_info('peername')[:2]
  File "/usr/local/lib/python3.8/site-packages/telnetlib3/client_base.py", line 207, in get_extra_info
    return self._extra.get(name, self._transport._extra.get(name, default))
AttributeError: 'NoneType' object has no attribute '_extra'
Call stack:
  File "/usr/local/lib/python3.8/threading.py", line 890, in _bootstrap
    self._bootstrap_inner()
  File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
Unable to print the message and arguments - possible formatting error.
Use the traceback above to help find the error.

I can resolve this simply by:

def get_extra_info(self, name, default=None): 
    """Get optional client protocol or transport information.""" 
    if self._transport is None:
        return ("", "")
    return self._extra.get(name, self._transport._extra.get(name, default)) 

So I would love to know if we can merge contributors preferred solution or if I should just handle this upstream or by monkey patching the repr.

jquast commented 3 years ago

Thanks for the bug report, I think there is related activity to resolve this in progress, I’m glad folks are finally finding and picking this library up, I’ll try to fix this soon, looking for contributors :) best wishes

jquast commented 3 years ago

this fix is going to pypi shortly as 1.0.4

jquast commented 3 years ago

https://pypi.org/project/telnetlib3/1.0.4/