Skip to content

Commit

Permalink
fix: make agnosticcontextmanager as protected
Browse files Browse the repository at this point in the history
Signed-off-by: QuentinN42 <quentin@lieumont.fr>
  • Loading branch information
QuentinN42 committed Jan 28, 2024
1 parent a054a97 commit 11c51cb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 23 additions & 43 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand All @@ -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)
Expand Down Expand Up @@ -441,7 +421,7 @@ def start_span(
The newly-created span.
"""

@agnosticcontextmanager
@_agnosticcontextmanager
@abstractmethod
def start_as_current_span(
self,
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-api/tests/trace/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions opentelemetry-api/tests/trace/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@


import asyncio
import contextlib
from unittest import TestCase

from opentelemetry.trace import (
INVALID_SPAN,
NoOpTracer,
Span,
Tracer,
agnosticcontextmanager,
_agnosticcontextmanager,
get_current_span,
)

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 11c51cb

Please sign in to comment.