Coder Social home page Coder Social logo

Comments (7)

serhiy-storchaka avatar serhiy-storchaka commented on May 28, 2024

How can it call Telnet.__exit__ before finishing Telnet.__init__?

from cpython.

NichtJens avatar NichtJens commented on May 28, 2024

How can it call Telnet.__exit__ before finishing Telnet.__init__?

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 AliasTelnet.__init__ will call Telnet.__exit__ on its way out.

from cpython.

serhiy-storchaka avatar serhiy-storchaka commented on May 28, 2024

Why does it call __exit__ on its way out? Normally, __exit__ is only called after __enter__, implicitly in the with statement, and you need already created object for this. __init__ should finish successfully before you can use the object as a context manager.

from cpython.

NichtJens avatar NichtJens commented on May 28, 2024

I am sorry, I misread the traceback. It does not happen in __exit__ but in __del__. It's actually saying that in the traceback:

Exception ignored in: <function Telnet.__del__ at 0x7faeb8656ee0>

from cpython.

NichtJens avatar NichtJens commented on May 28, 2024

I updated the report accordingly.

from cpython.

serhiy-storchaka avatar serhiy-storchaka commented on May 28, 2024

Indeed, this is a bug. Your second version is more preferable because it is simpler. There is already a check for self.sock.

Note that the telnetlib module was deprecated in Python 3.11 and removed in 3.13.

3.12 is the only version that will get the fix. 3.11 and older only accept security fixes.

from cpython.

NichtJens avatar NichtJens commented on May 28, 2024

Your second version is more preferable because it is simpler. There is already a check for self.sock.

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 sock = self.sock) feels cleaner.

But both are fine ...

Note that the telnetlib module was deprecated in Python 3.11 and removed in 3.13.

3.12 is the only version that will get the fix. 3.11 and older only accept security fixes.

Yes, I figured that -- see last paragraph in the report :)

from cpython.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.