From 51dffd0f5a2dfb9fe72e711e91ed1f02cd454a54 Mon Sep 17 00:00:00 2001 From: rdb Date: Wed, 16 Apr 2014 13:09:12 +0000 Subject: [PATCH] Commit patch by cfsworks to clean up use of exec() and eval() --- direct/src/distributed/MsgTypes.py | 4 +--- direct/src/distributed/MsgTypesCMU.py | 4 +--- direct/src/gui/OnscreenGeom.py | 5 ++--- direct/src/gui/OnscreenImage.py | 5 ++--- direct/src/gui/OnscreenText.py | 5 ++--- direct/src/leveleditor/ObjectMgrBase.py | 12 ++++++------ direct/src/particles/Particles.py | 4 ++-- direct/src/showbase/ElementTree.py | 5 +---- direct/src/showbase/PythonUtil.py | 2 +- 9 files changed, 18 insertions(+), 28 deletions(-) diff --git a/direct/src/distributed/MsgTypes.py b/direct/src/distributed/MsgTypes.py index 283701e3b4..231cf78733 100644 --- a/direct/src/distributed/MsgTypes.py +++ b/direct/src/distributed/MsgTypes.py @@ -107,9 +107,7 @@ MsgName2Id = { MsgId2Names = invertDictLossless(MsgName2Id) # 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 QUIET_ZONE_IGNORED_LIST = [ diff --git a/direct/src/distributed/MsgTypesCMU.py b/direct/src/distributed/MsgTypesCMU.py index 8501866a5b..b91a505453 100644 --- a/direct/src/distributed/MsgTypesCMU.py +++ b/direct/src/distributed/MsgTypesCMU.py @@ -26,6 +26,4 @@ MsgName2Id = { MsgId2Names = invertDictLossless(MsgName2Id) # 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) diff --git a/direct/src/gui/OnscreenGeom.py b/direct/src/gui/OnscreenGeom.py index fdd0f90f72..d9ef571aa6 100644 --- a/direct/src/gui/OnscreenGeom.py +++ b/direct/src/gui/OnscreenGeom.py @@ -113,8 +113,7 @@ class OnscreenGeom(DirectObject, NodePath): for option, value in kw.items(): # Use option string to access setter function 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 (setter == self.setHpr) or (setter == self.setScale)) and @@ -133,7 +132,7 @@ class OnscreenGeom(DirectObject, NodePath): def cget(self, option): # Get current configuration setting. # 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() # Allow index style refererences diff --git a/direct/src/gui/OnscreenImage.py b/direct/src/gui/OnscreenImage.py index 21486d2fb3..50923eec19 100644 --- a/direct/src/gui/OnscreenImage.py +++ b/direct/src/gui/OnscreenImage.py @@ -130,8 +130,7 @@ class OnscreenImage(DirectObject, NodePath): for option, value in kw.items(): # Use option string to access setter function 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 (setter == self.setHpr) or (setter == self.setScale)) and @@ -150,7 +149,7 @@ class OnscreenImage(DirectObject, NodePath): def cget(self, option): # Get current configuration setting. # 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() # Allow index style refererences diff --git a/direct/src/gui/OnscreenText.py b/direct/src/gui/OnscreenText.py index f8160065db..ed212d2643 100644 --- a/direct/src/gui/OnscreenText.py +++ b/direct/src/gui/OnscreenText.py @@ -381,8 +381,7 @@ class OnscreenText(DirectObject, NodePath): for option, value in kw.items(): # Use option string to access setter function try: - setter = eval('self.set' + - string.upper(option[0]) + option[1:]) + setter = getattr(self, 'set' + option[0].upper() + option[1:]) if setter == self.setPos: setter(value[0], value[1]) else: @@ -397,7 +396,7 @@ class OnscreenText(DirectObject, NodePath): def cget(self, option): # Get current configuration setting. # 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() def setAlign(self, align): diff --git a/direct/src/leveleditor/ObjectMgrBase.py b/direct/src/leveleditor/ObjectMgrBase.py index 7838d3922b..2524ca404e 100755 --- a/direct/src/leveleditor/ObjectMgrBase.py +++ b/direct/src/leveleditor/ObjectMgrBase.py @@ -189,9 +189,9 @@ class ObjectMgrBase: if funcName.startswith('.'): # when it's using default objectHandler 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 - func = Functor(eval("base.objectHandler%s"%funcName)) + func = Functor(getattr(base, "objectHandler%s"%funcName)) else: # when it's not using default objectHandler, whole name of the handling obj # should be included in function name @@ -686,11 +686,11 @@ class ObjectMgrBase: if type(funcName) == types.StringType: if funcName.startswith('.'): 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 - 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: func = Functor(eval(funcName), **kwargs) undoFunc = Functor(eval(funcName), **undoKwargs) diff --git a/direct/src/particles/Particles.py b/direct/src/particles/Particles.py index 974ff4af9e..cdf0550d05 100644 --- a/direct/src/particles/Particles.py +++ b/direct/src/particles/Particles.py @@ -354,7 +354,7 @@ class Particles(ParticleSystem): else: file.write(targ+'.renderer.setColorBlendMode(ColorBlendAttrib.%s)\n' % cbmLut[cbMode]) cim = self.renderer.getColorInterpolationManager() - segIdList = eval('['+cim.getSegmentIdList().replace(' ',', ')+']') + segIdList = [int(seg) for seg in cim.getSegmentIdList().split()] for sid in segIdList: seg = cim.getSegment(sid) if seg.isEnabled(): @@ -457,7 +457,7 @@ class Particles(ParticleSystem): else: file.write(targ+'.renderer.setColorBlendMode(ColorBlendAttrib.%s)\n' % cbmLut[cbMode]) cim = self.renderer.getColorInterpolationManager() - segIdList = eval('['+cim.getSegmentIdList().replace(' ',', ')+']') + segIdList = [int(seg) for seg in cim.getSegmentIdList().split()] for sid in segIdList: seg = cim.getSegment(sid) if seg.isEnabled(): diff --git a/direct/src/showbase/ElementTree.py b/direct/src/showbase/ElementTree.py index 3f368edd39..4e52ff2396 100755 --- a/direct/src/showbase/ElementTree.py +++ b/direct/src/showbase/ElementTree.py @@ -749,10 +749,7 @@ def _encode(s, encoding): except AttributeError: 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 = { "&": "&", diff --git a/direct/src/showbase/PythonUtil.py b/direct/src/showbase/PythonUtil.py index 9f18522c2e..1bcade4031 100644 --- a/direct/src/showbase/PythonUtil.py +++ b/direct/src/showbase/PythonUtil.py @@ -4188,7 +4188,7 @@ def unescapeHtmlString(s): char = ' ' elif char == '%': if i < (len(s)-2): - num = eval('0x' + s[i+1:i+3]) + num = int(s[i+1:i+3], 16) char = chr(num) i += 2 i += 1