From: Brian Wellington Date: Fri, 18 Mar 2022 19:26:31 +0000 (-0700) Subject: Use contextlib.nullcontext(). X-Git-Tag: v2.3.0rc1~89^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3210e504fd992b8b88c17503b960b9f60e1b424d;p=thirdparty%2Fdnspython.git Use contextlib.nullcontext(). Replaces uses of contextlib.ExitStack, since nullcontext() is available in 3.7. --- diff --git a/dns/message.py b/dns/message.py index 709d71dd..de347441 100644 --- a/dns/message.py +++ b/dns/message.py @@ -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] diff --git a/dns/query.py b/dns/query.py index 493d283c..04eaca4a 100644 --- a/dns/query.py +++ b/dns/query.py @@ -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( diff --git a/dns/resolver.py b/dns/resolver.py index ac34be87..74fbd336 100644 --- a/dns/resolver.py +++ b/dns/resolver.py @@ -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 diff --git a/dns/zone.py b/dns/zone.py index cba2e54f..76386b64 100644 --- a/dns/zone.py +++ b/dns/zone.py @@ -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,