From 2ccd48e9215cb78934665454235c3e33bf07772c Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 22 Jul 2020 00:24:56 +0100 Subject: fix an incorrect comment --- synapse/http/server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/http/server.py b/synapse/http/server.py index cff49202f4..8e003689c4 100644 --- a/synapse/http/server.py +++ b/synapse/http/server.py @@ -575,8 +575,8 @@ def respond_with_json_bytes( if send_cors: set_cors_headers(request) - # todo: we can almost certainly avoid this copy and encode the json straight into - # the bytesIO, but it would involve faffing around with string->bytes wrappers. + # note that this is zero-copy (the bytesio shares a copy-on-write buffer with + # the original `bytes`). bytes_io = BytesIO(json_bytes) producer = NoRangeStaticProducer(request, bytes_io) -- cgit 1.5.1 From 15997618e21e2398fab20300b9380b0fce2b32d1 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Wed, 22 Jul 2020 00:40:27 +0100 Subject: Clean up PreserveLoggingContext (#7877) This had some dead code and some just plain wrong docstrings. --- changelog.d/7877.misc | 1 + synapse/logging/context.py | 29 +++++++++++++---------------- 2 files changed, 14 insertions(+), 16 deletions(-) create mode 100644 changelog.d/7877.misc diff --git a/changelog.d/7877.misc b/changelog.d/7877.misc new file mode 100644 index 0000000000..a62aa0329c --- /dev/null +++ b/changelog.d/7877.misc @@ -0,0 +1 @@ +Clean up `PreserveLoggingContext`. diff --git a/synapse/logging/context.py b/synapse/logging/context.py index 8b9c4e38bd..cbeeb870cb 100644 --- a/synapse/logging/context.py +++ b/synapse/logging/context.py @@ -566,36 +566,33 @@ class LoggingContextFilter(logging.Filter): return True -class PreserveLoggingContext(object): - """Captures the current logging context and restores it when the scope is - exited. Used to restore the context after a function using - @defer.inlineCallbacks is resumed by a callback from the reactor.""" +class PreserveLoggingContext: + """Context manager which replaces the logging context - __slots__ = ["current_context", "new_context", "has_parent"] + The previous logging context is restored on exit.""" + + __slots__ = ["_old_context", "_new_context"] def __init__( self, new_context: LoggingContextOrSentinel = SENTINEL_CONTEXT ) -> None: - self.new_context = new_context + self._new_context = new_context def __enter__(self) -> None: - """Captures the current logging context""" - self.current_context = set_current_context(self.new_context) - - if self.current_context: - self.has_parent = self.current_context.previous_context is not None + self._old_context = set_current_context(self._new_context) def __exit__(self, type, value, traceback) -> None: - """Restores the current logging context""" - context = set_current_context(self.current_context) + context = set_current_context(self._old_context) - if context != self.new_context: + if context != self._new_context: if not context: - logger.warning("Expected logging context %s was lost", self.new_context) + logger.warning( + "Expected logging context %s was lost", self._new_context + ) else: logger.warning( "Expected logging context %s but found %s", - self.new_context, + self._new_context, context, ) -- cgit 1.5.1