From 792afbfab286585c32d5679c01072d97c8b2b7c6 Mon Sep 17 00:00:00 2001 From: Cedric Brandily Date: Wed, 1 Mar 2017 17:45:07 +0100 Subject: [PATCH] Fix has_registry_receivers when super.__new__ is object.__new__ This change updates has_registry_receivers implementation in order to call super.__new__ without args, kwargs parameters if super.__new__ is object.__new__ (which forbids args, kwargs parameters). Without this fix decorated class instantiation with positional or/and keyword arguments fail. Change-Id: I25880f6142f294fdde9db7f453d5e649c79029d0 --- neutron/callbacks/registry.py | 7 ++++++- neutron/tests/unit/callbacks/test_registry.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/neutron/callbacks/registry.py b/neutron/callbacks/registry.py index fcad880a8c7..13a15774e97 100644 --- a/neutron/callbacks/registry.py +++ b/neutron/callbacks/registry.py @@ -83,7 +83,12 @@ def has_registry_receivers(klass): def replacement_new(cls, *args, **kwargs): if new_inherited: # class didn't define __new__ so we need to call inherited __new__ - instance = super(klass, cls).__new__(cls, *args, **kwargs) + super_new = super(klass, cls).__new__ + if super_new is object.__new__: + # object.__new__ doesn't accept args nor kwargs + instance = super_new(cls) + else: + instance = super_new(cls, *args, **kwargs) else: instance = orig_new(cls, *args, **kwargs) if getattr(instance, '_DECORATED_METHODS_SUBSCRIBED', False): diff --git a/neutron/tests/unit/callbacks/test_registry.py b/neutron/tests/unit/callbacks/test_registry.py index 202d0bb08a7..bccaf331e2f 100644 --- a/neutron/tests/unit/callbacks/test_registry.py +++ b/neutron/tests/unit/callbacks/test_registry.py @@ -52,6 +52,13 @@ class AnotherObjectWithDecoratedCallback(ObjectWithDecoratedCallback, self.counter2 += 1 +@registry.has_registry_receivers +class CallbackClassWithParameters(object): + + def __init__(self, dummy): + pass + + class CallBacksManagerTestCase(base.BaseTestCase): def test_decorated_inst_method_receives(self): @@ -81,3 +88,6 @@ class CallBacksManagerTestCase(base.BaseTestCase): def test_new_inheritance_not_broken(self): self.assertTrue(AnotherObjectWithDecoratedCallback().new_called) + + def test_object_new_not_broken(self): + CallbackClassWithParameters('dummy')