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

This commit is contained in:
David Rose 2003-07-11 21:57:59 +00:00
parent 67942bfee6
commit b6ec3f4063
3 changed files with 44 additions and 21 deletions

View File

@ -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=[]

View File

@ -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()

View File

@ -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