Skip to content

Commit

Permalink
Merge branch 'Python3_update' into fix-full-write-guard
Browse files Browse the repository at this point in the history
  • Loading branch information
loechel committed Mar 27, 2017
2 parents 310bd32 + a3e9051 commit 1593aca
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 251 deletions.
12 changes: 6 additions & 6 deletions src/RestrictedPython/Limits.py
Expand Up @@ -14,7 +14,7 @@
limited_builtins = {}


def _limited_range(iFirst, *args):
def limited_range(iFirst, *args):
# limited range function from Martijn Pieters
RANGELIMIT = 1000
if not len(args):
Expand All @@ -35,22 +35,22 @@ def _limited_range(iFirst, *args):
return range(iStart, iEnd, iStep)


limited_builtins['range'] = _limited_range
limited_builtins['range'] = limited_range


def _limited_list(seq):
def limited_list(seq):
if isinstance(seq, str):
raise TypeError('cannot convert string to list')
return list(seq)


limited_builtins['list'] = _limited_list
limited_builtins['list'] = limited_list


def _limited_tuple(seq):
def limited_tuple(seq):
if isinstance(seq, str):
raise TypeError('cannot convert string to tuple')
return tuple(seq)


limited_builtins['tuple'] = _limited_tuple
limited_builtins['tuple'] = limited_tuple
54 changes: 26 additions & 28 deletions src/RestrictedPython/RCompile.py
Expand Up @@ -14,32 +14,31 @@
Python standard library.
"""

from compiler import ast as c_ast
from compiler import misc as c_misc
from compiler import parse as c_parse
from compiler import syntax as c_syntax
from compile import CompileResult
from compiler import ast
from compiler import misc
from compiler import parse
from compiler import pycodegen
from compiler import syntax
from compiler.pycodegen import AbstractCompileMode
from compiler.pycodegen import Expression
from compiler.pycodegen import findOp
from compiler.pycodegen import FunctionCodeGenerator # noqa
from compiler.pycodegen import Interactive
from compiler.pycodegen import Module
from compiler.pycodegen import ModuleCodeGenerator
from RestrictedPython import CompileResult
from RestrictedPython import MutatingWalker
from RestrictedPython.RestrictionMutator import RestrictionMutator
from RestrictionMutator import RestrictionMutator

import MutatingWalker

def _niceParse(source, filename, mode):

def niceParse(source, filename, mode):
if isinstance(source, unicode):
# Use the utf-8-sig BOM so the compiler
# detects this as a UTF-8 encoded string.
source = '\xef\xbb\xbf' + source.encode('utf-8')
try:
compiler_code = c_parse(source, mode)
# ast_code = ast.parse(source, filename, mode)
return compiler_code
return parse(source, mode)
except:
# Try to make a clean error message using
# the builtin Python compiler.
Expand All @@ -62,25 +61,24 @@ def __init__(self, source, filename):
AbstractCompileMode.__init__(self, source, filename)

def parse(self):
code = _niceParse(self.source, self.filename, self.mode)
return code
return niceParse(self.source, self.filename, self.mode)

def _get_tree(self):
c_tree = self.parse()
MutatingWalker.walk(c_tree, self.rm)
tree = self.parse()
MutatingWalker.walk(tree, self.rm)
if self.rm.errors:
raise SyntaxError(self.rm.errors[0])
c_misc.set_filename(self.filename, c_tree)
c_syntax.check(c_tree)
return c_tree
misc.set_filename(self.filename, tree)
syntax.check(tree)
return tree

def compile(self):
tree = self._get_tree()
gen = self.CodeGeneratorClass(tree)
self.code = gen.getCode()


def _compileAndTuplize(gen):
def compileAndTuplize(gen):
try:
gen.compile()
except TypeError as v:
Expand All @@ -104,22 +102,22 @@ def compile_restricted_function(p, body, name, filename, globalize=None):
appeared in a global statement at the top of the function).
"""
gen = RFunction(p, body, name, filename, globalize)
return _compileAndTuplize(gen)
return compileAndTuplize(gen)


def compile_restricted_exec(source, filename='<string>'):
"""Compiles a restricted code suite."""
gen = RModule(source, filename)
return _compileAndTuplize(gen)
return compileAndTuplize(gen)


def compile_restricted_eval(source, filename='<string>'):
"""Compiles a restricted expression."""
gen = RExpression(source, filename)
return _compileAndTuplize(gen)
return compileAndTuplize(gen)


def compile_restricted(source, filename, mode): # OLD
def compile_restricted(source, filename, mode):
"""Replacement for the builtin compile() function."""
if mode == "single":
gen = RInteractive(source, filename)
Expand Down Expand Up @@ -249,23 +247,23 @@ def __init__(self, p, body, name, filename, globals):
def parse(self):
# Parse the parameters and body, then combine them.
firstline = 'def f(%s): pass' % self.params
tree = _niceParse(firstline, '<function parameters>', 'exec')
tree = niceParse(firstline, '<function parameters>', 'exec')
f = tree.node.nodes[0]
body_code = _niceParse(self.body, self.filename, 'exec')
body_code = niceParse(self.body, self.filename, 'exec')
# Stitch the body code into the function.
f.code.nodes = body_code.node.nodes
f.name = self.name
# Look for a docstring, if there are any nodes at all
if len(f.code.nodes) > 0:
stmt1 = f.code.nodes[0]
if (isinstance(stmt1, c_ast.Discard) and
isinstance(stmt1.expr, c_ast.Const) and
if (isinstance(stmt1, ast.Discard) and
isinstance(stmt1.expr, ast.Const) and
isinstance(stmt1.expr.value, str)):
f.doc = stmt1.expr.value
# The caller may specify that certain variables are globals
# so that they can be referenced before a local assignment.
# The only known example is the variables context, container,
# script, traverse_subpath in PythonScripts.
if self.globals:
f.code.nodes.insert(0, c_ast.Global(self.globals))
f.code.nodes.insert(0, ast.Global(self.globals))
return tree
8 changes: 4 additions & 4 deletions src/RestrictedPython/SelectCompiler.py
Expand Up @@ -18,10 +18,10 @@
from compiler.consts import OP_ASSIGN
from compiler.consts import OP_DELETE
from compiler.transformer import parse
from RestrictedPython.RCompile import compile_restricted
from RestrictedPython.RCompile import compile_restricted_eval
from RestrictedPython.RCompile import compile_restricted_exec
from RestrictedPython.RCompile import compile_restricted_function
from RCompile import compile_restricted
from RCompile import compile_restricted_eval
from RCompile import compile_restricted_exec
from RCompile import compile_restricted_function

# Use the compiler from the standard library.
import compiler
6 changes: 6 additions & 0 deletions src/RestrictedPython/Utilities.py
Expand Up @@ -25,6 +25,12 @@
utility_builtins['set'] = set
utility_builtins['frozenset'] = frozenset

try:
import sets
utility_builtins['sets'] = sets
except ImportError:
pass

try:
import DateTime
utility_builtins['DateTime'] = DateTime.DateTime
Expand Down
172 changes: 0 additions & 172 deletions src/RestrictedPython/test_helper.py

This file was deleted.

9 changes: 4 additions & 5 deletions src/RestrictedPython/tests/testCompile.py
Expand Up @@ -12,8 +12,7 @@
#
##############################################################################

# Need to be ported
from RestrictedPython.RCompile import _niceParse
from RestrictedPython.RCompile import niceParse

import compiler.ast
import unittest
Expand All @@ -25,11 +24,11 @@ def testUnicodeSource(self):
# We support unicode sourcecode.
source = u"u'Ä väry nice säntänce with umlauts.'"

parsed = _niceParse(source, "test.py", "exec")
parsed = niceParse(source, "test.py", "exec")
self.failUnless(isinstance(parsed, compiler.ast.Module))
parsed = _niceParse(source, "test.py", "single")
parsed = niceParse(source, "test.py", "single")
self.failUnless(isinstance(parsed, compiler.ast.Module))
parsed = _niceParse(source, "test.py", "eval")
parsed = niceParse(source, "test.py", "eval")
self.failUnless(isinstance(parsed, compiler.ast.Expression))


Expand Down

0 comments on commit 1593aca

Please sign in to comment.