Beispiel #1
0
 def test_remove(self):
     w = WeakSet()
     dummy = Dummy()
     w.add(dummy)
     self.assertEqual(dummy in w, True)
     w.remove(dummy)
     self.assertEqual(dummy in w, False)
Beispiel #2
0
 def test_remove(self):
     w = WeakSet()
     dummy = Dummy()
     w.add(dummy)
     self.assertEqual(dummy in w, True)
     w.remove(dummy)
     self.assertEqual(dummy in w, False)
 def test_remove(self):
     from transaction.weakset import WeakSet
     w = WeakSet()
     dummy = Dummy()
     w.add(dummy)
     self.assertEqual(dummy in w, True)
     w.remove(dummy)
     self.assertEqual(dummy in w, False)
 def test_remove(self):
     from transaction.weakset import WeakSet
     w = WeakSet()
     dummy = Dummy()
     w.add(dummy)
     self.assertEqual(dummy in w, True)
     w.remove(dummy)
     self.assertEqual(dummy in w, False)
Beispiel #5
0
class TransactionManager(object):

    def __init__(self):
        self._txn = None
        self._synchs = WeakSet()

    def begin(self):
        if self._txn is not None:
            self._txn.abort()
        txn = self._txn = Transaction(self._synchs, self)
        _new_transaction(txn, self._synchs)
        return txn

    def get(self):
        if self._txn is None:
            self._txn = Transaction(self._synchs, self)
        return self._txn

    def free(self, txn):
        assert txn is self._txn
        self._txn = None

    def registerSynch(self, synch):
        self._synchs.add(synch)

    def unregisterSynch(self, synch):
        self._synchs.remove(synch)

    def isDoomed(self):
        return self.get().isDoomed()

    def doom(self):
        return self.get().doom()

    def commit(self):
        return self.get().commit()

    def abort(self):
        return self.get().abort()

    def savepoint(self, optimistic=False):
        return self.get().savepoint(optimistic)
Beispiel #6
0
class TransactionManager(object):
    def __init__(self):
        self._txn = None
        self._synchs = WeakSet()

    def begin(self):
        if self._txn is not None:
            self._txn.abort()
        txn = self._txn = Transaction(self._synchs, self)
        _new_transaction(txn, self._synchs)
        return txn

    def get(self):
        if self._txn is None:
            self._txn = Transaction(self._synchs, self)
        return self._txn

    def free(self, txn):
        assert txn is self._txn
        self._txn = None

    def registerSynch(self, synch):
        self._synchs.add(synch)

    def unregisterSynch(self, synch):
        self._synchs.remove(synch)

    def isDoomed(self):
        return self.get().isDoomed()

    def doom(self):
        return self.get().doom()

    def commit(self):
        return self.get().commit()

    def abort(self):
        return self.get().abort()

    def savepoint(self, optimistic=False):
        return self.get().savepoint(optimistic)
Beispiel #7
0
class TransactionManager(object):
    def __init__(self):
        self._txn = None
        self._synchs = WeakSet()

    def begin(self):
        """ See ITransactionManager.
        """
        if self._txn is not None:
            self._txn.abort()
        txn = self._txn = Transaction(self._synchs, self)
        _new_transaction(txn, self._synchs)
        return txn

    __enter__ = lambda self: self.begin()

    def get(self):
        """ See ITransactionManager.
        """
        if self._txn is None:
            self._txn = Transaction(self._synchs, self)
        return self._txn

    def free(self, txn):
        if txn is not self._txn:
            raise ValueError("Foreign transaction")
        self._txn = None

    def registerSynch(self, synch):
        """ See ITransactionManager.
        """
        self._synchs.add(synch)
        if self._txn is not None:
            synch.newTransaction(self._txn)

    def unregisterSynch(self, synch):
        """ See ITransactionManager.
        """
        self._synchs.remove(synch)

    def clearSynchs(self):
        """ See ITransactionManager.
        """
        self._synchs.clear()

    def registeredSynchs(self):
        """ See ITransactionManager.
        """
        return bool(self._synchs)

    def isDoomed(self):
        """ See ITransactionManager.
        """
        return self.get().isDoomed()

    def doom(self):
        """ See ITransactionManager.
        """
        return self.get().doom()

    def commit(self):
        """ See ITransactionManager.
        """
        return self.get().commit()

    def abort(self):
        """ See ITransactionManager.
        """
        return self.get().abort()

    def __exit__(self, t, v, tb):
        if v is None:
            self.commit()
        else:
            self.abort()

    def savepoint(self, optimistic=False):
        """ See ITransactionManager.
        """
        return self.get().savepoint(optimistic)

    def attempts(self, number=3):
        if number <= 0:
            raise ValueError("number must be positive")
        while number:
            number -= 1
            if number:
                attempt = Attempt(self)
                yield attempt
                if attempt.success:
                    break
            else:
                yield self

    def _retryable(self, error_type, error):
        if issubclass(error_type, TransientError):
            return True

        for dm in self.get()._resources:
            should_retry = getattr(dm, 'should_retry', None)
            if (should_retry is not None) and should_retry(error):
                return True

    run_no_func_types = int, type(None)

    def run(self, func=None, tries=3):
        if isinstance(func, self.run_no_func_types):
            if func is not None:
                tries = func
            return lambda func: self.run(func, tries)

        if tries <= 0:
            raise ValueError("tries must be positive")

        name = func.__name__
        doc = func.__doc__
        if name != '_':
            if doc:
                doc = name + '\n\n' + doc
            else:
                doc = name

        if doc and not isinstance(doc, text_type):
            doc = doc.decode('utf-8')

        for i in range(1, tries + 1):
            txn = self.begin()
            if doc:
                txn.note(doc)

            try:
                result = func()
                txn.commit()
            except Exception as v:
                if i == tries:
                    raise  # that was our last chance
                retry = self._retryable(v.__class__, v)
                txn.abort()
                if not retry:
                    raise
            else:
                return result
Beispiel #8
0
class TransactionManager(object):

    def __init__(self):
        self._txn = None
        self._synchs = WeakSet()

    def begin(self):
        """ See ITransactionManager.
        """
        if self._txn is not None:
            self._txn.abort()
        txn = self._txn = Transaction(self._synchs, self)
        _new_transaction(txn, self._synchs)
        return txn

    __enter__ = lambda self: self.begin()

    def get(self):
        """ See ITransactionManager.
        """
        if self._txn is None:
            self._txn = Transaction(self._synchs, self)
        return self._txn

    def free(self, txn):
        if txn is not self._txn:
            raise ValueError("Foreign transaction")
        self._txn = None

    def registerSynch(self, synch):
        """ See ITransactionManager.
        """
        self._synchs.add(synch)
        if self._txn is not None:
            synch.newTransaction(self._txn)

    def unregisterSynch(self, synch):
        """ See ITransactionManager.
        """
        self._synchs.remove(synch)

    def clearSynchs(self):
        """ See ITransactionManager.
        """
        self._synchs.clear()

    def registeredSynchs(self):
        """ See ITransactionManager.
        """
        return bool(self._synchs)

    def isDoomed(self):
        """ See ITransactionManager.
        """
        return self.get().isDoomed()

    def doom(self):
        """ See ITransactionManager.
        """
        return self.get().doom()

    def commit(self):
        """ See ITransactionManager.
        """
        return self.get().commit()

    def abort(self):
        """ See ITransactionManager.
        """
        return self.get().abort()

    def __exit__(self, t, v, tb):
        if v is None:
            self.commit()
        else:
            self.abort()

    def savepoint(self, optimistic=False):
        """ See ITransactionManager.
        """
        return self.get().savepoint(optimistic)

    def attempts(self, number=3):
        if number <= 0:
            raise ValueError("number must be positive")
        while number:
            number -= 1
            if number:
                yield Attempt(self)
            else:
                yield self

    def _retryable(self, error_type, error):
        if issubclass(error_type, TransientError):
            return True

        for dm in self.get()._resources:
            should_retry = getattr(dm, 'should_retry', None)
            if (should_retry is not None) and should_retry(error):
                return True
Beispiel #9
0
class TransactionManager(object):
    def __init__(self):
        self._txn = None
        self._synchs = WeakSet()

    def begin(self):
        """ See ITransactionManager.
        """
        if self._txn is not None:
            self._txn.abort()
        txn = self._txn = Transaction(self._synchs, self)
        _new_transaction(txn, self._synchs)
        return txn

    __enter__ = lambda self: self.begin()

    def get(self):
        """ See ITransactionManager.
        """
        if self._txn is None:
            self._txn = Transaction(self._synchs, self)
        return self._txn

    def free(self, txn):
        if txn is not self._txn:
            raise ValueError("Foreign transaction")
        self._txn = None

    def registerSynch(self, synch):
        """ See ITransactionManager.
        """
        self._synchs.add(synch)
        if self._txn is not None:
            synch.newTransaction(self._txn)

    def unregisterSynch(self, synch):
        """ See ITransactionManager.
        """
        self._synchs.remove(synch)

    def clearSynchs(self):
        """ See ITransactionManager.
        """
        self._synchs.clear()

    def registeredSynchs(self):
        """ See ITransactionManager.
        """
        return bool(self._synchs)

    def isDoomed(self):
        """ See ITransactionManager.
        """
        return self.get().isDoomed()

    def doom(self):
        """ See ITransactionManager.
        """
        return self.get().doom()

    def commit(self):
        """ See ITransactionManager.
        """
        return self.get().commit()

    def abort(self):
        """ See ITransactionManager.
        """
        return self.get().abort()

    def __exit__(self, t, v, tb):
        if v is None:
            self.commit()
        else:
            self.abort()

    def savepoint(self, optimistic=False):
        """ See ITransactionManager.
        """
        return self.get().savepoint(optimistic)

    def attempts(self, number=3):
        if number <= 0:
            raise ValueError("number must be positive")
        while number:
            number -= 1
            if number:
                yield Attempt(self)
            else:
                yield self

    def _retryable(self, error_type, error):
        if issubclass(error_type, TransientError):
            return True

        for dm in self.get()._resources:
            should_retry = getattr(dm, 'should_retry', None)
            if (should_retry is not None) and should_retry(error):
                return True
Beispiel #10
0
class TransactionManager(object):
    def __init__(self, explicit=False):
        self.explicit = explicit
        self._txn = None
        self._synchs = WeakSet()

    def begin(self):
        """ See ITransactionManager.
        """
        if self._txn is not None:
            if self.explicit:
                raise AlreadyInTransaction()
            self._txn.abort()
        txn = self._txn = Transaction(self._synchs, self)
        _new_transaction(txn, self._synchs)
        return txn

    __enter__ = lambda self: self.begin()

    def get(self):
        """ See ITransactionManager.
        """
        if self._txn is None:
            if self.explicit:
                raise NoTransaction()
            self._txn = Transaction(self._synchs, self)
        return self._txn

    def free(self, txn):
        if txn is not self._txn:
            raise ValueError("Foreign transaction")
        self._txn = None

    def registerSynch(self, synch):
        """ See ITransactionManager.
        """
        self._synchs.add(synch)
        if self._txn is not None:
            synch.newTransaction(self._txn)

    def unregisterSynch(self, synch):
        """ See ITransactionManager.
        """
        self._synchs.remove(synch)

    def clearSynchs(self):
        """ See ITransactionManager.
        """
        self._synchs.clear()

    def registeredSynchs(self):
        """ See ITransactionManager.
        """
        return bool(self._synchs)

    def isDoomed(self):
        """ See ITransactionManager.
        """
        return self.get().isDoomed()

    def doom(self):
        """ See ITransactionManager.
        """
        return self.get().doom()

    def commit(self):
        """ See ITransactionManager.
        """
        return self.get().commit()

    def abort(self):
        """ See ITransactionManager.
        """
        return self.get().abort()

    def __exit__(self, t, v, tb):
        if v is None:
            self.commit()
        else:
            self.abort()

    def savepoint(self, optimistic=False):
        """ See ITransactionManager.
        """
        return self.get().savepoint(optimistic)

    def attempts(self, number=3):
        if number <= 0:
            raise ValueError("number must be positive")
        while number:
            number -= 1
            if number:
                attempt = Attempt(self)
                yield attempt
                if attempt.success:
                    break
            else:
                yield self

    def _retryable(self, error_type, error):
        if issubclass(error_type, TransientError):
            return True

        for dm in self.get()._resources:
            should_retry = getattr(dm, 'should_retry', None)
            if (should_retry is not None) and should_retry(error):
                return True
        return False

    run_no_func_types = int, type(None)

    def run(self, func=None, tries=3):
        if isinstance(func, self.run_no_func_types):
            if func is not None:
                tries = func
            return lambda func: self.run(func, tries)

        if tries <= 0:
            raise ValueError("tries must be positive")

        # These are ordinarily native strings, but that's
        # not required. A callable class could override them
        # to anything, and a Python 2.7 file could have
        # imported `from __future__ import unicode_literals`
        # which gets unicode docstrings.
        name = func.__name__
        doc = func.__doc__

        name = text_(name) if name else u''
        doc = text_(doc) if doc else u''

        if name != u'_':
            if doc:
                doc = name + u'\n\n' + doc
            else:
                doc = name

        for i in range(1, tries + 1):  # pragma: no branch
            txn = self.begin()
            if doc:
                txn.note(doc)

            try:
                result = func()
                txn.commit()
            except Exception as v:
                if i == tries:
                    raise  # that was our last chance
                retry = self._retryable(v.__class__, v)
                txn.abort()
                if not retry:
                    raise
            else:
                return result
Beispiel #11
0
class TransactionManager(object):
    def __init__(self):
        self._txn = None
        self._synchs = WeakSet()

    def begin(self):
        if self._txn is not None:
            self._txn.abort()
        txn = self._txn = Transaction(self._synchs, self)
        _new_transaction(txn, self._synchs)
        return txn

    __enter__ = lambda self: self.begin()

    def get(self):
        if self._txn is None:
            self._txn = Transaction(self._synchs, self)
        return self._txn

    def free(self, txn):
        assert txn is self._txn
        self._txn = None

    def registerSynch(self, synch):
        self._synchs.add(synch)

    def unregisterSynch(self, synch):
        self._synchs.remove(synch)

    def isDoomed(self):
        return self.get().isDoomed()

    def doom(self):
        return self.get().doom()

    def commit(self):
        return self.get().commit()

    def abort(self):
        return self.get().abort()

    def __exit__(self, t, v, tb):
        if v is None:
            self.commit()
        else:
            self.abort()

    def savepoint(self, optimistic=False):
        return self.get().savepoint(optimistic)

    def attempts(self, number=3):
        assert number > 0
        while number:
            number -= 1
            if number:
                yield Attempt(self)
            else:
                yield self

    def _retryable(self, error_type, error):
        if issubclass(error_type, TransientError):
            return True

        for dm in self.get()._resources:
            should_retry = getattr(dm, 'should_retry', None)
            if (should_retry is not None) and should_retry(error):
                return True
Beispiel #12
0
class TransactionManager(object):

    def __init__(self):
        self._txn = None
        self._synchs = WeakSet()

    def begin(self):
        if self._txn is not None:
            self._txn.abort()
        txn = self._txn = Transaction(self._synchs, self)
        _new_transaction(txn, self._synchs)
        return txn

    __enter__ = lambda self: self.begin()

    def get(self):
        if self._txn is None:
            self._txn = Transaction(self._synchs, self)
        return self._txn

    def free(self, txn):
        assert txn is self._txn
        self._txn = None

    def registerSynch(self, synch):
        self._synchs.add(synch)

    def unregisterSynch(self, synch):
        self._synchs.remove(synch)

    def isDoomed(self):
        return self.get().isDoomed()

    def doom(self):
        return self.get().doom()

    def commit(self):
        return self.get().commit()

    def abort(self):
        return self.get().abort()

    def __exit__(self, t, v, tb):
        if v is None:
            self.commit()
        else:
            self.abort()

    def savepoint(self, optimistic=False):
        return self.get().savepoint(optimistic)

    def attempts(self, number=3):
        assert number > 0
        while number:
            number -= 1
            if number:
                yield Attempt(self)
            else:
                yield self

    def _retryable(self, error_type, error):
        if issubclass(error_type, TransientError):
            return True

        for dm in self.get()._resources:
            should_retry = getattr(dm, 'should_retry', None)
            if (should_retry is not None) and should_retry(error):
                return True
Beispiel #13
0
class TransactionManager(object):

    def __init__(self):
        self._txn = None
        self._synchs = WeakSet()

    def begin(self):
        """ See ITransactionManager.
        """
        if self._txn is not None:
            self._txn.abort()
        txn = self._txn = Transaction(self._synchs, self)
        _new_transaction(txn, self._synchs)
        return txn

    __enter__ = lambda self: self.begin()

    def get(self):
        """ See ITransactionManager.
        """
        if self._txn is None:
            self._txn = Transaction(self._synchs, self)
        return self._txn

    def free(self, txn):
        if txn is not self._txn:
            raise ValueError("Foreign transaction")
        self._txn = None

    def registerSynch(self, synch):
        """ See ITransactionManager.
        """
        self._synchs.add(synch)
        if self._txn is not None:
            synch.newTransaction(self._txn)

    def unregisterSynch(self, synch):
        """ See ITransactionManager.
        """
        self._synchs.remove(synch)

    def clearSynchs(self):
        """ See ITransactionManager.
        """
        self._synchs.clear()

    def registeredSynchs(self):
        """ See ITransactionManager.
        """
        return bool(self._synchs)

    def isDoomed(self):
        """ See ITransactionManager.
        """
        return self.get().isDoomed()

    def doom(self):
        """ See ITransactionManager.
        """
        return self.get().doom()

    def commit(self):
        """ See ITransactionManager.
        """
        return self.get().commit()

    def abort(self):
        """ See ITransactionManager.
        """
        return self.get().abort()

    def __exit__(self, t, v, tb):
        if v is None:
            self.commit()
        else:
            self.abort()

    def savepoint(self, optimistic=False):
        """ See ITransactionManager.
        """
        return self.get().savepoint(optimistic)

    def attempts(self, number=3):
        if number <= 0:
            raise ValueError("number must be positive")
        while number:
            number -= 1
            if number:
                attempt = Attempt(self)
                yield attempt
                if attempt.success:
                    break
            else:
                yield self

    def _retryable(self, error_type, error):
        if issubclass(error_type, TransientError):
            return True

        for dm in self.get()._resources:
            should_retry = getattr(dm, 'should_retry', None)
            if (should_retry is not None) and should_retry(error):
                return True

    run_no_func_types = int, type(None)
    def run(self, func=None, tries=3):
        if isinstance(func, self.run_no_func_types):
            if func is not None:
                tries = func
            return lambda func: self.run(func, tries)

        if tries <= 0:
            raise ValueError("tries must be positive")

        name = func.__name__
        doc = func.__doc__
        if name != '_':
            if doc:
                doc = name + '\n\n' + doc
            else:
                doc = name

        if doc and not isinstance(doc, text_type):
            doc = doc.decode('utf-8')

        for i in range(1, tries + 1):  # pragma: no branch
            txn = self.begin()
            if doc:
                txn.note(doc)

            try:
                result = func()
                txn.commit()
            except Exception as v:
                if i == tries:
                    raise # that was our last chance
                retry = self._retryable(v.__class__, v)
                txn.abort()
                if not retry:
                    raise
            else:
                return result