From 3a7ba7237314e183cbece1e39cf0762bf9e53fa8 Mon Sep 17 00:00:00 2001 From: Frederic Lepied Date: Fri, 19 Apr 2002 18:17:02 +0000 Subject: load the config file using the context of mseclib. --- share/msec.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/share/msec.py b/share/msec.py index ed8ea0c..f281bae 100755 --- a/share/msec.py +++ b/share/msec.py @@ -28,10 +28,37 @@ except IOError: _ = str # Eval a file +import mseclib +def import_only_mseclib(name, globals = None, locals = None, fromlist = None): + """ Import hook to allow only the mseclib module to be imported. """ + + if name == 'mseclib': + return mseclib + else: + raise ImportError, '%s cannot be imported' % name + def eval_file(name): - file = os.fdopen(os.open(os.path.expanduser(name), os.O_RDONLY)) - imp.load_source('', name, file) - file.close() + """ Eval level.local file. Only allow mseclib to be imported for + backward compatibility. """ + + globals = {} + locals = {} + builtins = {} + + # Insert symbols from mseclib into globals + non_exported_names = ['FAKE', 'indirect', 'commit_changes'] + for attrib_name in dir(mseclib): + if attrib_name[0] != '_' and attrib_name not in non_exported_names: + globals[attrib_name] = getattr(mseclib, attrib_name) + + # Set import hook -- it needs to be in globals['__builtins'] so we make + # a copy of builtins to put there + builtins.update(__builtins__.__dict__) + builtins['__import__'] = import_only_mseclib + globals['__builtins__'] = builtins + + # Exec file + execfile(os.path.expanduser(name), globals, locals) # program _name = 'msec' @@ -217,7 +244,7 @@ if os.path.exists(CONFIG): try: eval_file(CONFIG) except: - log(_('Error loading %s: %s') % (CONFIG, sys.exc_value[0])) + log(_('Error loading %s: %s') % (CONFIG, str(sys.exc_value))) commit_changes() -- cgit v1.2.1