Commit 5b7bb59c authored by Evan Simpson's avatar Evan Simpson

Collector #445: Add internal global declaration for Script bindings.

parent 005b17f1
......@@ -33,6 +33,8 @@ Zope Changes
(such as storages, databases, or logging handlers) to be used.
Bugs fixed
- Collector #445: Add internal global declaration for Script bindings.
- Collector #616: Make CONTEXTS available to TALES Python expressions.
- Collector #1074: Give Script execution context a __name__
......
......@@ -17,7 +17,7 @@ This product provides support for Script objects containing restricted
Python code.
"""
__version__='$Revision: 1.49 $'[11:-2]
__version__='$Revision: 1.50 $'[11:-2]
import sys, os, traceback, re, marshal, new
from Globals import DTMLFile, MessageDialog, package_home
......@@ -220,11 +220,13 @@ class PythonScript(Script, Historical, Cacheable):
else:
self._newfun(marshal.loads(self._code))
def _compiler(self, *args):
return RestrictedPython.compile_restricted_function(*args)
def _compiler(self, *args, **kw):
return RestrictedPython.compile_restricted_function(*args, **kw)
def _compile(self):
bind_names = self.getBindingAssignments().getAssignedNamesInOrder()
r = self._compiler(self._params, self._body or 'pass',
self.id, self.meta_type)
self.id, self.meta_type,
globalize=bind_names)
code = r[0]
errors = r[1]
self.warnings = tuple(r[2])
......
......@@ -14,7 +14,7 @@
Python standard library.
"""
__version__='$Revision: 1.3 $'[11:-2]
__version__='$Revision: 1.4 $'[11:-2]
from compiler import ast, parse, misc, syntax
......@@ -79,10 +79,11 @@ class RFunction(RModule):
"""A restricted Python function built from parts.
"""
def __init__(self, p, body, name, filename):
def __init__(self, p, body, name, filename, globalize=None):
self.params = p
self.body = body
self.name = name
self.globalize = globalize
RModule.__init__(self, None, filename)
def parse(self):
......@@ -100,6 +101,8 @@ class RFunction(RModule):
isinstance(stmt1.expr, ast.Const) and
type(stmt1.expr.value) is type('')):
f.doc = stmt1.expr.value
if self.globalize:
f.code.nodes.insert(0, ast.Global(map(str, self.globalize)))
return tree
......@@ -110,14 +113,14 @@ def compileAndTuplize(gen):
return None, (str(v),), gen.rm.warnings, gen.rm.used_names
return gen.getCode(), (), gen.rm.warnings, gen.rm.used_names
def compile_restricted_function(p, body, name, filename):
def compile_restricted_function(p, body, name, filename, globalize=None):
"""Compiles a restricted code object for a function.
The function can be reconstituted using the 'new' module:
new.function(<code>, <globals>)
"""
gen = RFunction(p, body, name, filename)
gen = RFunction(p, body, name, filename, globalize=globalize)
return compileAndTuplize(gen)
def compile_restricted_exec(s, filename='<string>'):
......
......@@ -11,7 +11,7 @@
#
##############################################################################
__version__='$Revision: 1.3 $'[11:-2]
__version__='$Revision: 1.4 $'[11:-2]
import sys
from traceback import format_exception_only
......@@ -43,7 +43,7 @@ import MutatingWalker
from RestrictionMutator import RestrictionMutator
from compiler_2_1 import ast, visitor, pycodegen
def compile_restricted_function(p, body, name, filename):
def compile_restricted_function(p, body, name, filename, globalize=None):
'''Compile a restricted code object for a function.
The function can be reconstituted using the 'new' module:
......@@ -63,6 +63,8 @@ def compile_restricted_function(p, body, name, filename):
f = tree.node.nodes[0]
btree, err = tryParsing(body, 'exec')
if err: return err
if globalize is not None:
btree.node.nodes.insert(0, ast.Global(map(str, globalize)))
f.code.nodes = btree.node.nodes
f.name = name
# Look for a docstring
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment