Explorar o código

Show helpful error when user gives config name ending with '.py'

Ask Solem %!s(int64=13) %!d(string=hai) anos
pai
achega
977e2f7d58
Modificáronse 2 ficheiros con 23 adicións e 2 borrados
  1. 14 1
      celery/loaders/default.py
  2. 9 1
      celery/utils/__init__.py

+ 14 - 1
celery/loaders/default.py

@@ -16,12 +16,20 @@ import warnings
 
 from ..datastructures import AttributeDict
 from ..exceptions import NotConfigured
-from ..utils import find_module
+from ..utils import find_module, NotAPackage
 
 from .base import BaseLoader
 
 DEFAULT_CONFIG_MODULE = "celeryconfig"
 
+CONFIG_INVALID_NAME = """
+Error: Module '%(module)s' doesn't exist, or it's not a valid \
+Python module name.
+"""
+
+CONFIG_WITH_SUFFIX = CONFIG_INVALID_NAME + """
+Did you mean '%(suggest)s'?
+"""
 
 class Loader(BaseLoader):
     """The loader used by the default app."""
@@ -39,6 +47,11 @@ class Loader(BaseLoader):
                                      DEFAULT_CONFIG_MODULE)
         try:
             self.find_module(configname)
+        except NotAPackage:
+            if configname.endswith('.py'):
+                raise NotAPackage(CONFIG_WITH_SUFFIX % {
+                    "module": configname, "suggest": configname[:-3]})
+                raise NotAPackage(CONFIG_INVALID_NAME % {"module": configname})
         except ImportError:
             warnings.warn(NotConfigured(
                 "No %r module found! Please make sure it exists and "

+ 9 - 1
celery/utils/__init__.py

@@ -355,6 +355,10 @@ def cwd_in_path():
                 pass
 
 
+class NotAPackage(Exception):
+    pass
+
+
 def find_module(module, path=None, imp=None):
     """Version of :func:`imp.find_module` supporting dots."""
     if imp is None:
@@ -364,7 +368,11 @@ def find_module(module, path=None, imp=None):
             last = None
             parts = module.split(".")
             for i, part in enumerate(parts[:-1]):
-                path = imp(".".join(parts[:i + 1])).__path__
+                mpart = imp(".".join(parts[:i + 1]))
+                try:
+                    path = mpart.__path__
+                except AttributeError:
+                    raise NotAPackage(module)
                 last = _imp.find_module(parts[i + 1], path)
             return last
         return _imp.find_module(module)