Skip to content

Commit

Permalink
Lint the code.
Browse files Browse the repository at this point in the history
Additionally move the code to a src directory so meta/config can be
used later on.
  • Loading branch information
Michael Howitz committed Oct 30, 2020
1 parent 88299fe commit 12b413e
Show file tree
Hide file tree
Showing 17 changed files with 372 additions and 232 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ python:
- 3.8
- pypy2
- pypy3
matrix:
include:
- name: "lint"
python: 3.7
env: TOXENV="lint"

install:
- pip install zope.testrunner coverage coveralls
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def _read_file(filename):
url="https://github.com/zopefoundation/transaction",
license="ZPL 2.1",
platforms=["any"],
packages=find_packages(),
packages=find_packages('src'),
package_dir={'': 'src'},
include_package_data=True,
zip_safe=False,
test_suite="transaction.tests",
Expand Down
10 changes: 5 additions & 5 deletions transaction/__init__.py → src/transaction/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"""

#: Default implementation of `~ITransaction`
from transaction._transaction import Transaction
from transaction._transaction import Transaction # noqa: F401 unused import
#: Default implementation of `~ISavepoint`
from transaction._transaction import Savepoint
from transaction._transaction import Savepoint # noqa: F401 unused import
#: A single-threaded `~ITransactionManager`
from transaction._manager import TransactionManager
from transaction._manager import TransactionManager # noqa: F401 unused import
#: A thread-safe `~ITransactionManager`
from transaction._manager import ThreadTransactionManager

Expand All @@ -29,8 +29,8 @@
# via getattr and getattribute; see http://bugs.python.org/issue12022. On
# Python 3, you must use ``with transaction.manager`` instead.

#: The default transaction manager (a `~.ThreadTransactionManager`). All other functions in
#: this module refer to this object.
#: The default transaction manager (a `~.ThreadTransactionManager`). All other
#: functions in this module refer to this object.
manager = ThreadTransactionManager()
#: See `.ITransactionManager.get`
get = __enter__ = manager.get
Expand Down
22 changes: 13 additions & 9 deletions transaction/_compat.py → src/transaction/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@

if PY3: # pragma: no cover
text_type = str
else: # pragma: no cover
else: # pragma: no cover
# py2
text_type = unicode
text_type = unicode # noqa: F821 undefined name 'unicode'


def text_(s):
if not isinstance(s, text_type): # pragma: no cover
s = s.decode('utf-8')
return s


if PY3: # pragma: no cover
def native_(s, encoding='latin-1', errors='strict'):
if isinstance(s, text_type):
Expand All @@ -28,10 +30,12 @@ def native_(s, encoding='latin-1', errors='strict'):

if PY3: # pragma: no cover
from io import StringIO
else: # pragma: no cover
else: # pragma: no cover
from io import BytesIO
# Prevent crashes in IPython when writing tracebacks if a commit fails
# ref: https://github.com/ipython/ipython/issues/9126#issuecomment-174966638
# ref:
# https://github.com/ipython/ipython/issues/9126#issuecomment-174966638

class StringIO(BytesIO):
def write(self, s):
s = native_(s, encoding='utf-8')
Expand All @@ -40,11 +44,11 @@ def write(self, s):

if PY3: # pragma: no cover
def reraise(tp, value, tb=None):
if value.__traceback__ is not tb: # pragma: no cover
if value.__traceback__ is not tb: # pragma: no cover
raise value.with_traceback(tb)
raise value

else: # pragma: no cover
else: # pragma: no cover
def exec_(code, globs=None, locs=None):
"""Execute code in a namespace."""
if globs is None:
Expand All @@ -64,6 +68,6 @@ def exec_(code, globs=None, locs=None):

try: # pragma: no cover
from threading import get_ident as get_thread_ident
except ImportError: # pragma: no cover
# py2
from thread import get_ident as get_thread_ident
except ImportError: # pragma: no cover
# PY2
from thread import get_ident as get_thread_ident # noqa: F401 unused
25 changes: 13 additions & 12 deletions transaction/_manager.py → src/transaction/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ def _new_transaction(txn, synchs):

@implementer(ITransactionManager)
class TransactionManager(object):
"""
Single-thread implementation of `~transaction.interfaces.ITransactionManager`.
"""Single-thread implementation of
`~transaction.interfaces.ITransactionManager`.
"""

def __init__(self, explicit=False):
Expand All @@ -71,8 +71,7 @@ def __init__(self, explicit=False):
self._synchs = WeakSet()

def begin(self):
""" See `~transaction.interfaces.ITransactionManager`.
"""
"""See `~transaction.interfaces.ITransactionManager`."""
if self._txn is not None:
if self.explicit:
raise AlreadyInTransaction()
Expand All @@ -81,11 +80,11 @@ def begin(self):
_new_transaction(txn, self._synchs)
return txn

__enter__ = lambda self: self.begin()
def __enter__(self):
self.begin()

def get(self):
""" See `~transaction.interfaces.ITransactionManager`.
"""
"""See `~transaction.interfaces.ITransactionManager`."""
if self._txn is None:
if self.explicit:
raise NoTransaction()
Expand Down Expand Up @@ -174,6 +173,7 @@ def _retryable(self, error_type, error):
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:
Expand Down Expand Up @@ -222,8 +222,8 @@ def run(self, func=None, tries=3):

@implementer(ITransactionManager)
class ThreadTransactionManager(threading.local):
"""
Thread-local `transaction manager <transaction.interfaces.ITransactionManager>`.
"""Thread-local
`transaction manager <transaction.interfaces.ITransactionManager>`.
A thread-local transaction manager can be used as a global
variable, but has a separate copy for each thread.
Expand Down Expand Up @@ -289,6 +289,7 @@ def attempts(self, number=3):
def run(self, func=None, tries=3):
return self.manager.run(func, tries)


class Attempt(object):

success = False
Expand All @@ -300,8 +301,8 @@ def _retry_or_raise(self, t, v, tb):
retry = self.manager._retryable(t, v)
self.manager.abort()
if retry:
return retry # suppress the exception if necessary
reraise(t, v, tb) # otherwise reraise the exception
return retry # suppress the exception if necessary
reraise(t, v, tb) # otherwise reraise the exception

def __enter__(self):
return self.manager.__enter__()
Expand All @@ -310,7 +311,7 @@ def __exit__(self, t, v, tb):
if v is None:
try:
self.manager.commit()
except:
except: # noqa: E722 do not use bare 'except'
return self._retry_or_raise(*sys.exc_info())
else:
self.success = True
Expand Down

0 comments on commit 12b413e

Please sign in to comment.