Skip to content

Commit

Permalink
Merge pull request #7 from zopefoundation/maurits-attributeerror-modu…
Browse files Browse the repository at this point in the history
…le-file-issue-6

Catch and ignore AttributeError for module.__file__.
  • Loading branch information
mauritsvanrees committed Feb 28, 2019
2 parents 8a9ed45 + 7fdf681 commit cb646ad
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ dist/
*.egg-info/
.tox/
!.gitattributes
!.gitkeep
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ New features:

Bug fixes:

- *add item here*
- Catch and ignore AttributeError for ``module.__file__``.
Fixes `issue 6 <https://github.com/zopefoundation/z3c.autoinclude/issues/6>`_.
[maurits]


0.3.8 (2018-11-04)
Expand Down
13 changes: 11 additions & 2 deletions src/z3c/autoinclude/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from z3c.autoinclude.utils import DistributionManager
from z3c.autoinclude.utils import ZCMLInfo


logger = logging.getLogger("z3c.autoinclude")


class DependencyFinder(DistributionManager):

def includableInfo(self, zcml_to_look_for):
Expand All @@ -25,12 +29,17 @@ def includableInfo(self, zcml_to_look_for):
try:
module = resolve(dotted_name)
except ImportError as exc:
logging.getLogger("z3c.autoinclude").warn(
logger.warning(
"resolve(%r) raised import error: %s" % (dotted_name, exc))
continue
module_file = getattr(module, '__file__', None)
if module_file is None:
logger.warning(
"%r has no __file__ attribute" % dotted_name)
continue
for candidate in zcml_to_look_for:
candidate_path = os.path.join(
os.path.dirname(module.__file__), candidate)
os.path.dirname(module_file), candidate)
if os.path.isfile(candidate_path):
result[candidate].append(dotted_name)
return result
Expand Down
21 changes: 21 additions & 0 deletions src/z3c/autoinclude/dependency.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,24 @@ Note that it will not catch DistributionNotFound errors::
Traceback (most recent call last):
...
DistributionNotFound: ...

We do not want to break when there are problems with some dependencies.
If a dependency is not found or is not a module, we should not try to load its zcml.
See https://github.com/zopefoundation/z3c.autoinclude/issues/6.
This is easiest to test by some hacking.

>>> from z3c.autoinclude.utils import DistributionManager
>>> DistributionManager.orig_dottedNames = DistributionManager.dottedNames
>>> def dottedNames(self):
... result = self.orig_dottedNames()
... result.extend(['z3c.autoinclude.tests.nomodulefile', 'nosuchpackage'])
... return result
>>> DistributionManager.dottedNames = dottedNames
>>> pprint(b_include_finder.includableInfo(['configure.zcml',
... 'meta.zcml']))
{'configure.zcml': ['F.H'], 'meta.zcml': ['testdirective', 'F.G', 'F.H']}

Restore the original dottedNames method:

>>> DistributionManager.dottedNames = DistributionManager.orig_dottedNames
>>> del DistributionManager.orig_dottedNames
6 changes: 6 additions & 0 deletions src/z3c/autoinclude/tests/nomodulefile/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
# This directory is used in a test in dependency.txt.
# We need a package with a module that has no __file__ attribute.
# This can happen in corner cases, for example with 'backports' on Python 2.7.
# See https://github.com/zopefoundation/z3c.autoinclude/issues/6.
del __file__

0 comments on commit cb646ad

Please sign in to comment.