GadgetReactor / pyHS100

Python Library to control TPLink Switch (HS100 / HS110)
Other
409 stars 128 forks source link

Crash when __repr__ is called on SmartStrip device #165

Closed scootdj closed 5 years ago

scootdj commented 5 years ago

SmartDevice defines a repr that calls self.is_on.

is_on must be implemented by subclasses. SmartStrip defines is_on differently with an additional parameter (index) and a different return value 'Any'.

Infinite recursion happens when repr tries to call the SmartStrip.is_on. Because the value returned by SmartDevice.is_on is not a string, a recursive call to repr happens.

I have worked around it by changing SmartDevice.repr to:

def repr(self): if isinstance(self.is_on, str): is_on = self.is_on else: is_on = self.is_on() return "<%s at %s (%s), is_on: %s - dev specific: %s>" % ( self.class.name, self.host, self.alias, is_on, self.state_information)

jimboca commented 5 years ago

This issue looks like it's fixed in @rytilahti api_and_tests_cleanup branch. Can we get that merged in and released?

eode commented 5 years ago

Cleaned that up for copypasta, if anyone wants it.

def fixed_repr(self):
    if isinstance(self.is_on, str):
        is_on = self.is_on
    else:
        is_on = self.is_on()
    return "<%s at %s (%s), is_on: %s - dev specific: %s>" % (
        self.__class__.__name__,
        self.host,
        self.alias,
        is_on,
        self.state_information)

pyHS100.SmartStrip.__repr__ = fixed_repr
newAM commented 5 years ago

I know #151 fixes this, but I am not sure what the timeline is like on that so I created #169 as a targeted fix for this, plus unit tests to catch this in the future.