Skip to content
Myr

Neater close logic (#1541)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by GitHub · parent c26425a · Browse files · 1 file+30 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+30 added−1 removed

  • other:+30 added−1 removed

1 file+30 added−1 removed

modified:httpx/_client.py+30 added-1 removed
67 unmodified lines
68)697071class ClientState(enum.Enum):72 # UNOPENED:73 # The client has been instantiated, but has not been used to send a request,74 # or been opened by entering the context of a `with` block.75 UNOPENED = 176 # OPENED:77 # The client has either sent a request, or is within a `with` block.78 OPENED = 279 # CLOSED:80 # The client has either exited the `with` block, or `close()` has81 # been called explicitly.82 CLOSED = 3838485class BaseClient:
1061 unmodified lines
1147 if transport is not None:1148 transport.__exit__(exc_type, exc_value, traceback)11491150 def __del__(self) -> None:1151 if self._state == ClientState.OPENED:1143 self.close()1152 self.close()115311541155class AsyncClient(BaseClient):1156 """
635 unmodified lines
1792 await proxy.__aexit__(exc_type, exc_value, traceback)17931794 def __del__(self) -> None:1795 if self._state == ClientState.OPENED:1796 # Unlike the sync case, we cannot silently close the client when1797 # it is garbage collected, because `.aclose()` is an async operation,1798 # but `__del__` is not.1799 #1800 # For this reason we require explicit close management for1801 # `AsyncClient`, and issue a warning on unclosed clients.1802 #1803 # The context managed style is usually preferable, because it neatly1804 # ensures proper resource cleanup:1805 #1806 # async with httpx.AsyncClient() as client:1807 # ...1808 #1809 # However, an explicit call to `aclose()` is also sufficient:1810 #1811 # client = httpx.AsyncClient()1812 # try:1813 # ...1814 # finally:1815 # await client.aclose()1816 warnings.warn(1817 f"Unclosed {self!r}. "1818 "See https://www.python-httpx.org/async/#opening-and-closing-clients "1819 "for details."
62 unmodified lines