Activity log for bug #2000000

Date Who What changed Old value New value Message
2022-12-18 10:15:07 FeRD (Frank Dana) bug added bug
2022-12-18 10:15:07 FeRD (Frank Dana) attachment added Fixes for condition errors in Python code https://bugs.launchpad.net/bugs/2000000/+attachment/5636243/+files/python_fixes.patch
2022-12-20 00:52:08 FeRD (Frank Dana) summary Multiple instances of Python object-testing errors Multiple instances of Python object-testing mistakes
2022-12-20 00:56:25 FeRD (Frank Dana) description The HPLIP Python code contains multiple instances where a string literal is compared with a variable using identity comparison ("is" / "is not"), which as the language reference will tell you (and a warning output to the terminal notes) does not generally work. IOW, taking an example from ui5/devmgr5.py: if self.latest_available_version is not "": ...will always evaluate to True, because even if self.latest_available_version is set to "", that empty string is a _different_ instance of str than the one at the end of the condition. The correct way to write such a comparison is, if self.latest_available_version != "": "is not" and "is" should generally only be used to check whether two variables refer to the _exact same object_ or literal. The most common uses are "somevar is None" or "somevar is not None". None is a singleton object, so there can only ever be one instance of None in the runtime. Additionally, base/pexpect/__init__.py contains an order-of-evaluation error on line 789: if timeout < 0 and timeout is not None: if timeout ever actually is None, attempting to compare it to an integer will trigger an exception, "TypeError: '<' not supported between instances of 'NoneType' and 'int'". To properly short-circuit the evaluation, the order of the two tests must be reversed. The attached patch contains fixes for the pexpect condition, and replaces all instances of "is not <string literal>" that I could find with "!= <string literal>". I can't guarantee there aren't still others lurking in the code somewhere, though. The HPLIP Python code contains multiple instances where a string literal is compared with a variable using identity comparison ("is" / "is not"), which as the language reference will tell you (and a warning output to the terminal notes) is generally ill-advised. IOW, taking an example from ui5/devmgr5.py: if self.latest_available_version is not "": The way to write such a comparison without triggering a SyntaxWarning is, if self.latest_available_version != "": "is not" and "is" should generally only be used to check whether two variables refer to the _exact same object_ or literal. The most common uses are "somevar is None" or "somevar is not None". None is a singleton object, so there can only ever be one instance of None in the runtime. Additionally, base/pexpect/__init__.py contains an order-of-evaluation error on line 789: if timeout < 0 and timeout is not None: if timeout ever actually is None, attempting to compare it to an integer will trigger an exception, "TypeError: '<' not supported between instances of 'NoneType' and 'int'". To properly short-circuit the evaluation, the order of the two tests must be reversed. The attached patch contains fixes for the pexpect condition, and replaces all instances of "is not <string literal>" that I could find with "!= <string literal>". I can't guarantee there aren't still others lurking in the code somewhere, though.