Commit c00fc402 authored by Arnaud Fontaine's avatar Arnaud Fontaine

As per PEP 302, raise ImportError if a Component module could not be loaded.

Modules to be handled by the Component import hook must have already been
filtered properly in find_module().
parent edb60c79
...@@ -188,32 +188,30 @@ class ComponentDynamicPackage(ModuleType): ...@@ -188,32 +188,30 @@ class ComponentDynamicPackage(ModuleType):
try: try:
version, component_name = component_name.split('.') version, component_name = component_name.split('.')
version = version[:-self.__version_suffix_len] version = version[:-self.__version_suffix_len]
except ValueError: except ValueError, error:
return None raise ImportError("%s: should be %s.VERSION.COMPONENT_REFERENCE (%s)" % \
(fullname, self._namespace, error))
try: try:
component = self._registry_dict[component_name][version] component = self._registry_dict[component_name][version]
except KeyError: except KeyError:
LOG("ERP5Type.dynamic", INFO, raise ImportError("%s: version %s of Component %s could not be found" % \
"Could not find version %s of Component %s" % (version, (fullname, version, component_name))
component_name))
return None
else: else:
try: try:
component_version_dict = self._registry_dict[component_name] component_version_dict = self._registry_dict[component_name]
except KeyError: except KeyError:
LOG("ERP5Type.dynamic", INFO, raise ImportError("%s: Component %s could not be found" % (fullname,
"Could not find Component " + component_name) component_name))
return None
for version in site.getVersionPriorityList(): for version in site.getVersionPriorityList():
component = component_version_dict.get(version) component = component_version_dict.get(version)
if component is not None: if component is not None:
break break
else: else:
return None raise ImportError("%s: no version of Component %s in Site priority" % \
(fullname, component_name))
try: try:
module = getattr(getattr(self, version + '_version'), component_name) module = getattr(getattr(self, version + '_version'), component_name)
...@@ -244,12 +242,14 @@ class ComponentDynamicPackage(ModuleType): ...@@ -244,12 +242,14 @@ class ComponentDynamicPackage(ModuleType):
try: try:
component.load(new_module.__dict__, validated_only=True) component.load(new_module.__dict__, validated_only=True)
except: except Exception, error:
del sys.modules[component_id] del sys.modules[component_id]
if component_id_alias: if component_id_alias:
del sys.modules[component_id_alias] del sys.modules[component_id_alias]
raise raise ImportError("%s: cannot load Component %s (%s)" % (fullname,
component_name,
error))
new_module.__path__ = [] new_module.__path__ = []
new_module.__loader__ = self new_module.__loader__ = self
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment