diff options
-rw-r--r-- | logilab/common/registry.py | 16 | ||||
-rw-r--r-- | test/data/regobjects2.py | 2 | ||||
-rw-r--r-- | test/unittest_registry.py | 9 |
3 files changed, 21 insertions, 6 deletions
diff --git a/logilab/common/registry.py b/logilab/common/registry.py index 476bef8..83f467b 100644 --- a/logilab/common/registry.py +++ b/logilab/common/registry.py @@ -212,12 +212,22 @@ class RegistrableInstance(RegistrableObject): """Add a __module__ attribute telling the module where the instance was created, for automatic registration. """ + module = kwargs.pop('__module__', None) obj = super(RegistrableInstance, cls).__new__(cls) - # XXX subclass must no override __new__ - filepath = tb.extract_stack(limit=2)[0][0] - obj.__module__ = _modname_from_path(filepath) + if module is None: + warn('instantiate {0} with ' + '__module__=__name__'.format(cls.__name__), + DeprecationWarning) + # XXX subclass must no override __new__ + filepath = tb.extract_stack(limit=2)[0][0] + obj.__module__ = _modname_from_path(filepath) + else: + obj.__module__ = module return obj + def __init__(self, __module__=None): + super(RegistrableInstance, self).__init__() + class Registry(dict): """The registry store a set of implementations associated to identifier: diff --git a/test/data/regobjects2.py b/test/data/regobjects2.py index 5c28b51..091b9f7 100644 --- a/test/data/regobjects2.py +++ b/test/data/regobjects2.py @@ -5,4 +5,4 @@ class MyRegistrableInstance(RegistrableInstance): __select__ = yes() __registry__ = 'zereg' -instance = MyRegistrableInstance() +instance = MyRegistrableInstance(__module__=__name__) diff --git a/test/unittest_registry.py b/test/unittest_registry.py index 8c5c791..1c07e4c 100644 --- a/test/unittest_registry.py +++ b/test/unittest_registry.py @@ -200,13 +200,18 @@ class RegistryStoreTC(TestCase): class RegistrableInstanceTC(TestCase): def test_instance_modulename(self): + with warnings.catch_warnings(record=True) as warns: + obj = RegistrableInstance() + self.assertEqual(obj.__module__, 'unittest_registry') + self.assertIn('instantiate RegistrableInstance with __module__=__name__', + [str(w.message) for w in warns]) # no inheritance - obj = RegistrableInstance() + obj = RegistrableInstance(__module__=__name__) self.assertEqual(obj.__module__, 'unittest_registry') # with inheritance from another python file with prepended_syspath(self.datadir): from regobjects2 import instance, MyRegistrableInstance - instance2 = MyRegistrableInstance() + instance2 = MyRegistrableInstance(__module__=__name__) self.assertEqual(instance.__module__, 'regobjects2') self.assertEqual(instance2.__module__, 'unittest_registry') |