File: test_name.py

package info (click to toggle)
restrictedpython 8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,072 kB
  • sloc: python: 4,043; makefile: 193
file content (163 lines) | stat: -rw-r--r-- 4,600 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from RestrictedPython import compile_restricted_exec
from tests.helper import restricted_exec


BAD_NAME_STARTING_WITH_UNDERSCORE = """\
def bad_name():
    __ = 12
"""


def test_RestrictingNodeTransformer__visit_Name__1():
    """It denies a variable name starting in `__`."""
    result = compile_restricted_exec(BAD_NAME_STARTING_WITH_UNDERSCORE)
    assert result.errors == (
        'Line 2: "__" is an invalid variable name because it starts with "_"',)


BAD_NAME_OVERRIDE_GUARD_WITH_NAME = """\
def overrideGuardWithName():
    _getattr = None
"""


def test_RestrictingNodeTransformer__visit_Name__2():
    """It denies a variable name starting in `_`."""
    result = compile_restricted_exec(BAD_NAME_OVERRIDE_GUARD_WITH_NAME)
    assert result.errors == (
        'Line 2: "_getattr" is an invalid variable name because '
        'it starts with "_"',)


def test_RestrictingNodeTransformer__visit_Name__2_5():
    """It allows `_` as variable name."""
    glb = restricted_exec('_ = 2411')
    assert glb['_'] == 2411


BAD_NAME_OVERRIDE_OVERRIDE_GUARD_WITH_FUNCTION = """\
def overrideGuardWithFunction():
    def _getattr(o):
        return o
"""


def test_RestrictingNodeTransformer__visit_Name__3():
    """It denies a function name starting in `_`."""
    result = compile_restricted_exec(
        BAD_NAME_OVERRIDE_OVERRIDE_GUARD_WITH_FUNCTION)
    assert result.errors == (
        'Line 2: "_getattr" is an invalid variable name because it '
        'starts with "_"',)


BAD_NAME_OVERRIDE_GUARD_WITH_CLASS = """\
def overrideGuardWithClass():
    class _getattr:
        pass
"""


def test_RestrictingNodeTransformer__visit_Name__4():
    """It denies a class name starting in `_`."""
    result = compile_restricted_exec(BAD_NAME_OVERRIDE_GUARD_WITH_CLASS)
    assert result.errors == (
        'Line 2: "_getattr" is an invalid variable name because it '
        'starts with "_"',)


BAD_NAME_IN_WITH = """\
def with_as_bad_name():
    with x as _leading_underscore:
        pass
"""


def test_RestrictingNodeTransformer__visit_Name__4_4():
    """It denies a variable name in with starting in `_`."""
    result = compile_restricted_exec(BAD_NAME_IN_WITH)
    assert result.errors == (
        'Line 2: "_leading_underscore" is an invalid variable name because '
        'it starts with "_"',)


BAD_NAME_IN_COMPOUND_WITH = """\
def compound_with_bad_name():
    with a as b, c as _restricted_name:
        pass
"""


def test_RestrictingNodeTransformer__visit_Name__4_5():
    """It denies a variable name in with starting in `_`."""
    result = compile_restricted_exec(BAD_NAME_IN_COMPOUND_WITH)
    assert result.errors == (
        'Line 2: "_restricted_name" is an invalid variable name because '
        'it starts with "_"',)


BAD_NAME_DICT_COMP = """\
def dict_comp_bad_name():
    {y: y for _restricted_name in x}
"""


def test_RestrictingNodeTransformer__visit_Name__4_6():
    """It denies a variable name starting in `_` in a dict comprehension."""
    result = compile_restricted_exec(BAD_NAME_DICT_COMP)
    assert result.errors == (
        'Line 2: "_restricted_name" is an invalid variable name because '
        'it starts with "_"',)


BAD_NAME_SET_COMP = """\
def set_comp_bad_name():
    {y for _restricted_name in x}
"""


def test_RestrictingNodeTransformer__visit_Name__4_7():
    """It denies a variable name starting in `_` in a dict comprehension."""
    result = compile_restricted_exec(BAD_NAME_SET_COMP)
    assert result.errors == (
        'Line 2: "_restricted_name" is an invalid variable name because '
        'it starts with "_"',)


BAD_NAME_ENDING_WITH___ROLES__ = """\
def bad_name():
    myvar__roles__ = 12
"""


def test_RestrictingNodeTransformer__visit_Name__5():
    """It denies a variable name ending in `__roles__`."""
    result = compile_restricted_exec(BAD_NAME_ENDING_WITH___ROLES__)
    assert result.errors == (
        'Line 2: "myvar__roles__" is an invalid variable name because it '
        'ends with "__roles__".',)


BAD_NAME_PRINTED = """\
def bad_name():
    printed = 12
"""


def test_RestrictingNodeTransformer__visit_Name__6():
    """It denies a variable named `printed`."""
    result = compile_restricted_exec(BAD_NAME_PRINTED)
    assert result.errors == ('Line 2: "printed" is a reserved name.',)


BAD_NAME_PRINT = """\
def bad_name():
    def print():
        pass
"""


def test_RestrictingNodeTransformer__visit_Name__7():
    """It denies a variable named `print`."""
    result = compile_restricted_exec(BAD_NAME_PRINT)
    assert result.errors == ('Line 2: "print" is a reserved name.',)