Exemple #1
0
    def tm_tween(request):
        if 'repoze.tm.active' in request.environ:
            # don't handle txn mgmt if repoze.tm is in the WSGI pipeline
            return handler(request)

        if activate is not None:
            if not activate(request):
                return handler(request)

        manager = getattr(request, 'tm', None)
        if manager is None: # pragma: no cover (pyramid < 1.4)
            manager = create_tm(request)
            request.tm = manager
        number = attempts
        if annotate_user:
            if hasattr(request, 'unauthenticated_userid'):
                userid = request.unauthenticated_userid
            else: # pragma no cover (for pyramid < 1.5)
                from pyramid.security import unauthenticated_userid
                userid = unauthenticated_userid(request)
        else:
            userid = None

        while number:
            number -= 1
            try:
                manager.begin()
                # make_body_seekable will copy wsgi.input if necessary,
                # otherwise it will rewind the copy to position zero
                if attempts != 1:
                    request.make_body_seekable()
                t = manager.get()
                if userid:
                    userid = native_(userid, 'utf-8')
                    t.setUser(userid, '')
                try:
                    t.note(native_(request.path_info, 'utf-8'))
                except UnicodeDecodeError:
                    t.note("Unable to decode path as unicode")
                response = handler(request)
                if manager.isDoomed():
                    raise AbortResponse(response)
                if commit_veto is not None:
                    veto = commit_veto(request, response)
                    if veto:
                        raise AbortResponse(response)
                manager.commit()
                return response
            except AbortResponse as e:
                manager.abort()
                return e.response
            except:
                exc_info = sys.exc_info()
                try:
                    retryable = manager._retryable(*exc_info[:-1])
                    manager.abort()
                    if (number <= 0) or (not retryable):
                        reraise(*exc_info)
                finally:
                    del exc_info # avoid leak
 def test_handler_w_unicode_unauthenticated_userid(self):
     from pyramid_tm.compat import native_
     from pyramid_tm.compat import PY3
     USERID = b'phred/\xd1\x80\xd0\xb5\xd1\x81'.decode('utf-8')
     self.config.testing_securitypolicy(userid=USERID)
     self._callFUT()
     if PY3:
         self.assertEqual(self.txn.username, ' phred/рес')
     else:
         self.assertEqual(self.txn.username, ' ' + native_(USERID, 'utf-8'))
 def test_handler_w_unicode_unauthenticated_userid(self):
     from pyramid_tm.compat import native_
     from pyramid_tm.compat import PY3
     USERID = b'phred/\xd1\x80\xd0\xb5\xd1\x81'.decode('utf-8')
     self.config.testing_securitypolicy(userid=USERID)
     self._callFUT()
     if PY3:
         self.assertEqual(self.txn.username, ' phred/рес')
     else:
         self.assertEqual(self.txn.username,
                          ' ' + native_(USERID, 'utf-8'))