From b6c867e7d8f8c34138efadc5efd0c9f30bff9ecb Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Tue, 30 Jun 2009 23:09:17 +0000 Subject: - Bug fix: when a ``repoze.bfg.resource.PackageOverrides`` class was instantiated, and the package it was overriding already had a ``__loader__`` attribute, it would fail at startup time, even if the ``__loader__`` attribute was another PackageOverrides instance. We now replace any ``__loader__`` that is also a PackageOverrides instance. Symptom: ``ConfigurationExecutionError: : Package already has a __loader__ (probably a module in a zipped egg)``. --- CHANGES.txt | 10 +++++++++- repoze/bfg/resource.py | 8 +++++--- repoze/bfg/tests/test_resource.py | 9 ++++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 61458163c..3a3c39495 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,7 +1,15 @@ Next release ============ -- ... +- Bug fix: when a ``repoze.bfg.resource.PackageOverrides`` class was + instantiated, and the package it was overriding already had a + ``__loader__`` attribute, it would fail at startup time, even if the + ``__loader__`` attribute was another PackageOverrides instance. We + now replace any ``__loader__`` that is also a PackageOverrides + instance. Symptom: ``ConfigurationExecutionError: : Package + already has a __loader__ (probably a module in a zipped egg)``. 1.0a7 (2009-06-30) ================== diff --git a/repoze/bfg/resource.py b/repoze/bfg/resource.py index 4112925b4..276c52878 100644 --- a/repoze/bfg/resource.py +++ b/repoze/bfg/resource.py @@ -50,9 +50,11 @@ class PackageOverrides: implements(IPackageOverrides) # pkg_resources arg in kw args below for testing def __init__(self, package, pkg_resources=pkg_resources): - if hasattr(package, '__loader__'): - raise TypeError('Package %s already has a __loader__ ' - '(probably a module in a zipped egg)' % package) + if hasattr(package, '__loader__') and not isinstance(package.__loader__, + self.__class__): + raise TypeError('Package %s already has a non-%s __loader__ ' + '(probably a module in a zipped egg)' % + (package, self.__class__)) # We register ourselves as a __loader__ *only* to support the # setuptools _find_adapter adapter lookup; this class doesn't # actually support the PEP 302 loader "API". This is diff --git a/repoze/bfg/tests/test_resource.py b/repoze/bfg/tests/test_resource.py index 9f466b002..5bc1be152 100644 --- a/repoze/bfg/tests/test_resource.py +++ b/repoze/bfg/tests/test_resource.py @@ -123,11 +123,18 @@ class TestPackageOverrides(unittest.TestCase): pkg_resources = DummyPkgResources() return klass(package, pkg_resources=pkg_resources) - def test_ctor_package_already_has_loader(self): + def test_ctor_package_already_has_loader_of_different_type(self): package = DummyPackage('package') package.__loader__ = True self.assertRaises(TypeError, self._makeOne, package) + def test_ctor_package_already_has_loader_of_same_type(self): + dummy_pkg_resources = DummyPkgResources() + package = DummyPackage('package') + package.__loader__ = self._makeOne(package) + po = self._makeOne(package) + self.assertEqual(package.__loader__, po) + def test_ctor_sets_loader(self): package = DummyPackage('package') po = self._makeOne(package) -- cgit v1.2.3