Browse Source

Commit patch by cfsworks to clean up use of exec() and eval()

rdb 11 years ago
parent
commit
51dffd0f5a

+ 1 - 3
direct/src/distributed/MsgTypes.py

@@ -107,9 +107,7 @@ MsgName2Id = {
 MsgId2Names = invertDictLossless(MsgName2Id)
 MsgId2Names = invertDictLossless(MsgName2Id)
     
     
 # put msg names in module scope, assigned to msg value
 # put msg names in module scope, assigned to msg value
-for name, value in MsgName2Id.items():
-    exec('%s = %s' % (name, value))
-del name, value
+globals().update(MsgName2Id)
 
 
 # These messages are ignored when the client is headed to the quiet zone
 # These messages are ignored when the client is headed to the quiet zone
 QUIET_ZONE_IGNORED_LIST = [
 QUIET_ZONE_IGNORED_LIST = [

+ 1 - 3
direct/src/distributed/MsgTypesCMU.py

@@ -26,6 +26,4 @@ MsgName2Id = {
 MsgId2Names = invertDictLossless(MsgName2Id)
 MsgId2Names = invertDictLossless(MsgName2Id)
     
     
 # put msg names in module scope, assigned to msg value
 # put msg names in module scope, assigned to msg value
-for name, value in MsgName2Id.items():
-    exec('%s = %s' % (name, value))
-del name, value
+globals().update(MsgName2Id)

+ 2 - 3
direct/src/gui/OnscreenGeom.py

@@ -113,8 +113,7 @@ class OnscreenGeom(DirectObject, NodePath):
         for option, value in kw.items():
         for option, value in kw.items():
             # Use option string to access setter function
             # Use option string to access setter function
             try:
             try:
-                setter = eval('self.set' +
-                              string.upper(option[0]) + option[1:])
+                setter = getattr(self, 'set' + option[0].upper() + option[1:])
                 if (((setter == self.setPos) or
                 if (((setter == self.setPos) or
                      (setter == self.setHpr) or
                      (setter == self.setHpr) or
                      (setter == self.setScale)) and
                      (setter == self.setScale)) and
@@ -133,7 +132,7 @@ class OnscreenGeom(DirectObject, NodePath):
     def cget(self, option):
     def cget(self, option):
         # Get current configuration setting.
         # Get current configuration setting.
         # This is for compatibility with DirectGui functions
         # This is for compatibility with DirectGui functions
-        getter = eval('self.get' + string.upper(option[0]) + option[1:])
+        getter = getattr(self, 'get' + option[0].upper() + option[1:])
         return getter()
         return getter()
 
 
     # Allow index style refererences
     # Allow index style refererences

+ 2 - 3
direct/src/gui/OnscreenImage.py

@@ -130,8 +130,7 @@ class OnscreenImage(DirectObject, NodePath):
         for option, value in kw.items():
         for option, value in kw.items():
             # Use option string to access setter function
             # Use option string to access setter function
             try:
             try:
-                setter = eval('self.set' +
-                              string.upper(option[0]) + option[1:])
+                setter = getattr(self, 'set' + option[0].upper() + option[1:])
                 if (((setter == self.setPos) or
                 if (((setter == self.setPos) or
                      (setter == self.setHpr) or
                      (setter == self.setHpr) or
                      (setter == self.setScale)) and
                      (setter == self.setScale)) and
@@ -150,7 +149,7 @@ class OnscreenImage(DirectObject, NodePath):
     def cget(self, option):
     def cget(self, option):
         # Get current configuration setting.
         # Get current configuration setting.
         # This is for compatibility with DirectGui functions
         # This is for compatibility with DirectGui functions
-        getter = eval('self.get' + string.upper(option[0]) + option[1:])
+        getter = getattr(self, 'get' + option[0].upper() + option[1:])
         return getter()
         return getter()
 
 
     # Allow index style refererences
     # Allow index style refererences

+ 2 - 3
direct/src/gui/OnscreenText.py

@@ -381,8 +381,7 @@ class OnscreenText(DirectObject, NodePath):
         for option, value in kw.items():
         for option, value in kw.items():
             # Use option string to access setter function
             # Use option string to access setter function
             try:
             try:
-                setter = eval('self.set' +
-                              string.upper(option[0]) + option[1:])
+                setter = getattr(self, 'set' + option[0].upper() + option[1:])
                 if setter == self.setPos:
                 if setter == self.setPos:
                     setter(value[0], value[1])
                     setter(value[0], value[1])
                 else:
                 else:
@@ -397,7 +396,7 @@ class OnscreenText(DirectObject, NodePath):
     def cget(self, option):
     def cget(self, option):
         # Get current configuration setting.
         # Get current configuration setting.
         # This is for compatibility with DirectGui functions
         # This is for compatibility with DirectGui functions
-        getter = eval('self.get' + string.upper(option[0]) + option[1:])
+        getter = getattr(self, 'get' + option[0].upper() + option[1:])
         return getter()
         return getter()
 
 
     def setAlign(self, align):
     def setAlign(self, align):

+ 6 - 6
direct/src/leveleditor/ObjectMgrBase.py

@@ -189,9 +189,9 @@ class ObjectMgrBase:
                         if funcName.startswith('.'):
                         if funcName.startswith('.'):
                             # when it's using default objectHandler
                             # when it's using default objectHandler
                             if self.editor:
                             if self.editor:
-                                func = Functor(eval("self.editor.objectHandler%s"%funcName))
+                                func = Functor(getattr(self.editor, "objectHandler%s"%funcName))
                             else: # when loaded outside of LE
                             else: # when loaded outside of LE
-                                func = Functor(eval("base.objectHandler%s"%funcName))                        
+                                func = Functor(getattr(base, "objectHandler%s"%funcName))                        
                         else:
                         else:
                             # when it's not using default objectHandler, whole name of the handling obj
                             # when it's not using default objectHandler, whole name of the handling obj
                             # should be included in function name
                             # should be included in function name
@@ -686,11 +686,11 @@ class ObjectMgrBase:
                 if type(funcName) == types.StringType:
                 if type(funcName) == types.StringType:
                     if funcName.startswith('.'):
                     if funcName.startswith('.'):
                         if self.editor:
                         if self.editor:
-                            func = Functor(eval("self.editor.objectHandler%s"%funcName), **kwargs)
-                            undoFunc = Functor(eval("self.editor.objectHandler%s"%funcName), **undoKwargs)
+                            func = Functor(getattr(self.editor, "objectHandler%s"%funcName), **kwargs)
+                            undoFunc = Functor(getattr(self.editor, "objectHandler%s"%funcName), **undoKwargs)
                         else: # when loaded outside of LE
                         else: # when loaded outside of LE
-                            func = Functor(eval("base.objectHandler%s"%funcName), **kwargs)
-                            undoFunc = Functor(eval("base.objectHandler%s"%funcName), **undoKwargs)                    
+                            func = Functor(getattr(base, "objectHandler%s"%funcName), **kwargs)
+                            undoFunc = Functor(getattr(base, ".objectHandler%s"%funcName), **undoKwargs)                    
                     else:
                     else:
                         func = Functor(eval(funcName), **kwargs)
                         func = Functor(eval(funcName), **kwargs)
                         undoFunc = Functor(eval(funcName), **undoKwargs)
                         undoFunc = Functor(eval(funcName), **undoKwargs)

+ 2 - 2
direct/src/particles/Particles.py

@@ -354,7 +354,7 @@ class Particles(ParticleSystem):
                     else:
                     else:
                         file.write(targ+'.renderer.setColorBlendMode(ColorBlendAttrib.%s)\n' % cbmLut[cbMode])
                         file.write(targ+'.renderer.setColorBlendMode(ColorBlendAttrib.%s)\n' % cbmLut[cbMode])
             cim = self.renderer.getColorInterpolationManager()
             cim = self.renderer.getColorInterpolationManager()
-            segIdList = eval('['+cim.getSegmentIdList().replace(' ',', ')+']')
+            segIdList = [int(seg) for seg in cim.getSegmentIdList().split()]
             for sid in segIdList:
             for sid in segIdList:
                 seg = cim.getSegment(sid)
                 seg = cim.getSegment(sid)
                 if seg.isEnabled():
                 if seg.isEnabled():
@@ -457,7 +457,7 @@ class Particles(ParticleSystem):
                     else:
                     else:
                         file.write(targ+'.renderer.setColorBlendMode(ColorBlendAttrib.%s)\n' % cbmLut[cbMode])
                         file.write(targ+'.renderer.setColorBlendMode(ColorBlendAttrib.%s)\n' % cbmLut[cbMode])
             cim = self.renderer.getColorInterpolationManager()
             cim = self.renderer.getColorInterpolationManager()
-            segIdList = eval('['+cim.getSegmentIdList().replace(' ',', ')+']')
+            segIdList = [int(seg) for seg in cim.getSegmentIdList().split()]
             for sid in segIdList:
             for sid in segIdList:
                 seg = cim.getSegment(sid)
                 seg = cim.getSegment(sid)
                 if seg.isEnabled():
                 if seg.isEnabled():

+ 1 - 4
direct/src/showbase/ElementTree.py

@@ -749,10 +749,7 @@ def _encode(s, encoding):
     except AttributeError:
     except AttributeError:
         return s # 1.5.2: assume the string uses the right encoding
         return s # 1.5.2: assume the string uses the right encoding
 
 
-if sys.version[:3] == "1.5":
-    _escape = re.compile(r"[&<>\"\x80-\xff]+") # 1.5.2
-else:
-    _escape = re.compile(eval(r'u"[&<>\"\u0080-\uffff]+"'))
+_escape = re.compile(u"[&<>\"\u0080-\uffff]+")
 
 
 _escape_map = {
 _escape_map = {
     "&": "&amp;",
     "&": "&amp;",

+ 1 - 1
direct/src/showbase/PythonUtil.py

@@ -4188,7 +4188,7 @@ def unescapeHtmlString(s):
             char = ' '
             char = ' '
         elif char == '%':
         elif char == '%':
             if i < (len(s)-2):
             if i < (len(s)-2):
-                num = eval('0x' + s[i+1:i+3])
+                num = int(s[i+1:i+3], 16)
                 char = chr(num)
                 char = chr(num)
                 i += 2
                 i += 2
         i += 1
         i += 1