From 3fd2da41249d3799644eade20d2d6bd500d1d0e2 Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 11 Jul 2003 22:58:15 +0000 Subject: [PATCH] better fix for eval-import problems --- direct/src/distributed/ClientDistClass.py | 29 +++++++++++++--------- direct/src/distributed/ClientDistUpdate.py | 28 ++++++++++----------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/direct/src/distributed/ClientDistClass.py b/direct/src/distributed/ClientDistClass.py index 3b3e0aef8f..9a1b811447 100644 --- a/direct/src/distributed/ClientDistClass.py +++ b/direct/src/distributed/ClientDistClass.py @@ -3,7 +3,13 @@ from PandaModules import * import DirectNotifyGlobal import ClientDistUpdate -import PythonUtil +import sys + +# 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. +# This is important for redefine to work, and is a good idea anyways. +moduleGlobals = globals() +moduleLocals = locals() class ClientDistClass: notify = DirectNotifyGlobal.directNotify.newCategory("ClientDistClass") @@ -19,19 +25,18 @@ class ClientDistClass: self.allRequiredCDU = self.listRequiredCDU(self.allCDU) # Import the class, and store the constructor - if not PythonUtil.findPythonModule(self.name): - self.notify.warning("%s.py does not exist." % (self.name)) + try: + exec("import " + self.name, moduleGlobals, moduleLocals) + except ImportError, e: + self.notify.warning("Unable to import %s.py: %s" % (self.name, e)) self.constructor = None - - else: - exec("import " + self.name) + return - 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 - + 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): diff --git a/direct/src/distributed/ClientDistUpdate.py b/direct/src/distributed/ClientDistUpdate.py index f5ca5ee556..2df4e0b13b 100644 --- a/direct/src/distributed/ClientDistUpdate.py +++ b/direct/src/distributed/ClientDistUpdate.py @@ -3,7 +3,6 @@ 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. @@ -23,21 +22,22 @@ class ClientDistUpdate: self.divisors = [] self.deriveTypesFromParticle(dcField) # Figure out our function pointer - 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: + try: 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 + except ImportError, e: + # Don't bother reporting this error; the ClientDistClass + # will catch it. + #self.notify.warning("Unable to import %s.py: %s" % (cdc.name, e)) + self.func = None + return + try: + self.func = eval(cdc.name + "." + cdc.name + "." + self.name) + except (NameError, AttributeError), e: + # Only catch name and attribute errors + # as all other errors are legit errors + #self.notify.warning(cdc.name + "." + self.name + " does not exist") + self.func = None return def deriveTypesFromParticle(self, dcField):