Skip to content

Commit

Permalink
Add preliminary Python 3.8 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Howitz committed Feb 8, 2019
1 parent c7a90b4 commit 492b545
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 14 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -25,7 +25,6 @@ matrix:
env: TOXENV=py38,py38-datetime
dist: xenial
sudo: true
allow_failures:
- python: "3.8-dev"
env: TOXENV=py38,py38-datetime

Expand Down
4 changes: 3 additions & 1 deletion docs/CHANGES.rst
Expand Up @@ -4,7 +4,9 @@ Changes
4.0b8 (unreleased)
------------------

- Nothing changed yet.
- Add preliminary support for Python 3.8. as of 3.8.0a1 is released.

- Allow the ``...`` (Ellipsis) statement. It is needed to support Python 3.8.


4.0b7 (2018-10-30)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -53,6 +53,7 @@ def read(*rnames):
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: Implementation :: CPython',
'Topic :: Security',
],
Expand Down
1 change: 1 addition & 0 deletions src/RestrictedPython/_compat.py
Expand Up @@ -9,6 +9,7 @@
IS_PY35_OR_GREATER = _version.major == 3 and _version.minor >= 5
IS_PY36_OR_GREATER = _version.major == 3 and _version.minor >= 6
IS_PY37_OR_GREATER = _version.major == 3 and _version.minor >= 7
IS_PY38_OR_GREATER = _version.major == 3 and _version.minor >= 8

if IS_PY2:
basestring = basestring # NOQA: F821 # Python 2 only built-in function
Expand Down
28 changes: 23 additions & 5 deletions src/RestrictedPython/transformer.py
Expand Up @@ -537,17 +537,24 @@ def node_contents_visit(self, node):
# ast for Literals

def visit_Num(self, node):
"""Allow integer numbers without restrictions."""
"""Allow integer numbers without restrictions.
Replaced by Constant in Python 3.8.
"""
return self.node_contents_visit(node)

def visit_Str(self, node):
"""Allow string literals without restrictions."""
"""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)

Expand All @@ -567,16 +574,27 @@ def visit_Dict(self, node):
"""Allow dict literals without restrictions."""
return self.node_contents_visit(node)

def visit_Constant(self, node):
"""Allow constant 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
"""
return self.node_contents_visit(node)

def visit_Ellipsis(self, node):
"""Deny using `...`.
"""Allow using `...`.
Ellipsis is exists only in Python 3.
Replaced by Constant in Python 3.8.
"""
self.not_allowed(node)
return self.node_contents_visit(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)

Expand Down
6 changes: 5 additions & 1 deletion tests/test_compile.py
Expand Up @@ -2,6 +2,7 @@
from RestrictedPython import CompileResult
from RestrictedPython._compat import IS_PY2
from RestrictedPython._compat import IS_PY3
from RestrictedPython._compat import IS_PY38_OR_GREATER
from tests import c_eval
from tests import c_exec
from tests import c_single
Expand Down Expand Up @@ -39,7 +40,10 @@ def test_compile__compile_restricted_invalid_mode_input():
def test_compile__invalid_syntax():
with pytest.raises(SyntaxError) as err:
compile_restricted(INVALID_ASSINGMENT, '<string>', 'exec')
assert "can't assign to literal at statement:" in str(err.value)
if IS_PY38_OR_GREATER:
assert "cannot assign to literal at statement:" in str(err.value)
else:
assert "can't assign to literal at statement:" in str(err.value)


@pytest.mark.parametrize(*c_exec)
Expand Down
10 changes: 4 additions & 6 deletions tests/transformer/test_base_types.py
@@ -1,5 +1,4 @@
from RestrictedPython._compat import IS_PY2
from tests import c_exec
from tests import e_eval

import pytest
Expand All @@ -25,8 +24,7 @@ def test_Set(e_eval):

@pytest.mark.skipif(IS_PY2,
reason="... is new in Python 3")
@pytest.mark.parametrize(*c_exec)
def test_Ellipsis(c_exec):
"""It prevents using the `ellipsis` statement."""
result = c_exec('...')
assert result.errors == ('Line 1: Ellipsis statements are not allowed.',)
@pytest.mark.parametrize(*e_eval)
def test_Ellipsis(e_eval):
"""It allows using the `...` statement."""
assert e_eval('...') == Ellipsis
1 change: 1 addition & 0 deletions tox.ini
Expand Up @@ -5,6 +5,7 @@ envlist =
py36,
py36-datetime,
py37,
py38,
# pypy,
# pypy3,
docs,
Expand Down

0 comments on commit 492b545

Please sign in to comment.