-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
subclasses of telnetlib.Telnet may crash when used as context manager #118042
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
Comments
How can it call |
I am confused. I think I gave an example for this in the report. The short answer is "subclasses". Here is the relevant part of the example: class AliasTelnet(telnetlib.Telnet):
def __init__(self, alias):
host = ALIAS_TO_HOST.get(alias)
if not host:
raise ValueError(f'cannot map alias "{alias}" to host name')
super().__init__(host) Raising something in |
Why does it call |
I am sorry, I misread the traceback. It does not happen in Exception ignored in: <function Telnet.__del__ at 0x7faeb8656ee0> |
I updated the report accordingly. |
Indeed, this is a bug. Your second version is more preferable because it is simpler. There is already a check for Note that the 3.12 is the only version that will get the fix. 3.11 and older only accept security fixes. |
I would have argued the opposite. In particular since you felt it was needed to add the comment: # for __del__() So, solving the problem where it happens (reading But both are fine ...
Yes, I figured that -- see last paragraph in the report :) |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
Subclasses of
telnetlib.Telnet
may crash when used as context manager due to missingself.sock
:A simple example that can trigger the issue would be a Telnet subclass where aliases to hostnames would be provided for making the telnet connection:
What happens is that upon leaving the context due to an exception
Telnet.__del__
is called, which callsTelnet.close
. If this happens beforeTelnet.__init__
was called (i.e., the linesuper().__init__(host)
above) thesock
attribute does not exist yet.A simple solution would be to adjust the
close
method to this:Alternatively,
sock = None
could be attached to the class:Probably the first version is preferable since it solves the problem where it happens.
I can, of course, provide a PR for either of the changes. Question is whether it's worth it since telnetlib is going to be deprecated. (Sadly, I'd like to add. I actually like using it and telnetlib3 is not really a straight forward replacement due to being fully async. Any way to petition for telnetlib to stay?)
CPython versions tested on:
3.8, 3.12
Operating systems tested on:
Linux
Linked PRs
The text was updated successfully, but these errors were encountered: