Commits

uniqx  committed b077557

refactored execution of code objects into a new function

  • Participants
  • Parent commits 58902d8

Comments (0)

Files changed (1)

 
   # unload if already initialized
   if handle['init']:
-    __name__ = '__pyld_unload__'
-    exec(handle['code_object_unload'])
-    __name__ = '__main__'
+    exec_code_object(handle,'unload')
 
   # (re)read the file
   read_and_compile_script(handle)
 
   # initialize if not yet initialized
   if not handle['init']:
-    __name__ = '__pyld_init__'
-    exec(handle['code_object_init'])
-    __name__ = '__main__'
+    exec_code_object(handle,'init')
     handle['init'] = True
 
   # exec reload
-  __name__ = '__pyld_reload__'
-  exec(handle['code_object_reload'])
-  __name__ = '__main__'
+  exec_code_object(handle,'reload')
 
   # update modification time
   handle['mtime'] = mtime
         pass
 
 
+def exec_code_object(handle,target):
+  '''executes a prepared code object that is stored inside the handle already.
+  if no code object for the given target exists, calling this function will
+  have no effect, but won't fail either.
+
+  :param str target: valid target are: init, reload, unload
+  '''
+
+  if target in ('init','reload','unload'):
+    try:
+      __name__ = '__pyld_'+target+'__'
+      exec(handle['code_object_'+target])
+      __name__ = '__main__'
+    except KeyError:
+      pass
+
 
 def read_and_compile_script(handle):
   '''reads a script file, parses it and creates code objects for