From 43b36281ab1b4a950ebd4ca2788e8a355dc26bd9 Mon Sep 17 00:00:00 2001 From: Alexander Loechel Date: Wed, 16 May 2018 18:00:47 +0200 Subject: [PATCH] deny _ method names on modul level --- src/RestrictedPython/transformer.py | 3 ++- tests/transformer/test_functiondef.py | 19 +++++-------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/RestrictedPython/transformer.py b/src/RestrictedPython/transformer.py index c896a96..33880e2 100644 --- a/src/RestrictedPython/transformer.py +++ b/src/RestrictedPython/transformer.py @@ -396,7 +396,8 @@ def check_name(self, node, name, allow_magic_methods=False): if (name.startswith('_') and name != '_' and not (allow_magic_methods - and name in ALLOWED_FUNC_NAMES)): + and name in ALLOWED_FUNC_NAMES + and node.col_offset != 0)): self.error( node, '"{name}" is an invalid variable name because it ' diff --git a/tests/transformer/test_functiondef.py b/tests/transformer/test_functiondef.py index e99aec6..e675027 100644 --- a/tests/transformer/test_functiondef.py +++ b/tests/transformer/test_functiondef.py @@ -138,19 +138,6 @@ def test_RestrictingNodeTransformer__visit_FunctionDef__8( _getiter_.reset_mock() -FUNC_NAMES_TEST = """ -def __init__(test): - test -""" - - -@pytest.mark.parametrize(*c_exec) -def test_RestrictingNodeTransformer__module_func_def_name(c_exec): - """It allows to define functions with the name of allowed magic methods.""" - result = c_exec(FUNC_NAMES_TEST) - assert result.errors == () - - BLACKLISTED_FUNC_NAMES_CALL_TEST = """ def __init__(test): test @@ -161,9 +148,13 @@ def __init__(test): @pytest.mark.parametrize(*c_exec) def test_RestrictingNodeTransformer__module_func_def_name_call(c_exec): - """It forbids to use names of allowed magic methods.""" + """It forbids definition and usage of magic methods as functions ... + + ... at module level. + """ result = c_exec(BLACKLISTED_FUNC_NAMES_CALL_TEST) # assert result.errors == ('Line 1: ') assert result.errors == ( + 'Line 2: "__init__" is an invalid variable name because it starts with "_"', # NOQA: E501 'Line 5: "__init__" is an invalid variable name because it starts with "_"', # NOQA: E501 )