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 * 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: if not PythonUtil.findPythonModule(self.name):
self.notify.warning("%s.py does not exist." % (self.name))
self.constructor = None
else:
exec("import " + self.name) exec("import " + self.name)
self.constructor = eval(self.name + "." + self.name)
except ImportError, e: try:
self.notify.warning("Unable to import %s.py: %s" % (self.name, e)) self.constructor = eval(self.name + "." + self.name)
self.constructor = None except (NameError, AttributeError), e:
except (NameError, AttributeError), e: self.notify.warning("%s.%s does not exist: %s" % (self.name, self.name, e))
self.notify.warning("%s.%s does not exist: %s" % (self.name, self.name, e)) self.constructor = None
self.constructor = None
return None return
def parseFields(self, dcClass): def parseFields(self, dcClass):
fields=[] fields=[]

View File

@ -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: 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) exec("import " + cdc.name, moduleGlobals, moduleLocals)
self.func = eval(cdc.name + "." + cdc.name + "." + self.name) try:
# Only catch name and attribute errors self.func = eval(cdc.name + "." + cdc.name + "." + self.name)
# as all other errors are legit errors # Only catch name and attribute errors
except ImportError, e: # as all other errors are legit errors
self.notify.warning("Unable to import %s.py: %s" % (cdc.name, e)) except (NameError, AttributeError), e:
self.func = None #self.notify.warning(cdc.name + "." + self.name + " does not exist")
except (NameError, AttributeError), e: self.func = None
#self.notify.warning(cdc.name + "." + self.name +
# " does not exist") return
self.func = None
return None
def deriveTypesFromParticle(self, dcField): def deriveTypesFromParticle(self, dcField):
dcFieldAtomic = dcField.asAtomicField() dcFieldAtomic = dcField.asAtomicField()

View File

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