Guest User

Untitled

a guest
Jul 16th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. """
  2. <Author>
  3. Armon Dadgar
  4.  
  5. <Start Date>
  6. October 21st, 2009
  7.  
  8. <Description>
  9. This module provides a wrapper around the namespace layer, which allows
  10. namespaces to be dynamically created and manipulated.
  11.  
  12. """
  13.  
  14. # Used for safety checking
  15. import safe
  16.  
  17. # Used for thread-specific storage
  18. import threading
  19.  
  20.  
  21. # This is to work around safe...
  22. safe_compile = compile
  23.  
  24. # This is the default context provided to a namespace
  25. # See _get_default_context_copy()
  26. DEFAULT_CONTEXT = {}
  27.  
  28. # BAD: This should be done properly
  29. DEFAULT_CONTEXT["thread_local"] = threading.local
  30.  
  31.  
  32. # Initialize the default context with the user functions
  33. # Wrap this in an if to prevent initialization on every import
  34. _initialized = False
  35.  
  36. # Returns a copy of the default context, properly initialized
  37. def get_default_context_copy():
  38. global _initialized
  39. if not _initialized:
  40. _initialized = True
  41.  
  42. # Use the namespace module to setup the default context
  43. import namespace
  44. namespace.wrap_and_insert_api_functions(DEFAULT_CONTEXT)
  45.  
  46. # Return a copy of the default context, this allows for
  47. # isolation of modules using the default context
  48. copy = DEFAULT_CONTEXT.copy()
  49.  
  50. # Insert things that would require a deep copy (since dict.copy() returns a shallow copy)
  51.  
  52. # Since mycontext is a dictionary, we need a _separate_ copy rather than a shallow
  53. # copy which would be a reference to the same dictionary
  54. copy["mycontext"] = {}
  55.  
  56. # Return the copy
  57. return copy
  58.  
  59.  
  60. # This class is used to represent a namespace
  61. class VirtualNamespace(object):
  62. """
  63. The VirtualNamespace class is used as a wrapper around the
  64. context and instructions represented by a virtual namespace.
  65. """
  66.  
  67. # Constructor
  68. def __init__(self, code, name="<string>"):
  69. """
  70. <Purpose>
  71. Initializes the VirtualNamespace class.
  72.  
  73. <Arguments>
  74.  
  75. code:
  76. (String) The code to run in the namespace
  77.  
  78. name:
  79. (String, optional) The name to use for the code. When the module is
  80. being executed, if there is an exception, this name will appear in
  81. the trace back.
  82.  
  83. <Exceptions>
  84. A safety check is performed on the code, and an exception will be raised
  85. if the code fails the safety check.
  86. """
  87. # Check for the code
  88. # Do a type check
  89. if type(code) != str:
  90. raise Exception, "Code must be a string!"
  91.  
  92. if type(name) != str:
  93. raise Exception, "Name must be a string!"
  94.  
  95. # Remove any windows carriage returns
  96. code = code.replace('\r\n','\n')
  97.  
  98. # Do a safety check
  99. try:
  100. safe.safe_check(code)
  101. except Exception, e:
  102. raise Exception, "Code failed safety check! Error: "+str(e)
  103.  
  104. # All good, store the compiled byte code
  105. self.code = safe_compile(code,name,"exec")
  106.  
  107.  
  108. # Evaluates the virtual namespace
  109. def evaluate(self,context):
  110. """
  111. <Purpose>
  112. Evaluates the wrapped code within a context.
  113.  
  114. <Arguments>
  115. context: A global context to use when executing the code.
  116.  
  117. <Exceptions>
  118. Any that may be raised by the code that is being evaluated.
  119. """
  120. # Call safe_run
  121. safe.safe_run(self.code, context)
  122.  
  123. # Make this available
  124. DEFAULT_CONTEXT["VirtualNamespace"] = VirtualNamespace
Add Comment
Please, Sign In to add comment