server/session.py
changeset 8815 18022ed7c7a5
parent 8814 37fd037c0526
child 8816 fe321c0f6d13
equal deleted inserted replaced
8814:37fd037c0526 8815:18022ed7c7a5
   111     methods.
   111     methods.
   112     """
   112     """
   113     def __init__(self, session, mode, *categories):
   113     def __init__(self, session, mode, *categories):
   114         assert mode in (HOOKS_ALLOW_ALL, HOOKS_DENY_ALL)
   114         assert mode in (HOOKS_ALLOW_ALL, HOOKS_DENY_ALL)
   115         self.session = session
   115         self.session = session
       
   116         self.tx = session._tx
   116         self.mode = mode
   117         self.mode = mode
   117         self.categories = categories
   118         self.categories = categories
   118         self.oldmode = None
   119         self.oldmode = None
   119         self.changes = ()
   120         self.changes = ()
   120 
   121 
   121     def __enter__(self):
   122     def __enter__(self):
   122         self.oldmode = self.session._tx.hooks_mode
   123         self.oldmode = self.tx.hooks_mode
   123         self.session._tx.hooks_mode = self.mode
   124         self.tx.hooks_mode = self.mode
   124         if self.mode is HOOKS_DENY_ALL:
   125         if self.mode is HOOKS_DENY_ALL:
   125             self.changes = self.session.enable_hook_categories(*self.categories)
   126             self.changes = self.tx.enable_hook_categories(*self.categories)
   126         else:
   127         else:
   127             self.changes = self.session.disable_hook_categories(*self.categories)
   128             self.changes = self.tx.disable_hook_categories(*self.categories)
   128         self.session._tx.ctx_count += 1
   129         self.tx.ctx_count += 1
   129 
   130 
   130     def __exit__(self, exctype, exc, traceback):
   131     def __exit__(self, exctype, exc, traceback):
   131         tx = self.session._tx
   132         self.tx.ctx_count -= 1
   132         tx.ctx_count -= 1
   133         if self.tx.ctx_count == 0:
   133         if tx.ctx_count == 0:
   134             self.session._clear_thread_storage(self.tx)
   134             self.session._clear_thread_storage(tx)
       
   135         else:
   135         else:
   136             try:
   136             try:
   137                 if self.categories:
   137                 if self.categories:
   138                     if self.mode is HOOKS_DENY_ALL:
   138                     if self.mode is HOOKS_DENY_ALL:
   139                         self.session.disable_hook_categories(*self.categories)
   139                         self.tx.disable_hook_categories(*self.categories)
   140                     else:
   140                     else:
   141                         self.session.enable_hook_categories(*self.categories)
   141                         self.tx.enable_hook_categories(*self.categories)
   142             finally:
   142             finally:
   143                 self.session._tx.hooks_mode = self.oldmode
   143                 self.tx.hooks_mode = self.oldmode
   144 
   144 
   145 
   145 
   146 class security_enabled(object):
   146 class security_enabled(object):
   147     """context manager to control security w/ session.execute,
   147     """context manager to control security w/ session.execute,
   148 
   148