Merge tag '1.3.3' into debian/unstable
Release 1.3.3
This commit is contained in:
commit
caa878ef96
12
AUTHORS.rst
12
AUTHORS.rst
@ -19,3 +19,15 @@ Patches and Suggestions
|
||||
- Rees Dooley
|
||||
- Saul Shanabrook
|
||||
- Daniel Nephin
|
||||
- Simeon Visser
|
||||
- Joshua Harlow
|
||||
- Pierre-Yves Chibon
|
||||
- Haïkel Guémar
|
||||
- Thomas Goirand
|
||||
- James Page
|
||||
- Josh Marshall
|
||||
- Dougal Matthews
|
||||
- Monty Taylor
|
||||
- Maxym Shalenyi
|
||||
- Jonathan Herriott
|
||||
- Job Evers
|
17
HISTORY.rst
17
HISTORY.rst
@ -2,6 +2,23 @@
|
||||
|
||||
History
|
||||
-------
|
||||
1.3.3 (2014-12-14)
|
||||
++++++++++++++++++
|
||||
- Add minimum six version of 1.7.0 since anything less will break things
|
||||
|
||||
1.3.2 (2014-11-09)
|
||||
++++++++++++++++++
|
||||
- Ensure we wrap the decorated functions to prevent information loss
|
||||
- Allow a jitter value to be passed in
|
||||
|
||||
1.3.1 (2014-09-30)
|
||||
++++++++++++++++++
|
||||
- Add requirements.txt to MANIFEST.in to fix pip installs
|
||||
|
||||
1.3.0 (2014-09-30)
|
||||
++++++++++++++++++
|
||||
- Add upstream six dependency, remove embedded six functionality
|
||||
|
||||
1.2.3 (2014-08-25)
|
||||
++++++++++++++++++
|
||||
- Add support for custom wait and stop functions
|
||||
|
@ -1 +1 @@
|
||||
include README.rst HISTORY.rst AUTHORS.rst LICENSE NOTICE
|
||||
include README.rst HISTORY.rst AUTHORS.rst LICENSE NOTICE requirements.txt
|
||||
|
@ -0,0 +1 @@
|
||||
six>=1.7.0
|
62
retrying.py
62
retrying.py
@ -12,59 +12,12 @@
|
||||
## See the License for the specific language governing permissions and
|
||||
## limitations under the License.
|
||||
|
||||
## --- The following is for portions of the "six" module ----------------------
|
||||
##
|
||||
## Copyright (c) 2010-2014 Benjamin Peterson
|
||||
##
|
||||
## Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
## of this software and associated documentation files (the "Software"), to deal
|
||||
## in the Software without restriction, including without limitation the rights
|
||||
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
## copies of the Software, and to permit persons to whom the Software is
|
||||
## furnished to do so, subject to the following conditions:
|
||||
##
|
||||
## The above copyright notice and this permission notice shall be included in all
|
||||
## copies or substantial portions of the Software.
|
||||
##
|
||||
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
## SOFTWARE.
|
||||
## ----------------------------------------------------------------------------
|
||||
|
||||
import random
|
||||
import six
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
|
||||
# Python 3 compatibility hacks, pilfered from https://pypi.python.org/pypi/six/1.6.1
|
||||
PY3 = sys.version_info[0] == 3
|
||||
if PY3:
|
||||
def reraise(tp, value, tb=None):
|
||||
if value.__traceback__ is not tb:
|
||||
raise value.with_traceback(tb)
|
||||
raise value
|
||||
|
||||
else:
|
||||
def exec_(_code_, _globs_=None, _locs_=None):
|
||||
"""Execute code in a namespace."""
|
||||
if _globs_ is None:
|
||||
frame = sys._getframe(1)
|
||||
_globs_ = frame.f_globals
|
||||
if _locs_ is None:
|
||||
_locs_ = frame.f_locals
|
||||
del frame
|
||||
elif _locs_ is None:
|
||||
_locs_ = _globs_
|
||||
exec("""exec _code_ in _globs_, _locs_""")
|
||||
|
||||
|
||||
exec_("""def reraise(tp, value, tb=None):
|
||||
raise tp, value, tb
|
||||
""")
|
||||
|
||||
# sys.maxint / 2, since Python 3.2 doesn't have a sys.maxint...
|
||||
MAX_WAIT = 1073741823
|
||||
@ -79,6 +32,8 @@ def retry(*dargs, **dkw):
|
||||
# support both @retry and @retry() as valid syntax
|
||||
if len(dargs) == 1 and callable(dargs[0]):
|
||||
def wrap_simple(f):
|
||||
|
||||
@six.wraps(f)
|
||||
def wrapped_f(*args, **kw):
|
||||
return Retrying().call(f, *args, **kw)
|
||||
|
||||
@ -88,6 +43,8 @@ def retry(*dargs, **dkw):
|
||||
|
||||
else:
|
||||
def wrap(f):
|
||||
|
||||
@six.wraps(f)
|
||||
def wrapped_f(*args, **kw):
|
||||
return Retrying(*dargs, **dkw).call(f, *args, **kw)
|
||||
|
||||
@ -110,7 +67,8 @@ class Retrying(object):
|
||||
retry_on_result=None,
|
||||
wrap_exception=False,
|
||||
stop_func=None,
|
||||
wait_func=None):
|
||||
wait_func=None,
|
||||
wait_jitter_max=None):
|
||||
|
||||
self._stop_max_attempt_number = 5 if stop_max_attempt_number is None else stop_max_attempt_number
|
||||
self._stop_max_delay = 100 if stop_max_delay is None else stop_max_delay
|
||||
@ -121,6 +79,7 @@ class Retrying(object):
|
||||
self._wait_incrementing_increment = 100 if wait_incrementing_increment is None else wait_incrementing_increment
|
||||
self._wait_exponential_multiplier = 1 if wait_exponential_multiplier is None else wait_exponential_multiplier
|
||||
self._wait_exponential_max = MAX_WAIT if wait_exponential_max is None else wait_exponential_max
|
||||
self._wait_jitter_max = 0 if wait_jitter_max is None else wait_jitter_max
|
||||
|
||||
# TODO add chaining of stop behaviors
|
||||
# stop behavior
|
||||
@ -255,6 +214,9 @@ class Retrying(object):
|
||||
raise RetryError(attempt)
|
||||
else:
|
||||
sleep = self.wait(attempt_number, delay_since_first_attempt_ms)
|
||||
if self._wait_jitter_max:
|
||||
jitter = random.random() * self._wait_jitter_max
|
||||
sleep = sleep + max(0, jitter)
|
||||
time.sleep(sleep / 1000.0)
|
||||
|
||||
attempt_number += 1
|
||||
@ -282,7 +244,7 @@ class Attempt(object):
|
||||
if wrap_exception:
|
||||
raise RetryError(self)
|
||||
else:
|
||||
reraise(self.value[0], self.value[1], self.value[2])
|
||||
six.reraise(self.value[0], self.value[1], self.value[2])
|
||||
else:
|
||||
return self.value
|
||||
|
||||
|
15
setup.py
15
setup.py
@ -29,12 +29,20 @@ CLASSIFIERS = [
|
||||
'Topic :: Utilities',
|
||||
]
|
||||
|
||||
with open('README.rst') as file_readme:
|
||||
readme = file_readme.read()
|
||||
|
||||
with open('HISTORY.rst') as file_history:
|
||||
history = file_history.read()
|
||||
|
||||
with open('requirements.txt') as file_requirements:
|
||||
requirements = file_requirements.read().splitlines()
|
||||
|
||||
settings.update(
|
||||
name='retrying',
|
||||
version='1.2.3',
|
||||
version='1.3.3',
|
||||
description='Retrying',
|
||||
long_description=open('README.rst').read() + '\n\n' +
|
||||
open('HISTORY.rst').read(),
|
||||
long_description=readme + '\n\n' + history,
|
||||
author='Ray Holder',
|
||||
license='Apache 2.0',
|
||||
url='https://github.com/rholder/retrying',
|
||||
@ -42,6 +50,7 @@ settings.update(
|
||||
keywords="decorator decorators retry retrying exception exponential backoff",
|
||||
py_modules= ['retrying'],
|
||||
test_suite="test_retrying",
|
||||
install_requires=requirements,
|
||||
)
|
||||
|
||||
|
||||
|
@ -19,6 +19,7 @@ from retrying import RetryError
|
||||
from retrying import Retrying
|
||||
from retrying import retry
|
||||
|
||||
|
||||
class TestStopConditions(unittest.TestCase):
|
||||
|
||||
def test_never_stop(self):
|
||||
@ -38,7 +39,7 @@ class TestStopConditions(unittest.TestCase):
|
||||
self.assertTrue(r.stop(2, 1001))
|
||||
|
||||
def test_legacy_explicit_stop_type(self):
|
||||
r = Retrying(stop="stop_after_attempt")
|
||||
Retrying(stop="stop_after_attempt")
|
||||
|
||||
def test_stop_func(self):
|
||||
r = Retrying(stop_func=lambda attempt, delay: attempt == delay)
|
||||
@ -70,7 +71,9 @@ class TestWaitConditions(unittest.TestCase):
|
||||
times.add(r.wait(1, 6546))
|
||||
times.add(r.wait(1, 6546))
|
||||
times.add(r.wait(1, 6546))
|
||||
self.assertTrue(len(times) > 1) # this is kind of non-deterministic...
|
||||
|
||||
# this is kind of non-deterministic...
|
||||
self.assertTrue(len(times) > 1)
|
||||
for t in times:
|
||||
self.assertTrue(t >= 1000)
|
||||
self.assertTrue(t <= 2000)
|
||||
@ -82,7 +85,9 @@ class TestWaitConditions(unittest.TestCase):
|
||||
times.add(r.wait(1, 6546))
|
||||
times.add(r.wait(1, 6546))
|
||||
times.add(r.wait(1, 6546))
|
||||
self.assertTrue(len(times) > 1) # this is kind of non-deterministic...
|
||||
|
||||
# this is kind of non-deterministic...
|
||||
self.assertTrue(len(times) > 1)
|
||||
for t in times:
|
||||
self.assertTrue(t >= 0)
|
||||
self.assertTrue(t <= 2000)
|
||||
@ -119,7 +124,7 @@ class TestWaitConditions(unittest.TestCase):
|
||||
self.assertEqual(r.wait(50, 0), 50000)
|
||||
|
||||
def test_legacy_explicit_wait_type(self):
|
||||
r = Retrying(wait="exponential_sleep")
|
||||
Retrying(wait="exponential_sleep")
|
||||
|
||||
def test_wait_func(self):
|
||||
r = Retrying(wait_func=lambda attempt, delay: attempt * delay)
|
||||
@ -146,6 +151,7 @@ class NoneReturnUntilAfterCount:
|
||||
return None
|
||||
return True
|
||||
|
||||
|
||||
class NoIOErrorAfterCount:
|
||||
"""
|
||||
This class holds counter state for invoking a method several times in a row.
|
||||
@ -164,6 +170,7 @@ class NoIOErrorAfterCount:
|
||||
raise IOError("Hi there, I'm an IOError")
|
||||
return True
|
||||
|
||||
|
||||
class NoNameErrorAfterCount:
|
||||
"""
|
||||
This class holds counter state for invoking a method several times in a row.
|
||||
@ -182,6 +189,7 @@ class NoNameErrorAfterCount:
|
||||
raise NameError("Hi there, I'm a NameError")
|
||||
return True
|
||||
|
||||
|
||||
class CustomError(Exception):
|
||||
"""
|
||||
This is a custom exception class. Note that For Python 2.x, we don't
|
||||
@ -198,6 +206,7 @@ class CustomError(Exception):
|
||||
def __str__(self):
|
||||
return repr(self.value)
|
||||
|
||||
|
||||
class NoCustomErrorAfterCount:
|
||||
"""
|
||||
This class holds counter state for invoking a method several times in a row.
|
||||
@ -217,40 +226,49 @@ class NoCustomErrorAfterCount:
|
||||
raise CustomError(derived_message)
|
||||
return True
|
||||
|
||||
|
||||
def retry_if_result_none(result):
|
||||
return result is None
|
||||
|
||||
|
||||
def retry_if_exception_of_type(retryable_types):
|
||||
def retry_if_exception_these_types(exception):
|
||||
print("Detected Exception of type: {0}".format(str(type(exception))))
|
||||
return isinstance(exception, retryable_types)
|
||||
return retry_if_exception_these_types
|
||||
|
||||
|
||||
def current_time_ms():
|
||||
return int(round(time.time() * 1000))
|
||||
|
||||
|
||||
@retry(wait_fixed=50, retry_on_result=retry_if_result_none)
|
||||
def _retryable_test_with_wait(thing):
|
||||
return thing.go()
|
||||
|
||||
|
||||
@retry(stop_max_attempt_number=3, retry_on_result=retry_if_result_none)
|
||||
def _retryable_test_with_stop(thing):
|
||||
return thing.go()
|
||||
|
||||
|
||||
@retry(retry_on_exception=retry_if_exception_of_type(IOError))
|
||||
def _retryable_test_with_exception_type_io(thing):
|
||||
return thing.go()
|
||||
|
||||
|
||||
@retry(retry_on_exception=retry_if_exception_of_type(IOError), wrap_exception=True)
|
||||
def _retryable_test_with_exception_type_io_wrap(thing):
|
||||
return thing.go()
|
||||
|
||||
|
||||
@retry(
|
||||
stop_max_attempt_number=3,
|
||||
retry_on_exception=retry_if_exception_of_type(IOError))
|
||||
def _retryable_test_with_exception_type_io_attempt_limit(thing):
|
||||
return thing.go()
|
||||
|
||||
|
||||
@retry(
|
||||
stop_max_attempt_number=3,
|
||||
retry_on_exception=retry_if_exception_of_type(IOError),
|
||||
@ -258,28 +276,34 @@ def _retryable_test_with_exception_type_io_attempt_limit(thing):
|
||||
def _retryable_test_with_exception_type_io_attempt_limit_wrap(thing):
|
||||
return thing.go()
|
||||
|
||||
|
||||
@retry
|
||||
def _retryable_default(thing):
|
||||
return thing.go()
|
||||
|
||||
|
||||
@retry()
|
||||
def _retryable_default_f(thing):
|
||||
return thing.go()
|
||||
|
||||
|
||||
@retry(retry_on_exception=retry_if_exception_of_type(CustomError))
|
||||
def _retryable_test_with_exception_type_custom(thing):
|
||||
return thing.go()
|
||||
|
||||
|
||||
@retry(retry_on_exception=retry_if_exception_of_type(CustomError), wrap_exception=True)
|
||||
def _retryable_test_with_exception_type_custom_wrap(thing):
|
||||
return thing.go()
|
||||
|
||||
|
||||
@retry(
|
||||
stop_max_attempt_number=3,
|
||||
retry_on_exception=retry_if_exception_of_type(CustomError))
|
||||
def _retryable_test_with_exception_type_custom_attempt_limit(thing):
|
||||
return thing.go()
|
||||
|
||||
|
||||
@retry(
|
||||
stop_max_attempt_number=3,
|
||||
retry_on_exception=retry_if_exception_of_type(CustomError),
|
||||
@ -287,6 +311,7 @@ def _retryable_test_with_exception_type_custom_attempt_limit(thing):
|
||||
def _retryable_test_with_exception_type_custom_attempt_limit_wrap(thing):
|
||||
return thing.go()
|
||||
|
||||
|
||||
class TestDecoratorWrapper(unittest.TestCase):
|
||||
|
||||
def test_with_wait(self):
|
||||
|
Loading…
Reference in New Issue
Block a user