diff --git a/tests/unit/test_vcr.py b/tests/unit/test_vcr.py index a17535f..7150f15 100644 --- a/tests/unit/test_vcr.py +++ b/tests/unit/test_vcr.py @@ -283,6 +283,8 @@ class TestVCRClass(VCR().test_case()): def no_decoration(self): assert httplib.HTTPConnection == _HTTPConnection + self.test_dynamically_added() + assert httplib.HTTPConnection == _HTTPConnection def test_one(self): with force_reset(): @@ -293,3 +295,11 @@ class TestVCRClass(VCR().test_case()): def test_two(self): assert httplib.HTTPConnection != _HTTPConnection + + +def test_dynamically_added(self): + assert httplib.HTTPConnection != _HTTPConnection + + +TestVCRClass.test_dynamically_added = test_dynamically_added +del test_dynamically_added diff --git a/vcr/util.py b/vcr/util.py index 9a44f9b..dd66320 100644 --- a/vcr/util.py +++ b/vcr/util.py @@ -98,13 +98,24 @@ def auto_decorate( decorator, predicate=lambda name, value: isinstance(value, types.FunctionType) ): + def maybe_decorate(attribute, value): + if predicate(attribute, value): + value = decorator(value) + return value + class DecorateAll(type): + def __setattr__(cls, attribute, value): + return super(DecorateAll, cls).__setattr__( + attribute, maybe_decorate(attribute, value) + ) + def __new__(cls, name, bases, attributes_dict): - for attribute, value in attributes_dict.items(): - if predicate(attribute, value): - attributes_dict[attribute] = decorator(value) + new_attributes_dict = dict( + (attribute, maybe_decorate(attribute, value)) + for attribute, value in attributes_dict.items() + ) return super(DecorateAll, cls).__new__( - cls, name, bases, attributes_dict + cls, name, bases, new_attributes_dict ) return DecorateAll