From 11c51cbc25fd2dfeba40d291a37684dfe456b549 Mon Sep 17 00:00:00 2001 From: QuentinN42 Date: Sun, 28 Jan 2024 18:19:30 +0100 Subject: [PATCH] fix: make agnosticcontextmanager as protected Signed-off-by: QuentinN42 --- .pylintrc | 2 +- .../src/opentelemetry/trace/__init__.py | 66 +++++++------------ opentelemetry-api/tests/trace/test_proxy.py | 2 +- opentelemetry-api/tests/trace/test_tracer.py | 5 +- .../src/opentelemetry/sdk/trace/__init__.py | 2 +- 5 files changed, 28 insertions(+), 49 deletions(-) diff --git a/.pylintrc b/.pylintrc index 4e4c3a965eb..562c31c9b13 100644 --- a/.pylintrc +++ b/.pylintrc @@ -165,7 +165,7 @@ notes=FIXME, # List of decorators that produce context managers, such as # contextlib.contextmanager. Add to this list to register other decorators that # produce valid context managers. -contextmanager-decorators=contextlib.contextmanager, agnosticcontextmanager +contextmanager-decorators=contextlib.contextmanager, _agnosticcontextmanager # List of members which are set dynamically and missed by pylint inference # system, and so shouldn't trigger E1101 when accessed. Python regular diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index 1aecf9ed8b3..27aea9c46b9 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -157,47 +157,27 @@ def __exit__(self, typ, value, traceback): next(self.gen) except StopIteration: return False - else: - raise RuntimeError("generator didn't stop") - else: - if value is None: - # Need to force instantiation so we can reliably - # tell if we get the same exception back - value = typ() - try: - self.gen.throw(typ, value, traceback) - except StopIteration as exc: - # Suppress StopIteration *unless* it's the same exception that - # was passed to throw(). This prevents a StopIteration - # raised inside the "with" statement from being suppressed. - return exc is not value - except RuntimeError as exc: - # Don't re-raise the passed in exception. (issue27122) - if exc is value: - exc.__traceback__ = traceback - return False - # Avoid suppressing if a StopIteration exception - # was passed to throw() and later wrapped into a RuntimeError - # (see PEP 479 for sync generators; async generators also - # have this behavior). But do this only if the exception wrapped - # by the RuntimeError is actually Stop(Async)Iteration (see - # issue29692). - if isinstance(value, StopIteration) and exc.__cause__ is value: - exc.__traceback__ = traceback - return False - raise - except BaseException as exc: - # only re-raise if it's *not* the exception that was - # passed to throw(), because __exit__() must not raise - # an exception unless __exit__() itself failed. But throw() - # has to raise the exception to signal propagation, so this - # fixes the impedance mismatch between the throw() protocol - # and the __exit__() protocol. - if exc is not value: - raise + raise RuntimeError("generator didn't stop") + if value is None: + value = typ() + try: + self.gen.throw(typ, value, traceback) + except StopIteration as exc: + return exc is not value + except RuntimeError as exc: + if exc is value: exc.__traceback__ = traceback return False - raise RuntimeError("generator didn't stop after throw()") + if isinstance(value, StopIteration) and exc.__cause__ is value: + exc.__traceback__ = traceback + return False + raise + except BaseException as exc: + if exc is not value: + raise + exc.__traceback__ = traceback + return False + raise RuntimeError("generator didn't stop after throw()") def __call__(self, func): """Mixing contextlib.ContextDecorator.__call__ and contextlib.AsyncContextDecorator.__call__ @@ -222,7 +202,7 @@ def wrapper(*args, **kwargs): return wrapper -def agnosticcontextmanager(func): +def _agnosticcontextmanager(func): @functools.wraps(func) def helper(*args, **kwds): return _AgnosticContextManager(func, args, kwds) @@ -441,7 +421,7 @@ def start_span( The newly-created span. """ - @agnosticcontextmanager + @_agnosticcontextmanager @abstractmethod def start_as_current_span( self, @@ -548,7 +528,7 @@ def _tracer(self) -> Tracer: def start_span(self, *args, **kwargs) -> Span: # type: ignore return self._tracer.start_span(*args, **kwargs) # type: ignore - @agnosticcontextmanager # type: ignore + @_agnosticcontextmanager # type: ignore def start_as_current_span(self, *args, **kwargs) -> Iterator[Span]: # type: ignore with self._tracer.start_as_current_span(*args, **kwargs) as span: # type: ignore yield span @@ -574,7 +554,7 @@ def start_span( # pylint: disable=unused-argument,no-self-use return INVALID_SPAN - @agnosticcontextmanager + @_agnosticcontextmanager def start_as_current_span( self, name: str, diff --git a/opentelemetry-api/tests/trace/test_proxy.py b/opentelemetry-api/tests/trace/test_proxy.py index 4ffe1444006..38302b69307 100644 --- a/opentelemetry-api/tests/trace/test_proxy.py +++ b/opentelemetry-api/tests/trace/test_proxy.py @@ -39,7 +39,7 @@ class TestTracer(trace.NoOpTracer): def start_span(self, *args, **kwargs): return TestSpan(INVALID_SPAN_CONTEXT) - @trace.agnosticcontextmanager + @trace._agnosticcontextmanager # pylint: disable=protected-access def start_as_current_span(self, *args, **kwargs): # type: ignore with trace.use_span(self.start_span(*args, **kwargs)) as span: # type: ignore yield span diff --git a/opentelemetry-api/tests/trace/test_tracer.py b/opentelemetry-api/tests/trace/test_tracer.py index 687a05504df..f231380351d 100644 --- a/opentelemetry-api/tests/trace/test_tracer.py +++ b/opentelemetry-api/tests/trace/test_tracer.py @@ -14,7 +14,6 @@ import asyncio -import contextlib from unittest import TestCase from opentelemetry.trace import ( @@ -22,7 +21,7 @@ NoOpTracer, Span, Tracer, - agnosticcontextmanager, + _agnosticcontextmanager, get_current_span, ) @@ -47,7 +46,7 @@ class MockTracer(Tracer): def start_span(self, *args, **kwargs): return INVALID_SPAN - @agnosticcontextmanager + @_agnosticcontextmanager # pylint: disable=protected-access def start_as_current_span(self, *args, **kwargs): lst.append(1) yield INVALID_SPAN diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 8e604b5e24e..112d6b5a727 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -1013,7 +1013,7 @@ def __init__( self._span_limits = span_limits self._instrumentation_scope = instrumentation_scope - @trace_api.agnosticcontextmanager + @trace_api._agnosticcontextmanager # pylint: disable=protected-access def start_as_current_span( self, name: str,