Skip to content

Commit

Permalink
- Prevent DeprecationWarnings from ast.Str and ast.Num on Pyt…
Browse files Browse the repository at this point in the history
…hon 3.12
  • Loading branch information
dataflake committed Aug 26, 2023
1 parent 34954e2 commit c9d1a37
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
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:
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

0 comments on commit c9d1a37

Please sign in to comment.