Sfoglia il codice sorgente

better fix for eval-import problems

David Rose 22 anni fa
parent
commit
3fd2da4124

+ 17 - 12
direct/src/distributed/ClientDistClass.py

@@ -3,7 +3,13 @@
 from PandaModules import *
 from PandaModules import *
 import DirectNotifyGlobal
 import DirectNotifyGlobal
 import ClientDistUpdate
 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:
 class ClientDistClass:
     notify = DirectNotifyGlobal.directNotify.newCategory("ClientDistClass")
     notify = DirectNotifyGlobal.directNotify.newCategory("ClientDistClass")
@@ -19,19 +25,18 @@ class ClientDistClass:
         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
-        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
             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
         return
 
 
     def parseFields(self, dcClass):
     def parseFields(self, dcClass):

+ 14 - 14
direct/src/distributed/ClientDistUpdate.py

@@ -3,7 +3,6 @@
 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.
@@ -23,21 +22,22 @@ class ClientDistUpdate:
         self.divisors = []
         self.divisors = []
         self.deriveTypesFromParticle(dcField)
         self.deriveTypesFromParticle(dcField)
         # Figure out our function pointer
         # 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)
             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
         return
 
 
     def deriveTypesFromParticle(self, dcField):
     def deriveTypesFromParticle(self, dcField):