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 DeprecationWarnings under Python 3.12 #259

Merged
merged 2 commits into from
Aug 29, 2023
Merged
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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Features
Fixes
+++++

- Prevent DeprecationWarnings from ``ast.Str`` and ``ast.Num`` on Python 3.12

- Forbid using some attributes providing access to restricted Python internals.


Expand Down
20 changes: 14 additions & 6 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


# Avoid DeprecationWarnings under Python 3.12 and up
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 @@ -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 @@ -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