Browse Source

don't wrap import within a try..except block

David Rose 22 years ago
parent
commit
b6ec3f4063

+ 15 - 9
direct/src/distributed/ClientDistClass.py

@@ -3,6 +3,7 @@
 from PandaModules import *
 from PandaModules import *
 import DirectNotifyGlobal
 import DirectNotifyGlobal
 import ClientDistUpdate
 import ClientDistUpdate
+import PythonUtil
 
 
 class ClientDistClass:
 class ClientDistClass:
     notify = DirectNotifyGlobal.directNotify.newCategory("ClientDistClass")
     notify = DirectNotifyGlobal.directNotify.newCategory("ClientDistClass")
@@ -16,17 +17,22 @@ class ClientDistClass:
         self.name2CDU = self.createName2CDUDict(self.allCDU)
         self.name2CDU = self.createName2CDUDict(self.allCDU)
         self.broadcastRequiredCDU = self.listBroadcastRequiredCDU(self.allCDU)
         self.broadcastRequiredCDU = self.listBroadcastRequiredCDU(self.allCDU)
         self.allRequiredCDU = self.listRequiredCDU(self.allCDU)
         self.allRequiredCDU = self.listRequiredCDU(self.allCDU)
+
         # Import the class, and store the constructor
         # Import the class, and store the constructor
-        try:
-            exec("import " + self.name)
-            self.constructor = eval(self.name + "." + self.name)
-        except ImportError, e:
-            self.notify.warning("Unable to import %s.py: %s" % (self.name, e))
+        if not PythonUtil.findPythonModule(self.name):
+            self.notify.warning("%s.py does not exist." % (self.name))
             self.constructor = None
             self.constructor = None
-        except (NameError, AttributeError), e:
-            self.notify.warning("%s.%s does not exist: %s" % (self.name, self.name, e))
-            self.constructor = None 
-        return None
+            
+        else:
+            exec("import " + self.name)
+
+            try:
+                self.constructor = eval(self.name + "." + self.name)
+            except (NameError, AttributeError), e:
+                self.notify.warning("%s.%s does not exist: %s" % (self.name, self.name, e))
+                self.constructor = None
+                
+        return
 
 
     def parseFields(self, dcClass):
     def parseFields(self, dcClass):
         fields=[]
         fields=[]

+ 16 - 12
direct/src/distributed/ClientDistUpdate.py

@@ -3,6 +3,7 @@
 import DirectNotifyGlobal
 import DirectNotifyGlobal
 import Datagram
 import Datagram
 from MsgTypes import *
 from MsgTypes import *
+import PythonUtil
 
 
 # These are stored here so that the distributed classes we load on the fly
 # These are stored here so that the distributed classes we load on the fly
 # can be exec'ed in the module namespace as if we imported them normally.
 # can be exec'ed in the module namespace as if we imported them normally.
@@ -22,19 +23,22 @@ class ClientDistUpdate:
         self.divisors = []
         self.divisors = []
         self.deriveTypesFromParticle(dcField)
         self.deriveTypesFromParticle(dcField)
         # Figure out our function pointer
         # Figure out our function pointer
-        try:
-            exec("import " + cdc.name, moduleGlobals, moduleLocals)
-            self.func = eval(cdc.name + "." + cdc.name + "." + self.name)
-        # Only catch name and attribute errors
-        # as all other errors are legit errors
-        except ImportError, e:
-            self.notify.warning("Unable to import %s.py: %s" % (cdc.name, e))
-            self.func = None
-        except (NameError, AttributeError), e:
-            #self.notify.warning(cdc.name + "." + self.name +
-            #                                " does not exist")
+        if not PythonUtil.findPythonModule(cdc.name):
+            # The ClientDistClass already reported this warning.
+            #self.notify.warning("%s.py does not exist." % (cdc.name))
             self.func = None
             self.func = None
-        return None
+
+        else:
+            exec("import " + cdc.name, moduleGlobals, moduleLocals)
+            try:
+                self.func = eval(cdc.name + "." + cdc.name + "." + self.name)
+                # Only catch name and attribute errors
+                # as all other errors are legit errors
+            except (NameError, AttributeError), e:
+                #self.notify.warning(cdc.name + "." + self.name + " does not exist")
+                self.func = None
+
+        return
 
 
     def deriveTypesFromParticle(self, dcField):
     def deriveTypesFromParticle(self, dcField):
         dcFieldAtomic = dcField.asAtomicField()
         dcFieldAtomic = dcField.asAtomicField()

+ 13 - 0
direct/src/showbase/PythonUtil.py

@@ -5,6 +5,7 @@ import math
 import operator
 import operator
 import inspect
 import inspect
 import os
 import os
+import sys
 
 
 
 
 # NOTE: ifAbsentPut has been replaced with Python's dictionary's builtin setdefault
 # NOTE: ifAbsentPut has been replaced with Python's dictionary's builtin setdefault
@@ -674,6 +675,18 @@ def lineTag(baseFileName=1, verbose=0, separator=':'):
         return '%s%s%s%s%s' % (fileName, separator, lineNum, separator,
         return '%s%s%s%s%s' % (fileName, separator, lineNum, separator,
                                funcName)
                                funcName)
 
 
+def findPythonModule(module):
+    # Look along the python load path for the indicated filename.
+    # Returns the located pathname, or None if the filename is not
+    # found.
+    filename = module + '.py'
+    for dir in sys.path:
+        pathname = os.path.join(dir, filename)
+        if os.path.exists(pathname):
+            return pathname
+        
+    return None
+
 class PureVirtual:
 class PureVirtual:
     """ Python classes that want to have C++-style pure-virtual functions
     """ Python classes that want to have C++-style pure-virtual functions
     can derive from this class and call 'derivedMustOverride' from their
     can derive from this class and call 'derivedMustOverride' from their