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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
import RestrictedPython
from RestrictedPython.PrintCollector import PrintCollector
compiler = RestrictedPython.compile.compile_restricted_exec
ALLOWED_PRINT_FUNCTION = """
from __future__ import print_function
print ('Hello World!')
"""
ALLOWED_PRINT_FUNCTION_WITH_END = """
from __future__ import print_function
print ('Hello World!', end='')
"""
ALLOWED_PRINT_FUNCTION_MULTI_ARGS = """
from __future__ import print_function
print ('Hello World!', 'Hello Earth!')
"""
ALLOWED_PRINT_FUNCTION_WITH_SEPARATOR = """
from __future__ import print_function
print ('a', 'b', 'c', sep='|', end='!')
"""
PRINT_FUNCTION_WITH_NONE_SEPARATOR = """
from __future__ import print_function
print ('a', 'b', sep=None)
"""
PRINT_FUNCTION_WITH_NONE_END = """
from __future__ import print_function
print ('a', 'b', end=None)
"""
PRINT_FUNCTION_WITH_NONE_FILE = """
from __future__ import print_function
print ('a', 'b', file=None)
"""
def test_print_function__simple_prints():
glb = {'_print_': PrintCollector, '_getattr_': None}
code, errors = compiler(ALLOWED_PRINT_FUNCTION)[:2]
assert errors == ()
assert code is not None
exec(code, glb)
assert glb['_print']() == 'Hello World!\n'
code, errors = compiler(ALLOWED_PRINT_FUNCTION_WITH_END)[:2]
assert code is not None
assert errors == ()
exec(code, glb)
assert glb['_print']() == 'Hello World!'
code, errors = compiler(ALLOWED_PRINT_FUNCTION_MULTI_ARGS)[:2]
assert code is not None
assert errors == ()
exec(code, glb)
assert glb['_print']() == 'Hello World! Hello Earth!\n'
code, errors = compiler(ALLOWED_PRINT_FUNCTION_WITH_SEPARATOR)[:2]
assert code is not None
assert errors == ()
exec(code, glb)
assert glb['_print']() == "a|b|c!"
code, errors = compiler(PRINT_FUNCTION_WITH_NONE_SEPARATOR)[:2]
assert code is not None
assert errors == ()
exec(code, glb)
assert glb['_print']() == "a b\n"
code, errors = compiler(PRINT_FUNCTION_WITH_NONE_END)[:2]
assert code is not None
assert errors == ()
exec(code, glb)
assert glb['_print']() == "a b\n"
code, errors = compiler(PRINT_FUNCTION_WITH_NONE_FILE)[:2]
assert code is not None
assert errors == ()
exec(code, glb)
assert glb['_print']() == "a b\n"
ALLOWED_PRINT_FUNCTION_WITH_STAR_ARGS = """
from __future__ import print_function
to_print = (1, 2, 3)
print(*to_print)
"""
def test_print_function_with_star_args(mocker):
_apply_ = mocker.stub()
_apply_.side_effect = lambda func, *args, **kwargs: func(*args, **kwargs)
glb = {
'_print_': PrintCollector,
'_getattr_': None,
"_apply_": _apply_
}
code, errors = compiler(ALLOWED_PRINT_FUNCTION_WITH_STAR_ARGS)[:2]
assert code is not None
assert errors == ()
exec(code, glb)
assert glb['_print']() == "1 2 3\n"
_apply_.assert_called_once_with(glb['_print']._call_print, 1, 2, 3)
ALLOWED_PRINT_FUNCTION_WITH_KWARGS = """
from __future__ import print_function
to_print = (1, 2, 3)
kwargs = {'sep': '-', 'end': '!', 'file': None}
print(*to_print, **kwargs)
"""
def test_print_function_with_kw_args(mocker):
_apply_ = mocker.stub()
_apply_.side_effect = lambda func, *args, **kwargs: func(*args, **kwargs)
glb = {
'_print_': PrintCollector,
'_getattr_': None,
"_apply_": _apply_
}
code, errors = compiler(ALLOWED_PRINT_FUNCTION_WITH_KWARGS)[:2]
assert code is not None
assert errors == ()
exec(code, glb)
assert glb['_print']() == "1-2-3!"
_apply_.assert_called_once_with(
glb['_print']._call_print,
1,
2,
3,
end='!',
file=None,
sep='-')
PROTECT_WRITE_ON_FILE = """
from __future__ import print_function
print ('a', 'b', file=stream)
"""
def test_print_function__protect_file(mocker):
_getattr_ = mocker.stub()
_getattr_.side_effect = getattr
stream = mocker.stub()
stream.write = mocker.stub()
glb = {
'_print_': PrintCollector,
'_getattr_': _getattr_,
'stream': stream
}
code, errors = compiler(PROTECT_WRITE_ON_FILE)[:2]
assert code is not None
assert errors == ()
exec(code, glb)
_getattr_.assert_called_once_with(stream, 'write')
stream.write.assert_has_calls([
mocker.call('a'),
mocker.call(' '),
mocker.call('b'),
mocker.call('\n')
])
# 'printed' is scope aware.
# => on a new function scope a new printed is generated.
INJECT_PRINT_COLLECTOR_NESTED = """
from __future__ import print_function
def f2():
return 'f2'
def f1():
print ('f1')
def inner():
print ('inner')
return printed
return inner() + printed + f2()
def main():
print ('main')
return f1() + printed
"""
def test_print_function__nested_print_collector():
code, errors = compiler(INJECT_PRINT_COLLECTOR_NESTED)[:2]
glb = {"_print_": PrintCollector, '_getattr_': None}
exec(code, glb)
ret = glb['main']()
assert ret == 'inner\nf1\nf2main\n'
WARN_PRINTED_NO_PRINT = """
def foo():
return printed
"""
def test_print_function__with_printed_no_print():
code, errors, warnings = compiler(WARN_PRINTED_NO_PRINT)[:3]
assert code is not None
assert errors == ()
assert warnings == ["Line 2: Doesn't print, but reads 'printed' variable."]
WARN_PRINTED_NO_PRINT_NESTED = """
from __future__ import print_function
print ('a')
def foo():
return printed
printed
"""
def test_print_function__with_printed_no_print_nested():
code, errors, warnings = compiler(WARN_PRINTED_NO_PRINT_NESTED)[:3]
assert code is not None
assert errors == ()
assert warnings == ["Line 4: Doesn't print, but reads 'printed' variable."]
WARN_PRINT_NO_PRINTED = """
from __future__ import print_function
def foo():
print (1)
"""
def test_print_function__with_print_no_printed():
code, errors, warnings = compiler(WARN_PRINT_NO_PRINTED)[:3]
assert code is not None
assert errors == ()
assert warnings == ["Line 3: Prints, but never reads 'printed' variable."]
WARN_PRINT_NO_PRINTED_NESTED = """
from __future__ import print_function
print ('a')
def foo():
print ('x')
printed
"""
def test_print_function__with_print_no_printed_nested():
code, errors, warnings = compiler(WARN_PRINT_NO_PRINTED_NESTED)[:3]
assert code is not None
assert errors == ()
assert warnings == ["Line 4: Prints, but never reads 'printed' variable."]
# python generates a new frame/scope for:
# modules, functions, class, lambda, all the comprehensions
# For class, lambda and comprehensions *no* new print collector scope should be
# generated.
NO_PRINT_SCOPES = """
from __future__ import print_function
def class_scope():
class A:
print ('a')
return printed
def lambda_scope():
func = lambda x: print(x)
func(1)
func(2)
return printed
def comprehension_scope():
[print(1) for _ in range(2)]
return printed
"""
def test_print_function_no_new_scope():
code, errors = compiler(NO_PRINT_SCOPES)[:2]
glb = {
'_print_': PrintCollector,
'__metaclass__': type,
'_getattr_': None,
'_getiter_': lambda ob: ob
}
exec(code, glb)
ret = glb['class_scope']()
assert ret == 'a\n'
ret = glb['lambda_scope']()
assert ret == '1\n2\n'
ret = glb['comprehension_scope']()
assert ret == '1\n1\n'
PASS_PRINT_FUNCTION = """
from __future__ import print_function
def main():
def do_stuff(func):
func(1)
func(2)
do_stuff(print)
return printed
"""
def test_print_function_pass_print_function():
code, errors = compiler(PASS_PRINT_FUNCTION)[:2]
glb = {'_print_': PrintCollector, '_getattr_': None}
exec(code, glb)
ret = glb['main']()
assert ret == '1\n2\n'
CONDITIONAL_PRINT = """
from __future__ import print_function
def func(cond):
if cond:
print(1)
return printed
"""
def test_print_function_conditional_print():
code, errors = compiler(CONDITIONAL_PRINT)[:2]
glb = {'_print_': PrintCollector, '_getattr_': None}
exec(code, glb)
assert glb['func'](True) == '1\n'
assert glb['func'](False) == ''
|