__restricted_builtin_funcs__.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. ################################################################################
  2. #
  3. # __restricted_builtin_funcs__.py
  4. #
  5. # This contains functions used by __restricted_builtin__ to do certain
  6. # potentially dangerous actions in a safe mode
  7. #
  8. ################################################################################
  9. import __builtin__
  10. def r_import(name, globals = {}, locals = {}, fromlist = []):
  11. '''Restricted __import__ only allows importing of specific modules'''
  12. ok_modules = ("mud", "obj", "char", "room", "exit", "account", "mudsock",
  13. "event", "action", "random", "traceback", "utils",
  14. "__restricted_builtin__")
  15. if name not in ok_modules:
  16. raise ImportError, "Untrusted module, %s" % name
  17. return __builtin__.__import__(name, globals, locals, fromlist)
  18. def r_open(file, mode = "r", buf = -1):
  19. if mode not in ('r', 'rb'):
  20. raise IOError, "can't open files for writing in restricted mode"
  21. return open(file, mode, buf)
  22. def r_exec(code):
  23. """exec is disabled in restricted mode"""
  24. raise NotImplementedError,"execution of code is disabled"
  25. def r_eval(code):
  26. """eval is disabled in restricted mode"""
  27. raise NotImplementedError,"evaluating code is disabled"
  28. def r_execfile(file):
  29. """executing files is disabled in restricted mode"""
  30. raise NotImplementedError,"executing files is disabled"
  31. def r_reload(module):
  32. """reloading modules is disabled in restricted mode"""
  33. raise NotImplementedError, "reloading modules is disabled"
  34. def r_unload(module):
  35. """unloading modules is disabled in restricted mode"""
  36. raise NotImplementedError, "unloading modules is disabled"