From b6ec3f406307971a68442e6da37a3bca28a7985b Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 11 Jul 2003 21:57:59 +0000 Subject: [PATCH] don't wrap import within a try..except block --- direct/src/distributed/ClientDistClass.py | 24 ++++++++++++------- direct/src/distributed/ClientDistUpdate.py | 28 ++++++++++++---------- direct/src/showbase/PythonUtil.py | 13 ++++++++++ 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/direct/src/distributed/ClientDistClass.py b/direct/src/distributed/ClientDistClass.py index 49e57acf7e..3b3e0aef8f 100644 --- a/direct/src/distributed/ClientDistClass.py +++ b/direct/src/distributed/ClientDistClass.py @@ -3,6 +3,7 @@ from PandaModules import * import DirectNotifyGlobal import ClientDistUpdate +import PythonUtil class ClientDistClass: notify = DirectNotifyGlobal.directNotify.newCategory("ClientDistClass") @@ -16,17 +17,22 @@ class ClientDistClass: self.name2CDU = self.createName2CDUDict(self.allCDU) self.broadcastRequiredCDU = self.listBroadcastRequiredCDU(self.allCDU) self.allRequiredCDU = self.listRequiredCDU(self.allCDU) + # 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 - 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): fields=[] diff --git a/direct/src/distributed/ClientDistUpdate.py b/direct/src/distributed/ClientDistUpdate.py index da1f5d3606..f5ca5ee556 100644 --- a/direct/src/distributed/ClientDistUpdate.py +++ b/direct/src/distributed/ClientDistUpdate.py @@ -3,6 +3,7 @@ import DirectNotifyGlobal import Datagram from MsgTypes import * +import PythonUtil # 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. @@ -22,19 +23,22 @@ class ClientDistUpdate: self.divisors = [] self.deriveTypesFromParticle(dcField) # Figure out our function pointer - try: + 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 + + else: 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") - self.func = None - return None + 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): dcFieldAtomic = dcField.asAtomicField() diff --git a/direct/src/showbase/PythonUtil.py b/direct/src/showbase/PythonUtil.py index 76fef9b36a..fbac947fa8 100644 --- a/direct/src/showbase/PythonUtil.py +++ b/direct/src/showbase/PythonUtil.py @@ -5,6 +5,7 @@ import math import operator import inspect import os +import sys # 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, 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: """ Python classes that want to have C++-style pure-virtual functions can derive from this class and call 'derivedMustOverride' from their