Skip to content

Commit

Permalink
Re-enable coverage for debugpy.common.log and use comments to exclude…
Browse files Browse the repository at this point in the history
… specific lines instead.
  • Loading branch information
Pavel Minaev authored and int19h committed Sep 5, 2023
1 parent 8e91dfb commit 9df16ce
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
6 changes: 1 addition & 5 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ omit =
src/debugpy/_version.py
src/debugpy/_vendored/*
src/debugpy/server/*
src/debugpy/common/log.py
data_file = coverage/.coverage

[report]
exclude_lines =
# Have to re-enable the standard pragma.
pragma: no cover

exclude_also =
# __repr__ is mostly used for error messages.
def __repr__

Expand Down
12 changes: 6 additions & 6 deletions src/debugpy/common/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, filename, file, levels=LEVELS, close_file=True):
platform.machine(),
platform.python_implementation(),
platform.python_version(),
64 if sys.maxsize > 2 ** 32 else 32,
64 if sys.maxsize > 2**32 else 32,
debugpy.__version__,
_to_files=[self],
)
Expand All @@ -78,7 +78,7 @@ def write(self, level, output):
try:
self.file.write(output)
self.file.flush()
except Exception:
except Exception: # pragma: no cover
pass

def close(self):
Expand All @@ -90,7 +90,7 @@ def close(self):
if self.close_file:
try:
self.file.close()
except Exception:
except Exception: # pragma: no cover
pass

def __enter__(self):
Expand Down Expand Up @@ -151,7 +151,7 @@ def write_format(level, format_string, *args, **kwargs):

try:
text = format_string.format(*args, **kwargs)
except Exception:
except Exception: # pragma: no cover
reraise_exception()

return write(level, text, kwargs.pop("_to_files", all))
Expand Down Expand Up @@ -253,7 +253,7 @@ def to_file(filename=None, prefix=None, levels=LEVELS):
return NoLog()
try:
os.makedirs(log_dir)
except OSError:
except OSError: # pragma: no cover
pass
filename = f"{log_dir}/{prefix}-{os.getpid()}.log"

Expand Down Expand Up @@ -347,7 +347,7 @@ def report_paths(get_paths, label=None):
importlib_metadata = None
try:
import importlib_metadata
except ImportError:
except ImportError: # pragma: no cover
try:
from importlib import metadata as importlib_metadata
except ImportError:
Expand Down
18 changes: 9 additions & 9 deletions src/debugpy/common/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def from_socket(cls, sock, name=None):
def cleanup():
try:
sock.shutdown(socket.SHUT_RDWR)
except Exception:
except Exception: # pragma: no cover
pass
sock.close()

Expand Down Expand Up @@ -155,7 +155,7 @@ def close(self):
self._reader.close()
finally:
self._cleanup()
except Exception:
except Exception: # pragma: no cover
log.reraise_exception("Error while closing {0} message stream", self.name)

def _log_message(self, dir, data, logger=log.debug):
Expand Down Expand Up @@ -206,7 +206,7 @@ def log_message_and_reraise_exception(format_string="", *args, **kwargs):
while True:
try:
line = read_line()
except Exception:
except Exception: # pragma: no cover
# Only log it if we have already read some headers, and are looking
# for a blank line terminating them. If this is the very first read,
# there's no message data to log in any case, and the caller might
Expand All @@ -229,7 +229,7 @@ def log_message_and_reraise_exception(format_string="", *args, **kwargs):
length = int(headers[b"Content-Length"])
if not (0 <= length <= self.MAX_BODY_SIZE):
raise ValueError
except (KeyError, ValueError):
except (KeyError, ValueError): # pragma: no cover
try:
raise IOError("Content-Length is missing or invalid:")
except Exception:
Expand All @@ -253,12 +253,12 @@ def log_message_and_reraise_exception(format_string="", *args, **kwargs):
body = b"".join(raw_chunks[body_start:])
try:
body = body.decode("utf-8")
except Exception:
except Exception: # pragma: no cover
log_message_and_reraise_exception()

try:
body = decoder.decode(body)
except Exception:
except Exception: # pragma: no cover
log_message_and_reraise_exception()

# If parsed successfully, log as JSON for readability.
Expand All @@ -285,7 +285,7 @@ def write_json(self, value, encoder=None):

try:
body = encoder.encode(value)
except Exception:
except Exception: # pragma: no cover
self._log_message("<--", repr(value), logger=log.reraise_exception)
body = body.encode("utf-8")

Expand All @@ -297,7 +297,7 @@ def write_json(self, value, encoder=None):
written = writer.write(data[data_written:])
data_written += written
writer.flush()
except Exception as exc:
except Exception as exc: # pragma: no cover
self._log_message("<--", value, logger=log.swallow_exception)
raise JsonIOError(stream=self, cause=exc)

Expand Down Expand Up @@ -344,7 +344,7 @@ def __init__(self, message, items=None):
def __repr__(self):
try:
return format(json.repr(self))
except Exception:
except Exception: # pragma: no cover
return super().__repr__()

def __call__(self, key, validate, optional=False):
Expand Down
16 changes: 8 additions & 8 deletions src/debugpy/common/sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def create_server(host, port=0, backlog=socket.SOMAXCONN, timeout=None):
else:
try:
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except (AttributeError, OSError):
except (AttributeError, OSError): # pragma: no cover
pass # Not available everywhere
server.bind((host, port))
if timeout is not None:
server.settimeout(timeout)
server.listen(backlog)
except Exception:
except Exception: # pragma: no cover
server.close()
raise
return server
Expand All @@ -56,19 +56,19 @@ def _new_sock():
# and closes the connection after 5 failed ping (TCP_KEEPCNT), or 15 seconds
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
except (AttributeError, OSError):
except (AttributeError, OSError): # pragma: no cover
pass # May not be available everywhere.
try:
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 1)
except (AttributeError, OSError):
except (AttributeError, OSError): # pragma: no cover
pass # May not be available everywhere.
try:
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 3)
except (AttributeError, OSError):
except (AttributeError, OSError): # pragma: no cover
pass # May not be available everywhere.
try:
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)
except (AttributeError, OSError):
except (AttributeError, OSError): # pragma: no cover
pass # May not be available everywhere.
return sock

Expand All @@ -82,7 +82,7 @@ def close_socket(sock):
"""Shutdown and close the socket."""
try:
shut_down(sock)
except Exception:
except Exception: # pragma: no cover
pass
sock.close()

Expand All @@ -98,7 +98,7 @@ def serve(name, handler, host, port=0, backlog=socket.SOMAXCONN, timeout=None):

try:
listener = create_server(host, port, backlog, timeout)
except Exception:
except Exception: # pragma: no cover
log.reraise_exception(
"Error listening for incoming {0} connections on {1}:{2}:", name, host, port
)
Expand Down

0 comments on commit 9df16ce

Please sign in to comment.