Skip to content

Commit c397c8a

Browse files
author
Michael Howitz
authored
Avoid deprecation warnings when using Python 3.8+. (#218)
1 parent 7f705eb commit c397c8a

File tree

5 files changed

+66
-53
lines changed

5 files changed

+66
-53
lines changed

.meta.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# https://github.com/zopefoundation/meta/tree/master/config/pure-python
33
[meta]
44
template = "pure-python"
5-
commit-id = "3ffdf08eda5163647c93d7e25faca354d176ae77"
5+
commit-id = "886173bf647a6e80a985544c32053c932b1949e2"
66

77
[python]
88
with-pypy = false
@@ -51,7 +51,7 @@ testenv-additional = [
5151
" coverage combine",
5252
" coverage html",
5353
" coverage report -m --fail-under=100",
54-
"depends = py27,py35,py36,py39-datetime,py37,py38,py39,coverage",
54+
"depends = py27,py35,py36,py37,py38,py39,py39-datetime,py310,coverage",
5555
]
5656
coverage-basepython = "python3.8"
5757
coverage-command = "pytest --cov=src --cov=tests --cov-report= {posargs}"
@@ -60,7 +60,7 @@ coverage-setenv = [
6060
]
6161

6262
[coverage]
63-
fail-under = 98.9
63+
fail-under = 98.7
6464

6565
[manifest]
6666
additional-rules = [

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Changes
66

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

9+
- Avoid deprecation warnings when using Python 3.8+.
10+
(`#192 <https://github.com/zopefoundation/RestrictedPython/issues/192>`_)
11+
912
- Allow to use the package with Python 3.10 -- Caution: No security audit has
1013
been done so far.
1114

constraints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ isort >= 4.3.2
55
# Needed for Appveyor as long as PY2 is supported:
66
pytest < 5
77
pytest-html < 2
8+
# coverage 6+ no longer supports Python 2 and coverage results of older
9+
# versions cannot not combined with newer ones:
10+
coverage < 6

src/RestrictedPython/transformer.py

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from ._compat import IS_PY3
2727
from ._compat import IS_PY34_OR_GREATER
2828
from ._compat import IS_PY35_OR_GREATER
29+
from ._compat import IS_PY38_OR_GREATER
2930

3031
import ast
3132
import contextlib
@@ -541,27 +542,65 @@ def node_contents_visit(self, node):
541542

542543
# ast for Literals
543544

544-
def visit_Num(self, node):
545-
"""Allow integer numbers without restrictions.
545+
if IS_PY38_OR_GREATER:
546546

547-
Replaced by Constant in Python 3.8.
548-
"""
549-
return self.node_contents_visit(node)
547+
def visit_Constant(self, node):
548+
"""Allow constant literals with restriction for Ellipsis.
550549
551-
def visit_Str(self, node):
552-
"""Allow string literals without restrictions.
550+
Constant replaces Num, Str, Bytes, NameConstant and Ellipsis in
551+
Python 3.8+.
552+
:see: https://docs.python.org/dev/whatsnew/3.8.html#deprecated
553+
"""
554+
if node.value is Ellipsis:
555+
# Deny using `...`.
556+
# Special handling necessary as ``self.not_allowed(node)``
557+
# would return the Error Message:
558+
# 'Constant statements are not allowed.'
559+
# which is only partial true.
560+
self.error(node, 'Ellipsis statements are not allowed.')
561+
return
562+
return self.node_contents_visit(node)
553563

554-
Replaced by Constant in Python 3.8.
555-
"""
556-
return self.node_contents_visit(node)
564+
else:
557565

558-
def visit_Bytes(self, node):
559-
"""Allow bytes literals without restrictions.
566+
def visit_Num(self, node):
567+
"""Allow integer numbers without restrictions.
560568
561-
Bytes is Python 3 only.
562-
Replaced by Constant in Python 3.8.
563-
"""
564-
return self.node_contents_visit(node)
569+
Replaced by Constant in Python 3.8.
570+
"""
571+
return self.node_contents_visit(node)
572+
573+
def visit_Str(self, node):
574+
"""Allow string literals without restrictions.
575+
576+
Replaced by Constant in Python 3.8.
577+
"""
578+
return self.node_contents_visit(node)
579+
580+
def visit_Bytes(self, node):
581+
"""Allow bytes literals without restrictions.
582+
583+
Bytes is Python 3 only.
584+
Replaced by Constant in Python 3.8.
585+
"""
586+
return self.node_contents_visit(node)
587+
588+
def visit_Ellipsis(self, node):
589+
"""Deny using `...`.
590+
591+
Ellipsis exists only in Python 3.
592+
Replaced by Constant in Python 3.8.
593+
"""
594+
return self.not_allowed(node)
595+
596+
def visit_NameConstant(self, node):
597+
"""Allow constant literals (True, False, None) without ...
598+
599+
restrictions.
600+
601+
Replaced by Constant in Python 3.8.
602+
"""
603+
return self.node_contents_visit(node)
565604

566605
def visit_List(self, node):
567606
"""Allow list literals without restrictions."""
@@ -587,38 +626,6 @@ def visit_JoinedStr(self, node):
587626
"""Allow joined string without restrictions."""
588627
return self.node_contents_visit(node)
589628

590-
def visit_Constant(self, node):
591-
"""Allow constant literals with restriction for Ellipsis.
592-
593-
Constant replaces Num, Str, Bytes, NameConstant and Ellipsis in
594-
Python 3.8+.
595-
:see: https://docs.python.org/dev/whatsnew/3.8.html#deprecated
596-
"""
597-
if node.value is Ellipsis:
598-
# Deny using `...`.
599-
# Special handling necessary as ``self.not_allowed(node)``
600-
# would return the Error Message:
601-
# 'Constant statements are not allowed.'
602-
# which is only partial true.
603-
self.error(node, 'Ellipsis statements are not allowed.')
604-
return
605-
return self.node_contents_visit(node)
606-
607-
def visit_Ellipsis(self, node):
608-
"""Deny using `...`.
609-
610-
Ellipsis is exists only in Python 3.
611-
Replaced by Constant in Python 3.8.
612-
"""
613-
return self.not_allowed(node)
614-
615-
def visit_NameConstant(self, node):
616-
"""Allow constant literals (True, False, None) without restrictions.
617-
618-
Replaced by Constant in Python 3.8.
619-
"""
620-
return self.node_contents_visit(node)
621-
622629
# ast for Variables
623630

624631
def visit_Name(self, node):

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ commands =
5151
coverage combine
5252
coverage html
5353
coverage report -m --fail-under=100
54-
depends = py27,py35,py36,py39-datetime,py37,py38,py39,coverage
54+
depends = py27,py35,py36,py37,py38,py39,py39-datetime,py310,coverage
5555

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

9797
[coverage:run]
9898
branch = True

0 commit comments

Comments
 (0)