@@ -39,14 +39,15 @@ def __exit__(self, exc_type, exc_value, tb):
3939
4040class _ScopeManager (object ):
4141
42- def __init__ (self , hub ):
42+ def __init__ (self , hub , layer ):
4343 self ._hub = hub
44+ self ._layer = layer
4445
4546 def __enter__ (self ):
4647 return self
4748
4849 def __exit__ (self , exc_type , exc_value , tb ):
49- self ._hub ._stack .pop ()
50+ assert self ._hub ._stack .pop () == self . _layer , 'popped wrong scope'
5051
5152
5253class Hub (with_metaclass (HubMeta )):
@@ -162,11 +163,18 @@ def add_event_processor(self, factory):
162163 self ._pending_processors .append (factory )
163164
164165 def push_scope (self ):
165- """Pushes a new layer on the scope stack."""
166+ """Pushes a new layer on the scope stack. Returns a context manager
167+ that should be used to pop the scope again."""
166168 self ._flush_event_processors ()
167169 client , scope = self ._stack [- 1 ]
168- self ._stack .append ((client , copy .copy (scope )))
169- return _ScopeManager (self )
170+ new_layer = (client , copy .copy (scope ))
171+ self ._stack .append (new_layer )
172+ return _ScopeManager (self , new_layer )
173+
174+ def pop_scope_unsafe (self ):
175+ """Pops a scope layer from the stack. Try to use the context manager
176+ `push_scope()` instead."""
177+ self ._stack .pop ()
170178
171179 @contextmanager
172180 def configure_scope (self ):
0 commit comments