]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Use contextlib.nullcontext().
authorBrian Wellington <bwelling@xbill.org>
Fri, 18 Mar 2022 19:26:31 +0000 (12:26 -0700)
committerBrian Wellington <bwelling@xbill.org>
Fri, 18 Mar 2022 19:26:31 +0000 (12:26 -0700)
Replaces uses of contextlib.ExitStack, since nullcontext() is available
in 3.7.

dns/message.py
dns/query.py
dns/resolver.py
dns/zone.py

index 709d71dd2301ab03157d79bcfb9412730aab6f72..de347441c924864640baf6eb98f6d876e45f5e9c 100644 (file)
@@ -1585,9 +1585,11 @@ def from_file(
     Returns a ``dns.message.Message object``
     """
 
-    with contextlib.ExitStack() as stack:
-        if isinstance(f, str):
-            f = stack.enter_context(open(f))
+    if isinstance(f, str):
+        cm = open(f)
+    else:
+        cm = contextlib.nullcontext(f)
+    with cm as f:
         return from_text(f, idna_codec, one_rr_per_rrset)
     assert False  # for mypy  lgtm[py/unreachable-statement]
 
index 493d283c9b7ed8113bd4f05f0340aca72d0b0db0..04eaca4a08a961bee0dfd1bfa67fcc8031a79a2b 100644 (file)
@@ -379,20 +379,15 @@ def https(
             "Cannot use httpx for this operation, and requests is not available."
         )
 
-    with contextlib.ExitStack() as stack:
-        if not session:
-            if _is_httpx:
-                session = stack.enter_context(
-                    httpx.Client(
-                        http1=True,
-                        http2=_have_http2,
-                        verify=verify,
-                        transport=transport,
-                    )
-                )
-            else:
-                session = stack.enter_context(requests.sessions.Session())
-
+    if session:
+        cm = contextlib.nullcontext(session)
+    elif _is_httpx:
+        cm = httpx.Client(
+            http1=True, http2=_have_http2, verify=verify, transport=transport
+        )
+    else:
+        cm = requests.sessions.Session()
+    with cm as session:
         if transport_adapter:
             session.mount(url, transport_adapter)
 
@@ -635,11 +630,11 @@ def udp(
         where, port, source, source_port
     )
     (begin_time, expiration) = _compute_times(timeout)
-    with contextlib.ExitStack() as stack:
-        if sock:
-            s = sock
-        else:
-            s = stack.enter_context(_make_socket(af, socket.SOCK_DGRAM, source))
+    if sock:
+        cm = contextlib.nullcontext(sock)
+    else:
+        cm = _make_socket(af, socket.SOCK_DGRAM, source)
+    with cm as s:
         send_udp(s, wire, destination, expiration)
         (r, received_time) = receive_udp(
             s,
@@ -918,14 +913,15 @@ def tcp(
 
     wire = q.to_wire()
     (begin_time, expiration) = _compute_times(timeout)
-    with contextlib.ExitStack() as stack:
-        if sock:
-            s = sock
-        else:
-            (af, destination, source) = _destination_and_source(
-                where, port, source, source_port
-            )
-            s = stack.enter_context(_make_socket(af, socket.SOCK_STREAM, source))
+    if sock:
+        cm = contextlib.nullcontext(sock)
+    else:
+        (af, destination, source) = _destination_and_source(
+            where, port, source, source_port
+        )
+        cm = _make_socket(af, socket.SOCK_STREAM, source)
+    with cm as s:
+        if not sock:
             _connect(s, destination, expiration)
         send_tcp(s, wire, expiration)
         (r, received_time) = receive_tcp(
index ac34be8779d18184aadefdead37266c2d25fe359..74fbd336a164ab8dc62a52f2100f95acf12e4deb 100644 (file)
@@ -903,14 +903,15 @@ class BaseResolver:
 
         """
 
-        with contextlib.ExitStack() as stack:
-            if isinstance(f, str):
-                try:
-                    f = stack.enter_context(open(f))
-                except OSError:
-                    # /etc/resolv.conf doesn't exist, can't be read, etc.
-                    raise NoResolverConfiguration(f"cannot open {f}")
-
+        if isinstance(f, str):
+            try:
+                cm = open(f)
+            except OSError:
+                # /etc/resolv.conf doesn't exist, can't be read, etc.
+                raise NoResolverConfiguration(f"cannot open {f}")
+        else:
+            cm = contextlib.nullcontext(f)
+        with cm as f:
             for l in f:
                 if len(l) == 0 or l[0] == "#" or l[0] == ";":
                     continue
index cba2e54f2063edc5dd61cefea08aab30c2495ab7..76386b64f99b7be3f878633462c8949a41fbceb0 100644 (file)
@@ -641,10 +641,11 @@ class Zone(dns.transaction.TransactionManager):
         one.
         """
 
-        with contextlib.ExitStack() as stack:
-            if isinstance(f, str):
-                f = stack.enter_context(open(f, "wb"))
-
+        if isinstance(f, str):
+            cm = open(f, "wb")
+        else:
+            cm = contextlib.nullcontext(f)
+        with cm as f:
             # must be in this way, f.encoding may contain None, or even
             # attribute may not be there
             file_enc = getattr(f, "encoding", None)
@@ -1290,11 +1291,13 @@ def from_file(
     Returns a subclass of ``dns.zone.Zone``.
     """
 
-    with contextlib.ExitStack() as stack:
-        if isinstance(f, str):
-            if filename is None:
-                filename = f
-            f = stack.enter_context(open(f))
+    if isinstance(f, str):
+        if filename is None:
+            filename = f
+        cm = open(f)
+    else:
+        cm = contextlib.nullcontext(f)
+    with cm as f:
         return from_text(
             f,
             origin,