Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ast deprecations for 3.12 #257

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/RestrictedPython/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@
from ._compat import IS_PY38_OR_GREATER


if IS_PY38_OR_GREATER:
astStr = ast.Constant
astNum = ast.Constant
else: # pragma: no cover
astStr = ast.Str
astNum = ast.Num


# For AugAssign the operator must be converted to a string.
IOPERATOR_TO_STR = {
ast.Add: '+=',
Expand Down Expand Up @@ -119,14 +127,14 @@ def copy_locations(new_node, old_node):
assert 'lineno' in new_node._attributes
new_node.lineno = old_node.lineno

if IS_PY38_OR_GREATER:
if IS_PY38_OR_GREATER: # pragma: no branch
assert 'end_lineno' in new_node._attributes
new_node.end_lineno = old_node.end_lineno

assert 'col_offset' in new_node._attributes
new_node.col_offset = old_node.col_offset

if IS_PY38_OR_GREATER:
if IS_PY38_OR_GREATER: # pragma: no branch
assert 'end_col_offset' in new_node._attributes
new_node.end_col_offset = old_node.end_col_offset

Expand Down Expand Up @@ -272,7 +280,7 @@ def gen_unpack_spec(self, tpl):
"""
spec = ast.Dict(keys=[], values=[])

spec.keys.append(ast.Str('childs'))
spec.keys.append(astStr('childs'))
spec.values.append(ast.Tuple([], ast.Load()))

# starred elements in a sequence do not contribute into the min_len.
Expand All @@ -292,12 +300,12 @@ def gen_unpack_spec(self, tpl):

elif isinstance(val, ast.Tuple):
el = ast.Tuple([], ast.Load())
el.elts.append(ast.Num(idx - offset))
el.elts.append(astNum(idx - offset))
el.elts.append(self.gen_unpack_spec(val))
spec.values[0].elts.append(el)

spec.keys.append(ast.Str('min_len'))
spec.values.append(ast.Num(min_len))
spec.keys.append(astStr('min_len'))
spec.values.append(astNum(min_len))

return spec

Expand Down Expand Up @@ -484,7 +492,7 @@ def inject_print_collector(self, node, position=0):
if isinstance(node, ast.Module):
_print.lineno = position
_print.col_offset = position
if IS_PY38_OR_GREATER:
if IS_PY38_OR_GREATER: # pragma: no branch
_print.end_lineno = position
_print.end_col_offset = position
ast.fix_missing_locations(_print)
Expand Down Expand Up @@ -546,7 +554,7 @@ def visit_Constant(self, node):
return
return self.node_contents_visit(node)

else:
else: # pragma: no cover

def visit_Num(self, node):
"""Allow integer numbers without restrictions.
Expand Down Expand Up @@ -903,7 +911,7 @@ def visit_Attribute(self, node):
node = self.node_contents_visit(node)
new_node = ast.Call(
func=ast.Name('_getattr_', ast.Load()),
args=[node.value, ast.Str(node.attr)],
args=[node.value, astStr(node.attr)],
keywords=[])

copy_locations(new_node, node)
Expand Down Expand Up @@ -1107,7 +1115,7 @@ def visit_AugAssign(self, node):
value=ast.Call(
func=ast.Name('_inplacevar_', ast.Load()),
args=[
ast.Str(IOPERATOR_TO_STR[type(node.op)]),
astStr(IOPERATOR_TO_STR[type(node.op)]),
ast.Name(node.target.id, ast.Load()),
node.value
],
Expand Down
2 changes: 1 addition & 1 deletion tests/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_compile__invalid_syntax():
assert "SyntaxError: cannot assign to literal here." in str(err.value)
elif IS_PY38_OR_GREATER:
assert "cannot assign to literal at statement:" in str(err.value)
else:
else: # pragma: no cover
assert "can't assign to literal at statement:" in str(err.value)


Expand Down
12 changes: 6 additions & 6 deletions tests/test_compile_restricted_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_compile_restricted_function():
safe_locals = {}
exec(result.code, safe_globals, safe_locals)
hello_world = safe_locals['hello_world']
assert type(hello_world) == FunctionType
assert isinstance(hello_world, FunctionType)
assert hello_world() == 'Hello World!\n'


Expand Down Expand Up @@ -102,7 +102,7 @@ def test_compile_restricted_function_with_arguments():
safe_locals = {}
exec(result.code, safe_globals, safe_locals)
hello_world = safe_locals['hello_world']
assert type(hello_world) == FunctionType
assert isinstance(hello_world, FunctionType)
assert hello_world('Hello ', 'World!') == 'Hello World!\n'


Expand Down Expand Up @@ -136,7 +136,7 @@ def test_compile_restricted_function_can_access_global_variables():
safe_locals = {}
exec(result.code, safe_globals, safe_locals)
hello_world = safe_locals['hello_world']
assert type(hello_world) == FunctionType
assert isinstance(hello_world, FunctionType)
assert hello_world() == 'Hello World!\n'


Expand Down Expand Up @@ -165,7 +165,7 @@ def test_compile_restricted_function_pretends_the_code_is_executed_in_a_global_s
safe_locals = {}
exec(result.code, safe_globals, safe_locals)
hello_world = safe_locals['hello_world']
assert type(hello_world) == FunctionType
assert isinstance(hello_world, FunctionType)
hello_world()
assert safe_globals['output'] == 'foobar'

Expand Down Expand Up @@ -195,7 +195,7 @@ def test_compile_restricted_function_allows_invalid_python_identifiers_as_functi
safe_locals = {}
exec(result.code, safe_globals, safe_locals)
generated_function = tuple(safe_locals.values())[0]
assert type(generated_function) == FunctionType
assert isinstance(generated_function, FunctionType)
generated_function()
assert safe_globals['output'] == 'foobar'

Expand Down Expand Up @@ -246,7 +246,7 @@ def test_compile_restricted_function_invalid_syntax():
assert error_msg.startswith(
"Line 1: SyntaxError: cannot assign to literal at statement:"
)
else:
else: # pragma: no cover
assert error_msg.startswith(
"Line 1: SyntaxError: can't assign to literal at statement:"
)
2 changes: 1 addition & 1 deletion tests/transformer/test_dict_comprehension.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_dict_comprehension_with_attrs(mocker):
mocker.call(z[1], 'k'),
mocker.call(z[1], 'v'),
])
else:
else: # praga: no cover
calls.extend([
mocker.call(z[0], 'k'),
mocker.call(z[1], 'k'),
Expand Down