summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2012-02-14 05:23:25 -0500
committerChris McDonough <chrism@plope.com>2012-02-14 05:23:25 -0500
commit5ad401ad0b7692e55c21662416642b8ac0fde449 (patch)
treefe678c1d0b3bfa60bdde91c097b3fc016a7cea24
parente45c8075bda2012fef878774812a683750406fff (diff)
downloadpyramid-5ad401ad0b7692e55c21662416642b8ac0fde449.tar.gz
pyramid-5ad401ad0b7692e55c21662416642b8ac0fde449.tar.bz2
pyramid-5ad401ad0b7692e55c21662416642b8ac0fde449.zip
- Better error message when a .pyc-only module is ``config.include`` -ed.
This is not permitted due to error reporting requirements, and a better error message is shown when it is attempted. Previously it would fail with something like "AttributeError: 'NoneType' object has no attribute 'rfind'". Fixed #420.
-rw-r--r--CHANGES.txt6
-rw-r--r--pyramid/config/__init__.py11
-rw-r--r--pyramid/tests/test_config/test_init.py20
3 files changed, 35 insertions, 2 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index 411681d81..6bea84e2f 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -17,6 +17,12 @@ Features
The error message now contains information about the view callable itself
as well as the result of calling it.
+- Better error message when a .pyc-only module is ``config.include`` -ed.
+ This is not permitted due to error reporting requirements, and a better
+ error message is shown when it is attempted. Previously it would fail with
+ something like "AttributeError: 'NoneType' object has no attribute
+ 'rfind'".
+
Dependencies
------------
diff --git a/pyramid/config/__init__.py b/pyramid/config/__init__.py
index 1656b5410..06d3c6abf 100644
--- a/pyramid/config/__init__.py
+++ b/pyramid/config/__init__.py
@@ -253,6 +253,7 @@ class Configurator(
info = ''
object_description = staticmethod(object_description)
introspectable = Introspectable
+ inspect = inspect
def __init__(self,
registry=None,
@@ -706,7 +707,7 @@ class Configurator(
route_prefix = None
c = self.maybe_dotted(callable)
- module = inspect.getmodule(c)
+ module = self.inspect.getmodule(c)
if module is c:
try:
c = getattr(module, 'includeme')
@@ -716,7 +717,13 @@ class Configurator(
)
spec = module.__name__ + ':' + c.__name__
- sourcefile = inspect.getsourcefile(c)
+ sourcefile = self.inspect.getsourcefile(c)
+
+ if sourcefile is None:
+ raise ConfigurationError(
+ 'No source file for module %r (.py file must exist, '
+ 'refusing to use orphan .pyc or .pyo file).' % module.__name__)
+
if action_state.processSpec(spec):
configurator = self.__class__(
diff --git a/pyramid/tests/test_config/test_init.py b/pyramid/tests/test_config/test_init.py
index d237b3fe8..283800e1e 100644
--- a/pyramid/tests/test_config/test_init.py
+++ b/pyramid/tests/test_config/test_init.py
@@ -739,6 +739,26 @@ pyramid.tests.test_config.dummy_include2""",
root_config.include(dummy_subapp, route_prefix='nested')
+ def test_include_with_missing_source_file(self):
+ from pyramid.exceptions import ConfigurationError
+ import inspect
+ config = self._makeOne()
+ class DummyInspect(object):
+ def getmodule(self, c):
+ return inspect.getmodule(c)
+ def getsourcefile(self, c):
+ return None
+ config.inspect = DummyInspect()
+ try:
+ config.include('pyramid.tests.test_config.dummy_include')
+ except ConfigurationError as e:
+ self.assertEqual(
+ e.args[0],
+ "No source file for module 'pyramid.tests.test_config' (.py "
+ "file must exist, refusing to use orphan .pyc or .pyo file).")
+ else: # pragma: no cover
+ raise AssertionError
+
def test_with_context(self):
config = self._makeOne()
context = DummyZCMLContext()