Comment 0 for bug 2000000

Revision history for this message
FeRD (Frank Dana) (ferdnyc) wrote : Multiple instances of Python object-testing errors

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.