Skip to content

Commit

Permalink
Avoid deprecation warnings when using Python 3.8+. (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Howitz committed Nov 19, 2021
1 parent 7f705eb commit c397c8a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 53 deletions.
6 changes: 3 additions & 3 deletions .meta.toml
Expand Up @@ -2,7 +2,7 @@
# https://github.com/zopefoundation/meta/tree/master/config/pure-python
[meta]
template = "pure-python"
commit-id = "3ffdf08eda5163647c93d7e25faca354d176ae77"
commit-id = "886173bf647a6e80a985544c32053c932b1949e2"

[python]
with-pypy = false
Expand Down Expand Up @@ -51,7 +51,7 @@ testenv-additional = [
" coverage combine",
" coverage html",
" coverage report -m --fail-under=100",
"depends = py27,py35,py36,py39-datetime,py37,py38,py39,coverage",
"depends = py27,py35,py36,py37,py38,py39,py39-datetime,py310,coverage",
]
coverage-basepython = "python3.8"
coverage-command = "pytest --cov=src --cov=tests --cov-report= {posargs}"
Expand All @@ -60,7 +60,7 @@ coverage-setenv = [
]

[coverage]
fail-under = 98.9
fail-under = 98.7

[manifest]
additional-rules = [
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -6,6 +6,9 @@ Changes

- Document that ``__name__`` is needed to define classes.

- Avoid deprecation warnings when using Python 3.8+.
(`#192 <https://github.com/zopefoundation/RestrictedPython/issues/192>`_)

- Allow to use the package with Python 3.10 -- Caution: No security audit has
been done so far.

Expand Down
3 changes: 3 additions & 0 deletions constraints.txt
Expand Up @@ -5,3 +5,6 @@ isort >= 4.3.2
# Needed for Appveyor as long as PY2 is supported:
pytest < 5
pytest-html < 2
# coverage 6+ no longer supports Python 2 and coverage results of older
# versions cannot not combined with newer ones:
coverage < 6
103 changes: 55 additions & 48 deletions src/RestrictedPython/transformer.py
Expand Up @@ -26,6 +26,7 @@
from ._compat import IS_PY3
from ._compat import IS_PY34_OR_GREATER
from ._compat import IS_PY35_OR_GREATER
from ._compat import IS_PY38_OR_GREATER

import ast
import contextlib
Expand Down Expand Up @@ -541,27 +542,65 @@ def node_contents_visit(self, node):

# ast for Literals

def visit_Num(self, node):
"""Allow integer numbers without restrictions.
if IS_PY38_OR_GREATER:

Replaced by Constant in Python 3.8.
"""
return self.node_contents_visit(node)
def visit_Constant(self, node):
"""Allow constant literals with restriction for Ellipsis.
def visit_Str(self, node):
"""Allow string literals without restrictions.
Constant replaces Num, Str, Bytes, NameConstant and Ellipsis in
Python 3.8+.
:see: https://docs.python.org/dev/whatsnew/3.8.html#deprecated
"""
if node.value is Ellipsis:
# Deny using `...`.
# Special handling necessary as ``self.not_allowed(node)``
# would return the Error Message:
# 'Constant statements are not allowed.'
# which is only partial true.
self.error(node, 'Ellipsis statements are not allowed.')
return
return self.node_contents_visit(node)

Replaced by Constant in Python 3.8.
"""
return self.node_contents_visit(node)
else:

def visit_Bytes(self, node):
"""Allow bytes literals without restrictions.
def visit_Num(self, node):
"""Allow integer numbers without restrictions.
Bytes is Python 3 only.
Replaced by Constant in Python 3.8.
"""
return self.node_contents_visit(node)
Replaced by Constant in Python 3.8.
"""
return self.node_contents_visit(node)

def visit_Str(self, node):
"""Allow string literals without restrictions.
Replaced by Constant in Python 3.8.
"""
return self.node_contents_visit(node)

def visit_Bytes(self, node):
"""Allow bytes literals without restrictions.
Bytes is Python 3 only.
Replaced by Constant in Python 3.8.
"""
return self.node_contents_visit(node)

def visit_Ellipsis(self, node):
"""Deny using `...`.
Ellipsis exists only in Python 3.
Replaced by Constant in Python 3.8.
"""
return self.not_allowed(node)

def visit_NameConstant(self, node):
"""Allow constant literals (True, False, None) without ...
restrictions.
Replaced by Constant in Python 3.8.
"""
return self.node_contents_visit(node)

def visit_List(self, node):
"""Allow list literals without restrictions."""
Expand All @@ -587,38 +626,6 @@ def visit_JoinedStr(self, node):
"""Allow joined string without restrictions."""
return self.node_contents_visit(node)

def visit_Constant(self, node):
"""Allow constant literals with restriction for Ellipsis.
Constant replaces Num, Str, Bytes, NameConstant and Ellipsis in
Python 3.8+.
:see: https://docs.python.org/dev/whatsnew/3.8.html#deprecated
"""
if node.value is Ellipsis:
# Deny using `...`.
# Special handling necessary as ``self.not_allowed(node)``
# would return the Error Message:
# 'Constant statements are not allowed.'
# which is only partial true.
self.error(node, 'Ellipsis statements are not allowed.')
return
return self.node_contents_visit(node)

def visit_Ellipsis(self, node):
"""Deny using `...`.
Ellipsis is exists only in Python 3.
Replaced by Constant in Python 3.8.
"""
return self.not_allowed(node)

def visit_NameConstant(self, node):
"""Allow constant literals (True, False, None) without restrictions.
Replaced by Constant in Python 3.8.
"""
return self.node_contents_visit(node)

# ast for Variables

def visit_Name(self, node):
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Expand Up @@ -51,7 +51,7 @@ commands =
coverage combine
coverage html
coverage report -m --fail-under=100
depends = py27,py35,py36,py39-datetime,py37,py38,py39,coverage
depends = py27,py35,py36,py37,py38,py39,py39-datetime,py310,coverage

[testenv:lint]
basepython = python3
Expand Down Expand Up @@ -92,7 +92,7 @@ commands =
pytest --cov=src --cov=tests --cov-report= {posargs}
coverage run -a -m sphinx -b doctest -d {envdir}/.cache/doctrees docs {envdir}/.cache/doctest
coverage html
coverage report -m --fail-under=98.9
coverage report -m --fail-under=98.7

[coverage:run]
branch = True
Expand Down

0 comments on commit c397c8a

Please sign in to comment.