Skip to content

Commit

Permalink
WIP - updates for Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
oz123 committed Oct 22, 2016
1 parent 19b2f99 commit 74a78c9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/RestrictedPython/test_helper.py
Expand Up @@ -22,6 +22,7 @@
"""

import dis
import sys
import types

from dis import findlinestarts
Expand Down Expand Up @@ -135,23 +136,34 @@ def __init__(self, opcode, pos):


def _disassemble(co, lasti=-1):
# in py2 code is str
code = co.co_code
#in py3 code is bytes
#if sys.version_info.major > 2:
# code = code.decode()
labels = dis.findlabels(code)
linestarts = dict(findlinestarts(co))
n = len(code)
i = 0
extended_arg = 0
free = co.co_cellvars + co.co_freevars
while i < n:
op = ord(code[i])
if sys.version_info.major < 3:
op = ord(code[i])
else:
op = code[i]
o = Op(op, i)
i += 1
if i in linestarts and i > 0:
o.line = linestarts[i]
if i in labels:
o.target = True
if op > dis.HAVE_ARGUMENT:
arg = ord(code[i]) + ord(code[i + 1]) * 256 + extended_arg
if sys.version_info.major < 3:
arg = ord(code[i]) + ord(code[i + 1]) * 256 + extended_arg
else:
arg = code[i] + code[i + 1] * 256 + extended_arg

extended_arg = 0
i += 2
if op == dis.EXTENDED_ARG:
Expand Down
37 changes: 37 additions & 0 deletions src/RestrictedPython/tests/security_in_syntax.py
Expand Up @@ -87,3 +87,40 @@ def no_augmeneted_assignment_to_slice():

def no_augmeneted_assignment_to_slice2():
a[x:y:z] += c

# These are all supposed to raise a SyntaxError when using
# compile_restricted() but not when using compile().
# Each function in this module is compiled using compile_restricted().


def with_as_bad_name():
with x as _leading_underscore:
pass


def relative_import_as_bad_name():
from .x import y as _leading_underscore


def except_as_bad_name():
try:
1 / 0
except Exception as _leading_underscore:
pass

# These are all supposed to raise a SyntaxError when using
# compile_restricted() but not when using compile().
# Each function in this module is compiled using compile_restricted().


def dict_comp_bad_name():
{y: y for _restricted_name in x}


def set_comp_bad_name():
{y for _restricted_name in x}


def compound_with_bad_name():
with a as b, c as _restricted_name:
pass
10 changes: 6 additions & 4 deletions src/RestrictedPython/transformer.py
Expand Up @@ -123,7 +123,7 @@
#ast.Nonlocal,
ast.ClassDef,
ast.Module,
ast.Param
ast.Param,
]


Expand Down Expand Up @@ -158,7 +158,9 @@
ast.Bytes,
ast.Starred,
ast.arg,
#ast.Try, # Try should not be supported
ast.Try, # Try should not be supported
ast.TryExcept, # TryExcept should not be supported
ast.NameConstant
])

if version >= (3, 4):
Expand Down Expand Up @@ -317,8 +319,8 @@ def check_name(self, node, name):
self.error(node, '"%s" is an invalid variable name because '
'it ends with "__roles__".' % name)

elif name == "printed":
self.error(node, '"printed" is a reserved name.')
# elif name == "printed":
# self.error(node, '"printed" is a reserved name.')

def transform_seq_unpack(self, tpl):
"""Protects sequence unpacking with _getiter_"""
Expand Down

0 comments on commit 74a78c9

Please sign in to comment.