summaryrefslogtreecommitdiffstats
path: root/src/testdir/test87.in
blob: 31de37b997a7af6adee6bdb1bb2da9014a91d021 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
Tests for various python features.     vim: set ft=vim :

STARTTEST
:so small.vim
:set noswapfile
:if !has('python3') | e! test.ok | wq! test.out | endif
:lang C
:fun Test()
:py3 import vim
:py3 cb = vim.current.buffer
:let l = []
:py3 l=vim.bindeval('l')
:py3 f=vim.bindeval('function("strlen")')
:" Extending List directly with different types
:py3 l+=[1, "as'd", [1, 2, f, {'a': 1}]]
:$put =string(l)
:$put =string(l[-1])
:try
:  $put =string(l[-4])
:catch
:  $put =v:exception[:13]
:endtry
:" List assignment
:py3 l[0]=0
:$put =string(l)
:py3 l[-2]=f
:$put =string(l)
:"
:" Extending Dictionary directly with different types
:let d = {}
:fun d.f()
:  return 1
:endfun
py3 << EOF
d=vim.bindeval('d')
d['1']='asd'
d.update()  # Must not do anything, including throwing errors
d.update(b=[1, 2, f])
d.update((('-1', {'a': 1}),))
d.update({'0': -1})
dk = d.keys()
dv = d.values()
di = d.items()
dk.sort(key=repr)
dv.sort(key=repr)
di.sort(key=repr)
EOF
:$put =py3eval('d[''f''](self={})')
:$put =py3eval('repr(dk)')
:$put =substitute(py3eval('repr(dv)'),'0x\x\+','','g')
:$put =substitute(py3eval('repr(di)'),'0x\x\+','','g')
:for [key, Val] in sort(items(d))
:  $put =string(key) . ' : ' . string(Val)
:  unlet key Val
:endfor
:py3 del dk
:py3 del di
:py3 del dv
:"
:" removing items with del
:py3 del l[2]
:$put =string(l)
:let l = range(8)
:py3 l=vim.bindeval('l')
:try
:   py3 del l[:3]
:   py3 del l[1:]
:catch
:   $put =v:exception
:endtry
:$put =string(l)
:"
:py3 del d['-1']
:py3 del d['f']
:$put =string(py3eval('d.get(''b'', 1)'))
:$put =string(py3eval('d.pop(''b'')'))
:$put =string(py3eval('d.get(''b'', 1)'))
:$put =string(py3eval('d.pop(''1'', 2)'))
:$put =string(py3eval('d.pop(''1'', 2)'))
:$put =py3eval('repr(d.has_key(''0''))')
:$put =py3eval('repr(d.has_key(''1''))')
:$put =py3eval('repr(''0'' in d)')
:$put =py3eval('repr(''1'' in d)')
:$put =py3eval('repr(list(iter(d)))')
:$put =string(d)
:$put =py3eval('repr(d.popitem())')
:$put =py3eval('repr(d.get(''0''))')
:$put =py3eval('repr(list(iter(d)))')
:"
:" removing items out of range: silently skip items that don't exist
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:" The following two ranges delete nothing as they match empty list:
:py3 del l[2:1]
:$put =string(l)
:py3 del l[2:2]
:$put =string(l)
:py3 del l[2:3]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[2:4]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[2:5]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[2:6]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:" The following two ranges delete nothing as they match empty list:
:py3 del l[-1:2]
:$put =string(l)
:py3 del l[-2:2]
:$put =string(l)
:py3 del l[-3:2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[-4:2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[-5:2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[-6:2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[::2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[3:0:-2]
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 del l[2:4:-2]
:$put =string(l)
:"
:" Slice assignment to a list
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 l[0:0]=['a']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 l[1:2]=['b']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 l[2:4]=['c']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 l[4:4]=['d']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 l[-1:2]=['e']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 l[-10:2]=['f']
:$put =string(l)
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:py3 l[2:-10]=['g']
:$put =string(l)
:let l = []
:py3 l=vim.bindeval('l')
:py3 l[0:0]=['h']
:$put =string(l)
:let l = range(8)
:py3 l=vim.bindeval('l')
:py3 l[2:6:2] = [10, 20]
:$put =string(l)
:let l = range(8)
:py3 l=vim.bindeval('l')
:py3 l[6:2:-2] = [10, 20]
:$put =string(l)
:let l = range(8)
:py3 l=vim.bindeval('l')
:py3 l[6:2] = ()
:$put =string(l)
:let l = range(8)
:py3 l=vim.bindeval('l')
:py3 l[6:2:1] = ()
:$put =string(l)
:let l = range(8)
:py3 l=vim.bindeval('l')
:py3 l[2:2:1] = ()
:$put =string(l)
:"
:" Locked variables
:let l = [0, 1, 2, 3]
:py3 l=vim.bindeval('l')
:lockvar! l
py3 << EOF
def emsg(ei):
    return ei[0].__name__ + ':' + repr(ei[1].args)

try:
    l[2]='i'
except vim.error:
    cb.append('l[2] threw vim.error: ' + emsg(sys.exc_info()))
EOF
:$put =string(l)
:unlockvar! l
:"
:" Function calls
py3 << EOF
import sys
import re

py33_type_error_pattern = re.compile('^__call__\(\) takes (\d+) positional argument but (\d+) were given$')
py37_exception_repr = re.compile(r'([^\(\),])(\)+)$')

def ee(expr, g=globals(), l=locals()):
    cb = vim.current.buffer
    try:
        try:
            exec(expr, g, l)
        except Exception as e:
            if sys.version_info >= (3, 3) and e.__class__ is AttributeError and str(e).find('has no attribute')>=0 and not str(e).startswith("'vim."):
                msg = repr((e.__class__, AttributeError(str(e)[str(e).rfind(" '") + 2:-1])))
            elif sys.version_info >= (3, 3) and e.__class__ is ImportError and str(e).find('No module named \'') >= 0:
                msg = repr((e.__class__, ImportError(str(e).replace("'", ''))))
            elif sys.version_info >= (3, 6) and e.__class__ is ModuleNotFoundError:
                # Python 3.6 gives ModuleNotFoundError, change it to an ImportError
                msg = repr((ImportError, ImportError(str(e).replace("'", ''))))
            elif sys.version_info >= (3, 3) and e.__class__ is TypeError:
                m = py33_type_error_pattern.search(str(e))
                if m:
                    msg = '__call__() takes exactly {0} positional argument ({1} given)'.format(m.group(1), m.group(2))
                    msg = repr((e.__class__, TypeError(msg)))
                else:
                    msg = repr((e.__class__, e))
                    # Messages changed with Python 3.6, change new to old.
                    newmsg1 = """'argument must be str, bytes or bytearray, not None'"""
                    oldmsg1 = '''"Can't convert 'NoneType' object to str implicitly"'''
                    if msg.find(newmsg1) > -1:
                        msg = msg.replace(newmsg1, oldmsg1)
                    newmsg2 = """'argument must be str, bytes or bytearray, not int'"""
                    oldmsg2 = '''"Can't convert 'int' object to str implicitly"'''
                    if msg.find(newmsg2) > -1:
                        msg = msg.replace(newmsg2, oldmsg2)
            elif sys.version_info >= (3, 5) and e.__class__ is ValueError and str(e) == 'embedded null byte':
                msg = repr((TypeError, TypeError('expected bytes with no null')))
            else:
                msg = repr((e.__class__, e))
                # Some Python versions say can't, others cannot.
                if msg.find('can\'t') > -1:
                    msg = msg.replace('can\'t', 'cannot')
                # Some Python versions use single quote, some double quote
                if msg.find('"cannot ') > -1:
                    msg = msg.replace('"cannot ', '\'cannot ')
                if msg.find(' attributes"') > -1:
                    msg = msg.replace(' attributes"', ' attributes\'')
            if sys.version_info >= (3, 7):
                msg = py37_exception_repr.sub(r'\1,\2', msg)
            cb.append(expr + ':' + msg)
        else:
            cb.append(expr + ':NOT FAILED')
    except Exception as e:
        msg = repr((e.__class__, e))
        if sys.version_info >= (3, 7):
            msg = py37_exception_repr.sub(r'\1,\2', msg)
        cb.append(expr + '::' + msg)
EOF
:fun New(...)
:   return ['NewStart']+a:000+['NewEnd']
:endfun
:fun DictNew(...) dict
:   return ['DictNewStart']+a:000+['DictNewEnd', self]
:endfun
:let l=[function('New'), function('DictNew')]
:py3 l=vim.bindeval('l')
:py3 l.extend(list(l[0](1, 2, 3)))
:$put =string(l)
:py3 l.extend(list(l[1](1, 2, 3, self={'a': 'b'})))
:$put =string(l)
:py3 l+=[l[0].name]
:$put =string(l)
:py3 ee('l[1](1, 2, 3)')
:py3 f=l[0]
:delfunction New
:py3 ee('f(1, 2, 3)')
:if has('float')
:   let l=[0.0]
:   py3 l=vim.bindeval('l')
:   py3 l.extend([0.0])
:   $put =string(l)
:else
:   $put ='[0.0, 0.0]'
:endif
:let messages=[]
:delfunction DictNew
py3 <<EOF
import sys
d=vim.bindeval('{}')
m=vim.bindeval('messages')
def em(expr, g=globals(), l=locals()):
    try:
        exec(expr, g, l)
    except Exception as e:
        if sys.version_info >= (3, 5) and e.__class__ is ValueError and str(e) == 'embedded null byte':
            m.extend([TypeError.__name__])
        else:
            m.extend([e.__class__.__name__])

em('d["abc1"]')
em('d["abc1"]="\\0"')
em('d["abc1"]=vim')
em('d[""]=1')
em('d["a\\0b"]=1')
em('d[b"a\\0b"]=1')

em('d.pop("abc1")')
em('d.popitem()')
del em
del m
EOF
:$put =messages
:unlet messages
:" locked and scope attributes
:let d={} | let dl={} | lockvar dl
:for s in split("d dl v: g:")
:    let name=tr(s, ':', 's')
:    execute 'py3 '.name.'=vim.bindeval("'.s.'")'
:    let toput=s.' : '.join(map(['locked', 'scope'], 'v:val.":".py3eval(name.".".v:val)'), ';')
:    $put =toput
:endfor
:silent! let d.abc2=1
:silent! let dl.abc3=1
:py3 d.locked=True
:py3 dl.locked=False
:silent! let d.def=1
:silent! let dl.def=1
:put ='d:'.string(d)
:put ='dl:'.string(dl)
:unlet d dl
:
:let l=[] | let ll=[] | lockvar ll
:for s in split("l ll")
:    let name=tr(s, ':', 's')
:    execute 'py3 '.name.'=vim.bindeval("'.s.'")'
:    let toput=s.' : locked:'.py3eval(name.'.locked')
:    $put =toput
:endfor
:silent! call extend(l, [0])
:silent! call extend(ll, [0])
:py3 l.locked=True
:py3 ll.locked=False
:silent! call extend(l, [1])
:silent! call extend(ll, [1])
:put ='l:'.string(l)
:put ='ll:'.string(ll)
:unlet l ll
:"
:" py3eval()
:let l=py3eval('[0, 1, 2]')
:$put =string(l)
:let d=py3eval('{"a": "b", "c": 1, "d": ["e"]}')
:$put =sort(items(d))
:let v:errmsg = ''
:$put ='py3eval(\"None\") = ' . py3eval('None') . v:errmsg
:if has('float')
:   let f=py3eval('0.0')
:   $put =string(f)
:else
:   $put ='0.0'
:endif
:" Invalid values:
:for e in ['"\0"', '{"\0": 1}', 'undefined_name', 'vim']
:   try
:      let v=py3eval(e)
:   catch
:      let toput=e.":\t".v:exception[:13]
:      $put =toput
:   endtry
:endfor
:"
:" threading
:let l = [0]
:py3 l=vim.bindeval('l')
py3 <<EOF
import threading
import time

class T(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.t = 0
        self.running = True

    def run(self):
        while self.running:
            self.t += 1
            time.sleep(0.1)

t = T()
del T
t.start()
EOF
:sleep 1
:py3 t.running = False
:py3 t.join()
:" Check if the background thread is working.  Count should be 10, but on a
:" busy system (AppVeyor) it can be much lower.
:py3 l[0] = t.t > 4
:py3 del time
:py3 del threading
:py3 del t
:$put =string(l)
:"
:" settrace
:let l = []
:py3 l=vim.bindeval('l')
py3 <<EOF
import sys

def traceit(frame, event, arg):
    global l
    if event == "line":
        l += [frame.f_lineno]
    return traceit

def trace_main():
    for i in range(5):
        pass
EOF
:py3 sys.settrace(traceit)
:py3 trace_main()
:py3 sys.settrace(None)
:py3 del traceit
:py3 del trace_main
:$put =string(l)
:"
:" Slice
:py3 ll = vim.bindeval('[0, 1, 2, 3, 4, 5]')
:py3 l = ll[:4]
:$put =string(py3eval('l'))
:py3 l = ll[2:]
:$put =string(py3eval('l'))
:py3 l = ll[:-4]
:$put =string(py3eval('l'))
:py3 l = ll[-2:]
:$put =string(py3eval('l'))
:py3 l = ll[2:4]
:$put =string(py3eval('l'))
:py3 l = ll[4:2]
:$put =string(py3eval('l'))
:py3 l = ll[-4:-2]
:$put =string(py3eval('l'))
:py3 l = ll[-2:-4]
:$put =string(py3eval('l'))
:py3 l = ll[:]
:$put =string(py3eval('l'))
:py3 l = ll[0:6]
:$put =string(py3eval('l'))
:py3 l = ll[-10:10]
:$put =string(py3eval('l'))
:py3 l = ll[4:2:-1]
:$put =string(py3eval('l'))
:py3 l = ll[::2]
:$put =string(py3eval('l'))
:py3 l = ll[4:2:1]
:$put =string(py3eval('l'))
:py3 del l
:"
:" Vars
:let g:foo = 'bac'
:let w:abc3 = 'def'
:let b:baz = 'bar'
:let t:bar = 'jkl'
:try
:  throw "Abc"
:catch
:  put =py3eval('vim.vvars[''exception'']')
:endtry
:put =py3eval('vim.vars[''foo'']')
:put =py3eval('vim.current.window.vars[''abc3'']')
:put =py3eval('vim.current.buffer.vars[''baz'']')
:put =py3eval('vim.current.tabpage.vars[''bar'']')
:"
:" Options
:" paste:          boolean, global
:" previewheight   number,  global
:" operatorfunc:   string,  global
:" number:         boolean, window-local
:" numberwidth:    number,  window-local
:" colorcolumn:    string,  window-local
:" statusline:     string,  window-local/global
:" autoindent:     boolean, buffer-local
:" shiftwidth:     number,  buffer-local
:" omnifunc:       string,  buffer-local
:" preserveindent: boolean, buffer-local/global
:" path:           string,  buffer-local/global
:let g:bufs=[bufnr('%')]
:new
:let g:bufs+=[bufnr('%')]
:vnew
:let g:bufs+=[bufnr('%')]
:wincmd j
:vnew
:let g:bufs+=[bufnr('%')]
:wincmd l
:fun RecVars(opt)
:  let gval =string(eval('&g:'.a:opt))
:  let wvals=join(map(range(1, 4),  'v:val.":".string(getwinvar(v:val, "&".a:opt))'))
:  let bvals=join(map(copy(g:bufs), 'v:val.":".string(getbufvar(v:val, "&".a:opt))'))
:  put ='  G: '.gval
:  put ='  W: '.wvals
:  put ='  B: '.wvals
:endfun
py3 << EOF
def e(s, g=globals(), l=locals()):
    try:
        exec(s, g, l)
    except Exception as e:
        vim.command('return ' + repr(e.__class__.__name__))

def ev(s, g=globals(), l=locals()):
    try:
        return eval(s, g, l)
    except Exception as e:
        vim.command('let exc=' + repr(e.__class__.__name__))
        return 0
EOF
:fun E(s)
:   python3 e(vim.eval('a:s'))
:endfun
:fun Ev(s)
:   let r=py3eval('ev(vim.eval("a:s"))')
:   if exists('exc')
:       throw exc
:   endif
:   return r
:endfun
:py3 gopts1=vim.options
:py3 wopts1=vim.windows[2].options
:py3 wopts2=vim.windows[0].options
:py3 wopts3=vim.windows[1].options
:py3 bopts1=vim.buffers[vim.bindeval("g:bufs")[2]].options
:py3 bopts2=vim.buffers[vim.bindeval("g:bufs")[1]].options
:py3 bopts3=vim.buffers[vim.bindeval("g:bufs")[0]].options
:$put ='wopts iters equal: '.py3eval('list(wopts1) == list(wopts2)')
:$put ='bopts iters equal: '.py3eval('list(bopts1) == list(bopts2)')
:py3 gset=set(iter(gopts1))
:py3 wset=set(iter(wopts1))
:py3 bset=set(iter(bopts1))
:set path=.,..,,
:let lst=[]
:let lst+=[['paste',          1,     0,     1,     2,      1,    1,      0    ]]
:let lst+=[['previewheight',  5,     1,     6,     'a',    0,    1,      0    ]]
:let lst+=[['operatorfunc',   'A',   'B',   'C',   2,      0,    1,      0    ]]
:let lst+=[['number',         0,     1,     1,     0,      1,    0,      1    ]]
:let lst+=[['numberwidth',    2,     3,     5,     -100,   0,    0,      1    ]]
:let lst+=[['colorcolumn',    '+1',  '+2',  '+3',  'abc4',  0,    0,      1    ]]
:let lst+=[['statusline',     '1',   '2',   '4',   0,      0,    1,      1    ]]
:let lst+=[['autoindent',     0,     1,     1,     2,      1,    0,      2    ]]
:let lst+=[['shiftwidth',     0,     2,     1,     3,      0,    0,      2    ]]
:let lst+=[['omnifunc',       'A',   'B',   'C',   1,      0,    0,      2    ]]
:let lst+=[['preserveindent', 0,     1,     1,     2,      1,    1,      2    ]]
:let lst+=[['path',           '.,,', ',,',  '.',   0,      0,    1,      2    ]]
:for       [oname,            oval1, oval2, oval3, invval, bool, global, local] in lst
:   py3 oname=vim.eval('oname')
:   py3 oval1=vim.bindeval('oval1')
:   py3 oval2=vim.bindeval('oval2')
:   py3 oval3=vim.bindeval('oval3')
:   if invval is 0 || invval is 1
:       py3 invval=bool(vim.bindeval('invval'))
:   else
:       py3 invval=vim.bindeval('invval')
:   endif
:   if bool
:       py3 oval1=bool(oval1)
:       py3 oval2=bool(oval2)
:       py3 oval3=bool(oval3)
:   endif
:   put ='>>> '.oname
:   $put ='  g/w/b:'.py3eval('oname in gset').'/'.py3eval('oname in wset').'/'.py3eval('oname in bset')
:   $put ='  g/w/b (in):'.py3eval('oname in gopts1').'/'.py3eval('oname in wopts1').'/'.py3eval('oname in bopts1')
:   for v in ['gopts1', 'wopts1', 'bopts1']
:       try
:           put ='  p/'.v.': '.Ev('repr('.v.'['''.oname.'''])')
:       catch
:           put ='  p/'.v.'! '.v:exception
:       endtry
:       let r=E(v.'['''.oname.''']=invval')
:       if r isnot 0
:           put ='  inv: '.string(invval).'! '.r
:       endif
:       for vv in (v is# 'gopts1' ? [v] : [v, v[:-2].'2', v[:-2].'3'])
:           let val=substitute(vv, '^.opts', 'oval', '')
:           let r=E(vv.'['''.oname.''']='.val)
:           if r isnot 0
:               put ='  '.vv.'! '.r
:           endif
:       endfor
:   endfor
:   call RecVars(oname)
:   for v in ['wopts3', 'bopts3']
:       let r=E('del '.v.'["'.oname.'"]')
:       if r isnot 0
:           put ='  del '.v.'! '.r
:       endif
:   endfor
:   call RecVars(oname)
:endfor
:delfunction RecVars
:delfunction E
:delfunction Ev
:py3 del ev
:py3 del e
:only
:for buf in g:bufs[1:]
:   execute 'bwipeout!' buf
:endfor
:py3 del gopts1
:py3 del wopts1
:py3 del wopts2
:py3 del wopts3
:py3 del bopts1
:py3 del bopts2
:py3 del bopts3
:py3 del oval1
:py3 del oval2
:py3 del oval3
:py3 del oname
:py3 del invval
:"
:" Test buffer object
:vnew
:put ='First line'
:put ='Second line'
:put ='Third line'
:1 delete _
:py3 b=vim.current.buffer
:wincmd w
:mark a
:augroup BUFS
:   autocmd BufFilePost * python3 cb.append(vim.eval('expand("<abuf>")') + ':BufFilePost:' + vim.eval('bufnr("%")'))
:   autocmd BufFilePre * python3 cb.append(vim.eval('expand("<abuf>")') + ':BufFilePre:' + vim.eval('bufnr("%")'))
:augroup END
py3 << EOF
# Tests BufferAppend and BufferItem
cb.append(b[0])
# Tests BufferSlice and BufferAssSlice
cb.append('abc5') # Will be overwritten
cb[-1:] = b[:-2]
# Test BufferLength and BufferAssSlice
cb.append('def') # Will not be overwritten
cb[len(cb):] = b[:]
# Test BufferAssItem and BufferMark
cb.append('ghi') # Will be overwritten
cb[-1] = repr((len(cb) - cb.mark('a')[0], cb.mark('a')[1]))
# Test BufferRepr
cb.append(repr(cb) + repr(b))
# Modify foreign buffer
b.append('foo')
b[0]='bar'
b[0:0]=['baz']
vim.command('call append("$", getbufline(%i, 1, "$"))' % b.number)
# Test assigning to name property
import os
old_name = cb.name
cb.name = 'foo'
cb.append(cb.name[-11:].replace(os.path.sep, '/'))
b.name = 'bar'
cb.append(b.name[-11:].replace(os.path.sep, '/'))
cb.name = old_name
cb.append(cb.name[-17:].replace(os.path.sep, '/'))
del old_name
# Test CheckBuffer
for _b in vim.buffers:
    if _b is not cb:
        vim.command('bwipeout! ' + str(_b.number))
del _b
cb.append('valid: b:%s, cb:%s' % (repr(b.valid), repr(cb.valid)))
for expr in ('b[1]','b[:] = ["A", "B"]','b[:]','b.append("abc6")'):
    try:
        exec(expr)
    except vim.error:
        pass
    else:
        # Usually a SEGV here
        # Should not happen in any case
        cb.append('No exception for ' + expr)
vim.command('cd .')
del b
EOF
:"
:" Test vim.buffers object
:set hidden
:edit a
:buffer #
:edit b
:buffer #
:edit c
:buffer #
py3 << EOF
# Check GCing iterator that was not fully exhausted
i = iter(vim.buffers)
cb.append('i:' + str(next(i)))
# and also check creating more than one iterator at a time
i2 = iter(vim.buffers)
cb.append('i2:' + str(next(i2)))
cb.append('i:' + str(next(i)))
# The following should trigger GC and not cause any problems
del i
del i2
i3 = iter(vim.buffers)
cb.append('i3:' + str(next(i3)))
del i3

prevnum = 0
for b in vim.buffers:
    # Check buffer order
    if prevnum >= b.number:
        cb.append('!!! Buffer numbers not in strictly ascending order')
    # Check indexing: vim.buffers[number].number == number
    cb.append(str(b.number) + ':' + repr(vim.buffers[b.number]) + '=' + repr(b))
    prevnum = b.number
del prevnum

cb.append(str(len(vim.buffers)))

bnums = list(map(lambda b: b.number, vim.buffers))[1:]

# Test wiping out buffer with existing iterator
i4 = iter(vim.buffers)
cb.append('i4:' + str(next(i4)))
vim.command('bwipeout! ' + str(bnums.pop(0)))
try:
    next(i4)
except vim.error:
    pass
else:
    cb.append('!!!! No vim.error')
i4 = iter(vim.buffers)
vim.command('bwipeout! ' + str(bnums.pop(-1)))
vim.command('bwipeout! ' + str(bnums.pop(-1)))
cb.append('i4:' + str(next(i4)))
try:
    next(i4)
except StopIteration:
    cb.append('StopIteration')
del i4
del bnums
EOF
:"
:" Test vim.{tabpage,window}list and vim.{tabpage,window} objects
:tabnew 0
:tabnew 1
:vnew a.1
:tabnew 2
:vnew a.2
:vnew b.2
:vnew c.2
py3 << EOF
cb.append('Number of tabs: ' + str(len(vim.tabpages)))
cb.append('Current tab pages:')

def W(w):
    if '(unknown)' in repr(w):
        return '<window object (unknown)>'
    else:
        return repr(w)

def Cursor(w, start=len(cb)):
    if w.buffer is cb:
        return repr((start - w.cursor[0], w.cursor[1]))
    else:
        return repr(w.cursor)

for t in vim.tabpages:
    cb.append('  ' + repr(t) + '(' + str(t.number) + ')' + ': ' + str(len(t.windows)) + ' windows, current is ' + W(t.window))
    cb.append('  Windows:')
    for w in t.windows:
        cb.append('    ' + W(w) + '(' + str(w.number) + ')' + ': displays buffer ' + repr(w.buffer) + '; cursor is at ' + Cursor(w))
        # Other values depend on the size of the terminal, so they are checked partly:
        for attr in ('height', 'row', 'width', 'col'):
            try:
                aval = getattr(w, attr)
                if type(aval) is not int:
                    raise TypeError
                if aval < 0:
                    raise ValueError
            except Exception as e:
                cb.append('!!!!!! Error while getting attribute ' + attr + ': ' + e.__class__.__name__)
        del aval
        del attr
        w.cursor = (len(w.buffer), 0)
del W
del Cursor
cb.append('Number of windows in current tab page: ' + str(len(vim.windows)))
if list(vim.windows) != list(vim.current.tabpage.windows):
    cb.append('!!!!!! Windows differ')
EOF
:"
:" Test vim.current
py3 << EOF
def H(o):
    return repr(o)
cb.append('Current tab page: ' + repr(vim.current.tabpage))
cb.append('Current window: ' + repr(vim.current.window) + ': ' + H(vim.current.window) + ' is ' + H(vim.current.tabpage.window))
cb.append('Current buffer: ' + repr(vim.current.buffer) + ': ' + H(vim.current.buffer) + ' is ' + H(vim.current.window.buffer)+ ' is ' + H(vim.current.tabpage.window.buffer))
del H
# Assigning: fails
try:
    vim.current.window = vim.tabpages[0].window
except ValueError:
    cb.append('ValueError at assigning foreign tab window')

for attr in ('window', 'tabpage', 'buffer'):
    try:
        setattr(vim.current, attr, None)
    except TypeError:
        cb.append('Type error at assigning None to vim.current.' + attr)
del attr

# Assigning: success
vim.current.tabpage = vim.tabpages[-2]
vim.current.buffer = cb
vim.current.window = vim.windows[0]
vim.current.window.cursor = (len(vim.current.buffer), 0)
cb.append('Current tab page: ' + repr(vim.current.tabpage))
cb.append('Current window: ' + repr(vim.current.window))
cb.append('Current buffer: ' + repr(vim.current.buffer))
cb.append('Current line: ' + repr(vim.current.line))
ws = list(vim.windows)
ts = list(vim.tabpages)
for b in vim.buffers:
    if b is not cb:
        vim.command('bwipeout! ' + str(b.number))
del b
cb.append('w.valid: ' + repr([w.valid for w in ws]))
cb.append('t.valid: ' + repr([t.valid for t in ts]))
del w
del t
del ts
del ws
EOF
:tabonly!
:only!
:"
:" Test types
py3 << EOF
for expr, attr in (
    ('vim.vars',                         'Dictionary'),
    ('vim.options',                      'Options'),
    ('vim.bindeval("{}")',               'Dictionary'),
    ('vim.bindeval("[]")',               'List'),
    ('vim.bindeval("function(\'tr\')")', 'Function'),
    ('vim.current.buffer',               'Buffer'),
    ('vim.current.range',                'Range'),
    ('vim.current.window',               'Window'),
    ('vim.current.tabpage',              'TabPage'),
):
    cb.append(expr + ':' + attr + ':' + repr(type(eval(expr)) is getattr(vim, attr)))
del expr
del attr
EOF
:"
:" Test __dir__() method
py3 << EOF
for name, o in (
        ('current',    vim.current),
        ('buffer',     vim.current.buffer),
        ('window',     vim.current.window),
        ('tabpage',    vim.current.tabpage),
        ('range',      vim.current.range),
        ('dictionary', vim.bindeval('{}')),
        ('list',       vim.bindeval('[]')),
        ('function',   vim.bindeval('function("tr")')),
        ('output',     sys.stdout),
    ):
    cb.append(name + ':' + ','.join(dir(o)))
del name
del o
EOF
:"
:" Test vim.*.__new__
:$put =string(py3eval('vim.Dictionary({})'))
:$put =string(py3eval('vim.Dictionary(a=1)'))
:$put =string(py3eval('vim.Dictionary(((''a'', 1),))'))
:$put =string(py3eval('vim.List()'))
:$put =string(py3eval('vim.List(iter(''abc7''))'))
:$put =string(py3eval('vim.Function(''tr'')'))
:$put =string(py3eval('vim.Function(''tr'', args=[123, 3, 4])'))
:$put =string(py3eval('vim.Function(''tr'', args=[])'))
:$put =string(py3eval('vim.Function(''tr'', self={})'))
:$put =string(py3eval('vim.Function(''tr'', args=[123, 3, 4], self={})'))
:$put ='auto_rebind'
:$put =string(py3eval('vim.Function(''tr'', auto_rebind=False)'))
:$put =string(py3eval('vim.Function(''tr'', args=[123, 3, 4], auto_rebind=False)'))
:$put =string(py3eval('vim.Function(''tr'', args=[], auto_rebind=False)'))
:$put =string(py3eval('vim.Function(''tr'', self={}, auto_rebind=False)'))
:$put =string(py3eval('vim.Function(''tr'', args=[123, 3, 4], self={}, auto_rebind=False)'))
:"
:" Test vim.Function
:function Args(...)
:   return a:000
:endfunction
:function SelfArgs(...) dict
:   return [a:000, self]
:endfunction
:" The following four lines should not crash
:let Pt = function('tr', [[]], {'l': []})
:py3 Pt = vim.bindeval('Pt')
:unlet Pt
:py3 del Pt
py3 << EOF
def ecall(out_prefix, func, *args, **kwargs):
    line = out_prefix + ': '
    try:
        ret = func(*args, **kwargs)
    except Exception:
        line += '!exception: ' + emsg(sys.exc_info())
    else:
        line += '!result: ' + str(vim.Function('string')(ret), 'utf-8')
    cb.append(line)
a = vim.Function('Args')
pa1 = vim.Function('Args', args=['abcArgsPA1'])
pa2 = vim.Function('Args', args=[])
pa3 = vim.Function('Args', args=['abcArgsPA3'], self={'abcSelfPA3': 'abcSelfPA3Val'})
pa4 = vim.Function('Args', self={'abcSelfPA4': 'abcSelfPA4Val'})
cb.append('a: ' + repr(a))
cb.append('pa1: ' + repr(pa1))
cb.append('pa2: ' + repr(pa2))
cb.append('pa3: ' + repr(pa3))
cb.append('pa4: ' + repr(pa4))
sa = vim.Function('SelfArgs')
psa1 = vim.Function('SelfArgs', args=['abcArgsPSA1'])
psa2 = vim.Function('SelfArgs', args=[])
psa3 = vim.Function('SelfArgs', args=['abcArgsPSA3'], self={'abcSelfPSA3': 'abcSelfPSA3Val'})
psa4 = vim.Function('SelfArgs', self={'abcSelfPSA4': 'abcSelfPSA4Val'})
psa5 = vim.Function('SelfArgs', self={'abcSelfPSA5': 'abcSelfPSA5Val'}, auto_rebind=0)
psa6 = vim.Function('SelfArgs', args=['abcArgsPSA6'], self={'abcSelfPSA6': 'abcSelfPSA6Val'}, auto_rebind=())
psa7 = vim.Function('SelfArgs', args=['abcArgsPSA7'], auto_rebind=[])
psa8 = vim.Function('SelfArgs', auto_rebind=False)
psa9 = vim.Function('SelfArgs', self={'abcSelfPSA9': 'abcSelfPSA9Val'}, auto_rebind=True)
psaA = vim.Function('SelfArgs', args=['abcArgsPSAA'], self={'abcSelfPSAA': 'abcSelfPSAAVal'}, auto_rebind=1)
psaB = vim.Function('SelfArgs', args=['abcArgsPSAB'], auto_rebind={'abcARPSAB': 'abcARPSABVal'})
psaC = vim.Function('SelfArgs', auto_rebind=['abcARPSAC'])
cb.append('sa: ' + repr(sa))
cb.append('psa1: ' + repr(psa1))
cb.append('psa2: ' + repr(psa2))
cb.append('psa3: ' + repr(psa3))
cb.append('psa4: ' + repr(psa4))
cb.append('psa5: ' + repr(psa5))
cb.append('psa6: ' + repr(psa6))
cb.append('psa7: ' + repr(psa7))
cb.append('psa8: ' + repr(psa8))
cb.append('psa9: ' + repr(psa9))
cb.append('psaA: ' + repr(psaA))
cb.append('psaB: ' + repr(psaB))
cb.append('psaC: ' + repr(psaC))

psar = vim.Function('SelfArgs', args=[{'abcArgsPSAr': 'abcArgsPSArVal'}], self={'abcSelfPSAr': 'abcSelfPSArVal'})
psar.args[0]['abcArgsPSAr2'] = [psar.self, psar.args[0]]
psar.self['rec'] = psar
psar.self['self'] = psar.self
psar.self['args'] = psar.args

try:
    cb.append('psar: ' + repr(psar))
except Exception:
    cb.append('!!!!!!!! Caught exception: ' + emsg(sys.exc_info()))
EOF
:$put ='s(a): '.string(py3eval('a'))
:$put ='s(pa1): '.string(py3eval('pa1'))
:$put ='s(pa2): '.string(py3eval('pa2'))
:$put ='s(pa3): '.string(py3eval('pa3'))
:$put ='s(pa4): '.string(py3eval('pa4'))
:$put ='s(sa): '.string(py3eval('sa'))
:$put ='s(psa1): '.string(py3eval('psa1'))
:$put ='s(psa2): '.string(py3eval('psa2'))
:$put ='s(psa3): '.string(py3eval('psa3'))
:$put ='s(psa4): '.string(py3eval('psa4'))
:$put ='s(psa5): '.string(py3eval('psa5'))
:$put ='s(psa6): '.string(py3eval('psa6'))
:$put ='s(psa7): '.string(py3eval('psa7'))
:$put ='s(psa8): '.string(py3eval('psa8'))
:$put ='s(psa9): '.string(py3eval('psa9'))
:$put ='s(psaA): '.string(py3eval('psaA'))
:$put ='s(psaB): '.string(py3eval('psaB'))
:$put ='s(psaC): '.string(py3eval('psaC'))
:
:for v in ['sa', 'psa1', 'psa2', 'psa3', 'psa4', 'psa5', 'psa6', 'psa7', 'psa8', 'psa9', 'psaA', 'psaB', 'psaC']
:   let d = {'f': py3eval(v)}
:   $put ='d.'.v.'(): '.string(d.f())
:endfor
:
:py3 ecall('a()', a, )
:py3 ecall('pa1()', pa1, )
:py3 ecall('pa2()', pa2, )
:py3 ecall('pa3()', pa3, )
:py3 ecall('pa4()', pa4, )
:py3 ecall('sa()', sa, )
:py3 ecall('psa1()', psa1, )
:py3 ecall('psa2()', psa2, )
:py3 ecall('psa3()', psa3, )
:py3 ecall('psa4()', psa4, )
:
:py3 ecall('a(42, 43)', a, 42, 43)
:py3 ecall('pa1(42, 43)', pa1, 42, 43)
:py3 ecall('pa2(42, 43)', pa2, 42, 43)
:py3 ecall('pa3(42, 43)', pa3, 42, 43)
:py3 ecall('pa4(42, 43)', pa4, 42, 43)
:py3 ecall('sa(42, 43)', sa, 42, 43)
:py3 ecall('psa1(42, 43)', psa1, 42, 43)
:py3 ecall('psa2(42, 43)', psa2, 42, 43)
:py3 ecall('psa3(42, 43)', psa3, 42, 43)
:py3 ecall('psa4(42, 43)', psa4, 42, 43)
:
:py3 ecall('a(42, self={"20": 1})', a, 42, self={'20': 1})
:py3 ecall('pa1(42, self={"20": 1})', pa1, 42, self={'20': 1})
:py3 ecall('pa2(42, self={"20": 1})', pa2, 42, self={'20': 1})
:py3 ecall('pa3(42, self={"20": 1})', pa3, 42, self={'20': 1})
:py3 ecall('pa4(42, self={"20": 1})', pa4, 42, self={'20': 1})
:py3 ecall('sa(42, self={"20": 1})', sa, 42, self={'20': 1})
:py3 ecall('psa1(42, self={"20": 1})', psa1, 42, self={'20': 1})
:py3 ecall('psa2(42, self={"20": 1})', psa2, 42, self={'20': 1})
:py3 ecall('psa3(42, self={"20": 1})', psa3, 42, self={'20': 1})
:py3 ecall('psa4(42, self={"20": 1})', psa4, 42, self={'20': 1})
:
:py3 ecall('a(self={"20": 1})', a, self={'20': 1})
:py3 ecall('pa1(self={"20": 1})', pa1, self={'20': 1})
:py3 ecall('pa2(self={"20": 1})', pa2, self={'20': 1})
:py3 ecall('pa3(self={"20": 1})', pa3, self={'20': 1})
:py3 ecall('pa4(self={"20": 1})', pa4, self={'20': 1})
:py3 ecall('sa(self={"20": 1})', sa, self={'20': 1})
:py3 ecall('psa1(self={"20": 1})', psa1, self={'20': 1})
:py3 ecall('psa2(self={"20": 1})', psa2, self={'20': 1})
:py3 ecall('psa3(self={"20": 1})', psa3, self={'20': 1})
:py3 ecall('psa4(self={"20": 1})', psa4, self={'20': 1})
py3 << EOF
def s(v):
    if v is None:
        return repr(v)
    else:
        return str(vim.Function('string')(v), 'utf-8')

cb.append('a.args: ' + s(a.args))
cb.append('pa1.args: ' + s(pa1.args))
cb.append('pa2.args: ' + s(pa2.args))
cb.append('pa3.args: ' + s(pa3.args))
cb.append('pa4.args: ' + s(pa4.args))
cb.append('sa.args: ' + s(sa.args))
cb.append('psa1.args: ' + s(psa1.args))
cb.append('psa2.args: ' + s(psa2.args))
cb.append('psa3.args: ' + s(psa3.args))
cb.append('psa4.args: ' + s(psa4.args))

cb.append('a.self: ' + s(a.self))
cb.append('pa1.self: ' + s(pa1.self))
cb.append('pa2.self: ' + s(pa2.self))
cb.append('pa3.self: ' + s(pa3.self))
cb.append('pa4.self: ' + s(pa4.self))
cb.append('sa.self: ' + s(sa.self))
cb.append('psa1.self: ' + s(psa1.self))
cb.append('psa2.self: ' + s(psa2.self))
cb.append('psa3.self: ' + s(psa3.self))
cb.append('psa4.self: ' + s(psa4.self))

cb.append('a.name: ' + s(a.name))
cb.append('pa1.name: ' + s(pa1.name))
cb.append('pa2.name: ' + s(pa2.name))
cb.append('pa3.name: ' + s(pa3.name))
cb.append('pa4.name: ' + s(pa4.name))
cb.append('sa.name: ' + s(sa.name))
cb.append('psa1.name: ' + s(psa1.name))
cb.append('psa2.name: ' + s(psa2.name))
cb.append('psa3.name: ' + s(psa3.name))
cb.append('psa4.name: ' + s(psa4.name))

cb.append('a.auto_rebind: ' + s(a.auto_rebind))
cb.append('pa1.auto_rebind: ' + s(pa1.auto_rebind))
cb.append('pa2.auto_rebind: ' + s(pa2.auto_rebind))
cb.append('pa3.auto_rebind: ' + s(pa3.auto_rebind))
cb.append('pa4.auto_rebind: ' + s(pa4.auto_rebind))
cb.append('sa.auto_rebind: ' + s(sa.auto_rebind))
cb.append('psa1.auto_rebind: ' + s(psa1.auto_rebind))
cb.append('psa2.auto_rebind: ' + s(psa2.auto_rebind))
cb.append('psa3.auto_rebind: ' + s(psa3.auto_rebind))
cb.append('psa4.auto_rebind: ' + s(psa4.auto_rebind))
cb.append('psa5.auto_rebind: ' + s(psa5.auto_rebind))
cb.append('psa6.auto_rebind: ' + s(psa6.auto_rebind))
cb.append('psa7.auto_rebind: ' + s(psa7.auto_rebind))
cb.append('psa8.auto_rebind: ' + s(psa8.auto_rebind))
cb.append('psa9.auto_rebind: ' + s(psa9.auto_rebind))
cb.append('psaA.auto_rebind: ' + s(psaA.auto_rebind))
cb.append('psaB.auto_rebind: ' + s(psaB.auto_rebind))
cb.append('psaC.auto_rebind: ' + s(psaC.auto_rebind))

del s

del a
del pa1
del pa2
del pa3
del pa4
del sa
del psa1
del psa2
del psa3
del psa4
del psa5
del psa6
del psa7
del psa8
del psa9
del psaA
del psaB
del psaC
del psar

del ecall
EOF
:"
:" Test stdout/stderr
:redir => messages
:py3 sys.stdout.write('abc8') ; sys.stdout.write('def')
:py3 sys.stderr.write('abc9') ; sys.stderr.write('def')
:py3 sys.stdout.writelines(iter('abcA'))
:py3 sys.stderr.writelines(iter('abcB'))
:redir END
:$put =string(substitute(messages, '\d\+', '', 'g'))
:" Test subclassing
:fun Put(...)
:   $put =string(a:000)
:   return a:000
:endfun
py3 << EOF
class DupDict(vim.Dictionary):
    def __setitem__(self, key, value):
        super(DupDict, self).__setitem__(key, value)
        super(DupDict, self).__setitem__('dup_' + key, value)
dd = DupDict()
dd['a'] = 'b'

class DupList(vim.List):
    def __getitem__(self, idx):
        return [super(DupList, self).__getitem__(idx)] * 2

dl = DupList()
dl2 = DupList(iter('abcC'))
dl.extend(dl2[0])

class DupFun(vim.Function):
    def __call__(self, arg):
        return super(DupFun, self).__call__(arg, arg)

df = DupFun('Put')
EOF
:$put =string(sort(keys(py3eval('dd'))))
:$put =string(py3eval('dl'))
:$put =string(py3eval('dl2'))
:$put =string(py3eval('df(2)'))
:$put =string(py3eval('dl') is# py3eval('dl'))
:$put =string(py3eval('dd') is# py3eval('dd'))
:$put =string(py3eval('df'))
:delfunction Put
py3 << EOF
del DupDict
del DupList
del DupFun
del dd
del dl
del dl2
del df
EOF
:"
:" Test chdir
py3 << EOF
import os
fnamemodify = vim.Function('fnamemodify')
cb.append(str(fnamemodify('.', ':p:h:t')))
cb.append(vim.eval('@%'))
os.chdir('..')
path = fnamemodify('.', ':p:h:t')
if path != b'src':
  # Running tests from a shadow directory, so move up another level
  # This will result in @% looking like shadow/testdir/test87.in, hence the
  # slicing to remove the leading path and path separator
  os.chdir('..')
  cb.append(str(fnamemodify('.', ':p:h:t')))
  cb.append(vim.eval('@%')[len(path)+1:].replace(os.path.sep, '/'))
  os.chdir(path)
else:
  cb.append(str(fnamemodify('.', ':p:h:t')))
  cb.append(vim.eval('@%').replace(os.path.sep, '/'))
del path
os.chdir('testdir')
cb.append(str(fnamemodify('.', ':p:h:t')))
cb.append(vim.eval('@%'))
del fnamemodify
EOF
:"
:" Test errors
:fun F() dict
:endfun
:fun D()
:endfun
py3 << EOF
d = vim.Dictionary()
ned = vim.Dictionary(foo='bar', baz='abcD')
dl = vim.Dictionary(a=1)
dl.locked = True
l = vim.List()
ll = vim.List('abcE')
ll.locked = True
nel = vim.List('abcO')
f = vim.Function('string')
fd = vim.Function('F')
fdel = vim.Function('D')
vim.command('delfunction D')

def subexpr_test(expr, name, subexprs):
    cb.append('>>> Testing %s using %s' % (name, expr))
    for subexpr in subexprs:
        ee(expr % subexpr)
    cb.append('<<< Finished')

def stringtochars_test(expr):
    return subexpr_test(expr, 'StringToChars', (
        '1',       # Fail type checks
        'b"\\0"',  # Fail PyString_AsStringAndSize(object, , NULL) check
        '"\\0"',   # Fail PyString_AsStringAndSize(bytes, , NULL) check
    ))

class Mapping(object):
    def __init__(self, d):
        self.d = d

    def __getitem__(self, key):
        return self.d[key]

    def keys(self):
        return self.d.keys()

    def items(self):
        return self.d.items()

def convertfrompyobject_test(expr, recurse=True):
    # pydict_to_tv
    stringtochars_test(expr % '{%s : 1}')
    if recurse:
        convertfrompyobject_test(expr % '{"abcF" : %s}', False)
    # pymap_to_tv
    stringtochars_test(expr % 'Mapping({%s : 1})')
    if recurse:
        convertfrompyobject_test(expr % 'Mapping({"abcG" : %s})', False)
    # pyseq_to_tv
    iter_test(expr)
    return subexpr_test(expr, 'ConvertFromPyObject', (
        'None',                 # Not conversible
        '{b"": 1}',             # Empty key not allowed
        '{"": 1}',              # Same, but with unicode object
        'FailingMapping()',     #
        'FailingMappingKey()',  #
        'FailingNumber()',      #
    ))

def convertfrompymapping_test(expr):
    convertfrompyobject_test(expr)
    return subexpr_test(expr, 'ConvertFromPyMapping', (
        '[]',
    ))

def iter_test(expr):
    return subexpr_test(expr, '*Iter*', (
        'FailingIter()',
        'FailingIterNext()',
    ))

def number_test(expr, natural=False, unsigned=False):
    if natural:
        unsigned = True
    return subexpr_test(expr, 'NumberToLong', (
        '[]',
        'None',
    ) + (('-1',) if unsigned else ())
    + (('0',) if natural else ()))

class FailingTrue(object):
    def __bool__(self):
        raise NotImplementedError('bool')

class FailingIter(object):
    def __iter__(self):
        raise NotImplementedError('iter')

class FailingIterNext(object):
    def __iter__(self):
        return self

    def __next__(self):
        raise NotImplementedError('next')

class FailingIterNextN(object):
    def __init__(self, n):
        self.n = n

    def __iter__(self):
        return self

    def __next__(self):
        if self.n:
            self.n -= 1
            return 1
        else:
            raise NotImplementedError('next N')

class FailingMappingKey(object):
    def __getitem__(self, item):
        raise NotImplementedError('getitem:mappingkey')

    def keys(self):
        return list("abcH")

class FailingMapping(object):
    def __getitem__(self):
        raise NotImplementedError('getitem:mapping')

    def keys(self):
        raise NotImplementedError('keys')

class FailingList(list):
    def __getitem__(self, idx):
        if i == 2:
            raise NotImplementedError('getitem:list')
        else:
            return super(FailingList, self).__getitem__(idx)

class NoArgsCall(object):
    def __call__(self):
        pass

class FailingCall(object):
    def __call__(self, path):
        raise NotImplementedError('call')

class FailingNumber(object):
    def __int__(self):
        raise NotImplementedError('int')

cb.append("> Output")
cb.append(">> OutputSetattr")
ee('del sys.stdout.softspace')
number_test('sys.stdout.softspace = %s', unsigned=True)
number_test('sys.stderr.softspace = %s', unsigned=True)
ee('assert sys.stdout.isatty()==False')
ee('assert sys.stdout.seekable()==False')
ee('sys.stdout.close()')
ee('sys.stdout.flush()')
ee('assert sys.stderr.isatty()==False')
ee('assert sys.stderr.seekable()==False')
ee('sys.stderr.close()')
ee('sys.stderr.flush()')
ee('sys.stdout.attr = None')
cb.append(">> OutputWrite")
ee('assert sys.stdout.writable()==True')
ee('assert sys.stdout.readable()==False')
ee('assert sys.stderr.writable()==True')
ee('assert sys.stderr.readable()==False')
ee('assert sys.stdout.closed()==False')
ee('assert sys.stderr.closed()==False')
ee('assert sys.stdout.errors=="strict"')
ee('assert sys.stderr.errors=="strict"')
ee('assert sys.stdout.encoding==sys.stderr.encoding')
ee('sys.stdout.write(None)')
cb.append(">> OutputWriteLines")
ee('sys.stdout.writelines(None)')
ee('sys.stdout.writelines([1])')
iter_test('sys.stdout.writelines(%s)')
cb.append("> VimCommand")
stringtochars_test('vim.command(%s)')
ee('vim.command("", 2)')
#! Not checked: vim->python exceptions translating: checked later
cb.append("> VimToPython")
#! Not checked: everything: needs errors in internal python functions
cb.append("> VimEval")
stringtochars_test('vim.eval(%s)')
ee('vim.eval("", FailingTrue())')
#! Not checked: everything: needs errors in internal python functions
cb.append("> VimEvalPy")
stringtochars_test('vim.bindeval(%s)')
ee('vim.eval("", 2)')
#! Not checked: vim->python exceptions translating: checked later
cb.append("> VimStrwidth")
stringtochars_test('vim.strwidth(%s)')
cb.append("> VimForeachRTP")
ee('vim.foreach_rtp(None)')
ee('vim.foreach_rtp(NoArgsCall())')
ee('vim.foreach_rtp(FailingCall())')
ee('vim.foreach_rtp(int, 2)')
cb.append('> import')
old_rtp = vim.options['rtp']
vim.options['rtp'] = os.getcwd().replace('\\', '\\\\').replace(',', '\\,')
ee('import xxx_no_such_module_xxx')
ee('import failing_import')
ee('import failing')
vim.options['rtp'] = old_rtp
del old_rtp
cb.append("> Options")
cb.append(">> OptionsItem")
ee('vim.options["abcQ"]')
ee('vim.options[""]')
stringtochars_test('vim.options[%s]')
cb.append(">> OptionsContains")
stringtochars_test('%s in vim.options')
cb.append("> Dictionary")
cb.append(">> DictionaryConstructor")
ee('vim.Dictionary("abcI")')
##! Not checked: py_dict_alloc failure
cb.append(">> DictionarySetattr")
ee('del d.locked')
ee('d.locked = FailingTrue()')
ee('vim.vvars.locked = False')
ee('d.scope = True')
ee('d.xxx = True')
cb.append(">> _DictionaryItem")
ee('d.get("a", 2, 3)')
stringtochars_test('d.get(%s)')
ee('d.pop("a")')
ee('dl.pop("a")')
cb.append(">> DictionaryContains")
ee('"" in d')
ee('0 in d')
cb.append(">> DictionaryIterNext")
ee('for i in ned: ned["a"] = 1')
del i
cb.append(">> DictionaryAssItem")
ee('dl["b"] = 1')
stringtochars_test('d[%s] = 1')
convertfrompyobject_test('d["a"] = %s')
cb.append(">> DictionaryUpdate")
cb.append(">>> kwargs")
cb.append(">>> iter")
ee('d.update(FailingMapping())')
ee('d.update([FailingIterNext()])')
ee('d.update([FailingIterNextN(1)])')
iter_test('d.update(%s)')
convertfrompyobject_test('d.update(%s)')
stringtochars_test('d.update(((%s, 0),))')
convertfrompyobject_test('d.update((("a", %s),))')
cb.append(">> DictionaryPopItem")
ee('d.popitem(1, 2)')
cb.append(">> DictionaryHasKey")
ee('d.has_key()')
cb.append("> List")
cb.append(">> ListConstructor")
ee('vim.List(1, 2)')
ee('vim.List(a=1)')
iter_test('vim.List(%s)')
convertfrompyobject_test('vim.List([%s])')
cb.append(">> ListItem")
ee('l[1000]')
cb.append(">> ListAssItem")
ee('ll[1] = 2')
ee('l[1000] = 3')
cb.append(">> ListAssSlice")
ee('ll[1:100] = "abcJ"')
iter_test('l[:] = %s')
ee('nel[1:10:2]  = "abcK"')
cb.append(repr(tuple(nel)))
ee('nel[1:10:2]  = "a"')
cb.append(repr(tuple(nel)))
ee('nel[1:1:-1]  = "a"')
cb.append(repr(tuple(nel)))
ee('nel[:] = FailingIterNextN(2)')
cb.append(repr(tuple(nel)))
convertfrompyobject_test('l[:] = [%s]')
cb.append(">> ListConcatInPlace")
iter_test('l.extend(%s)')
convertfrompyobject_test('l.extend([%s])')
cb.append(">> ListSetattr")
ee('del l.locked')
ee('l.locked = FailingTrue()')
ee('l.xxx = True')
cb.append("> Function")
cb.append(">> FunctionConstructor")
cb.append(">>> FunctionConstructor")
ee('vim.Function("123")')
ee('vim.Function("xxx_non_existent_function_xxx")')
ee('vim.Function("xxx#non#existent#function#xxx")')
ee('vim.Function("xxx_non_existent_function_xxx2", args=[])')
ee('vim.Function("xxx_non_existent_function_xxx3", self={})')
ee('vim.Function("xxx_non_existent_function_xxx4", args=[], self={})')
cb.append(">>> FunctionNew")
ee('vim.Function("tr", self="abcFuncSelf")')
ee('vim.Function("tr", args=427423)')
ee('vim.Function("tr", self="abcFuncSelf2", args="abcFuncArgs2")')
ee('vim.Function(self="abcFuncSelf2", args="abcFuncArgs2")')
ee('vim.Function("tr", "", self="abcFuncSelf2", args="abcFuncArgs2")')
ee('vim.Function("tr", "")')
cb.append(">> FunctionCall")
convertfrompyobject_test('f(%s)')
convertfrompymapping_test('fd(self=%s)')
cb.append("> TabPage")
cb.append(">> TabPageAttr")
ee('vim.current.tabpage.xxx')
cb.append("> TabList")
cb.append(">> TabListItem")
ee('vim.tabpages[1000]')
cb.append("> Window")
cb.append(">> WindowAttr")
ee('vim.current.window.xxx')
cb.append(">> WindowSetattr")
ee('vim.current.window.buffer = 0')
ee('vim.current.window.cursor = (100000000, 100000000)')
ee('vim.current.window.cursor = True')
number_test('vim.current.window.height = %s', unsigned=True)
number_test('vim.current.window.width = %s', unsigned=True)
ee('vim.current.window.xxxxxx = True')
cb.append("> WinList")
cb.append(">> WinListItem")
ee('vim.windows[1000]')
cb.append("> Buffer")
cb.append(">> StringToLine (indirect)")
ee('vim.current.buffer[0] = "\\na"')
ee('vim.current.buffer[0] = b"\\na"')
cb.append(">> SetBufferLine (indirect)")
ee('vim.current.buffer[0] = True')
cb.append(">> SetBufferLineList (indirect)")
ee('vim.current.buffer[:] = True')
ee('vim.current.buffer[:] = ["\\na", "bc"]')
cb.append(">> InsertBufferLines (indirect)")
ee('vim.current.buffer.append(None)')
ee('vim.current.buffer.append(["\\na", "bc"])')
ee('vim.current.buffer.append("\\nbc")')
cb.append(">> RBItem")
ee('vim.current.buffer[100000000]')
cb.append(">> RBAsItem")
ee('vim.current.buffer[100000000] = ""')
cb.append(">> BufferAttr")
ee('vim.current.buffer.xxx')
cb.append(">> BufferSetattr")
ee('vim.current.buffer.name = True')
ee('vim.current.buffer.xxx = True')
cb.append(">> BufferMark")
ee('vim.current.buffer.mark(0)')
ee('vim.current.buffer.mark("abcM")')
ee('vim.current.buffer.mark("!")')
cb.append(">> BufferRange")
ee('vim.current.buffer.range(1, 2, 3)')
cb.append("> BufMap")
cb.append(">> BufMapItem")
ee('vim.buffers[100000000]')
number_test('vim.buffers[%s]', natural=True)
cb.append("> Current")
cb.append(">> CurrentGetattr")
ee('vim.current.xxx')
cb.append(">> CurrentSetattr")
ee('vim.current.line = True')
ee('vim.current.buffer = True')
ee('vim.current.window = True')
ee('vim.current.tabpage = True')
ee('vim.current.xxx = True')
del d
del ned
del dl
del l
del ll
del nel
del f
del fd
del fdel
del subexpr_test
del stringtochars_test
del Mapping
del convertfrompyobject_test
del convertfrompymapping_test
del iter_test
del number_test
del FailingTrue
del FailingIter
del FailingIterNext
del FailingIterNextN
del FailingMapping
del FailingMappingKey
del FailingList
del NoArgsCall
del FailingCall
del FailingNumber
EOF
:delfunction F
:"
:" Test import
py3 << EOF
sys.path.insert(0, os.path.join(os.getcwd(), 'python_before'))
sys.path.append(os.path.join(os.getcwd(), 'python_after'))
vim.options['rtp'] = os.getcwd().replace(',', '\\,').replace('\\', '\\\\')
l = []
def callback(path):
    l.append(os.path.relpath(path))
vim.foreach_rtp(callback)
cb.append(repr(l))
del l
def callback(path):
    return os.path.relpath(path)
cb.append(repr(vim.foreach_rtp(callback)))
del callback
from module import dir as d
from modulex import ddir
cb.append(d + ',' + ddir)
import before
cb.append(before.dir)
import after
cb.append(after.dir)
import topmodule as tm
import topmodule.submodule as tms
import topmodule.submodule.subsubmodule.subsubsubmodule as tmsss
cb.append(tm.__file__.replace(os.path.sep, '/')[-len('modulex/topmodule/__init__.py'):])
cb.append(tms.__file__.replace(os.path.sep, '/')[-len('modulex/topmodule/submodule/__init__.py'):])
cb.append(tmsss.__file__.replace(os.path.sep, '/')[-len('modulex/topmodule/submodule/subsubmodule/subsubsubmodule.py'):])
del before
del after
del d
del ddir
del tm
del tms
del tmsss
EOF
:"
:" Test exceptions
:fun Exe(e)
:   execute a:e
:endfun
py3 << EOF
Exe = vim.bindeval('function("Exe")')
ee('vim.command("throw \'abcN\'")')
ee('Exe("throw \'def\'")')
ee('vim.eval("Exe(\'throw \'\'ghi\'\'\')")')
ee('vim.eval("Exe(\'echoerr \'\'jkl\'\'\')")')
ee('vim.eval("Exe(\'xxx_non_existent_command_xxx\')")')
ee('vim.eval("xxx_unknown_function_xxx()")')
ee('vim.bindeval("Exe(\'xxx_non_existent_command_xxx\')")')
del Exe
EOF
:delfunction Exe
:"
:" Regression: interrupting vim.command propagates to next vim.command
py3 << EOF
def test_keyboard_interrupt():
    try:
        vim.command('while 1 | endwhile')
    except KeyboardInterrupt:
        cb.append('Caught KeyboardInterrupt')
    except Exception:
        cb.append('!!!!!!!! Caught exception: ' + emsg(sys.exc_info()))
    else:
        cb.append('!!!!!!!! No exception')
    try:
        vim.command('$ put =\'Running :put\'')
    except KeyboardInterrupt:
        cb.append('!!!!!!!! Caught KeyboardInterrupt')
    except Exception:
        cb.append('!!!!!!!! Caught exception: ' + emsg(sys.exc_info()))
    else:
        cb.append('No exception')
EOF
:debuggreedy
:call inputsave()
:call feedkeys("s\ns\ns\ns\nq\n")
:redir => output
:debug silent! py3 test_keyboard_interrupt()
:redir END
:0 debuggreedy
:call inputrestore()
:silent $put =output
:unlet output
:py3 del test_keyboard_interrupt
:"
:" Cleanup
py3 << EOF
del cb
del ee
del emsg
del sys
del os
del vim
EOF
:endfun
:"
:fun RunTest()
:let checkrefs = !empty($PYTHONDUMPREFS)
:let start = getline(1, '$')
:for i in range(checkrefs ? 10 : 1)
:   if i != 0
:       %d _
:       call setline(1, start)
:   endif
:   call Test()
:   if i == 0
:       let result = getline(1, '$')
:   endif
:endfor
:if checkrefs
:   %d _
:   call setline(1, result)
:endif
:endfun
:"
:call RunTest()
:delfunction RunTest
:delfunction Test
:call garbagecollect(1)
:"
:/^start:/,$wq! test.out
:/^start:/,$w! test.out
:" vim: et ts=4 isk-=\:
:while getchar(0) isnot 0|endwhile
ENDTEST

start: