From e0ad955a42222ebadd1c6a6694d9ba3a9c40e4d1 Mon Sep 17 00:00:00 2001 From: John Vandenberg Date: Wed, 1 Jul 2020 15:31:49 +0700 Subject: [PATCH 1/2] Modernize --- fudge/__init__.py | 15 ++++++++------- fudge/inspector.py | 4 +++- fudge/patcher.py | 8 +++++--- fudge/tests/_py3_suite.py | 1 + fudge/tests/test_fudge.py | 9 +++++---- fudge/tests/test_import_all.py | 1 + fudge/tests/test_inspector.py | 1 + fudge/tests/test_inspector_import_all.py | 1 + fudge/tests/test_patcher.py | 1 + fudge/tests/test_registry.py | 21 +++++++++++---------- fudge/util.py | 3 ++- 11 files changed, 39 insertions(+), 26 deletions(-) diff --git a/fudge/__init__.py b/fudge/__init__.py index cf0aea7..02a62f9 100644 --- a/fudge/__init__.py +++ b/fudge/__init__.py @@ -5,11 +5,12 @@ """ +from __future__ import absolute_import __version__ = '1.1.1' import os import re import sys -import thread +import six.moves._thread import warnings from fudge.exc import FakeDeclarationError from fudge.patcher import * @@ -67,12 +68,12 @@ def expect_call(self, expected_call): this_call_order.add_expected_call(expected_call) def get_expected_calls(self): - self.expected_calls.setdefault(thread.get_ident(), []) - return self.expected_calls[thread.get_ident()] + self.expected_calls.setdefault(six.moves._thread.get_ident(), []) + return self.expected_calls[six.moves._thread.get_ident()] def get_expected_call_order(self): - self.expected_call_order.setdefault(thread.get_ident(), {}) - return self.expected_call_order[thread.get_ident()] + self.expected_call_order.setdefault(six.moves._thread.get_ident(), {}) + return self.expected_call_order[six.moves._thread.get_ident()] def remember_expected_call_order(self, expected_call_order): ordered_fakes = self.get_expected_call_order() @@ -340,10 +341,10 @@ def __call__(self, *args, **kwargs): if self.expected_kwarg_count is None: self.expected_kwarg_count = 0 - if len(kwargs.keys()) != self.expected_kwarg_count: + if len(list(kwargs.keys())) != self.expected_kwarg_count: raise AssertionError( "%s was called with %s keyword arg(s) but expected %s" % ( - self, len(kwargs.keys()), self.expected_kwarg_count)) + self, len(list(kwargs.keys())), self.expected_kwarg_count)) if self.unexpected_kwargs: for un_key, un_val in self.unexpected_kwargs.items(): diff --git a/fudge/inspector.py b/fudge/inspector.py index 4de684c..9466244 100644 --- a/fudge/inspector.py +++ b/fudge/inspector.py @@ -20,9 +20,11 @@ >>> fudge.clear_expectations() """ +from __future__ import absolute_import import warnings from fudge.util import fmt_val, fmt_dict_vals +import six __all__ = ['arg', 'arg_not'] @@ -505,7 +507,7 @@ def _repr_argspec(self): return self._make_argspec(fmt_val(self.part)) def stringlike(self, value): - if isinstance(value, (str, unicode)): + if isinstance(value, (str, six.text_type)): return value else: return str(value) diff --git a/fudge/patcher.py b/fudge/patcher.py index d787304..de308e0 100644 --- a/fudge/patcher.py +++ b/fudge/patcher.py @@ -4,6 +4,8 @@ See :ref:`using-fudge` for common scenarios. """ +from __future__ import absolute_import +import six __all__ = ['patch_object', 'with_patched_object', 'PatchHandler', 'patched_context', 'patch'] @@ -97,7 +99,7 @@ def caller(*args, **kw): except: etype, val, tb = sys.exc_info() self.__exit__(etype, val, tb) - raise etype, val, tb + six.reraise(etype, val, tb) else: self.__exit__(None, None, None) return value @@ -256,7 +258,7 @@ def patch_object(obj, attr_name, patched_value): 'clean' """ - if isinstance(obj, (str, unicode)): + if isinstance(obj, (str, six.text_type)): obj_path = adjusted_path = obj done = False exc = None @@ -276,7 +278,7 @@ def patch_object(obj, attr_name, patched_value): # We're at the top level module and it doesn't exist. # Raise the first exception since it will make more sense: etype, val, tb = exc - raise etype, val, tb + six.reraise(etype, val, tb) if not adjusted_path.count('.'): at_top_level = True for part in obj_path.split('.')[1:]: diff --git a/fudge/tests/_py3_suite.py b/fudge/tests/_py3_suite.py index 648512d..6d2b2af 100644 --- a/fudge/tests/_py3_suite.py +++ b/fudge/tests/_py3_suite.py @@ -5,6 +5,7 @@ # FIXME: this is dumb +from __future__ import absolute_import from fudge.tests.test_fudge import * from fudge.tests.test_import_all import * from fudge.tests.test_inspector import * diff --git a/fudge/tests/test_fudge.py b/fudge/tests/test_fudge.py index 786ac0a..5476b25 100644 --- a/fudge/tests/test_fudge.py +++ b/fudge/tests/test_fudge.py @@ -1,4 +1,5 @@ from __future__ import with_statement +from __future__ import absolute_import import sys import unittest @@ -63,7 +64,7 @@ def test_has_property(self): eq_(my_obj.vice, 'versa') try: my_obj.stuff - except Exception, exc: + except Exception as exc: eq_(str(exc), 'broken stuff') else: raise RuntimeError('expected Exception') @@ -89,7 +90,7 @@ def test_repr_shortens_long_values(self): ) try: fake.set_bits() - except AssertionError, exc: + except AssertionError as exc: eq_(str(exc), "fake:widget.set_bits('123456789101112131415161718192021222324252627...') " "was called unexpectedly with args ()") @@ -192,7 +193,7 @@ def test_arg_diffs_are_not_shortened(self): try: # this should not be shortened but the above arg spec should: fake.set_bits("99999999999999999999999999999999999999999999999999999999") - except AssertionError, exc: + except AssertionError as exc: eq_(str(exc), "fake:widget.set_bits('123456789101112131415161718192021222324252627...') " "was called unexpectedly with args " @@ -207,7 +208,7 @@ def test_kwarg_diffs_are_not_shortened(self): try: # this should not be shortened but the above arg spec should: fake.set_bits(newbits="99999999999999999999999999999999999999999999999999999999") - except AssertionError, exc: + except AssertionError as exc: eq_(str(exc), "fake:widget.set_bits(newbits='123456789101112131415161718192021222324252627...') " "was called unexpectedly with args " diff --git a/fudge/tests/test_import_all.py b/fudge/tests/test_import_all.py index 59c794b..3162b98 100644 --- a/fudge/tests/test_import_all.py +++ b/fudge/tests/test_import_all.py @@ -1,4 +1,5 @@ +from __future__ import absolute_import from fudge import * def test_import_all(): diff --git a/fudge/tests/test_inspector.py b/fudge/tests/test_inspector.py index 52ba3db..f38998d 100644 --- a/fudge/tests/test_inspector.py +++ b/fudge/tests/test_inspector.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +from __future__ import absolute_import import re import unittest diff --git a/fudge/tests/test_inspector_import_all.py b/fudge/tests/test_inspector_import_all.py index 6df21b4..752e16f 100644 --- a/fudge/tests/test_inspector_import_all.py +++ b/fudge/tests/test_inspector_import_all.py @@ -1,4 +1,5 @@ +from __future__ import absolute_import from fudge.inspector import * def test_import_all(): diff --git a/fudge/tests/test_patcher.py b/fudge/tests/test_patcher.py index 54e7ad5..2abdc61 100644 --- a/fudge/tests/test_patcher.py +++ b/fudge/tests/test_patcher.py @@ -1,4 +1,5 @@ from __future__ import with_statement +from __future__ import absolute_import import inspect import unittest diff --git a/fudge/tests/test_registry.py b/fudge/tests/test_registry.py index a518146..e294d8d 100644 --- a/fudge/tests/test_registry.py +++ b/fudge/tests/test_registry.py @@ -1,5 +1,6 @@ -import thread +from __future__ import absolute_import +import six.moves._thread import sys import unittest import fudge @@ -85,13 +86,13 @@ def test_global_clear_expectations(self): eq_(len(self.reg.get_expected_calls()), 1) exp_order = ExpectedCallOrder(self.fake) self.reg.remember_expected_call_order(exp_order) - eq_(self.reg.get_expected_call_order().keys(), [self.fake]) + eq_(list(self.reg.get_expected_call_order().keys()), [self.fake]) fudge.clear_expectations() eq_(len(self.reg.get_expected_calls()), 0, "clear_expectations() should reset expectations") - eq_(len(self.reg.get_expected_call_order().keys()), 0, + eq_(len(list(self.reg.get_expected_call_order().keys())), 0, "clear_expectations() should reset expected call order") def test_multithreading(self): @@ -114,7 +115,7 @@ def registry(num): exp_order = ExpectedCallOrder(self.fake) reg.remember_expected_call_order(exp_order) - eq_(len(reg.get_expected_call_order().keys()), 1) + eq_(len(list(reg.get_expected_call_order().keys())), 1) # registered first time on __init__ : exp = ExpectedCall(self.fake, 'callMe', call_order=exp_order) @@ -131,17 +132,17 @@ def registry(num): fudge.verify() fudge.clear_expectations() - except Exception, er: + except Exception as er: thread_run.errors.append(er) raise finally: thread_run.waiting -= 1 - thread.start_new_thread(registry, (1,)) - thread.start_new_thread(registry, (2,)) - thread.start_new_thread(registry, (3,)) - thread.start_new_thread(registry, (4,)) - thread.start_new_thread(registry, (5,)) + six.moves._thread.start_new_thread(registry, (1,)) + six.moves._thread.start_new_thread(registry, (2,)) + six.moves._thread.start_new_thread(registry, (3,)) + six.moves._thread.start_new_thread(registry, (4,)) + six.moves._thread.start_new_thread(registry, (5,)) count = 0 while thread_run.waiting > 0: diff --git a/fudge/util.py b/fudge/util.py index 1f81e12..3d3500e 100644 --- a/fudge/util.py +++ b/fudge/util.py @@ -1,4 +1,5 @@ +from __future__ import absolute_import try: from functools import wraps except ImportError: @@ -30,7 +31,7 @@ def fmt_dict_vals(dict_vals, shorten=True): """Returns list of key=val pairs formatted for inclusion in an informative text string. """ - items = dict_vals.items() + items = list(dict_vals.items()) if not items: return [fmt_val(None, shorten=shorten)] return ["%s=%s" % (k, fmt_val(v, shorten=shorten)) for k,v in items] From c6ab35fee4c4bd4d8ba6cefa3ef40c2ceef5c353 Mon Sep 17 00:00:00 2001 From: John Vandenberg Date: Wed, 1 Jul 2020 15:36:10 +0700 Subject: [PATCH 2/2] setup.py: Add dependency on six --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 29b412d..fd56ff6 100644 --- a/setup.py +++ b/setup.py @@ -51,7 +51,7 @@ def test_mailer(FakeSMTP): author_email='kumar.mcmillan@gmail.com', license="The MIT License", packages=find_packages(exclude=['ez_setup']), - install_requires=[], + install_requires=['six'], url='https://github.com/fudge-py/fudge', include_package_data=True, classifiers = [