From b6a282867b2fc275ba1e16e49e4fd8c41a9ec7f2 Mon Sep 17 00:00:00 2001
From: Arnaud Fontaine <arnaud.fontaine@nexedi.com>
Date: Thu, 4 Jul 2013 10:46:07 +0900
Subject: [PATCH] ZODB Components: Avoid masking exceptions as much as possible
 when loading a Component.

---
 product/ERP5Type/dynamic/portal_type_class.py | 24 +++++++++++++------
 product/ERP5Type/patches/ExternalMethod.py    | 13 +++++-----
 2 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/product/ERP5Type/dynamic/portal_type_class.py b/product/ERP5Type/dynamic/portal_type_class.py
index b4c44e2b23..5b9e6a9279 100644
--- a/product/ERP5Type/dynamic/portal_type_class.py
+++ b/product/ERP5Type/dynamic/portal_type_class.py
@@ -193,13 +193,23 @@ def generatePortalTypeClass(site, portal_type_name):
     type_class_namespace = document_class_registry.get(type_class, '')
     if not (type_class_namespace.startswith('Products.ERP5Type') or
             portal_type_name in core_portal_type_class_dict):
-      try:
-        klass = getattr(__import__('erp5.component.document.' + type_class,
-                                   fromlist=['erp5.component.document'],
-                                   level=0),
-                        type_class)
-      except (ImportError, AttributeError):
-        pass
+      import erp5.component.document
+      module_fullname = 'erp5.component.document.' + type_class
+      module_loader = erp5.component.document.find_module(module_fullname)
+      if module_loader is not None:
+        try:
+          module = module_loader.load_module(module_fullname)
+        except ImportError, e:
+          LOG("ERP5Type.dynamic", WARNING,
+              "Could not load Component module '%s': %s" % (module_fullname, e))
+        else:
+          try:
+            klass = getattr(module, type_class)
+          except AttributeError:
+            LOG("ERP5Type.dynamic", WARNING,
+                "Could not get class '%s' in Component module '%s'" % \
+                (type_class,
+                 module_fullname))
 
     if klass is None:
       type_class_path = document_class_registry.get(type_class)
diff --git a/product/ERP5Type/patches/ExternalMethod.py b/product/ERP5Type/patches/ExternalMethod.py
index fcb6d8ab57..2ae8a751c1 100644
--- a/product/ERP5Type/patches/ExternalMethod.py
+++ b/product/ERP5Type/patches/ExternalMethod.py
@@ -70,12 +70,10 @@ if 1:
           from kw.
         """
         try:
-            f = getattr(__import__('erp5.component.extension.' + self._module,
-                                   fromlist=['erp5.component.extension'],
-                                   level=0),
-                        self._function)
-
-        except (ImportError, AttributeError):
+            component_module = __import__('erp5.component.extension.' + self._module,
+                                          fromlist=['erp5.component.extension'],
+                                          level=0)
+        except ImportError:
             import Globals  # for data
 
             filePath = self.filepath()
@@ -93,6 +91,9 @@ if 1:
                 self.reloadIfChanged()
 
             f = None
+        else:
+            f = getattr(component_module, self._function)
+
         _v_f = getattr(self, '_v_f', None)
         if not _v_f or (f and f is not _v_f):
             f = self.getFunction(f=f)
-- 
2.30.9