/
test_transformer.py
1191 lines (900 loc) · 30.1 KB
/
test_transformer.py
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from RestrictedPython.Guards import guarded_iter_unpack_sequence
from RestrictedPython.Guards import guarded_unpack_sequence
import contextlib
import pytest
import RestrictedPython
import six
import sys
import types
# Define the arguments for @pytest.mark.parametrize to be able to test both the
# old and the new implementation to be equal:
compile = ('compile', [RestrictedPython.compile.compile_restricted_exec])
if sys.version_info < (3,):
from RestrictedPython import RCompile
compile[1].append(RCompile.compile_restricted_exec)
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__generic_visit__1(compile):
"""It compiles a number successfully."""
code, errors, warnings, used_names = compile('42')
assert 'code' == str(code.__class__.__name__)
assert errors == ()
assert warnings == []
assert used_names == {}
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__generic_visit__2(compile):
"""It compiles a function call successfully and returns the used name."""
code, errors, warnings, used_names = compile('max([1, 2, 3])')
assert errors == ()
assert warnings == []
assert 'code' == str(code.__class__.__name__)
if compile is RestrictedPython.compile.compile_restricted_exec:
# The new version not yet supports `used_names`:
assert used_names == {}
else:
assert used_names == {'max': True}
YIELD = """\
def no_yield():
yield 42
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__generic_visit__100(compile):
"""It is an error if the code contains a `yield` statement."""
code, errors, warnings, used_names = compile(YIELD)
assert ("Line 2: Yield statements are not allowed.",) == errors
assert warnings == []
assert used_names == {}
EXEC_STATEMENT = """\
def no_exec():
exec 'q = 1'
"""
@pytest.mark.skipif(sys.version_info >= (3, 0),
reason="exec statement no longer exists in Python 3")
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__generic_visit__102(compile):
"""It raises a SyntaxError if the code contains an `exec` statement."""
code, errors, warnings, used_names = compile(EXEC_STATEMENT)
assert ('Line 2: Exec statements are not allowed.',) == errors
@pytest.mark.skipif(
sys.version_info < (3, 0),
reason="exec statement in Python 3 raises SyntaxError itself")
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__generic_visit__103(compile):
"""It is an error if the code contains an `exec` statement."""
code, errors, warnings, used_names = compile(EXEC_STATEMENT)
assert (
"Line 2: SyntaxError: Missing parentheses in call to 'exec' in on "
"statement: exec 'q = 1'",) == errors
BAD_NAME = """\
def bad_name():
__ = 12
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Name__1(compile):
"""It is an error if a bad variable name is used."""
code, errors, warnings, used_names = compile(BAD_NAME)
assert ('Line 2: "__" is an invalid variable name because it starts with '
'"_"',) == errors
BAD_ATTR_UNDERSCORE = """\
def bad_attr():
some_ob = object()
some_ob._some_attr = 15
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Attribute__1(compile):
"""It is an error if a bad attribute name is used."""
code, errors, warnings, used_names = compile(BAD_ATTR_UNDERSCORE)
assert ('Line 3: "_some_attr" is an invalid attribute name because it '
'starts with "_".',) == errors
BAD_ATTR_ROLES = """\
def bad_attr():
some_ob = object()
some_ob.abc__roles__
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Attribute__2(compile):
"""It is an error if a bad attribute name is used."""
code, errors, warnings, used_names = compile(BAD_ATTR_ROLES)
assert ('Line 3: "abc__roles__" is an invalid attribute name because it '
'ends with "__roles__".',) == errors
TRANSFORM_ATTRIBUTE_ACCESS = """\
def func():
return a.b
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Attribute__3(compile, mocker):
code, errors, warnings, used_names = compile(TRANSFORM_ATTRIBUTE_ACCESS)
glb = {
'_getattr_': mocker.stub(),
'a': [],
'b': 'b'
}
six.exec_(code, glb)
glb['func']()
glb['_getattr_'].assert_called_once_with([], 'b')
ALLOW_UNDERSCORE_ONLY = """\
def func():
some_ob = object()
some_ob._
_ = some_ob
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Attribute__4(compile, mocker):
code, errors, warnings, used_names = compile(ALLOW_UNDERSCORE_ONLY)
assert errors == ()
assert warnings == []
assert code is not None
TRANSFORM_ATTRIBUTE_WRITE = """\
def func():
a.b = 'it works'
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Attribute__5(compile, mocker):
code, errors, warnings, used_names = compile(TRANSFORM_ATTRIBUTE_WRITE)
glb = {
'_write_': mocker.stub(),
'a': mocker.stub(),
}
glb['_write_'].return_value = glb['a']
six.exec_(code, glb)
glb['func']()
glb['_write_'].assert_called_once_with(glb['a'])
assert glb['a'].b == 'it works'
EXEC_FUNCTION = """\
def no_exec():
exec('q = 1')
"""
DISALLOW_TRACEBACK_ACCESS = """
try:
raise Exception()
except Exception as e:
tb = e.__traceback__
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Attribute__6(compile):
code, errors = compile(DISALLOW_TRACEBACK_ACCESS)[:2]
assert code is None
assert errors[0] == 'Line 5: "__traceback__" is an invalid attribute ' \
'name because it starts with "_".'
@pytest.mark.skipif(sys.version_info < (3, 0),
reason="exec is a statement in Python 2")
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Call__1(compile):
"""It is an error if the code call the `exec` function."""
code, errors, warnings, used_names = compile(EXEC_FUNCTION)
assert ("Line 2: Exec calls are not allowed.",) == errors
EVAL_FUNCTION = """\
def no_eval():
eval('q = 1')
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Call__2(compile):
"""It is an error if the code call the `eval` function."""
code, errors, warnings, used_names = compile(EVAL_FUNCTION)
if compile is RestrictedPython.compile.compile_restricted_exec:
assert ("Line 2: Eval calls are not allowed.",) == errors
else:
# `eval()` is allowed in the old implementation.
assert () == errors
ITERATORS = """
def for_loop(it):
c = 0
for a in it:
c = c + a
return c
def dict_comp(it):
return {a: a + a for a in it}
def list_comp(it):
return [a + a for a in it]
def set_comp(it):
return {a + a for a in it}
def generator(it):
return (a + a for a in it)
def nested_generator(it1, it2):
return (a+b for a in it1 if a > 0 for b in it2)
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__guard_iter(compile, mocker):
code, errors, warnings, used_names = compile(ITERATORS)
it = (1, 2, 3)
_getiter_ = mocker.stub()
_getiter_.side_effect = lambda x: x
glb = {'_getiter_': _getiter_}
six.exec_(code, glb)
ret = glb['for_loop'](it)
assert 6 == ret
_getiter_.assert_called_once_with(it)
_getiter_.reset_mock()
ret = glb['dict_comp'](it)
assert {1: 2, 2: 4, 3: 6} == ret
_getiter_.assert_called_once_with(it)
_getiter_.reset_mock()
ret = glb['list_comp'](it)
assert [2, 4, 6] == ret
_getiter_.assert_called_once_with(it)
_getiter_.reset_mock()
ret = glb['set_comp'](it)
assert {2, 4, 6} == ret
_getiter_.assert_called_once_with(it)
_getiter_.reset_mock()
ret = glb['generator'](it)
assert isinstance(ret, types.GeneratorType)
assert list(ret) == [2, 4, 6]
_getiter_.assert_called_once_with(it)
_getiter_.reset_mock()
ret = glb['nested_generator']((0, 1, 2), (1, 2))
assert isinstance(ret, types.GeneratorType)
assert list(ret) == [2, 3, 3, 4]
_getiter_.assert_has_calls([
mocker.call((0, 1, 2)),
mocker.call((1, 2)),
mocker.call((1, 2))])
_getiter_.reset_mock()
ITERATORS_WITH_UNPACK_SEQUENCE = """
def for_loop(it):
c = 0
for (a, b) in it:
c = c + a + b
return c
def dict_comp(it):
return {a: a + b for (a, b) in it}
def list_comp(it):
return [a + b for (a, b) in it]
def set_comp(it):
return {a + b for (a, b) in it}
def generator(it):
return (a + b for (a, b) in it)
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__guard_iter2(compile, mocker):
code, errors = compile(ITERATORS_WITH_UNPACK_SEQUENCE)[:2]
it = ((1, 2), (3, 4), (5, 6))
call_ref = [
mocker.call(it),
mocker.call(it[0]),
mocker.call(it[1]),
mocker.call(it[2])
]
_getiter_ = mocker.stub()
_getiter_.side_effect = lambda x: x
glb = {
'_getiter_': _getiter_,
'_iter_unpack_sequence_': guarded_iter_unpack_sequence
}
six.exec_(code, glb)
ret = glb['for_loop'](it)
assert ret == 21
_getiter_.assert_has_calls(call_ref)
_getiter_.reset_mock()
ret = glb['dict_comp'](it)
assert ret == {1: 3, 3: 7, 5: 11}
_getiter_.assert_has_calls(call_ref)
_getiter_.reset_mock()
ret = glb['list_comp'](it)
assert ret == [3, 7, 11]
_getiter_.assert_has_calls(call_ref)
_getiter_.reset_mock()
ret = glb['set_comp'](it)
assert ret == {3, 7, 11}
_getiter_.assert_has_calls(call_ref)
_getiter_.reset_mock()
# The old code did not run with unpack sequence inside generators
if compile == RestrictedPython.compile.compile_restricted_exec:
ret = list(glb['generator'](it))
assert ret == [3, 7, 11]
_getiter_.assert_has_calls(call_ref)
_getiter_.reset_mock()
GET_SUBSCRIPTS = """
def simple_subscript(a):
return a['b']
def slice_subscript_no_upper_bound(a):
return a[1:]
def slice_subscript_no_lower_bound(a):
return a[:1]
def slice_subscript_no_step(a):
return a[1:2]
def slice_subscript_with_step(a):
return a[1:2:3]
def extended_slice_subscript(a):
return a[0, :1, 1:, 1:2, 1:2:3]
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Subscript_1(compile, mocker):
code, errors, warnings, used_names = compile(GET_SUBSCRIPTS)
value = None
_getitem_ = mocker.stub()
_getitem_.side_effect = lambda ob, index: (ob, index)
glb = {'_getitem_': _getitem_}
six.exec_(code, glb)
ret = glb['simple_subscript'](value)
ref = (value, 'b')
assert ref == ret
_getitem_.assert_called_once_with(*ref)
_getitem_.reset_mock()
ret = glb['slice_subscript_no_upper_bound'](value)
ref = (value, slice(1, None, None))
assert ref == ret
_getitem_.assert_called_once_with(*ref)
_getitem_.reset_mock()
ret = glb['slice_subscript_no_lower_bound'](value)
ref = (value, slice(None, 1, None))
assert ref == ret
_getitem_.assert_called_once_with(*ref)
_getitem_.reset_mock()
ret = glb['slice_subscript_no_step'](value)
ref = (value, slice(1, 2, None))
assert ref == ret
_getitem_.assert_called_once_with(*ref)
_getitem_.reset_mock()
ret = glb['slice_subscript_with_step'](value)
ref = (value, slice(1, 2, 3))
assert ref == ret
_getitem_.assert_called_once_with(*ref)
_getitem_.reset_mock()
ret = glb['extended_slice_subscript'](value)
ref = (
value,
(
0,
slice(None, 1, None),
slice(1, None, None),
slice(1, 2, None),
slice(1, 2, 3)
)
)
assert ref == ret
_getitem_.assert_called_once_with(*ref)
_getitem_.reset_mock()
WRITE_SUBSCRIPTS = """
def assign_subscript(a):
a['b'] = 1
def del_subscript(a):
del a['b']
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Subscript_2(compile, mocker):
code, errors, warnings, used_names = compile(WRITE_SUBSCRIPTS)
value = {'b': None}
_write_ = mocker.stub()
_write_.side_effect = lambda ob: ob
glb = {'_write_': _write_}
six.exec_(code, glb)
glb['assign_subscript'](value)
assert value['b'] == 1
_write_.assert_called_once_with(value)
_write_.reset_mock()
glb['del_subscript'](value)
assert value == {}
_write_.assert_called_once_with(value)
_write_.reset_mock()
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_AugAssign(compile, mocker):
_inplacevar_ = mocker.stub()
_inplacevar_.side_effect = lambda op, val, expr: val + expr
glb = {'a': 1, '_inplacevar_': _inplacevar_}
code, errors = compile("a += 1")[:2]
six.exec_(code, glb)
assert code is not None
assert errors == ()
assert glb['a'] == 2
_inplacevar_.assert_called_once_with('+=', 1, 1)
_inplacevar_.reset_mock()
code, errors = compile("a.a += 1")[:2]
assert code is None
assert ('Line 1: Augmented assignment of attributes '
'is not allowed.',) == errors
code, errors = compile("a[a] += 1")[:2]
assert code is None
assert ('Line 1: Augmented assignment of object items and '
'slices is not allowed.',) == errors
FUNCTIONC_CALLS = """
def no_star_args_no_kwargs():
return foo(1, 2)
def star_args_no_kwargs():
star = (10, 20, 30)
return foo(1, 2, *star)
def star_args_kwargs():
star = (10, 20, 30)
kwargs = {'x': 100, 'z': 200}
return foo(1, 2, *star, r=9, **kwargs)
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Call(compile, mocker):
code, errors = compile(FUNCTIONC_CALLS)[:2]
_apply_ = mocker.stub()
_apply_.side_effect = lambda func, *args, **kwargs: func(*args, **kwargs)
glb = {
'_apply_': _apply_,
'foo': lambda *args, **kwargs: (args, kwargs)
}
six.exec_(code, glb)
ret = (glb['no_star_args_no_kwargs']())
assert ((1, 2), {}) == ret
assert _apply_.called is False
_apply_.reset_mock()
ret = (glb['star_args_no_kwargs']())
ref = ((1, 2, 10, 20, 30), {})
assert ref == ret
_apply_.assert_called_once_with(glb['foo'], *ref[0])
_apply_.reset_mock()
ret = (glb['star_args_kwargs']())
ref = ((1, 2, 10, 20, 30), {'r': 9, 'z': 200, 'x': 100})
assert ref == ret
_apply_.assert_called_once_with(glb['foo'], *ref[0], **ref[1])
_apply_.reset_mock()
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_FunctionDef_1(compile):
err_msg = 'Line 1: "_bad" is an invalid variable ' \
'name because it starts with "_"'
code, errors = compile("def foo(_bad): pass")[:2]
assert code is None
assert errors[0] == err_msg
code, errors = compile("def foo(_bad=1): pass")[:2]
assert code is None
assert errors[0] == err_msg
code, errors = compile("def foo(*_bad): pass")[:2]
assert code is None
assert errors[0] == err_msg
code, errors = compile("def foo(**_bad): pass")[:2]
assert code is None
assert errors[0] == err_msg
if sys.version_info.major == 2:
code, errors = compile("def foo((a, _bad)): pass")[:2]
assert code is None
assert errors[0] == err_msg
# The old one did not support nested checks.
if compile is RestrictedPython.compile.compile_restricted_exec:
code, errors = compile("def foo(a, (c, (_bad, c))): pass")[:2]
assert code is None
assert errors[0] == err_msg
if sys.version_info.major == 3:
code, errors = compile("def foo(good, *, _bad): pass")[:2]
assert code is None
assert errors[0] == err_msg
NESTED_SEQ_UNPACK = """
def nested((a, b, (c, (d, e)))):
return a, b, c, d, e
def nested_with_order((a, b), (c, d)):
return a, b, c, d
"""
@pytest.mark.skipif(
sys.version_info.major == 3,
reason="tuple parameter unpacking is gone in python 3")
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_FunctionDef_2(compile, mocker):
code, errors = compile('def simple((a, b)): return a, b')[:2]
_getiter_ = mocker.stub()
_getiter_.side_effect = lambda it: it
glb = {
'_getiter_': _getiter_,
'_unpack_sequence_': guarded_unpack_sequence
}
six.exec_(code, glb)
val = (1, 2)
ret = glb['simple'](val)
assert ret == val
_getiter_.assert_called_once_with(val)
_getiter_.reset_mock()
# The old RCompile did not support nested.
if compile is RestrictedPython.RCompile.compile_restricted_exec:
return
code, errors = compile(NESTED_SEQ_UNPACK)[:2]
six.exec_(code, glb)
val = (1, 2, (3, (4, 5)))
ret = glb['nested'](val)
assert ret == (1, 2, 3, 4, 5)
assert 3 == _getiter_.call_count
_getiter_.assert_any_call(val)
_getiter_.assert_any_call(val[2])
_getiter_.assert_any_call(val[2][1])
_getiter_.reset_mock()
ret = glb['nested_with_order']((1, 2), (3, 4))
assert ret == (1, 2, 3, 4)
_getiter_.assert_has_calls([
mocker.call((1, 2)),
mocker.call((3, 4))])
_getiter_.reset_mock()
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Lambda_1(compile):
err_msg = 'Line 1: "_bad" is an invalid variable ' \
'name because it starts with "_"'
code, errors = compile("lambda _bad: None")[:2]
assert code is None
assert errors[0] == err_msg
code, errors = compile("lambda _bad=1: None")[:2]
assert code is None
assert errors[0] == err_msg
code, errors = compile("lambda *_bad: None")[:2]
assert code is None
assert errors[0] == err_msg
code, errors = compile("lambda **_bad: None")[:2]
assert code is None
assert errors[0] == err_msg
if sys.version_info.major == 2:
# The old one did not support tuples at all.
if compile is RestrictedPython.compile.compile_restricted_exec:
code, errors = compile("lambda (a, _bad): None")[:2]
assert code is None
assert errors[0] == err_msg
code, errors = compile("lambda (a, (c, (_bad, c))): None")[:2]
assert code is None
assert errors[0] == err_msg
if sys.version_info.major == 3:
code, errors = compile("lambda good, *, _bad: None")[:2]
assert code is None
assert errors[0] == err_msg
@pytest.mark.skipif(
sys.version_info.major == 3,
reason="tuple parameter unpacking is gone in python 3")
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Lambda_2(compile, mocker):
if compile is not RestrictedPython.compile.compile_restricted_exec:
return
_getiter_ = mocker.stub()
_getiter_.side_effect = lambda it: it
glb = {
'_getiter_': _getiter_,
'_unpack_sequence_': guarded_unpack_sequence,
'_getattr_': lambda ob, val: getattr(ob, val)
}
src = "m = lambda (a, (b, c)), *ag, **kw: a+b+c+sum(ag)+sum(kw.values())"
code, errors = compile(src)[:2]
six.exec_(code, glb)
ret = glb['m']((1, (2, 3)), 4, 5, 6, g=7, e=8)
assert ret == 36
assert 2 == _getiter_.call_count
_getiter_.assert_any_call((1, (2, 3)))
_getiter_.assert_any_call((2, 3))
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Assign(compile, mocker):
src = "orig = (a, (x, z)) = (c, d) = g"
code, errors = compile(src)[:2]
_getiter_ = mocker.stub()
_getiter_.side_effect = lambda it: it
glb = {
'_getiter_': _getiter_,
'_unpack_sequence_': guarded_unpack_sequence,
'g': (1, (2, 3)),
}
six.exec_(code, glb)
assert glb['a'] == 1
assert glb['x'] == 2
assert glb['z'] == 3
assert glb['c'] == 1
assert glb['d'] == (2, 3)
assert glb['orig'] == (1, (2, 3))
assert _getiter_.call_count == 3
_getiter_.assert_any_call((1, (2, 3)))
_getiter_.assert_any_call((2, 3))
_getiter_.reset_mock()
@pytest.mark.skipif(
sys.version_info.major == 2,
reason="starred assignments are python3 only")
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Assign2(compile, mocker):
src = "a, *d, (c, *e), x = (1, 2, 3, (4, 3, 4), 5)"
code, errors = compile(src)[:2]
_getiter_ = mocker.stub()
_getiter_.side_effect = lambda it: it
glb = {
'_getiter_': _getiter_,
'_unpack_sequence_': guarded_unpack_sequence
}
six.exec_(code, glb)
assert glb['a'] == 1
assert glb['d'] == [2, 3]
assert glb['c'] == 4
assert glb['e'] == [3, 4]
assert glb['x'] == 5
_getiter_.assert_has_calls([
mocker.call((1, 2, 3, (4, 3, 4), 5)),
mocker.call((4, 3, 4))])
TRY_EXCEPT_FINALLY = """
def try_except(m):
try:
m('try')
raise IndentationError('f1')
except IndentationError as error:
m('except')
def try_except_else(m):
try:
m('try')
except:
m('except')
else:
m('else')
def try_finally(m):
try:
m('try')
1 / 0
finally:
m('finally')
return
def try_except_finally(m):
try:
m('try')
1 / 0
except:
m('except')
finally:
m('finally')
def try_except_else_finally(m):
try:
m('try')
except:
m('except')
else:
m('else')
finally:
m('finally')
"""
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__error_handling(compile, mocker):
code, errors = compile(TRY_EXCEPT_FINALLY)[:2]
assert code is not None
glb = {}
six.exec_(code, glb)
trace = mocker.stub()
glb['try_except'](trace)
trace.assert_has_calls([
mocker.call('try'),
mocker.call('except')
])
trace.reset_mock()
glb['try_except_else'](trace)
trace.assert_has_calls([
mocker.call('try'),
mocker.call('else')
])
trace.reset_mock()
glb['try_finally'](trace)
trace.assert_has_calls([
mocker.call('try'),
mocker.call('finally')
])
trace.reset_mock()
glb['try_except_finally'](trace)
trace.assert_has_calls([
mocker.call('try'),
mocker.call('except'),
mocker.call('finally')
])
trace.reset_mock()
glb['try_except_else_finally'](trace)
trace.assert_has_calls([
mocker.call('try'),
mocker.call('else'),
mocker.call('finally')
])
trace.reset_mock()
EXCEPT_WITH_TUPLE_UNPACK = """
def tuple_unpack(err):
try:
raise err
except Exception as (a, (b, c)):
return a + b + c
"""
@pytest.mark.skipif(
sys.version_info.major == 3,
reason="tuple unpacking on exceptions is gone in python3")
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_ExceptHandler(compile, mocker):
code, errors = compile(EXCEPT_WITH_TUPLE_UNPACK)[:2]
assert code is not None
_getiter_ = mocker.stub()
_getiter_.side_effect = lambda it: it
glb = {
'_getiter_': _getiter_,
'_unpack_sequence_': guarded_unpack_sequence
}
six.exec_(code, glb)
err = Exception(1, (2, 3))
ret = glb['tuple_unpack'](err)
assert ret == 6
_getiter_.assert_has_calls([
mocker.call(err),
mocker.call((2, 3))])
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_Import(compile):
errmsg = 'Line 1: "%s" is an invalid variable name ' \
'because it starts with "_"'
code, errors = compile('import a')[:2]
assert code is not None
assert errors == ()
code, errors = compile('import _a')[:2]
assert code is None
assert errors[0] == (errmsg % '_a')
code, errors = compile('import _a as m')[:2]
assert code is None
assert errors[0] == (errmsg % '_a')
code, errors = compile('import a as _m')[:2]
assert code is None
assert errors[0] == (errmsg % '_m')
code, errors = compile('from a import m')[:2]
assert code is not None
assert errors == ()
code, errors = compile('from _a import m')[:2]
assert code is not None
assert errors == ()
code, errors = compile('from a import m as _n')[:2]
assert code is None
assert errors[0] == (errmsg % '_n')
code, errors = compile('from a import _m as n')[:2]
assert code is None
assert errors[0] == (errmsg % '_m')
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__visit_ClassDef(compile):
code, errors = compile('class Good: pass')[:2]
assert code is not None
assert errors == ()
# Do not allow class names which start with an underscore.
code, errors = compile('class _bad: pass')[:2]
assert code is None
assert errors[0] == 'Line 1: "_bad" is an invalid variable name ' \
'because it starts with "_"'
@pytest.mark.parametrize(*compile)
def test_transformer__RestrictingNodeTransformer__test_ternary_if(compile, mocker):
code, errors = compile('x.y = y.a if y.z else y.b')[:2]
assert code is not None
assert errors == ()
_getattr_ = mocker.stub()
_getattr_.side_effect = lambda ob, key: ob[key]
_write_ = mocker.stub()
_write_.side_effect = lambda ob: ob
glb = {
'_getattr_': _getattr_,
'_write_': _write_,
'x': mocker.stub(),
'y': {'a': 'a', 'b': 'b'},
}
glb['y']['z'] = True
six.exec_(code, glb)
assert glb['x'].y == 'a'
_write_.assert_called_once_with(glb['x'])
_getattr_.assert_has_calls([
mocker.call(glb['y'], 'z'),
mocker.call(glb['y'], 'a')])
_write_.reset_mock()
_getattr_.reset_mock()
glb['y']['z'] = False
six.exec_(code, glb)
assert glb['x'].y == 'b'
_write_.assert_called_once_with(glb['x'])
_getattr_.assert_has_calls([
mocker.call(glb['y'], 'z'),
mocker.call(glb['y'], 'b')])
WITH_STMT_WITH_UNPACK_SEQUENCE = """
def call(ctx):