def no_decoration(self):
assert httplib.HTTPConnection == _HTTPConnection
+ self.test_dynamically_added()
+ assert httplib.HTTPConnection == _HTTPConnection
def test_one(self):
with force_reset():
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
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