0

I am using: ipdb (ipdb==0.13.9).


For the with launch_ipdb_on_exception(): line in code-block from answer for Launch an IPython shell on exception

from ipdb import launch_ipdb_on_exception

def main():
    with launch_ipdb_on_exception():
        # The rest of the code goes here.
        [...]

I am getting following warning message: [not-context-manager] Context manager 'generator' doesn't implement __enter__ and __exit__. [E1129]

What is the main cause of this error? How could I prevent this warning message?

alper
  • 2,919
  • 9
  • 53
  • 102

1 Answers1

0

The goal of Python's with statement is syntactic sugar to properly manage external resources to your script such as files, locks, network connections, and others. You have to manage these resources; otherwise, a script will retain those resources forever causing a memory leak. The problem with the regular paradigm of

  1. file = open...
  2. file.write...
  3. file.close() occurs if an error occurs during the file.write call, then file.close will never be called causing an issue. In Python, there are generally two ways to deal with these issue.
  4. A try...finally construct
  5. A with construct

This second approach provides a [design] pattern to generate setup and teardown code as long as the resource that you are managing works with Python context manager paradigm. The reason for this is due to Python's with clause embodying C++'s well known "resource acquisition if initialization" paradigm (RAII): You need only write context manager classes--that is, classes with two special methods (dunder methods in Python terminology) enter and exit . enter must be callable without arguments. exit must be callable with three arguments: all None if the body completes without propagating exceptions, and otherwise the type, value, and traceback of the exception. This provides the same guaranteed finalization behavior as typical ctor/dtor pairs have for auto variables in C++, and try/finally statements have in Python or Java. In addition, you gain the ability to finalize differently depending on what exception, if any, propagates, as well as optionally blocking a propagating exception by returning a True value from exit.

Hope this helps and good-luck!