Bladeren bron

better DistObj importing

Joe Shochet 21 jaren geleden
bovenliggende
commit
283bee6b35
2 gewijzigde bestanden met toevoegingen van 18 en 29 verwijderingen
  1. 6 13
      direct/src/distributed/ClientDistClass.py
  2. 12 16
      direct/src/distributed/ClientDistUpdate.py

+ 6 - 13
direct/src/distributed/ClientDistClass.py

@@ -4,6 +4,7 @@ from PandaModules import *
 import DirectNotifyGlobal
 import DirectNotifyGlobal
 import ClientDistUpdate
 import ClientDistUpdate
 import sys
 import sys
+import ihooks
 
 
 # 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.
@@ -24,26 +25,18 @@ class ClientDistClass:
         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
-        try:
-             exec("import " + self.name, moduleGlobals, moduleLocals)
-        except ImportError, e:
-            self.notify.warning("Unable to import %s.py: %s" % (self.name, e))
+        stuff = ihooks.current_importer.get_loader().find_module(self.name)
+        if not stuff:
+            self.notify.warning("Unable to import %s.py" % (self.name))
             self.constructor = None
             self.constructor = None
             return
             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
-
+        module = __import__(self.name, moduleGlobals, moduleLocals)
+        self.constructor = getattr(module, self.name, None)
         # If this assertion fails, you probably had an import error in
         # If this assertion fails, you probably had an import error in
         # a file named in your toon.dc file, or in some file included
         # a file named in your toon.dc file, or in some file included
         # in a file named in your toon.dc file.
         # in a file named in your toon.dc file.
         assert(self.constructor != None)
         assert(self.constructor != None)
-        
-        return
 
 
     def parseFields(self, dcClass):
     def parseFields(self, dcClass):
         fields=[]
         fields=[]

+ 12 - 16
direct/src/distributed/ClientDistUpdate.py

@@ -3,6 +3,7 @@
 import DirectNotifyGlobal
 import DirectNotifyGlobal
 from PyDatagram import PyDatagram
 from PyDatagram import PyDatagram
 from MsgTypes import *
 from MsgTypes import *
+import ihooks
 
 
 # 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.
@@ -21,24 +22,19 @@ class ClientDistUpdate:
         self.types = []
         self.types = []
         self.divisors = []
         self.divisors = []
         self.deriveTypesFromParticle(dcField)
         self.deriveTypesFromParticle(dcField)
-        # Figure out our function pointer
-        try:
-            exec("import " + cdc.name, moduleGlobals, moduleLocals)
-        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")
+        stuff = ihooks.current_importer.get_loader().find_module(cdc.name)
+        if not stuff:
+            # This will be printed by ClientDistClass
+            # self.notify.warning("Unable to import %s.py" % (cdc.name))
             self.func = None
             self.func = None
-        return
+            return
+            
+        module = __import__(cdc.name, moduleGlobals, moduleLocals)
+        # If there is no class here, that is an error
+        classObj = getattr(module, cdc.name)
+        # If there is no func, it will just be None
+        self.func = getattr(classObj, self.name, None)
 
 
     def deriveTypesFromParticle(self, dcField):
     def deriveTypesFromParticle(self, dcField):
         dcFieldAtomic = dcField.asAtomicField()
         dcFieldAtomic = dcField.asAtomicField()