ebo avatar ebo committed 8c9455d

compile now creates function objects with the correct globals object.

Comments (0)

Files changed (1)

     return codeobj
 
 
-def compile(code):
-    """Compile code into a python function. Code may be a string or a function object"""
+def compile(code, g=None):
+    """Compile code into a python function. Code may be a string or a function object
+The globals object is autosensed when compiling a function object, for strings it inserts topcompiler modules globals. This can be overridden by providing a new globals object as second parameter."""
 
     if not PY_VERSION:
         raise TOPCompilerException("Incompatible Python Version. Expected version 2.6 or 2.7!")
     if isinstance(code, basestring):
         doc = code
         args = []
+        if g == None:
+            g = globals()
     elif isinstance(code, types.FunctionType):
         doc = code.__doc__
         args = code.func_code.co_varnames[:code.func_code.co_argcount]
+        if g == None:
+            g = code.func_globals
 
     ast = plygram.parser.parse(doc)
     TailRecursionAnalysis().analyse(ast)
 
     codeobj = compile_code(ast, args)
 
-    func = types.FunctionType(codeobj, globals())
+    func = types.FunctionType(codeobj, g)
     func.__doc__ = doc
     return func
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.