Sylvain Prat  committed ed944a0

ScopeManager.enter_scope now raise a ScopeReenterError when re-entering a
Improved the documentation

  • Participants
  • Parent commits ddb150e

Comments (0)

Files changed (4)

 * The locking mechanism is now available for all scopes.
 * The context manager is now responsible for updating the context dictionaries.
 * Fixed duplicate factory calls when providing a factory returning None
+* ScopeManager.enter_scope now raise a ScopeReenterError when re-entering a
+  scope
 * ScopeManager.exit_scope now raise a UndefinedScopeError when exiting an
   undeclared scope
 * Fixed the API documentation

File docs/source/api.rst

 .. autoexception:: yaak.inject.MissingFeatureError
+.. autoexception:: yaak.inject.ScopeError
 .. autoexception:: yaak.inject.UndefinedScopeError
+.. autoexception:: yaak.inject.ScopeReenterError
 .. autoexception:: yaak.inject.BindNotSupportedError

File yaak/

     """One instance per HTTP session"""
-class UndefinedScopeError(Exception):
+class ScopeError(Exception):
+    """Base class for all scope related errors"""
+class UndefinedScopeError(ScopeError):
     """Exception raised when using a scope that has not been entered yet."""
+class ScopeReenterError(ScopeError):
+    """Exception raised when re-entering a scope that has already been
+    entered."""
 # global application context
 _ApplicationContext = {}
 # global to acquire when updating the application context
         useful for implementing session scopes, when we want to reinstall
         a previous context. You can also pass a lock to acquire when modifying
         the context dictionary via the parameter *context_lock* if the scope
-        is subject to thread concurrency issues."""
+        is subject to thread concurrency issues. Raises a
+        :exc:`yaak.inject.ScopeReenterError` when re-entering an already
+        entered *scope*."""
+        if scope in self._context:
+            raise ScopeReenterError("Scope %s already defined" % scope)
         self._context[scope] = (context if context is not None else {},
 class BindNotSupportedError(Exception):
-    """Exception raised when a function could not be used in the bind method."""
+    """Exception raised when a function could not be used in the
+    :meth:`yaak.inject.bind` method."""
 def bind(func, **frozen_args):

File yaak/tests/

                           lambda: self.scope_manager._get_context('MyScope'))
+    def test_enter_scope_error(self):
+        self.scope_manager.enter_scope('MyScope')
+        self.assertRaises(inject.ScopeReenterError,
+                          lambda: self.scope_manager.enter_scope('MyScope'))
     def test_exit_scope_error(self):
                           lambda: self.scope_manager.exit_scope('MyScope'))