From 96c1fd64bdb9fe446b24a36b2353bd662d46ec94 Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Fri, 5 May 2017 11:44:52 +0200 Subject: [PATCH 01/11] isort config --- setup.cfg | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/setup.cfg b/setup.cfg index 66acb8c..5889066 100644 --- a/setup.cfg +++ b/setup.cfg @@ -32,12 +32,29 @@ isort_ignore = [isort] force_alphabetical_sort = True force_single_line = True + +sections=FUTURE,STDLIB,ZOPE,FIRSTPARTY,THIRDPARTY,LOCALFOLDER + +known_zope = zope,Products,zc,z3c + +import_heading_stdlib = Standard Library Imports +import_heading_firstparty = Zope Imports + lines_after_imports = 2 line_length = 200 skip = bootstrap.py not_skip = __init__.py [flake8] -exclude = src/RestrictedPython/tests, - src/RestrictedPython/__init__.py, - src/RestrictedPython/SelectCompiler.py, +exclude = + bootstrap.py, + src/RestrictedPython/tests, + src/RestrictedPython/SelectCompiler.py, + +ignore = + N801, + N802, + N803, + N805, + N806, + N812 From 848fdfaee5fca38a277bb7074c2be3d6b2bc6dfa Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Fri, 5 May 2017 12:01:30 +0200 Subject: [PATCH 02/11] isort config --- setup.cfg | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 5889066..bd0f3c6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -33,8 +33,10 @@ isort_ignore = force_alphabetical_sort = True force_single_line = True -sections=FUTURE,STDLIB,ZOPE,FIRSTPARTY,THIRDPARTY,LOCALFOLDER +sections = FUTURE,STDLIB,ZOPE,FIRSTPARTY,THIRDPARTY,LOCALFOLDER +known_future_library = future,__future__ +known_standard_library = os,sys,types known_zope = zope,Products,zc,z3c import_heading_stdlib = Standard Library Imports From bbf0d9dc49cc129ffcd89be53368eecbdcfc86f8 Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Fri, 5 May 2017 11:44:52 +0200 Subject: [PATCH 03/11] isort config --- setup.cfg | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/setup.cfg b/setup.cfg index 9409106..ac0763f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -32,6 +32,14 @@ isort_ignore = [isort] force_alphabetical_sort = True force_single_line = True + +sections=FUTURE,STDLIB,ZOPE,FIRSTPARTY,THIRDPARTY,LOCALFOLDER + +known_zope = zope,Products,zc,z3c + +import_heading_stdlib = Standard Library Imports +import_heading_firstparty = Zope Imports + lines_after_imports = 2 line_length = 200 skip = From b688894927e52786d1fb4790c78f78cf13bcded1 Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Fri, 5 May 2017 12:01:30 +0200 Subject: [PATCH 04/11] isort config --- setup.cfg | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index ac0763f..c2ba707 100644 --- a/setup.cfg +++ b/setup.cfg @@ -33,8 +33,10 @@ isort_ignore = force_alphabetical_sort = True force_single_line = True -sections=FUTURE,STDLIB,ZOPE,FIRSTPARTY,THIRDPARTY,LOCALFOLDER +sections = FUTURE,STDLIB,ZOPE,FIRSTPARTY,THIRDPARTY,LOCALFOLDER +known_future_library = future,__future__ +known_standard_library = os,sys,types known_zope = zope,Products,zc,z3c import_heading_stdlib = Standard Library Imports From 4f62609bbc8015a35e634cb722f3fd234a68fc41 Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Fri, 5 May 2017 15:21:21 +0200 Subject: [PATCH 05/11] new isort config --- setup.cfg | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/setup.cfg b/setup.cfg index c2ba707..ab15b75 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,20 +30,30 @@ isort_ignore = [isort] -force_alphabetical_sort = True +force_alphabetical_sort = False force_single_line = True +force_to_top = False +from_first = True -sections = FUTURE,STDLIB,ZOPE,FIRSTPARTY,THIRDPARTY,LOCALFOLDER +sections = FUTURE,STDLIB,COMPATLIB,FIRSTPARTY,THIRDPARTY,ZOPE,LOCALFOLDER -known_future_library = future,__future__ -known_standard_library = os,sys,types -known_zope = zope,Products,zc,z3c +known_compatlib = six +known_firstparty = +known_thirdparty = +known_zope = zope,Products,zc,z3c,ExtensionClass,Acquisition,Persistence,RestrictedPython +known_localfolder = RestrictedPython +import_heading_future_library = Future Imports import_heading_stdlib = Standard Library Imports -import_heading_firstparty = Zope Imports +import_heading_compatlib = Python 2 / 3 compatibility helper libraries +import_heading_firstparty = +import_heading_thirdparty = +import_heading_zope = Zope Imports +import_heading_localfolder = RestrictedPython internal imports -lines_after_imports = 2 line_length = 200 +lines_after_imports = 2 + skip = bootstrap.py not_skip = From 3383f0aa9e0d5848c8c4e8dfe75f14bb9caa886b Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Fri, 5 May 2017 16:37:42 +0200 Subject: [PATCH 06/11] new isort config --- setup.cfg | 16 ++++++++++------ src/RestrictedPython/Eval.py | 7 +++++-- src/RestrictedPython/Guards.py | 1 + src/RestrictedPython/MutatingWalker.py | 3 ++- src/RestrictedPython/RCompile.py | 10 ++++++---- src/RestrictedPython/RestrictionMutator.py | 3 ++- src/RestrictedPython/SelectCompiler.py | 10 ++++++---- src/RestrictedPython/Utilities.py | 2 ++ src/RestrictedPython/__init__.py | 1 + src/RestrictedPython/_compat.py | 2 ++ src/RestrictedPython/compile.py | 9 ++++++--- .../tests/restricted_module.py | 2 ++ src/RestrictedPython/tests/testCompile.py | 7 +++++-- src/RestrictedPython/tests/testREADME.py | 3 ++- src/RestrictedPython/tests/testRestrictions.py | 15 +++++++++------ src/RestrictedPython/tests/verify.py | 2 ++ src/RestrictedPython/transformer.py | 11 +++++++---- tests/__init__.py | 2 +- tests/builtins/test_limits.py | 6 ++++-- tests/builtins/test_utilities.py | 6 ++++-- tests/test_Guards.py | 8 ++++++-- tests/test_compile.py | 17 ++++++++++++----- tests/test_compile_restricted_function.py | 13 ++++++++++--- tests/test_eval.py | 6 ++++-- tests/test_print_function.py | 2 +- tests/test_print_stmt.py | 10 +++++++--- .../operators/test_arithmetic_operators.py | 8 ++++++-- .../operators/test_bit_wise_operators.py | 6 ++++-- .../operators/test_bool_operators.py | 6 ++++-- .../operators/test_comparison_operators.py | 6 ++++-- .../operators/test_identity_operators.py | 6 ++++-- .../operators/test_logical_operators.py | 6 ++++-- .../operators/test_unary_operators.py | 6 ++++-- tests/transformer/test_async.py | 8 ++++++-- tests/transformer/test_base_types.py | 8 ++++++-- tests/transformer/test_global_local.py | 8 ++++++-- tests/transformer/test_slice.py | 6 +++++- tests/transformer/test_subscript.py | 6 ++++-- tests/transformer/test_transformer.py | 18 ++++++++++++------ tests/transformer/test_yield.py | 8 ++++++-- 40 files changed, 194 insertions(+), 86 deletions(-) diff --git a/setup.cfg b/setup.cfg index ab15b75..74180ad 100644 --- a/setup.cfg +++ b/setup.cfg @@ -35,21 +35,25 @@ force_single_line = True force_to_top = False from_first = True -sections = FUTURE,STDLIB,COMPATLIB,FIRSTPARTY,THIRDPARTY,ZOPE,LOCALFOLDER +sections = FUTURE,STDLIB,COMPATLIBS,TESTLIBS,FIRSTPARTY,THIRDPARTY,ZOPE,LOCALFOLDER,TESTINTERNALS -known_compatlib = six +known_compatlibs = six +known_testlibs = pytest known_firstparty = known_thirdparty = known_zope = zope,Products,zc,z3c,ExtensionClass,Acquisition,Persistence,RestrictedPython known_localfolder = RestrictedPython +known_testinternals = tests -import_heading_future_library = Future Imports -import_heading_stdlib = Standard Library Imports -import_heading_compatlib = Python 2 / 3 compatibility helper libraries +import_heading_future_library = Future imports +import_heading_stdlib = Standard library imports +import_heading_compatlibs = Python 2 / 3 compatibility helper libraries +import_heading_testlibs = Test framework imports import_heading_firstparty = import_heading_thirdparty = -import_heading_zope = Zope Imports +import_heading_zope = Zope imports import_heading_localfolder = RestrictedPython internal imports +import_heading_testinternals = Test internals (fixures and helpers) line_length = 200 lines_after_imports = 2 diff --git a/src/RestrictedPython/Eval.py b/src/RestrictedPython/Eval.py index 836ea5e..e0ed2a5 100644 --- a/src/RestrictedPython/Eval.py +++ b/src/RestrictedPython/Eval.py @@ -12,11 +12,14 @@ ############################################################################## """Restricted Python Expressions.""" +# Standard library imports +# Standard Library Imports +import ast + +# RestrictedPython internal imports from ._compat import IS_PY2 from .compile import compile_restricted_eval -import ast - if IS_PY2: from string import maketrans diff --git a/src/RestrictedPython/Guards.py b/src/RestrictedPython/Guards.py index 21c8b4e..fd61d0c 100644 --- a/src/RestrictedPython/Guards.py +++ b/src/RestrictedPython/Guards.py @@ -15,6 +15,7 @@ # AccessControl.ZopeGuards contains a large set of wrappers for builtins. # DocumentTemplate.DT_UTil contains a few. +# RestrictedPython internal imports from ._compat import IS_PY2 diff --git a/src/RestrictedPython/MutatingWalker.py b/src/RestrictedPython/MutatingWalker.py index 4aaa1d1..2bb3439 100644 --- a/src/RestrictedPython/MutatingWalker.py +++ b/src/RestrictedPython/MutatingWalker.py @@ -11,8 +11,9 @@ # ############################################################################## +# Standard library imports +# Standard Library Imports from compiler import ast - import warnings diff --git a/src/RestrictedPython/RCompile.py b/src/RestrictedPython/RCompile.py index 12c7be3..016639e 100644 --- a/src/RestrictedPython/RCompile.py +++ b/src/RestrictedPython/RCompile.py @@ -14,7 +14,8 @@ Python standard library. """ -from compile import CompileResult +# Standard library imports +# Standard Library Imports from compiler import ast from compiler import misc from compiler import parse @@ -22,15 +23,16 @@ from compiler import syntax from compiler.pycodegen import AbstractCompileMode from compiler.pycodegen import Expression -from compiler.pycodegen import findOp from compiler.pycodegen import FunctionCodeGenerator # noqa from compiler.pycodegen import Interactive from compiler.pycodegen import Module from compiler.pycodegen import ModuleCodeGenerator -from RestrictionMutator import RestrictionMutator +from compiler.pycodegen import findOp +import warnings +from compile import CompileResult +from RestrictionMutator import RestrictionMutator import MutatingWalker -import warnings warnings.warn( diff --git a/src/RestrictedPython/RestrictionMutator.py b/src/RestrictedPython/RestrictionMutator.py index 743a0cb..630d1b9 100644 --- a/src/RestrictedPython/RestrictionMutator.py +++ b/src/RestrictedPython/RestrictionMutator.py @@ -17,12 +17,13 @@ code in various ways before sending it to pycodegen. """ +# Standard library imports +# Standard Library Imports from compiler import ast from compiler.consts import OP_APPLY from compiler.consts import OP_ASSIGN from compiler.consts import OP_DELETE from compiler.transformer import parse - import warnings diff --git a/src/RestrictedPython/SelectCompiler.py b/src/RestrictedPython/SelectCompiler.py index debda17..20a44e0 100644 --- a/src/RestrictedPython/SelectCompiler.py +++ b/src/RestrictedPython/SelectCompiler.py @@ -13,20 +13,22 @@ """Compiler selector. """ +# Standard library imports +# Standard Library Imports from compiler import ast from compiler.consts import OP_APPLY from compiler.consts import OP_ASSIGN from compiler.consts import OP_DELETE from compiler.transformer import parse +# Use the compiler from the standard library. +import compiler +import warnings + from RCompile import compile_restricted from RCompile import compile_restricted_eval from RCompile import compile_restricted_exec from RCompile import compile_restricted_function -# Use the compiler from the standard library. -import compiler -import warnings - warnings.warn( "This Module (RestrictedPython.SelectCompiler) is deprecated" diff --git a/src/RestrictedPython/Utilities.py b/src/RestrictedPython/Utilities.py index 160fecd..6658b64 100644 --- a/src/RestrictedPython/Utilities.py +++ b/src/RestrictedPython/Utilities.py @@ -11,6 +11,8 @@ # ############################################################################## +# Standard library imports +# Standard Library Imports import math import random import string diff --git a/src/RestrictedPython/__init__.py b/src/RestrictedPython/__init__.py index 04b55c5..b9c99b9 100644 --- a/src/RestrictedPython/__init__.py +++ b/src/RestrictedPython/__init__.py @@ -45,5 +45,6 @@ # Policy from RestrictedPython.transformer import RestrictingNodeTransformer # isort:skip +# RestrictedPython internal imports # from RestrictedPython.Eval import RestrictionCapableEval diff --git a/src/RestrictedPython/_compat.py b/src/RestrictedPython/_compat.py index 5979ca5..0862eaf 100644 --- a/src/RestrictedPython/_compat.py +++ b/src/RestrictedPython/_compat.py @@ -1,3 +1,5 @@ + +# Standard library imports import sys diff --git a/src/RestrictedPython/compile.py b/src/RestrictedPython/compile.py index 095896e..1417106 100644 --- a/src/RestrictedPython/compile.py +++ b/src/RestrictedPython/compile.py @@ -1,10 +1,13 @@ -from collections import namedtuple -from RestrictedPython._compat import IS_PY2 -from RestrictedPython.transformer import RestrictingNodeTransformer +# Standard library imports +from collections import namedtuple import ast import warnings +# RestrictedPython internal imports +from RestrictedPython._compat import IS_PY2 +from RestrictedPython.transformer import RestrictingNodeTransformer + CompileResult = namedtuple( 'CompileResult', 'code, errors, warnings, used_names') diff --git a/src/RestrictedPython/tests/restricted_module.py b/src/RestrictedPython/tests/restricted_module.py index e6e7cc4..84c9506 100644 --- a/src/RestrictedPython/tests/restricted_module.py +++ b/src/RestrictedPython/tests/restricted_module.py @@ -1,3 +1,5 @@ + +# Standard library imports import sys diff --git a/src/RestrictedPython/tests/testCompile.py b/src/RestrictedPython/tests/testCompile.py index 859b20d..0a9004e 100644 --- a/src/RestrictedPython/tests/testCompile.py +++ b/src/RestrictedPython/tests/testCompile.py @@ -12,11 +12,14 @@ # ############################################################################## -from RestrictedPython.RCompile import niceParse - +# Standard library imports +# Standard Library Imports import compiler.ast import unittest +# RestrictedPython internal imports +from RestrictedPython.RCompile import niceParse + class CompileTests(unittest.TestCase): diff --git a/src/RestrictedPython/tests/testREADME.py b/src/RestrictedPython/tests/testREADME.py index 6800c4d..991f3e4 100644 --- a/src/RestrictedPython/tests/testREADME.py +++ b/src/RestrictedPython/tests/testREADME.py @@ -13,8 +13,9 @@ ############################################################################## """Run tests in README.txt """ +# Standard library imports +# Standard Library Imports from doctest import DocFileSuite - import unittest diff --git a/src/RestrictedPython/tests/testRestrictions.py b/src/RestrictedPython/tests/testRestrictions.py index 7527ffc..3d18677 100644 --- a/src/RestrictedPython/tests/testRestrictions.py +++ b/src/RestrictedPython/tests/testRestrictions.py @@ -5,18 +5,21 @@ # AccessControl, so we need to define throwaway wrapper implementations # here instead. +# Standard library imports +# Standard Library Imports +import os +import re +import sys +import unittest + +# RestrictedPython internal imports from RestrictedPython import PrintCollector -from RestrictedPython.RCompile import compile_restricted from RestrictedPython.RCompile import RFunction from RestrictedPython.RCompile import RModule +from RestrictedPython.RCompile import compile_restricted from RestrictedPython.tests import restricted_module from RestrictedPython.tests import verify -import os -import re -import sys -import unittest - try: __file__ diff --git a/src/RestrictedPython/tests/verify.py b/src/RestrictedPython/tests/verify.py index c3122b4..baac6d9 100644 --- a/src/RestrictedPython/tests/verify.py +++ b/src/RestrictedPython/tests/verify.py @@ -21,6 +21,8 @@ function. """ +# Standard library imports +# Standard Library Imports import dis import types import warnings diff --git a/src/RestrictedPython/transformer.py b/src/RestrictedPython/transformer.py index bef1d6a..ee793ff 100644 --- a/src/RestrictedPython/transformer.py +++ b/src/RestrictedPython/transformer.py @@ -22,15 +22,18 @@ # http://docs.plone.org/develop/styleguide/python.html +# Standard library imports +# Standard Library Imports +import ast +import contextlib +import textwrap + +# RestrictedPython internal imports from ._compat import IS_PY2 from ._compat import IS_PY3 from ._compat import IS_PY34_OR_GREATER from ._compat import IS_PY35_OR_GREATER -import ast -import contextlib -import textwrap - # For AugAssign the operator must be converted to a string. IOPERATOR_TO_STR = { diff --git a/tests/__init__.py b/tests/__init__.py index e93f012..cd4427e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,5 +1,5 @@ +# RestrictedPython internal imports from RestrictedPython._compat import IS_PY2 - import RestrictedPython diff --git a/tests/builtins/test_limits.py b/tests/builtins/test_limits.py index ac034be..3fa769b 100644 --- a/tests/builtins/test_limits.py +++ b/tests/builtins/test_limits.py @@ -1,9 +1,11 @@ +# Test framework imports +import pytest + +# RestrictedPython internal imports from RestrictedPython.Limits import limited_list from RestrictedPython.Limits import limited_range from RestrictedPython.Limits import limited_tuple -import pytest - def test_limited_range_length_1(): result = limited_range(1) diff --git a/tests/builtins/test_utilities.py b/tests/builtins/test_utilities.py index 2a83782..bad3acb 100644 --- a/tests/builtins/test_utilities.py +++ b/tests/builtins/test_utilities.py @@ -1,7 +1,9 @@ -from RestrictedPython._compat import IS_PY3 - +# Test framework imports import pytest +# RestrictedPython internal imports +from RestrictedPython._compat import IS_PY3 + def test_string_in_utility_builtins(): import string diff --git a/tests/test_Guards.py b/tests/test_Guards.py index 32040b1..dd23040 100644 --- a/tests/test_Guards.py +++ b/tests/test_Guards.py @@ -1,9 +1,13 @@ +# Test framework imports +import pytest + +# RestrictedPython internal imports from RestrictedPython.Guards import safe_builtins + +# Test internals (fixures and helpers) from tests import e_eval from tests import e_exec -import pytest - @pytest.mark.parametrize(*e_eval) def test_Guards__safe_builtins__1(e_eval): diff --git a/tests/test_compile.py b/tests/test_compile.py index 46914ec..39a1ccd 100644 --- a/tests/test_compile.py +++ b/tests/test_compile.py @@ -1,16 +1,23 @@ -from RestrictedPython import compile_restricted + +# Standard library imports +import types + +# Test framework imports +import pytest + +# RestrictedPython internal imports from RestrictedPython import CompileResult +from RestrictedPython import compile_restricted from RestrictedPython._compat import IS_PY2 from RestrictedPython._compat import IS_PY3 +import RestrictedPython.compile + +# Test internals (fixures and helpers) from tests import c_eval from tests import c_exec from tests import c_single from tests import e_eval -import pytest -import RestrictedPython.compile -import types - def test_compile__compile_restricted_invalid_code_input(): with pytest.raises(TypeError): diff --git a/tests/test_compile_restricted_function.py b/tests/test_compile_restricted_function.py index ca51aba..50b5dac 100644 --- a/tests/test_compile_restricted_function.py +++ b/tests/test_compile_restricted_function.py @@ -1,10 +1,17 @@ -from RestrictedPython import PrintCollector -from RestrictedPython import safe_builtins -from tests import c_function + +# Standard library imports from types import FunctionType +# Test framework imports import pytest +# RestrictedPython internal imports +from RestrictedPython import PrintCollector +from RestrictedPython import safe_builtins + +# Test internals (fixures and helpers) +from tests import c_function + @pytest.mark.parametrize(*c_function) def test_compile_restricted_function(c_function): diff --git a/tests/test_eval.py b/tests/test_eval.py index f9046a4..6c605b7 100644 --- a/tests/test_eval.py +++ b/tests/test_eval.py @@ -1,7 +1,9 @@ -from RestrictedPython.Eval import RestrictionCapableEval - +# Test framework imports import pytest +# RestrictedPython internal imports +from RestrictedPython.Eval import RestrictionCapableEval + exp = """ {'a':[m.pop()]}['a'] \ diff --git a/tests/test_print_function.py b/tests/test_print_function.py index 047e0fe..6404a3d 100644 --- a/tests/test_print_function.py +++ b/tests/test_print_function.py @@ -1,5 +1,5 @@ +# RestrictedPython internal imports from RestrictedPython.PrintCollector import PrintCollector - import RestrictedPython diff --git a/tests/test_print_stmt.py b/tests/test_print_stmt.py index 1165600..35b7e59 100644 --- a/tests/test_print_stmt.py +++ b/tests/test_print_stmt.py @@ -1,10 +1,14 @@ +# Test framework imports +import pytest + +# RestrictedPython internal imports from RestrictedPython._compat import IS_PY3 from RestrictedPython.PrintCollector import PrintCollector -from tests import c_exec - -import pytest import RestrictedPython +# Test internals (fixures and helpers) +from tests import c_exec + pytestmark = pytest.mark.skipif( IS_PY3, diff --git a/tests/transformer/operators/test_arithmetic_operators.py b/tests/transformer/operators/test_arithmetic_operators.py index 9861c72..4131cb9 100644 --- a/tests/transformer/operators/test_arithmetic_operators.py +++ b/tests/transformer/operators/test_arithmetic_operators.py @@ -1,9 +1,13 @@ +# Test framework imports +import pytest + +# RestrictedPython internal imports from RestrictedPython._compat import IS_PY35_OR_GREATER + +# Test internals (fixures and helpers) from tests import c_eval from tests import e_eval -import pytest - # Arithmetic Operators diff --git a/tests/transformer/operators/test_bit_wise_operators.py b/tests/transformer/operators/test_bit_wise_operators.py index 7bbb001..c9668c8 100644 --- a/tests/transformer/operators/test_bit_wise_operators.py +++ b/tests/transformer/operators/test_bit_wise_operators.py @@ -1,7 +1,9 @@ -from tests import e_eval - +# Test framework imports import pytest +# Test internals (fixures and helpers) +from tests import e_eval + @pytest.mark.parametrize(*e_eval) def test_BitAnd(e_eval): diff --git a/tests/transformer/operators/test_bool_operators.py b/tests/transformer/operators/test_bool_operators.py index f31c6fe..b65c707 100644 --- a/tests/transformer/operators/test_bool_operators.py +++ b/tests/transformer/operators/test_bool_operators.py @@ -1,7 +1,9 @@ -from tests import e_eval - +# Test framework imports import pytest +# Test internals (fixures and helpers) +from tests import e_eval + @pytest.mark.parametrize(*e_eval) def test_Or(e_eval): diff --git a/tests/transformer/operators/test_comparison_operators.py b/tests/transformer/operators/test_comparison_operators.py index 9d6545d..491a1dc 100644 --- a/tests/transformer/operators/test_comparison_operators.py +++ b/tests/transformer/operators/test_comparison_operators.py @@ -1,7 +1,9 @@ -from tests import e_eval - +# Test framework imports import pytest +# Test internals (fixures and helpers) +from tests import e_eval + @pytest.mark.parametrize(*e_eval) def test_Eq(e_eval): diff --git a/tests/transformer/operators/test_identity_operators.py b/tests/transformer/operators/test_identity_operators.py index 526362e..61b35ed 100644 --- a/tests/transformer/operators/test_identity_operators.py +++ b/tests/transformer/operators/test_identity_operators.py @@ -1,7 +1,9 @@ -from tests import e_eval - +# Test framework imports import pytest +# Test internals (fixures and helpers) +from tests import e_eval + @pytest.mark.parametrize(*e_eval) def test_Is(e_eval): diff --git a/tests/transformer/operators/test_logical_operators.py b/tests/transformer/operators/test_logical_operators.py index 1d572c8..c8f6a64 100644 --- a/tests/transformer/operators/test_logical_operators.py +++ b/tests/transformer/operators/test_logical_operators.py @@ -1,7 +1,9 @@ -from tests import e_eval - +# Test framework imports import pytest +# Test internals (fixures and helpers) +from tests import e_eval + @pytest.mark.parametrize(*e_eval) def test_In(e_eval): diff --git a/tests/transformer/operators/test_unary_operators.py b/tests/transformer/operators/test_unary_operators.py index abab659..c86f3cb 100644 --- a/tests/transformer/operators/test_unary_operators.py +++ b/tests/transformer/operators/test_unary_operators.py @@ -1,7 +1,9 @@ -from tests import e_eval - +# Test framework imports import pytest +# Test internals (fixures and helpers) +from tests import e_eval + @pytest.mark.parametrize(*e_eval) def test_UAdd(e_eval): diff --git a/tests/transformer/test_async.py b/tests/transformer/test_async.py index f09a738..6b81e76 100644 --- a/tests/transformer/test_async.py +++ b/tests/transformer/test_async.py @@ -1,9 +1,13 @@ +# Test framework imports +import pytest + +# RestrictedPython internal imports from RestrictedPython import compile_restricted_exec from RestrictedPython._compat import IS_PY35_OR_GREATER from RestrictedPython.transformer import RestrictingNodeTransformer -from tests import c_exec -import pytest +# Test internals (fixures and helpers) +from tests import c_exec pytestmark = pytest.mark.skipif( diff --git a/tests/transformer/test_base_types.py b/tests/transformer/test_base_types.py index 9dc0867..c8d0416 100644 --- a/tests/transformer/test_base_types.py +++ b/tests/transformer/test_base_types.py @@ -1,9 +1,13 @@ +# Test framework imports +import pytest + +# RestrictedPython internal imports from RestrictedPython._compat import IS_PY2 + +# Test internals (fixures and helpers) from tests import c_exec from tests import e_eval -import pytest - @pytest.mark.parametrize(*e_eval) def test_Num(e_eval): diff --git a/tests/transformer/test_global_local.py b/tests/transformer/test_global_local.py index 34ea1ee..ef12921 100644 --- a/tests/transformer/test_global_local.py +++ b/tests/transformer/test_global_local.py @@ -1,9 +1,13 @@ +# Test framework imports +import pytest + +# RestrictedPython internal imports from RestrictedPython._compat import IS_PY3 + +# Test internals (fixures and helpers) from tests import c_exec from tests import e_exec -import pytest - GLOBAL_EXAMPLE = """ def x(): diff --git a/tests/transformer/test_slice.py b/tests/transformer/test_slice.py index 6908aa9..a127fb8 100644 --- a/tests/transformer/test_slice.py +++ b/tests/transformer/test_slice.py @@ -1,8 +1,12 @@ +# Standard library imports from operator import getitem -from tests import e_eval +# Test framework imports import pytest +# Test internals (fixures and helpers) +from tests import e_eval + @pytest.mark.parametrize(*e_eval) def test_slice(e_eval): diff --git a/tests/transformer/test_subscript.py b/tests/transformer/test_subscript.py index 6a74fc1..706036d 100644 --- a/tests/transformer/test_subscript.py +++ b/tests/transformer/test_subscript.py @@ -1,7 +1,9 @@ -from tests import e_exec - +# Test framework imports import pytest +# Test internals (fixures and helpers) +from tests import e_exec + SIMPLE_SUBSCRIPTS = """ def simple_subscript(a): diff --git a/tests/transformer/test_transformer.py b/tests/transformer/test_transformer.py index 0e2933c..a97f6fe 100644 --- a/tests/transformer/test_transformer.py +++ b/tests/transformer/test_transformer.py @@ -1,19 +1,25 @@ +# Standard library imports +import ast +import contextlib +import types + +# Test framework imports +import pytest + +# RestrictedPython internal imports from RestrictedPython import RestrictingNodeTransformer from RestrictedPython._compat import IS_PY2 from RestrictedPython._compat import IS_PY3 from RestrictedPython.Guards import guarded_iter_unpack_sequence from RestrictedPython.Guards import guarded_unpack_sequence from RestrictedPython.Guards import safe_builtins +import RestrictedPython + +# Test internals (fixures and helpers) from tests import c_exec from tests import e_eval from tests import e_exec -import ast -import contextlib -import pytest -import RestrictedPython -import types - def test_transformer__RestrictingNodeTransformer__generic_visit__1(): """It log an error if there is an unknown ast node visited.""" diff --git a/tests/transformer/test_yield.py b/tests/transformer/test_yield.py index 3a1ad03..a4c8c79 100644 --- a/tests/transformer/test_yield.py +++ b/tests/transformer/test_yield.py @@ -1,7 +1,11 @@ +# Test framework imports +import pytest + +# RestrictedPython internal imports from RestrictedPython._compat import IS_PY3 -from tests import c_exec -import pytest +# Test internals (fixures and helpers) +from tests import c_exec YIELD_EXAMPLE = """\ From b278966e0bc89177ee807ed222ae63d158b04552 Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Tue, 9 May 2017 22:25:00 +0200 Subject: [PATCH 07/11] isort cleanup --- src/RestrictedPython/Eval.py | 5 ++-- src/RestrictedPython/Guards.py | 2 +- src/RestrictedPython/MutatingWalker.py | 1 - src/RestrictedPython/PrintCollector.py | 1 + src/RestrictedPython/RCompile.py | 10 +++---- src/RestrictedPython/RestrictionMutator.py | 1 - src/RestrictedPython/SelectCompiler.py | 5 ++-- src/RestrictedPython/Utilities.py | 1 - src/RestrictedPython/__init__.py | 35 ++++++++++------------ src/RestrictedPython/transformer.py | 4 +-- 10 files changed, 29 insertions(+), 36 deletions(-) diff --git a/src/RestrictedPython/Eval.py b/src/RestrictedPython/Eval.py index e0ed2a5..1f77282 100644 --- a/src/RestrictedPython/Eval.py +++ b/src/RestrictedPython/Eval.py @@ -13,12 +13,11 @@ """Restricted Python Expressions.""" # Standard library imports -# Standard Library Imports import ast # RestrictedPython internal imports -from ._compat import IS_PY2 -from .compile import compile_restricted_eval +from RestrictedPython._compat import IS_PY2 +from RestrictedPython.compile import compile_restricted_eval if IS_PY2: diff --git a/src/RestrictedPython/Guards.py b/src/RestrictedPython/Guards.py index fd61d0c..635e60e 100644 --- a/src/RestrictedPython/Guards.py +++ b/src/RestrictedPython/Guards.py @@ -16,7 +16,7 @@ # DocumentTemplate.DT_UTil contains a few. # RestrictedPython internal imports -from ._compat import IS_PY2 +from RestrictedPython._compat import IS_PY2 if IS_PY2: diff --git a/src/RestrictedPython/MutatingWalker.py b/src/RestrictedPython/MutatingWalker.py index 2bb3439..80e9d36 100644 --- a/src/RestrictedPython/MutatingWalker.py +++ b/src/RestrictedPython/MutatingWalker.py @@ -12,7 +12,6 @@ ############################################################################## # Standard library imports -# Standard Library Imports from compiler import ast import warnings diff --git a/src/RestrictedPython/PrintCollector.py b/src/RestrictedPython/PrintCollector.py index 44ac972..16831d7 100644 --- a/src/RestrictedPython/PrintCollector.py +++ b/src/RestrictedPython/PrintCollector.py @@ -10,6 +10,7 @@ # FOR A PARTICULAR PURPOSE # ############################################################################## + from __future__ import print_function diff --git a/src/RestrictedPython/RCompile.py b/src/RestrictedPython/RCompile.py index 016639e..60edd7d 100644 --- a/src/RestrictedPython/RCompile.py +++ b/src/RestrictedPython/RCompile.py @@ -15,7 +15,6 @@ """ # Standard library imports -# Standard Library Imports from compiler import ast from compiler import misc from compiler import parse @@ -30,9 +29,10 @@ from compiler.pycodegen import findOp import warnings -from compile import CompileResult -from RestrictionMutator import RestrictionMutator -import MutatingWalker +# RestrictedPython internal imports +from RestrictedPython.compile import CompileResult +from RestrictedPython.MutatingWalker import walk +from RestrictedPython.RestrictionMutator import RestrictionMutator warnings.warn( @@ -76,7 +76,7 @@ def parse(self): def _get_tree(self): tree = self.parse() - MutatingWalker.walk(tree, self.rm) + walk(tree, self.rm) if self.rm.errors: raise SyntaxError(self.rm.errors[0]) misc.set_filename(self.filename, tree) diff --git a/src/RestrictedPython/RestrictionMutator.py b/src/RestrictedPython/RestrictionMutator.py index 630d1b9..49e8de2 100644 --- a/src/RestrictedPython/RestrictionMutator.py +++ b/src/RestrictedPython/RestrictionMutator.py @@ -18,7 +18,6 @@ """ # Standard library imports -# Standard Library Imports from compiler import ast from compiler.consts import OP_APPLY from compiler.consts import OP_ASSIGN diff --git a/src/RestrictedPython/SelectCompiler.py b/src/RestrictedPython/SelectCompiler.py index 20a44e0..be1898c 100644 --- a/src/RestrictedPython/SelectCompiler.py +++ b/src/RestrictedPython/SelectCompiler.py @@ -13,14 +13,15 @@ """Compiler selector. """ +# flake8: NOQA: 401 +# isort: skip + # Standard library imports -# Standard Library Imports from compiler import ast from compiler.consts import OP_APPLY from compiler.consts import OP_ASSIGN from compiler.consts import OP_DELETE from compiler.transformer import parse -# Use the compiler from the standard library. import compiler import warnings diff --git a/src/RestrictedPython/Utilities.py b/src/RestrictedPython/Utilities.py index 6658b64..e96d594 100644 --- a/src/RestrictedPython/Utilities.py +++ b/src/RestrictedPython/Utilities.py @@ -12,7 +12,6 @@ ############################################################################## # Standard library imports -# Standard Library Imports import math import random import string diff --git a/src/RestrictedPython/__init__.py b/src/RestrictedPython/__init__.py index b9c99b9..91f2c5a 100644 --- a/src/RestrictedPython/__init__.py +++ b/src/RestrictedPython/__init__.py @@ -13,9 +13,10 @@ """RestrictedPython package.""" # flake8: NOQA: E401 +# isort: skip # This is a file to define public API in the base namespace of the package. -# use: isor:skip to supress all isort related warnings / errors, +# use: isort:skip to supress all isort related warnings / errors, # as this file should be logically grouped imports @@ -25,26 +26,22 @@ # from RestrictedPython.RCompile import compile_restricted_exec # from RestrictedPython.RCompile import compile_restricted_function +# RestrictedPython internal imports # new API Style # compile_restricted methods: -from RestrictedPython.compile import compile_restricted # isort:skip -from RestrictedPython.compile import compile_restricted_eval # isort:skip -from RestrictedPython.compile import compile_restricted_exec # isort:skip -from RestrictedPython.compile import compile_restricted_function # isort:skip -from RestrictedPython.compile import compile_restricted_single # isort:skip - +from RestrictedPython.compile import CompileResult +from RestrictedPython.compile import compile_restricted +from RestrictedPython.compile import compile_restricted_eval +from RestrictedPython.compile import compile_restricted_exec +from RestrictedPython.compile import compile_restricted_function +from RestrictedPython.compile import compile_restricted_single +# +from RestrictedPython.Eval import RestrictionCapableEval # predefined builtins -from RestrictedPython.Guards import safe_builtins # isort:skip -from RestrictedPython.Limits import limited_builtins # isort:skip -from RestrictedPython.Utilities import utility_builtins # isort:skip - +from RestrictedPython.Guards import safe_builtins +from RestrictedPython.Limits import limited_builtins # Helper Methods -from RestrictedPython.PrintCollector import PrintCollector # isort:skip -from RestrictedPython.compile import CompileResult # isort:skip - +from RestrictedPython.PrintCollector import PrintCollector # Policy -from RestrictedPython.transformer import RestrictingNodeTransformer # isort:skip - -# RestrictedPython internal imports -# -from RestrictedPython.Eval import RestrictionCapableEval +from RestrictedPython.transformer import RestrictingNodeTransformer +from RestrictedPython.Utilities import utility_builtins diff --git a/src/RestrictedPython/transformer.py b/src/RestrictedPython/transformer.py index ee793ff..c1f9d59 100644 --- a/src/RestrictedPython/transformer.py +++ b/src/RestrictedPython/transformer.py @@ -17,13 +17,11 @@ the parsed python code to create a modified AST for a byte code generation. """ -# This package should follow the Plone Sytleguide for Python, +# This package should follow the Plone Styleguide for Python, # which differ from PEP8: # http://docs.plone.org/develop/styleguide/python.html - # Standard library imports -# Standard Library Imports import ast import contextlib import textwrap From 496b90218ba74a4e30cd1b1134bcae5895504814 Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Tue, 9 May 2017 22:25:33 +0200 Subject: [PATCH 08/11] better config --- .coveragerc | 5 ++++- setup.cfg | 2 +- tox.ini | 25 ++++++++++++++++++++----- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/.coveragerc b/.coveragerc index 755d326..ec4a95e 100644 --- a/.coveragerc +++ b/.coveragerc @@ -12,4 +12,7 @@ omit = #src/RestrictedPython/tests/*.py [report] -precision = 3 +precision = 2 + +[html] +directory = reports/coverage diff --git a/setup.cfg b/setup.cfg index 74180ad..7651df1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -67,7 +67,7 @@ not_skip = exclude = bootstrap.py, src/RestrictedPython/tests, - src/RestrictedPython/SelectCompiler.py, + #src/RestrictedPython/SelectCompiler.py, ignore = N801, diff --git a/tox.ini b/tox.ini index 9e4c822..665b08a 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,5 @@ [tox] envlist = - flake8, coverage-clean, py27, py27-datetime, @@ -12,6 +11,7 @@ envlist = pypy, docs, isort, + flake8, coverage-report, skip_missing_interpreters = False @@ -21,8 +21,8 @@ extras = develop test commands = - pytest --cov=src --cov-report=xml --html=report-{envname}.html --self-contained-html {posargs} - pytest --doctest-modules src/RestrictedPython/compile.py {posargs} + pytest --cov=src --cov-report=xml --html=reports/pytest/report-{envname}.html --self-contained-html {posargs} + pytest --doctest-modules src/RestrictedPython/compile.py README.rst {posargs} setenv = COVERAGE_FILE=.coverage.{envname} deps = @@ -84,8 +84,23 @@ commands = [testenv:flake8] basepython = python2.7 -deps = flake8 -commands = flake8 --doctests src tests setup.py +deps = + flake8 + flake8-html + flake8-coding + flake8-debugger + flake8-deprecated + flake8-isort + flake8-pep3101 + flake8-plone-hasattr + flake8-polyfill + flake8-print + flake8-quotes + flake8-string-format + flake8-todo +commands = + - flake8 --format=html --htmldir={toxinidir}/reports/flake8 --doctests src tests setup.py {posargs} + flake8 --doctests src tests setup.py {posargs} [testenv:docs] basepython = python2.7 From eb3069b0a39c0c54dd318c753261901fe13c4671 Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Tue, 9 May 2017 22:26:42 +0200 Subject: [PATCH 09/11] after config ignore reports folder in gitignores --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 09b8d55..e6e499a 100644 --- a/.gitignore +++ b/.gitignore @@ -21,7 +21,7 @@ pyvenv.cfg /eggs /fake-eggs /htmlcov -/report-*.html +/reports /include /lib /share From 142bfddd074faaf18346d2585145e53e5423733f Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Tue, 9 May 2017 22:27:22 +0200 Subject: [PATCH 10/11] better description and one example in README for Github and PyPI --- README.rst | 45 ++++++++++++++++++++++++++++++++- setup.py | 5 ++-- src/RestrictedPython/README.rst | 2 ++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 594534f..95e4728 100644 --- a/README.rst +++ b/README.rst @@ -2,6 +2,49 @@ RestrictedPython ================ -RestrictedPython is a defined subset of the Python language which allows to provide a program input into a trusted environment. +RestrictedPython is a tool that helps to define a subset of the Python language which allows to provide a program input into a trusted environment. +RestrictedPython is not a sandbox system or a secured environment, but it helps to define a trusted environment and execute untrusted code inside of it. For full documentation please see  http://restrictedpython.readthedocs.io/ or the local ``docs/index``. + +Example +======= + +To give a basic understanding what RestrictedPython does here two examples: + +An unproblematic code example +----------------------------- + +Python allows you to execute a large set of commands. +This would not harm any system. + + >>> from RestrictedPython import compile_restricted + >>> from RestrictedPython import safe_builtins + >>> + >>> source_code = """ + ... def example(): + ... return 'Hello World!' + ... """ + >>> + >>> locals = {} + >>> byte_code = compile_restricted(source_code, '', 'exec') + >>> exec(byte_code, safe_builtins, locals) + >>> + >>> locals['example']() + 'Hello World!' + +Problematic code example +------------------------ + +This example directly executed in Python could harm your system. + + >>> from RestrictedPython import compile_restricted + >>> from RestrictedPython import safe_builtins + >>> + >>> source_code = """ + ... import os + ... + ... os.listdir('/') + ... """ + >>> byte_code = compile_restricted(source_code, '', 'exec') + >>> # exec(byte_code, safe_builtins, {}) diff --git a/setup.py b/setup.py index 28a2352..0da4ea6 100644 --- a/setup.py +++ b/setup.py @@ -27,8 +27,9 @@ def read(*rnames): version='4.0a2.dev0', url='http://pypi.python.org/pypi/RestrictedPython', license='ZPL 2.1', - description='RestrictedPython provides a restricted execution ' - 'environment for Python, e.g. for running untrusted code.', + description='RestrictedPython is a defined subset of the Python language' + ' which allows to provide a program input into a trusted' + ' environment.', long_description=(read('README.rst') + '\n' + read('docs', 'CHANGES.rst')), classifiers=[ diff --git a/src/RestrictedPython/README.rst b/src/RestrictedPython/README.rst index ddf7f1b..a068a0d 100644 --- a/src/RestrictedPython/README.rst +++ b/src/RestrictedPython/README.rst @@ -1,5 +1,7 @@ .. contents:: +.. TODO:: move this documentation into docs + Overview ======== From 64d0abdbaa5160476761877d8d17d71fd3fadd12 Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Tue, 9 May 2017 22:27:46 +0200 Subject: [PATCH 11/11] added a test about import similar to new README --- tests/test_imports.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/test_imports.py diff --git a/tests/test_imports.py b/tests/test_imports.py new file mode 100644 index 0000000..9c34a36 --- /dev/null +++ b/tests/test_imports.py @@ -0,0 +1,31 @@ +""" +Tests about imports +""" + +# Test framework imports +import pytest + +# Test internals (fixures and helpers) +from tests import c_exec +from tests import e_exec + + +OS_IMPORT_EXAMPLE = """ +import os + +os.listdir('/') +""" + + +@pytest.mark.parametrize(*c_exec) +@pytest.mark.parametrize(*e_exec) +def test_os_import(c_exec, e_exec): + """Test that import should not work out of the box. + TODO: Why does this work. + """ + result = c_exec(OS_IMPORT_EXAMPLE) + assert result.code is not None + assert result.errors == () + + # with pytest.raises(NameError): + # e_exec(OS_IMPORT_EXAMPLE)