Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version: avoid infinite recursion #475

Merged
merged 1 commit into from
Mar 11, 2022

Conversation

silbe
Copy link
Contributor

@silbe silbe commented Mar 8, 2022

The comparison operators (<, >, etc.) are not translated 1:1 to customisation methods (__lt__, __gt__, ...) in Python. Instead the type of the operands plays a role in determining on which of the two sides the customisation method is invoked (see Python Language Reference section 3.3.1). This means a > b can end up invoking b.__lt__(a) rather than a.__gt__(b).

This behaviour can causes infinite recursion in Version.__lt__():

2022-03-07 13:47:52,087 - NanoVNASaver.Hardware.NanoVNA_V2 - ERROR - Timeout reading version registers
Traceback (most recent call last):
  File "/home/sascha/nanovna-saver/NanoVNASaver/Controls/SerialControl.py", line 73, in serialButtonClick
    self.connect_device()
  File "/home/sascha/nanovna-saver/NanoVNASaver/Controls/SerialControl.py", line 93, in connect_device
    self.app.vna = get_VNA(self.interface)
  File "/home/sascha/nanovna-saver/NanoVNASaver/Hardware/Hardware.py", line 101, in get_VNA
    return NAME2DEVICE[iface.comment](iface)
  File "/home/sascha/nanovna-saver/NanoVNASaver/Hardware/NanoVNA_V2.py", line 76, in __init__
    super().__init__(iface)
  File "/home/sascha/nanovna-saver/NanoVNASaver/Hardware/VNA.py", line 71, in __init__
    self.read_features()
  File "/home/sascha/nanovna-saver/NanoVNASaver/Hardware/NanoVNA_V2.py", line 107, in read_features
    if self.board_revision >= Version("2.0.4"):
  File "/home/sascha/nanovna-saver/NanoVNASaver/Version.py", line 63, in __le__
    return self < other or self == other
  File "/home/sascha/nanovna-saver/NanoVNASaver/Version.py", line 57, in __lt__
    return other > self
  File "/home/sascha/nanovna-saver/NanoVNASaver/Version.py", line 57, in __lt__
    return other > self
  File "/home/sascha/nanovna-saver/NanoVNASaver/Version.py", line 57, in __lt__
    return other > self
  [Previous line repeated 491 more times]
RecursionError: maximum recursion depth exceeded in comparison

Fix it by explicitly invoking the customisation methods we expect.

@silbe silbe requested a review from zarath as a code owner March 8, 2022 21:22
The comparison operators (<, >, etc.) are not translated 1:1 to
customisation methods (__lt__, __gt__, ...) in Python. Instead the
type of the operands plays a role in determining on which of the two
sides the customisation method is invoked (see Python Language
Reference section 3.3.1 [1]). This means 'a > b' can end up invoking
b.__lt__(a) rather than a.__gt__(b).

This behaviour can causes infinite recursion in Version.__lt__():

2022-03-07 13:47:52,087 - NanoVNASaver.Hardware.NanoVNA_V2 - ERROR - Timeout reading version registers
Traceback (most recent call last):
  File "/home/sascha/nanovna-saver/NanoVNASaver/Controls/SerialControl.py", line 73, in serialButtonClick
    self.connect_device()
  File "/home/sascha/nanovna-saver/NanoVNASaver/Controls/SerialControl.py", line 93, in connect_device
    self.app.vna = get_VNA(self.interface)
  File "/home/sascha/nanovna-saver/NanoVNASaver/Hardware/Hardware.py", line 101, in get_VNA
    return NAME2DEVICE[iface.comment](iface)
  File "/home/sascha/nanovna-saver/NanoVNASaver/Hardware/NanoVNA_V2.py", line 76, in __init__
    super().__init__(iface)
  File "/home/sascha/nanovna-saver/NanoVNASaver/Hardware/VNA.py", line 71, in __init__
    self.read_features()
  File "/home/sascha/nanovna-saver/NanoVNASaver/Hardware/NanoVNA_V2.py", line 107, in read_features
    if self.board_revision >= Version("2.0.4"):
  File "/home/sascha/nanovna-saver/NanoVNASaver/Version.py", line 63, in __le__
    return self < other or self == other
  File "/home/sascha/nanovna-saver/NanoVNASaver/Version.py", line 57, in __lt__
    return other > self
  File "/home/sascha/nanovna-saver/NanoVNASaver/Version.py", line 57, in __lt__
    return other > self
  File "/home/sascha/nanovna-saver/NanoVNASaver/Version.py", line 57, in __lt__
    return other > self
  [Previous line repeated 491 more times]
RecursionError: maximum recursion depth exceeded in comparison

Fix it by explicitly invoking the customisation methods we expect.

[1] https://docs.python.org/3/reference/datamodel.html#object.__lt__
@zarath
Copy link
Collaborator

zarath commented Mar 11, 2022

Thank you

@zarath zarath merged commit 4d4ff52 into NanoVNA-Saver:testing Mar 11, 2022
@silbe
Copy link
Contributor Author

silbe commented Mar 20, 2022

Thanks for reviewing and merging!

@silbe silbe deleted the version-recursion-fix branch March 20, 2022 09:56
@silbe silbe mentioned this pull request Mar 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants