{
    "mode": "pydoc",
    "parameter": "contextlib",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/pydoc/contextlib/json",
    "generated": "2026-06-02T15:01:22Z",
    "sections": {
        "NAME": {
            "content": "contextlib - Utilities for with-statement contexts.  See PEP 343.\n",
            "subsections": []
        },
        "MODULE REFERENCE": {
            "content": "https://docs.python.org/3.10/library/contextlib.html\n\nThe following documentation is automatically generated from the Python\nsource files.  It may be incomplete, incorrect or include features that\nare considered implementation detail and may vary between Python\nimplementations.  When in doubt, consult the module reference at the\nlocation listed above.\n",
            "subsections": []
        },
        "CLASSES": {
            "content": "abc.ABC(builtins.object)\nAbstractAsyncContextManager\nAsyncExitStack(BaseExitStack, AbstractAsyncContextManager)\naclosing\nAbstractContextManager\nExitStack(BaseExitStack, AbstractContextManager)\nclosing\nnullcontext(AbstractContextManager, AbstractAsyncContextManager)\nsuppress\nbuiltins.object\nContextDecorator\nBaseExitStack(builtins.object)\nAsyncExitStack(BaseExitStack, AbstractAsyncContextManager)\nExitStack(BaseExitStack, AbstractContextManager)\nRedirectStream(AbstractContextManager)\nredirectstderr\nredirectstdout\n",
            "subsections": [
                {
                    "name": "class AbstractAsyncContextManager",
                    "content": "|  An abstract base class for asynchronous context managers.\n|\n|  Method resolution order:\n|      AbstractAsyncContextManager\n|      abc.ABC\n|      builtins.object\n|\n|  Methods defined here:\n|\n|  async aenter(self)\n|      Return `self` upon entering the runtime context.\n|\n|  async aexit(self, exctype, excvalue, traceback)\n|      Raise any exception triggered within the runtime context.\n|\n|  ----------------------------------------------------------------------\n|  Class methods defined here:\n|\n|  classgetitem = GenericAlias(...) from abc.ABCMeta\n|      Represent a PEP 585 generic type\n|\n|      E.g. for t = list[int], t.origin is list and t.args is (int,).\n|\n|  subclasshook(C) from abc.ABCMeta\n|      Abstract classes can override this to customize issubclass().\n|\n|      This is invoked early on by abc.ABCMeta.subclasscheck().\n|      It should return True, False or NotImplemented.  If it returns\n|      NotImplemented, the normal algorithm is used.  Otherwise, it\n|      overrides the normal algorithm (and the outcome is cached).\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors defined here:\n|\n|  dict\n|      dictionary for instance variables (if defined)\n|\n|  weakref\n|      list of weak references to the object (if defined)\n|\n|  ----------------------------------------------------------------------\n|  Data and other attributes defined here:\n|\n|  abstractmethods = frozenset({'aexit'})\n"
                },
                {
                    "name": "class AbstractContextManager",
                    "content": "|  An abstract base class for context managers.\n|\n|  Method resolution order:\n|      AbstractContextManager\n|      abc.ABC\n|      builtins.object\n|\n|  Methods defined here:\n|\n|  enter(self)\n|      Return `self` upon entering the runtime context.\n|\n|  exit(self, exctype, excvalue, traceback)\n|      Raise any exception triggered within the runtime context.\n|\n|  ----------------------------------------------------------------------\n|  Class methods defined here:\n|\n|  classgetitem = GenericAlias(...) from abc.ABCMeta\n|      Represent a PEP 585 generic type\n|\n|      E.g. for t = list[int], t.origin is list and t.args is (int,).\n|\n|  subclasshook(C) from abc.ABCMeta\n|      Abstract classes can override this to customize issubclass().\n|\n|      This is invoked early on by abc.ABCMeta.subclasscheck().\n|      It should return True, False or NotImplemented.  If it returns\n|      NotImplemented, the normal algorithm is used.  Otherwise, it\n|      overrides the normal algorithm (and the outcome is cached).\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors defined here:\n|\n|  dict\n|      dictionary for instance variables (if defined)\n|\n|  weakref\n|      list of weak references to the object (if defined)\n|\n|  ----------------------------------------------------------------------\n|  Data and other attributes defined here:\n|\n|  abstractmethods = frozenset({'exit'})\n"
                },
                {
                    "name": "class AsyncExitStack",
                    "content": "|  Async context manager for dynamic management of a stack of exit\n|  callbacks.\n|\n|  For example:\n|      async with AsyncExitStack() as stack:\n|          connections = [await stack.enterasynccontext(getconnection())\n|              for i in range(5)]\n|          # All opened connections will automatically be released at the\n|          # end of the async with statement, even if attempts to open a\n|          # connection later in the list raise an exception.\n|\n|  Method resolution order:\n|      AsyncExitStack\n|      BaseExitStack\n|      AbstractAsyncContextManager\n|      abc.ABC\n|      builtins.object\n|\n|  Methods defined here:\n|\n|  async aenter(self)\n|      Return `self` upon entering the runtime context.\n|\n|  async aexit(self, *excdetails)\n|      Raise any exception triggered within the runtime context.\n|\n|  async aclose(self)\n|      Immediately unwind the context stack.\n|\n|  async enterasynccontext(self, cm)\n|      Enters the supplied async context manager.\n|\n|      If successful, also pushes its aexit method as a callback and\n|      returns the result of the aenter method.\n|\n|  pushasynccallback(self, callback, /, *args, kwds)\n|      Registers an arbitrary coroutine function and arguments.\n|\n|      Cannot suppress exceptions.\n|\n|  pushasyncexit(self, exit)\n|      Registers a coroutine function with the standard aexit method\n|      signature.\n|\n|      Can suppress exceptions the same way aexit method can.\n|      Also accepts any object with an aexit method (registering a call\n|      to the method instead of the object itself).\n|\n|  ----------------------------------------------------------------------\n|  Data and other attributes defined here:\n|\n|  abstractmethods = frozenset()\n|\n|  ----------------------------------------------------------------------\n|  Methods inherited from BaseExitStack:\n|\n|  init(self)\n|      Initialize self.  See help(type(self)) for accurate signature.\n|\n|  callback(self, callback, /, *args, kwds)\n|      Registers an arbitrary callback and arguments.\n|\n|      Cannot suppress exceptions.\n|\n|  entercontext(self, cm)\n|      Enters the supplied context manager.\n|\n|      If successful, also pushes its exit method as a callback and\n|      returns the result of the enter method.\n|\n|  popall(self)\n|      Preserve the context stack by transferring it to a new instance.\n|\n|  push(self, exit)\n|      Registers a callback with the standard exit method signature.\n|\n|      Can suppress exceptions the same way exit method can.\n|      Also accepts any object with an exit method (registering a call\n|      to the method instead of the object itself).\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors inherited from BaseExitStack:\n|\n|  dict\n|      dictionary for instance variables (if defined)\n|\n|  weakref\n|      list of weak references to the object (if defined)\n|\n|  ----------------------------------------------------------------------\n|  Class methods inherited from AbstractAsyncContextManager:\n|\n|  classgetitem = GenericAlias(...) from abc.ABCMeta\n|      Represent a PEP 585 generic type\n|\n|      E.g. for t = list[int], t.origin is list and t.args is (int,).\n|\n|  subclasshook(C) from abc.ABCMeta\n|      Abstract classes can override this to customize issubclass().\n|\n|      This is invoked early on by abc.ABCMeta.subclasscheck().\n|      It should return True, False or NotImplemented.  If it returns\n|      NotImplemented, the normal algorithm is used.  Otherwise, it\n|      overrides the normal algorithm (and the outcome is cached).\n"
                },
                {
                    "name": "class ContextDecorator",
                    "content": "|  A base class or mixin that enables context managers to work as decorators.\n|\n|  Methods defined here:\n|\n|  call(self, func)\n|      Call self as a function.\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors defined here:\n|\n|  dict\n|      dictionary for instance variables (if defined)\n|\n|  weakref\n|      list of weak references to the object (if defined)\n"
                },
                {
                    "name": "class ExitStack",
                    "content": "|  Context manager for dynamic management of a stack of exit callbacks.\n|\n|  For example:\n|      with ExitStack() as stack:\n|          files = [stack.entercontext(open(fname)) for fname in filenames]\n|          # All opened files will automatically be closed at the end of\n|          # the with statement, even if attempts to open files later\n|          # in the list raise an exception.\n|\n|  Method resolution order:\n|      ExitStack\n|      BaseExitStack\n|      AbstractContextManager\n|      abc.ABC\n|      builtins.object\n|\n|  Methods defined here:\n|\n|  enter(self)\n|      Return `self` upon entering the runtime context.\n|\n|  exit(self, *excdetails)\n|      Raise any exception triggered within the runtime context.\n|\n|  close(self)\n|      Immediately unwind the context stack.\n|\n|  ----------------------------------------------------------------------\n|  Data and other attributes defined here:\n|\n|  abstractmethods = frozenset()\n|\n|  ----------------------------------------------------------------------\n|  Methods inherited from BaseExitStack:\n|\n|  init(self)\n|      Initialize self.  See help(type(self)) for accurate signature.\n|\n|  callback(self, callback, /, *args, kwds)\n|      Registers an arbitrary callback and arguments.\n|\n|      Cannot suppress exceptions.\n|\n|  entercontext(self, cm)\n|      Enters the supplied context manager.\n|\n|      If successful, also pushes its exit method as a callback and\n|      returns the result of the enter method.\n|\n|  popall(self)\n|      Preserve the context stack by transferring it to a new instance.\n|\n|  push(self, exit)\n|      Registers a callback with the standard exit method signature.\n|\n|      Can suppress exceptions the same way exit method can.\n|      Also accepts any object with an exit method (registering a call\n|      to the method instead of the object itself).\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors inherited from BaseExitStack:\n|\n|  dict\n|      dictionary for instance variables (if defined)\n|\n|  weakref\n|      list of weak references to the object (if defined)\n|\n|  ----------------------------------------------------------------------\n|  Class methods inherited from AbstractContextManager:\n|\n|  classgetitem = GenericAlias(...) from abc.ABCMeta\n|      Represent a PEP 585 generic type\n|\n|      E.g. for t = list[int], t.origin is list and t.args is (int,).\n|\n|  subclasshook(C) from abc.ABCMeta\n|      Abstract classes can override this to customize issubclass().\n|\n|      This is invoked early on by abc.ABCMeta.subclasscheck().\n|      It should return True, False or NotImplemented.  If it returns\n|      NotImplemented, the normal algorithm is used.  Otherwise, it\n|      overrides the normal algorithm (and the outcome is cached).\n"
                },
                {
                    "name": "class aclosing",
                    "content": "|  aclosing(thing)\n|\n|  Async context manager for safely finalizing an asynchronously cleaned-up\n|  resource such as an async generator, calling its ``aclose()`` method.\n|\n|  Code like this:\n|\n|      async with aclosing(<module>.fetch(<arguments>)) as agen:\n|          <block>\n|\n|  is equivalent to this:\n|\n|      agen = <module>.fetch(<arguments>)\n|      try:\n|          <block>\n|      finally:\n|          await agen.aclose()\n|\n|  Method resolution order:\n|      aclosing\n|      AbstractAsyncContextManager\n|      abc.ABC\n|      builtins.object\n|\n|  Methods defined here:\n|\n|  async aenter(self)\n|      Return `self` upon entering the runtime context.\n|\n|  async aexit(self, *excinfo)\n|      Raise any exception triggered within the runtime context.\n|\n|  init(self, thing)\n|      Initialize self.  See help(type(self)) for accurate signature.\n|\n|  ----------------------------------------------------------------------\n|  Data and other attributes defined here:\n|\n|  abstractmethods = frozenset()\n|\n|  ----------------------------------------------------------------------\n|  Class methods inherited from AbstractAsyncContextManager:\n|\n|  classgetitem = GenericAlias(...) from abc.ABCMeta\n|      Represent a PEP 585 generic type\n|\n|      E.g. for t = list[int], t.origin is list and t.args is (int,).\n|\n|  subclasshook(C) from abc.ABCMeta\n|      Abstract classes can override this to customize issubclass().\n|\n|      This is invoked early on by abc.ABCMeta.subclasscheck().\n|      It should return True, False or NotImplemented.  If it returns\n|      NotImplemented, the normal algorithm is used.  Otherwise, it\n|      overrides the normal algorithm (and the outcome is cached).\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors inherited from AbstractAsyncContextManager:\n|\n|  dict\n|      dictionary for instance variables (if defined)\n|\n|  weakref\n|      list of weak references to the object (if defined)\n"
                },
                {
                    "name": "class closing",
                    "content": "|  closing(thing)\n|\n|  Context to automatically close something at the end of a block.\n|\n|  Code like this:\n|\n|      with closing(<module>.open(<arguments>)) as f:\n|          <block>\n|\n|  is equivalent to this:\n|\n|      f = <module>.open(<arguments>)\n|      try:\n|          <block>\n|      finally:\n|          f.close()\n|\n|  Method resolution order:\n|      closing\n|      AbstractContextManager\n|      abc.ABC\n|      builtins.object\n|\n|  Methods defined here:\n|\n|  enter(self)\n|      Return `self` upon entering the runtime context.\n|\n|  exit(self, *excinfo)\n|      Raise any exception triggered within the runtime context.\n|\n|  init(self, thing)\n|      Initialize self.  See help(type(self)) for accurate signature.\n|\n|  ----------------------------------------------------------------------\n|  Data and other attributes defined here:\n|\n|  abstractmethods = frozenset()\n|\n|  ----------------------------------------------------------------------\n|  Class methods inherited from AbstractContextManager:\n|\n|  classgetitem = GenericAlias(...) from abc.ABCMeta\n|      Represent a PEP 585 generic type\n|\n|      E.g. for t = list[int], t.origin is list and t.args is (int,).\n|\n|  subclasshook(C) from abc.ABCMeta\n|      Abstract classes can override this to customize issubclass().\n|\n|      This is invoked early on by abc.ABCMeta.subclasscheck().\n|      It should return True, False or NotImplemented.  If it returns\n|      NotImplemented, the normal algorithm is used.  Otherwise, it\n|      overrides the normal algorithm (and the outcome is cached).\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors inherited from AbstractContextManager:\n|\n|  dict\n|      dictionary for instance variables (if defined)\n|\n|  weakref\n|      list of weak references to the object (if defined)\n"
                },
                {
                    "name": "class nullcontext",
                    "content": "|  nullcontext(enterresult=None)\n|\n|  Context manager that does no additional processing.\n|\n|  Used as a stand-in for a normal context manager, when a particular\n|  block of code is only sometimes used with a normal context manager:\n|\n|  cm = optionalcm if condition else nullcontext()\n|  with cm:\n|      # Perform operation, using optionalcm if condition is True\n|\n|  Method resolution order:\n|      nullcontext\n|      AbstractContextManager\n|      AbstractAsyncContextManager\n|      abc.ABC\n|      builtins.object\n|\n|  Methods defined here:\n|\n|  async aenter(self)\n|      Return `self` upon entering the runtime context.\n|\n|  async aexit(self, *excinfo)\n|      Raise any exception triggered within the runtime context.\n|\n|  enter(self)\n|      Return `self` upon entering the runtime context.\n|\n|  exit(self, *excinfo)\n|      Raise any exception triggered within the runtime context.\n|\n|  init(self, enterresult=None)\n|      Initialize self.  See help(type(self)) for accurate signature.\n|\n|  ----------------------------------------------------------------------\n|  Data and other attributes defined here:\n|\n|  abstractmethods = frozenset()\n|\n|  ----------------------------------------------------------------------\n|  Class methods inherited from AbstractContextManager:\n|\n|  classgetitem = GenericAlias(...) from abc.ABCMeta\n|      Represent a PEP 585 generic type\n|\n|      E.g. for t = list[int], t.origin is list and t.args is (int,).\n|\n|  subclasshook(C) from abc.ABCMeta\n|      Abstract classes can override this to customize issubclass().\n|\n|      This is invoked early on by abc.ABCMeta.subclasscheck().\n|      It should return True, False or NotImplemented.  If it returns\n|      NotImplemented, the normal algorithm is used.  Otherwise, it\n|      overrides the normal algorithm (and the outcome is cached).\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors inherited from AbstractContextManager:\n|\n|  dict\n|      dictionary for instance variables (if defined)\n|\n|  weakref\n|      list of weak references to the object (if defined)\n"
                },
                {
                    "name": "class redirect_stderr",
                    "content": "|  redirectstderr(newtarget)\n|\n|  Context manager for temporarily redirecting stderr to another file.\n|\n|  Method resolution order:\n|      redirectstderr\n|      RedirectStream\n|      AbstractContextManager\n|      abc.ABC\n|      builtins.object\n|\n|  Data and other attributes defined here:\n|\n|  abstractmethods = frozenset()\n|\n|  ----------------------------------------------------------------------\n|  Methods inherited from RedirectStream:\n|\n|  enter(self)\n|      Return `self` upon entering the runtime context.\n|\n|  exit(self, exctype, excinst, exctb)\n|      Raise any exception triggered within the runtime context.\n|\n|  init(self, newtarget)\n|      Initialize self.  See help(type(self)) for accurate signature.\n|\n|  ----------------------------------------------------------------------\n|  Class methods inherited from AbstractContextManager:\n|\n|  classgetitem = GenericAlias(...) from abc.ABCMeta\n|      Represent a PEP 585 generic type\n|\n|      E.g. for t = list[int], t.origin is list and t.args is (int,).\n|\n|  subclasshook(C) from abc.ABCMeta\n|      Abstract classes can override this to customize issubclass().\n|\n|      This is invoked early on by abc.ABCMeta.subclasscheck().\n|      It should return True, False or NotImplemented.  If it returns\n|      NotImplemented, the normal algorithm is used.  Otherwise, it\n|      overrides the normal algorithm (and the outcome is cached).\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors inherited from AbstractContextManager:\n|\n|  dict\n|      dictionary for instance variables (if defined)\n|\n|  weakref\n|      list of weak references to the object (if defined)\n"
                },
                {
                    "name": "class redirect_stdout",
                    "content": "|  redirectstdout(newtarget)\n|\n|  Context manager for temporarily redirecting stdout to another file.\n|\n|  # How to send help() to stderr\n|  with redirectstdout(sys.stderr):\n|      help(dir)\n|\n|  # How to write help() to a file\n|  with open('help.txt', 'w') as f:\n|      with redirectstdout(f):\n|          help(pow)\n|\n|  Method resolution order:\n|      redirectstdout\n|      RedirectStream\n|      AbstractContextManager\n|      abc.ABC\n|      builtins.object\n|\n|  Data and other attributes defined here:\n|\n|  abstractmethods = frozenset()\n|\n|  ----------------------------------------------------------------------\n|  Methods inherited from RedirectStream:\n|\n|  enter(self)\n|      Return `self` upon entering the runtime context.\n|\n|  exit(self, exctype, excinst, exctb)\n|      Raise any exception triggered within the runtime context.\n|\n|  init(self, newtarget)\n|      Initialize self.  See help(type(self)) for accurate signature.\n|\n|  ----------------------------------------------------------------------\n|  Class methods inherited from AbstractContextManager:\n|\n|  classgetitem = GenericAlias(...) from abc.ABCMeta\n|      Represent a PEP 585 generic type\n|\n|      E.g. for t = list[int], t.origin is list and t.args is (int,).\n|\n|  subclasshook(C) from abc.ABCMeta\n|      Abstract classes can override this to customize issubclass().\n|\n|      This is invoked early on by abc.ABCMeta.subclasscheck().\n|      It should return True, False or NotImplemented.  If it returns\n|      NotImplemented, the normal algorithm is used.  Otherwise, it\n|      overrides the normal algorithm (and the outcome is cached).\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors inherited from AbstractContextManager:\n|\n|  dict\n|      dictionary for instance variables (if defined)\n|\n|  weakref\n|      list of weak references to the object (if defined)\n"
                },
                {
                    "name": "class suppress",
                    "content": "|  suppress(*exceptions)\n|\n|  Context manager to suppress specified exceptions\n|\n|  After the exception is suppressed, execution proceeds with the next\n|  statement following the with statement.\n|\n|       with suppress(FileNotFoundError):\n|           os.remove(somefile)\n|       # Execution still resumes here if the file was already removed\n|\n|  Method resolution order:\n|      suppress\n|      AbstractContextManager\n|      abc.ABC\n|      builtins.object\n|\n|  Methods defined here:\n|\n|  enter(self)\n|      Return `self` upon entering the runtime context.\n|\n|  exit(self, exctype, excinst, exctb)\n|      Raise any exception triggered within the runtime context.\n|\n|  init(self, *exceptions)\n|      Initialize self.  See help(type(self)) for accurate signature.\n|\n|  ----------------------------------------------------------------------\n|  Data and other attributes defined here:\n|\n|  abstractmethods = frozenset()\n|\n|  ----------------------------------------------------------------------\n|  Class methods inherited from AbstractContextManager:\n|\n|  classgetitem = GenericAlias(...) from abc.ABCMeta\n|      Represent a PEP 585 generic type\n|\n|      E.g. for t = list[int], t.origin is list and t.args is (int,).\n|\n|  subclasshook(C) from abc.ABCMeta\n|      Abstract classes can override this to customize issubclass().\n|\n|      This is invoked early on by abc.ABCMeta.subclasscheck().\n|      It should return True, False or NotImplemented.  If it returns\n|      NotImplemented, the normal algorithm is used.  Otherwise, it\n|      overrides the normal algorithm (and the outcome is cached).\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors inherited from AbstractContextManager:\n|\n|  dict\n|      dictionary for instance variables (if defined)\n|\n|  weakref\n|      list of weak references to the object (if defined)\n"
                }
            ]
        },
        "FUNCTIONS": {
            "content": "",
            "subsections": [
                {
                    "name": "asynccontextmanager",
                    "content": "@asynccontextmanager decorator.\n\nTypical usage:\n\n@asynccontextmanager\nasync def someasyncgenerator(<arguments>):\n<setup>\ntry:\nyield <value>\nfinally:\n<cleanup>\n\nThis makes this:\n\nasync with someasyncgenerator(<arguments>) as <variable>:\n<body>\n\nequivalent to this:\n\n<setup>\ntry:\n<variable> = <value>\n<body>\nfinally:\n<cleanup>\n"
                },
                {
                    "name": "contextmanager",
                    "content": "@contextmanager decorator.\n\nTypical usage:\n\n@contextmanager\ndef somegenerator(<arguments>):\n<setup>\ntry:\nyield <value>\nfinally:\n<cleanup>\n\nThis makes this:\n\nwith somegenerator(<arguments>) as <variable>:\n<body>\n\nequivalent to this:\n\n<setup>\ntry:\n<variable> = <value>\n<body>\nfinally:\n<cleanup>\n"
                }
            ]
        },
        "DATA": {
            "content": "all = ['asynccontextmanager', 'contextmanager', 'closing', 'nullco...\n",
            "subsections": []
        },
        "FILE": {
            "content": "/usr/lib/python3.10/contextlib.py\n\n",
            "subsections": []
        }
    },
    "summary": "contextlib - Utilities for with-statement contexts.  See PEP 343.",
    "flags": [],
    "examples": [],
    "see_also": []
}