Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
loechel committed May 18, 2018
1 parent 21d16a8 commit d9635a0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
15 changes: 7 additions & 8 deletions src/RestrictedPython/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,7 @@ def check_import_names(self, node):
This is a protection against rebinding dunder names like
_getitem_, _write_ via imports.
=> 'from _a import x' is ok, because '_a' is not added to the scope.
"""
if (isinstance(node, ast.ImportFrom)
and not node.module == '__future__'
and any(name.startswith('_') for name in node.module.split('.'))): # NOQA: E501
self.error(node, 'module name starts "_", which is forbidden.')

for name in node.names:
if '*' in name.name:
self.error(node, '"*" imports are not allowed.')
Expand Down Expand Up @@ -1161,7 +1154,13 @@ def visit_Import(self, node):

def visit_ImportFrom(self, node):
"""Allow `import from` statements with restrictions.
See check_import_names."""
See check_import_names.
=> 'from _a import x' is ok, because '_a' is not added to the scope.
"""
if not node.module == '__future__' and any(name.startswith('_') for name in node.module.split('.')): # NOQA: E501
self.error(node, 'module name starts "_", which is forbidden.')

return self.check_import_names(node)

def visit_alias(self, node):
Expand Down
21 changes: 6 additions & 15 deletions tests/transformer/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,16 @@ def test_RestrictingNodeTransformer__visit_Import__5(c_exec):

@pytest.mark.parametrize(*c_exec)
def test_RestrictingNodeTransformer__visit_Import__6(c_exec):
"""Deny imports from modules staring with `_`."""
"""Deny imports from modules starting with `_`."""
message = 'Line 1: module name starts "_", which is forbidden.'
result = c_exec('from _a import m')
assert result.errors == (
'Line 1: module name starts "_", which is forbidden.',
)
assert result.errors == (message,)
result = c_exec('from a._b import m')
assert result.errors == (
'Line 1: module name starts "_", which is forbidden.',
)
assert result.errors == (message,)
result = c_exec('from _a.b import m')
assert result.errors == (
'Line 1: module name starts "_", which is forbidden.',
)
assert result.errors == (message,)
result = c_exec('from _a._b import m as x')
assert result.errors == (
'Line 1: module name starts "_", which is forbidden.',
)
result = c_exec('from a import m as _n')
assert result.errors == (import_errmsg % '_n',)
assert result.errors == (message,)


@pytest.mark.parametrize(*c_exec)
Expand Down

0 comments on commit d9635a0

Please sign in to comment.