File: test_debugger.py

package info (click to toggle)
pydevd 3.3.0%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,892 kB
  • sloc: python: 77,508; cpp: 1,869; sh: 368; makefile: 50; ansic: 4
file content (4697 lines) | stat: -rw-r--r-- 185,350 bytes parent folder | download | duplicates (2)
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
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
# coding: utf-8
"""
The idea is that we record the commands sent to the debugger and reproduce them from this script
(so, this works as the client, which spawns the debugger as a separate process and communicates
to it as if it was run from the outside)

Note that it's a python script but it'll spawn a process to run as jython, ironpython and as python.
"""

import time

import pytest

from tests_python import debugger_unittest
from tests_python.debugger_unittest import (
    CMD_SET_PROPERTY_TRACE,
    REASON_CAUGHT_EXCEPTION,
    REASON_UNCAUGHT_EXCEPTION,
    REASON_STOP_ON_BREAKPOINT,
    REASON_THREAD_SUSPEND,
    overrides,
    CMD_THREAD_CREATE,
    CMD_GET_THREAD_STACK,
    REASON_STEP_INTO_MY_CODE,
    CMD_GET_EXCEPTION_DETAILS,
    IS_IRONPYTHON,
    IS_JYTHON,
    IS_CPYTHON,
    IS_APPVEYOR,
    wait_for_condition,
    CMD_GET_FRAME,
    CMD_GET_BREAKPOINT_EXCEPTION,
    CMD_THREAD_SUSPEND,
    CMD_STEP_OVER,
    REASON_STEP_OVER,
    CMD_THREAD_SUSPEND_SINGLE_NOTIFICATION,
    CMD_THREAD_RESUME_SINGLE_NOTIFICATION,
    REASON_STEP_RETURN,
    REASON_STEP_RETURN_MY_CODE,
    REASON_STEP_OVER_MY_CODE,
    REASON_STEP_INTO,
    CMD_THREAD_KILL,
    IS_PYPY,
    REASON_STOP_ON_START,
    CMD_SMART_STEP_INTO,
    CMD_GET_VARIABLE,
)
from _pydevd_bundle.pydevd_constants import (
    IS_WINDOWS,
    IS_PY38_OR_GREATER,
    IS_MAC,
    PYDEVD_USE_SYS_MONITORING,
    SUPPORT_ATTACH_TO_PID,
    IS_PY312_OR_GREATER,
)
from _pydevd_bundle.pydevd_comm_constants import CMD_RELOAD_CODE, CMD_INPUT_REQUESTED, CMD_RUN_CUSTOM_OPERATION
import json
import pydevd_file_utils
import subprocess
import threading
from _pydev_bundle import pydev_log
from urllib.parse import unquote, unquote_plus

from tests_python.debug_constants import *  # noqa
import sys

pytest_plugins = [
    str("tests_python.debugger_fixtures"),
]

builtin_qualifier = "builtins"


@pytest.mark.skipif(not IS_CPYTHON, reason="Test needs gc.get_referrers/reference counting to really check anything.")
def test_case_referrers(case_setup):
    with case_setup.test_file("_debugger_case1.py") as writer:
        writer.log.append("writing add breakpoint")
        writer.write_add_breakpoint(6, "set_up")

        writer.log.append("making initial run")
        writer.write_make_initial_run()

        writer.log.append("waiting for breakpoint hit")
        hit = writer.wait_for_breakpoint_hit()
        thread_id = hit.thread_id
        frame_id = hit.frame_id

        writer.log.append("get frame")
        writer.write_get_frame(thread_id, frame_id)

        writer.log.append("step over")
        writer.write_step_over(thread_id)
        hit = writer.wait_for_breakpoint_hit(108)
        thread_id = hit.thread_id
        frame_id = hit.frame_id

        writer.log.append("get frame")
        writer.write_get_frame(thread_id, frame_id)

        writer.log.append("run thread")
        writer.write_run_thread(thread_id)

        writer.log.append("asserting")
        try:
            assert 13 == writer._sequence, "Expected 13. Had: %s" % writer._sequence
        except:
            writer.log.append("assert failed!")
            raise
        writer.log.append("asserted")

        writer.finished_ok = True


def test_case_2(case_setup):
    with case_setup.test_file("_debugger_case2.py") as writer:
        writer.write_add_breakpoint(3, "Call4")  # seq = 3
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()
        thread_id = hit.thread_id
        frame_id = hit.frame_id

        writer.write_get_frame(thread_id, frame_id)  # Note: write get frame but not waiting for it to be gotten.

        writer.write_add_breakpoint(14, "Call2")

        writer.write_run_thread(thread_id)

        hit = writer.wait_for_breakpoint_hit()
        thread_id = hit.thread_id
        frame_id = hit.frame_id

        writer.write_get_frame(thread_id, frame_id)  # Note: write get frame but not waiting for it to be gotten.

        writer.write_run_thread(thread_id)

        writer.log.append("Checking sequence. Found: %s" % (writer._sequence))
        assert 15 == writer._sequence, "Expected 15. Had: %s" % writer._sequence

        writer.log.append("Marking finished ok.")
        writer.finished_ok = True


@pytest.mark.parametrize(
    "skip_suspend_on_breakpoint_exception, skip_print_breakpoint_exception",
    (
        [["NameError"], []],
        [["NameError"], ["NameError"]],
        [[], []],  # Empty means it'll suspend/print in any exception
        [[], ["NameError"]],
        [["ValueError"], ["Exception"]],
        [["Exception"], ["ValueError"]],  # ValueError will also suspend/print since we're dealing with a NameError
    ),
)
def test_case_breakpoint_condition_exc(case_setup, skip_suspend_on_breakpoint_exception, skip_print_breakpoint_exception):
    msgs_in_stderr = (
        "Error while evaluating expression in conditional breakpoint: i > 5",
        "Traceback (most recent call last):",
        'File "<string>", line 1, in <module>',
    )

    # It could be one or the other in PyPy depending on the version.
    msgs_one_in_stderr = (
        "NameError: name 'i' is not defined",
        "global name 'i' is not defined",
    )

    def _ignore_stderr_line(line):
        if original_ignore_stderr_line(line):
            return True

        for msg in msgs_in_stderr + msgs_one_in_stderr:
            if msg in line:
                return True

        return False

    with case_setup.test_file("_debugger_case_breakpoint_condition_exc.py") as writer:
        original_ignore_stderr_line = writer._ignore_stderr_line
        writer._ignore_stderr_line = _ignore_stderr_line

        writer.write_suspend_on_breakpoint_exception(skip_suspend_on_breakpoint_exception, skip_print_breakpoint_exception)

        expect_print = skip_print_breakpoint_exception in ([], ["ValueError"])
        expect_suspend = skip_suspend_on_breakpoint_exception in ([], ["ValueError"])

        breakpoint_id = writer.write_add_breakpoint(writer.get_line_index_with_content("break here"), "Call", condition="i > 5")

        if not expect_print:
            _original = writer.reader_thread.on_message_found

            def on_message_found(found_msg):
                for msg in msgs_in_stderr + msgs_one_in_stderr:
                    assert msg not in found_msg

            writer.reader_thread.on_message_found = on_message_found

        writer.write_make_initial_run()

        def check_error_msg(stderr):
            for msg in msgs_in_stderr:
                assert msg in stderr

            for msg in msgs_one_in_stderr:
                if msg in stderr:
                    break
            else:
                raise AssertionError("Did not find any of: %s in stderr: %s" % (msgs_one_in_stderr, stderr))

        if expect_print:
            msg, ctx = writer.wait_for_output()
            check_error_msg(msg.replace("&gt;", ">"))

        def wait_for_suspend():
            def accept_message(msg):
                if msg.startswith("%s\t" % CMD_GET_BREAKPOINT_EXCEPTION) or ('stop_reason="%s"' % REASON_STOP_ON_BREAKPOINT) in msg:
                    return True
                return False

            # Order is not guaranteed.
            msg1 = writer.wait_for_message(accept_message)
            msg2 = writer.wait_for_message(accept_message)
            try:
                hitmsg = msg1
                msg1.thread["stop_reason"]
            except:
                hitmsg = msg2
            hit = writer._get_stack_as_hit(hitmsg)
            writer.write_run_thread(hit.thread_id)

        if expect_suspend:
            wait_for_suspend()

        if IS_JYTHON:
            # Jython will break twice.
            if expect_suspend:
                wait_for_suspend()

        hit = writer.wait_for_breakpoint_hit()
        thread_id = hit.thread_id
        frame_id = hit.frame_id

        writer.write_get_frame(thread_id, frame_id)
        msg = writer.wait_for_message(CMD_GET_FRAME)
        name_to_value = {}
        for var in msg.var:
            name_to_value[var["name"]] = var["value"]
        assert name_to_value == {"i": "int: 6", "last_i": "int: 6"}

        writer.write_remove_breakpoint(breakpoint_id)

        writer.write_run_thread(thread_id)

        writer.finished_ok = True


def test_case_remove_breakpoint(case_setup):
    with case_setup.test_file("_debugger_case_remove_breakpoint.py") as writer:
        breakpoint_id = writer.write_add_breakpoint(writer.get_line_index_with_content("break here"))
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()
        writer.write_remove_breakpoint(breakpoint_id)
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_case_double_remove_breakpoint(case_setup):
    with case_setup.test_file("_debugger_case_remove_breakpoint.py") as writer:
        breakpoint_id = writer.write_add_breakpoint(writer.get_line_index_with_content("break here"))
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()
        writer.write_remove_breakpoint(breakpoint_id)
        writer.write_remove_breakpoint(breakpoint_id)  # Double-remove (just check that we don't have an error).
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(IS_IRONPYTHON, reason="This test fails once in a while due to timing issues on IronPython, so, skipping it.")
def test_case_3(case_setup):
    with case_setup.test_file("_debugger_case3.py") as writer:
        writer.write_make_initial_run()
        time.sleep(0.5)
        breakpoint_id = writer.write_add_breakpoint(4, "")

        hit = writer.wait_for_breakpoint_hit()
        thread_id = hit.thread_id
        frame_id = hit.frame_id

        writer.write_get_frame(thread_id, frame_id)

        writer.write_run_thread(thread_id)

        hit = writer.wait_for_breakpoint_hit()
        thread_id = hit.thread_id
        frame_id = hit.frame_id

        writer.write_get_frame(thread_id, frame_id)

        writer.write_remove_breakpoint(breakpoint_id)

        writer.write_run_thread(thread_id)

        writer.finished_ok = True


def test_case_suspend_thread(case_setup):
    with case_setup.test_file("_debugger_case4.py") as writer:
        writer.write_make_initial_run()

        thread_id = writer.wait_for_new_thread()

        writer.write_suspend_thread(thread_id)

        while True:
            hit = writer.wait_for_breakpoint_hit((REASON_THREAD_SUSPEND, REASON_STOP_ON_BREAKPOINT))
            if hit.name == "sleep":
                break  # Ok, broke on 'sleep'.
            else:
                # i.e.: if it doesn't hit on 'sleep', release and pause again.
                writer.write_run_thread(thread_id)
                time.sleep(0.1)
                writer.write_suspend_thread(thread_id)

        assert hit.thread_id == thread_id

        writer.write_evaluate_expression("%s\t%s\t%s" % (hit.thread_id, hit.frame_id, "LOCAL"), "exit_while_loop()")
        writer.wait_for_evaluation(
            [
                [
                    '<var name="exit_while_loop()" type="str" qualifier="{0}" value="str: ok'.format(builtin_qualifier),
                    '<var name="exit_while_loop()" type="str"  value="str: ok"',  # jython
                ]
            ]
        )

        writer.write_run_thread(thread_id)

        writer.finished_ok = True


# Jython has a weird behavior: it seems it has fine-grained locking so that when
# we're inside the tracing other threads don't run (so, we can have only one
# thread paused in the debugger).
@pytest.mark.skipif(IS_JYTHON, reason="Jython can only have one thread stopped at each time.")
def test_case_suspend_all_thread(case_setup):
    with case_setup.test_file("_debugger_case_suspend_all.py") as writer:
        writer.write_make_initial_run()

        main_thread_id = writer.wait_for_new_thread()  # Main thread
        thread_id1 = writer.wait_for_new_thread()  # Thread 1
        thread_id2 = writer.wait_for_new_thread()  # Thread 2

        # Ok, all threads created, let's wait for the main thread to get to the join.
        writer.wait_for_thread_join(main_thread_id)

        writer.write_suspend_thread("*")

        # Wait for 2 threads to be suspended (the main thread is already in a join, so, it can't actually
        # break out of it while others don't proceed).
        hit0 = writer.wait_for_breakpoint_hit(REASON_THREAD_SUSPEND)
        hit1 = writer.wait_for_breakpoint_hit(REASON_THREAD_SUSPEND)

        writer.write_evaluate_expression("%s\t%s\t%s" % (hit0.thread_id, hit0.frame_id, "LOCAL"), "exit_while_loop(1)")
        writer.wait_for_evaluation([['<var name="exit_while_loop(1)" type="str" qualifier="{0}" value="str: ok'.format(builtin_qualifier)]])

        writer.write_evaluate_expression("%s\t%s\t%s" % (hit1.thread_id, hit1.frame_id, "LOCAL"), "exit_while_loop(2)")
        writer.wait_for_evaluation('<var name="exit_while_loop(2)" type="str" qualifier="{0}" value="str: ok'.format(builtin_qualifier))

        writer.write_run_thread("*")

        writer.finished_ok = True


def test_case_5(case_setup):
    with case_setup.test_file("_debugger_case56.py") as writer:
        breakpoint_id = writer.write_add_breakpoint(2, "Call2")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()
        thread_id = hit.thread_id
        frame_id = hit.frame_id

        writer.write_get_frame(thread_id, frame_id)

        writer.write_remove_breakpoint(breakpoint_id)

        writer.write_step_return(thread_id)

        hit = writer.wait_for_breakpoint_hit("109")
        thread_id = hit.thread_id
        frame_id = hit.frame_id
        line = hit.line

        assert line == 8, "Expecting it to go to line 8. Went to: %s" % line

        writer.write_step_in(thread_id)

        hit = writer.wait_for_breakpoint_hit("107")
        thread_id = hit.thread_id
        frame_id = hit.frame_id
        line = hit.line

        # goes to line 4 in jython (function declaration line)
        assert line in (4, 5), "Expecting it to go to line 4 or 5. Went to: %s" % line

        writer.write_run_thread(thread_id)

        assert 15 == writer._sequence, "Expected 15. Had: %s" % writer._sequence

        writer.finished_ok = True


def test_case_6(case_setup):
    with case_setup.test_file("_debugger_case56.py") as writer:
        writer.write_add_breakpoint(2, "Call2")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()
        thread_id = hit.thread_id
        frame_id = hit.frame_id

        writer.write_get_frame(thread_id, frame_id)

        writer.write_step_return(thread_id)

        hit = writer.wait_for_breakpoint_hit("109")
        thread_id = hit.thread_id
        frame_id = hit.frame_id
        line = hit.line

        assert line == 8, "Expecting it to go to line 8. Went to: %s" % line

        writer.write_step_in(thread_id)

        hit = writer.wait_for_breakpoint_hit("107")
        thread_id = hit.thread_id
        frame_id = hit.frame_id
        line = hit.line

        # goes to line 4 in jython (function declaration line)
        assert line in (4, 5), "Expecting it to go to line 4 or 5. Went to: %s" % line

        writer.write_run_thread(thread_id)

        assert 13 == writer._sequence, "Expected 15. Had: %s" % writer._sequence

        writer.finished_ok = True


@pytest.mark.skipif(IS_IRONPYTHON, reason="This test is flaky on Jython, so, skipping it.")
def test_case_7(case_setup):
    # This test checks that we start without variables and at each step a new var is created, but on ironpython,
    # the variables exist all at once (with None values), so, we can't test it properly.
    with case_setup.test_file("_debugger_case_local_variables.py") as writer:
        writer.write_add_breakpoint(writer.get_line_index_with_content("Break here"), "Call")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit("111")

        writer.write_get_frame(hit.thread_id, hit.frame_id)

        writer.wait_for_vars("<xml></xml>")  # no vars at this point

        writer.write_step_over(hit.thread_id)

        writer.wait_for_breakpoint_hit("108")

        writer.write_get_frame(hit.thread_id, hit.frame_id)

        writer.wait_for_vars(
            [
                [
                    '<xml><var name="variable_for_test_1" type="int" qualifier="{0}" value="int%253A 10" />%0A</xml>'.format(
                        builtin_qualifier
                    ),
                    '<var name="variable_for_test_1" type="int"  value="int',  # jython
                ]
            ]
        )

        writer.write_step_over(hit.thread_id)

        writer.wait_for_breakpoint_hit("108")

        writer.write_get_frame(hit.thread_id, hit.frame_id)

        writer.wait_for_vars(
            [
                [
                    '<xml><var name="variable_for_test_1" type="int" qualifier="{0}" value="int%253A 10" />%0A<var name="variable_for_test_2" type="int" qualifier="{0}" value="int%253A 20" />%0A</xml>'.format(
                        builtin_qualifier
                    ),
                    '<var name="variable_for_test_1" type="int"  value="int%253A 10" />%0A<var name="variable_for_test_2" type="int"  value="int%253A 20" />%0A',  # jython
                ]
            ]
        )

        writer.write_run_thread(hit.thread_id)

        assert 17 == writer._sequence, "Expected 17. Had: %s" % writer._sequence

        writer.finished_ok = True


def test_case_8(case_setup):
    with case_setup.test_file("_debugger_case89.py") as writer:
        writer.write_add_breakpoint(10, "Method3")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit("111")

        writer.write_step_return(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit("109", line=15)

        writer.write_run_thread(hit.thread_id)

        assert 9 == writer._sequence, "Expected 9. Had: %s" % writer._sequence

        writer.finished_ok = True


def test_case_9(case_setup):
    with case_setup.test_file("_debugger_case89.py") as writer:
        writer.write_add_breakpoint(10, "Method3")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit("111")

        # Note: no active exception (should not give an error and should return no
        # exception details as there's no exception).
        writer.write_get_current_exception(hit.thread_id)

        msg = writer.wait_for_message(CMD_GET_EXCEPTION_DETAILS)
        assert msg.thread["id"] == hit.thread_id
        assert not hasattr(msg.thread, "frames")  # No frames should be found.

        writer.write_step_over(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit("108", line=11)

        writer.write_step_over(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit("108", line=12)

        writer.write_run_thread(hit.thread_id)

        assert 13 == writer._sequence, "Expected 13. Had: %s" % writer._sequence

        writer.finished_ok = True


def test_case_10(case_setup):
    with case_setup.test_file("_debugger_case_simple_calls.py") as writer:
        writer.write_add_breakpoint(2, "None")  # None or Method should make hit.
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit("111")

        writer.write_step_return(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit("109", line=11)

        writer.write_step_over(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit("108", line=12)

        writer.write_run_thread(hit.thread_id)

        assert 11 == writer._sequence, "Expected 11. Had: %s" % writer._sequence

        writer.finished_ok = True


def test_case_11(case_setup):
    with case_setup.test_file("_debugger_case_simple_calls.py") as writer:
        writer.write_add_breakpoint(2, "Method1")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT, line=2)
        assert hit.name == "Method1"

        writer.write_step_over(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(REASON_STEP_OVER, line=3)
        assert hit.name == "Method1"

        writer.write_step_over(hit.thread_id)

        if PYDEVD_USE_SYS_MONITORING:
            hit = writer.wait_for_breakpoint_hit(REASON_STEP_OVER, line=11)  # Reverts to step return
            assert hit.name == "Method2"

            writer.write_step_over(hit.thread_id)
            hit = writer.wait_for_breakpoint_hit(REASON_STEP_OVER, line=12)
            assert hit.name == "Method2"
        else:
            hit = writer.wait_for_breakpoint_hit(REASON_STEP_OVER, line=12)  # Reverts to step in
            assert hit.name == "Method2"

        writer.write_step_over(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(REASON_STEP_OVER, line=13)
        assert hit.name == "Method2"

        writer.write_step_over(hit.thread_id)

        if PYDEVD_USE_SYS_MONITORING:
            hit = writer.wait_for_breakpoint_hit(REASON_STEP_OVER, line=17)  # Reverts to step return
            assert hit.name == "<module>"

            writer.write_step_over(hit.thread_id)
            hit = writer.wait_for_breakpoint_hit(REASON_STEP_OVER, line=18)
            assert hit.name == "<module>"
        else:
            hit = writer.wait_for_breakpoint_hit(REASON_STEP_OVER, line=18)  # Reverts to step in
            assert hit.name == "<module>"

        # Finish with a step over
        writer.write_step_over(hit.thread_id)

        if IS_JYTHON:
            writer.write_run_thread(hit.thread_id)
        else:
            # Finish with a step over
            writer.write_step_over(hit.thread_id)

        writer.finished_ok = True


def test_case_12(case_setup):
    # Note: In CPython we now ignore the function names, so, we'll stop at the breakpoint in line 2
    # regardless of the function name (we decide whether to stop in a line or not through the function
    # lines).
    with case_setup.test_file("_debugger_case_simple_calls.py") as writer:
        writer.write_add_breakpoint(2, "")  # Should not be hit: setting empty function (not None) should only hit global.
        writer.write_add_breakpoint(6, "Method1a")
        writer.write_add_breakpoint(11, "Method2")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit("111", line=11)

        writer.write_step_return(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit("111", line=6 if IS_JYTHON else 2)  # not a return (it stopped in the other breakpoint)

        writer.write_run_thread(hit.thread_id)

        if not IS_JYTHON:
            hit = writer.wait_for_breakpoint_hit("111", line=6)

            writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(IS_IRONPYTHON, reason="Failing on IronPython (needs to be investigated).")
def test_case_property_trace_enable_disable(case_setup):
    with case_setup.test_file("_debugger_case13.py") as writer:

        def _ignore_stderr_line(line):
            if original_ignore_stderr_line(line):
                return True

            if IS_JYTHON:
                for expected in (
                    "RuntimeWarning: Parent module '_pydevd_bundle' not found while handling absolute import",
                    "import __builtin__",
                ):
                    if expected in line:
                        return True

            return False

        original_ignore_stderr_line = writer._ignore_stderr_line
        writer._ignore_stderr_line = _ignore_stderr_line

        writer.write_add_breakpoint(35, "main")
        writer.write("%s\t%s\t%s" % (CMD_SET_PROPERTY_TRACE, writer.next_seq(), "true;false;false;true"))
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit("111")

        writer.write_get_frame(hit.thread_id, hit.frame_id)

        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit("107", line=25)
        # Should go inside setter method

        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit("107", line=36)

        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit("107", line=21)
        # Should go inside getter method

        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit("107")

        # Disable property tracing
        writer.write("%s\t%s\t%s" % (CMD_SET_PROPERTY_TRACE, writer.next_seq(), "true;true;true;true"))
        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit("107", line=39)
        # Should Skip step into properties setter

        # Enable property tracing
        writer.write("%s\t%s\t%s" % (CMD_SET_PROPERTY_TRACE, writer.next_seq(), "true;false;false;true"))
        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit("107", line=8)
        # Should go inside getter method

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_case_14(case_setup):
    # Interactive Debug Console
    with case_setup.test_file("_debugger_case14.py") as writer:
        writer.write_add_breakpoint(22, "main")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit("111")
        assert hit.thread_id, "%s not valid." % hit.thread_id
        assert hit.frame_id, "%s not valid." % hit.frame_id

        # Access some variable
        writer.write_debug_console_expression("%s\t%s\tEVALUATE\tcarObj.color" % (hit.thread_id, hit.frame_id))
        writer.wait_for_var(["<more>False</more>", "%27Black%27"])
        assert 7 == writer._sequence, "Expected 9. Had: %s" % writer._sequence

        # Change some variable
        writer.write_debug_console_expression("%s\t%s\tEVALUATE\tcarObj.color='Red'" % (hit.thread_id, hit.frame_id))
        writer.write_debug_console_expression("%s\t%s\tEVALUATE\tcarObj.color" % (hit.thread_id, hit.frame_id))
        writer.wait_for_var(["<more>False</more>", "%27Red%27"])
        assert 11 == writer._sequence, "Expected 13. Had: %s" % writer._sequence

        # Iterate some loop
        writer.write_debug_console_expression("%s\t%s\tEVALUATE\tfor i in range(3):" % (hit.thread_id, hit.frame_id))
        writer.wait_for_var(["<xml><more>True</more></xml>"])
        writer.write_debug_console_expression("%s\t%s\tEVALUATE\t    print(i)" % (hit.thread_id, hit.frame_id))
        writer.wait_for_var(["<xml><more>True</more></xml>"])
        writer.write_debug_console_expression("%s\t%s\tEVALUATE\t" % (hit.thread_id, hit.frame_id))
        writer.wait_for_var(
            ['<xml><more>False</more><output message="0"></output><output message="1"></output><output message="2"></output></xml>']
        )
        assert 17 == writer._sequence, "Expected 19. Had: %s" % writer._sequence

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_case_15(case_setup):
    with case_setup.test_file("_debugger_case15.py") as writer:
        writer.write_add_breakpoint(22, "main")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT)

        # Access some variable
        writer.write_custom_operation(
            "%s\t%s\tEXPRESSION\tcarObj.color" % (hit.thread_id, hit.frame_id), "EXEC", "f=lambda x: 'val=%s' % x", "f"
        )
        writer.wait_for_custom_operation("val=Black")
        assert 7 == writer._sequence, "Expected 7. Had: %s" % writer._sequence

        writer.write_custom_operation(
            "%s\t%s\tEXPRESSION\tcarObj.color" % (hit.thread_id, hit.frame_id),
            "EXECFILE",
            debugger_unittest._get_debugger_test_file("_debugger_case15_execfile.py"),
            "f",
        )
        writer.wait_for_custom_operation("val=Black")
        assert 9 == writer._sequence, "Expected 9. Had: %s" % writer._sequence

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_case_16_resolve_numpy_array(case_setup):
    # numpy.ndarray resolver
    try:
        import numpy
    except ImportError:
        pytest.skip("numpy not available")
    with case_setup.test_file("_debugger_case16.py") as writer:
        writer.write_add_breakpoint(9, "main")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT)

        # In this test we check that the three arrays of different shapes, sizes and types
        # are all resolved properly as ndarrays.

        # First pass check is that we have all three expected variables defined
        writer.write_get_frame(hit.thread_id, hit.frame_id)
        writer.wait_for_multiple_vars(
            (
                (
                    '<var name="smallarray" type="ndarray" qualifier="numpy" value="ndarray%253A %255B 0.%252B1.j  1.%252B1.j  2.%252B1.j  3.%252B1.j  4.%252B1.j  5.%252B1.j  6.%252B1.j  7.%252B1.j  8.%252B1.j%250A  9.%252B1.j 10.%252B1.j 11.%252B1.j 12.%252B1.j 13.%252B1.j 14.%252B1.j 15.%252B1.j 16.%252B1.j 17.%252B1.j%250A 18.%252B1.j 19.%252B1.j 20.%252B1.j 21.%252B1.j 22.%252B1.j 23.%252B1.j 24.%252B1.j 25.%252B1.j 26.%252B1.j%250A 27.%252B1.j 28.%252B1.j 29.%252B1.j 30.%252B1.j 31.%252B1.j 32.%252B1.j 33.%252B1.j 34.%252B1.j 35.%252B1.j%250A 36.%252B1.j 37.%252B1.j 38.%252B1.j 39.%252B1.j 40.%252B1.j 41.%252B1.j 42.%252B1.j 43.%252B1.j 44.%252B1.j%250A 45.%252B1.j 46.%252B1.j 47.%252B1.j 48.%252B1.j 49.%252B1.j 50.%252B1.j 51.%252B1.j 52.%252B1.j 53.%252B1.j%250A 54.%252B1.j 55.%252B1.j 56.%252B1.j 57.%252B1.j 58.%252B1.j 59.%252B1.j 60.%252B1.j 61.%252B1.j 62.%252B1.j%250A 63.%252B1.j 64.%252B1.j 65.%252B1.j 66.%252B1.j 67.%252B1.j 68.%252B1.j 69.%252B1.j 70.%252B1.j 71.%252B1.j%250A 72.%252B1.j 73.%252B1.j 74.%252B1.j 75.%252B1.j 76.%252B1.j 77.%252B1.j 78.%252B1.j 79.%252B1.j 80.%252B1.j%250A 81.%252B1.j 82.%252B1.j 83.%252B1.j 84.%252B1.j 85.%252B1.j 86.%252B1.j 87.%252B1.j 88.%252B1.j 89.%252B1.j%250A 90.%252B1.j 91.%252B1.j 92.%252B1.j 93.%252B1.j 94.%252B1.j 95.%252B1.j 96.%252B1.j 97.%252B1.j 98.%252B1.j%250A 99.%252B1.j%255D" isContainer="True" />',
                    '<var name="smallarray" type="ndarray" qualifier="numpy" value="ndarray%253A %255B  0.%252B1.j   1.%252B1.j   2.%252B1.j   3.%252B1.j   4.%252B1.j   5.%252B1.j   6.%252B1.j   7.%252B1.j%250A   8.%252B1.j   9.%252B1.j  10.%252B1.j  11.%252B1.j  12.%252B1.j  13.%252B1.j  14.%252B1.j  15.%252B1.j%250A  16.%252B1.j  17.%252B1.j  18.%252B1.j  19.%252B1.j  20.%252B1.j  21.%252B1.j  22.%252B1.j  23.%252B1.j%250A  24.%252B1.j  25.%252B1.j  26.%252B1.j  27.%252B1.j  28.%252B1.j  29.%252B1.j  30.%252B1.j  31.%252B1.j%250A  32.%252B1.j  33.%252B1.j  34.%252B1.j  35.%252B1.j  36.%252B1.j  37.%252B1.j  38.%252B1.j  39.%252B1.j%250A  40.%252B1.j  41.%252B1.j  42.%252B1.j  43.%252B1.j  44.%252B1.j  45.%252B1.j  46.%252B1.j  47.%252B1.j%250A  48.%252B1.j  49.%252B1.j  50.%252B1.j  51.%252B1.j  52.%252B1.j  53.%252B1.j  54.%252B1.j  55.%252B1.j%250A  56.%252B1.j  57.%252B1.j  58.%252B1.j  59.%252B1.j  60.%252B1.j  61.%252B1.j  62.%252B1.j  63.%252B1.j%250A  64.%252B1.j  65.%252B1.j  66.%252B1.j  67.%252B1.j  68.%252B1.j  69.%252B1.j  70.%252B1.j  71.%252B1.j%250A  72.%252B1.j  73.%252B1.j  74.%252B1.j  75.%252B1.j  76.%252B1.j  77.%252B1.j  78.%252B1.j  79.%252B1.j%250A  80.%252B1.j  81.%252B1.j  82.%252B1.j  83.%252B1.j  84.%252B1.j  85.%252B1.j  86.%252B1.j  87.%252B1.j%250A  88.%252B1.j  89.%252B1.j  90.%252B1.j  91.%252B1.j  92.%252B1.j  93.%252B1.j  94.%252B1.j  95.%252B1.j%250A  96.%252B1.j  97.%252B1.j  98.%252B1.j  99.%252B1.j%255D" isContainer="True" />',
                ),
                (
                    '<var name="bigarray" type="ndarray" qualifier="numpy" value="ndarray%253A %255B%255B    0     1     2 ...  9997  9998  9999%255D%250A %255B10000 10001 10002 ... 19997 19998 19999%255D%250A %255B20000 20001 20002 ... 29997 29998 29999%255D%250A ...%250A %255B70000 70001 70002 ... 79997 79998 79999%255D%250A %255B80000 80001 80002 ... 89997 89998 89999%255D%250A %255B90000 90001 90002 ... 99997 99998 99999%255D%255D" isContainer="True" />',
                    '<var name="bigarray" type="ndarray" qualifier="numpy" value="ndarray%253A %255B%255B    0     1     2 ...%252C  9997  9998  9999%255D%250A %255B10000 10001 10002 ...%252C 19997 19998 19999%255D%250A %255B20000 20001 20002 ...%252C 29997 29998 29999%255D%250A ...%252C %250A %255B70000 70001 70002 ...%252C 79997 79998 79999%255D%250A %255B80000 80001 80002 ...%252C 89997 89998 89999%255D%250A %255B90000 90001 90002 ...%252C 99997 99998 99999%255D%255D" isContainer="True" />',
                ),
                # Any of the ones below will do.
                (
                    '<var name="hugearray" type="ndarray" qualifier="numpy" value="ndarray%253A %255B      0       1       2 ... 9999997 9999998 9999999%255D" isContainer="True" />',
                    '<var name="hugearray" type="ndarray" qualifier="numpy" value="ndarray%253A %255B      0       1       2 ...%252C 9999997 9999998 9999999%255D" isContainer="True" />',
                ),
            )
        )

        # For each variable, check each of the resolved (meta data) attributes...
        writer.write_get_variable(hit.thread_id, hit.frame_id, "smallarray")
        writer.wait_for_multiple_vars(
            (
                '<var name="min" type="complex128"',
                '<var name="max" type="complex128"',
                '<var name="shape" type="tuple"',
                '<var name="dtype"',
                '<var name="size" type="int"',
            )
        )
        # ...and check that the internals are resolved properly
        writer.write_get_variable(hit.thread_id, hit.frame_id, "smallarray\t__internals__")
        writer.wait_for_var('<var name="%27size%27')

        writer.write_get_variable(hit.thread_id, hit.frame_id, "bigarray")
        # isContainer could be true on some numpy versions, so, we only check for the var begin.
        writer.wait_for_multiple_vars(
            (
                [
                    '<var name="min" type="int64" qualifier="numpy" value="int64%253A 0"',
                    '<var name="min" type="int64" qualifier="numpy" value="int64%3A 0"',
                    '<var name="size" type="int" qualifier="{0}" value="int%3A 100000"'.format(builtin_qualifier),
                ],
                [
                    '<var name="max" type="int64" qualifier="numpy" value="int64%253A 99999"',
                    '<var name="max" type="int32" qualifier="numpy" value="int32%253A 99999"',
                    '<var name="max" type="int64" qualifier="numpy" value="int64%3A 99999"',
                    '<var name="max" type="int32" qualifier="numpy" value="int32%253A 99999"',
                ],
                '<var name="shape" type="tuple"',
                '<var name="dtype"',
                '<var name="size" type="int"',
            )
        )
        writer.write_get_variable(hit.thread_id, hit.frame_id, "bigarray\t__internals__")
        writer.wait_for_var('<var name="%27size%27')

        # this one is different because it crosses the magic threshold where we don't calculate
        # the min/max
        writer.write_get_variable(hit.thread_id, hit.frame_id, "hugearray")
        writer.wait_for_var(
            (
                [
                    '<var name="min" type="str" qualifier={0} value="str%253A ndarray too big%252C calculating min would slow down debugging" />'.format(
                        builtin_qualifier
                    ),
                    '<var name="min" type="str" qualifier={0} value="str%3A ndarray too big%252C calculating min would slow down debugging" />'.format(
                        builtin_qualifier
                    ),
                    '<var name="min" type="str" qualifier="{0}" value="str%253A ndarray too big%252C calculating min would slow down debugging" />'.format(
                        builtin_qualifier
                    ),
                    '<var name="min" type="str" qualifier="{0}" value="str%3A ndarray too big%252C calculating min would slow down debugging" />'.format(
                        builtin_qualifier
                    ),
                ],
                [
                    '<var name="max" type="str" qualifier={0} value="str%253A ndarray too big%252C calculating max would slow down debugging" />'.format(
                        builtin_qualifier
                    ),
                    '<var name="max" type="str" qualifier={0} value="str%3A ndarray too big%252C calculating max would slow down debugging" />'.format(
                        builtin_qualifier
                    ),
                    '<var name="max" type="str" qualifier="{0}" value="str%253A ndarray too big%252C calculating max would slow down debugging" />'.format(
                        builtin_qualifier
                    ),
                    '<var name="max" type="str" qualifier="{0}" value="str%3A ndarray too big%252C calculating max would slow down debugging" />'.format(
                        builtin_qualifier
                    ),
                ],
                '<var name="shape" type="tuple"',
                '<var name="dtype"',
                '<var name="size" type="int"',
            )
        )
        writer.write_get_variable(hit.thread_id, hit.frame_id, "hugearray\t__internals__")
        writer.wait_for_var('<var name="%27size%27')

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_case_dont_trace_comments(case_setup):
    # Check dont trace
    with case_setup.test_file("_debugger_case17.py") as writer:
        writer.write_enable_dont_trace(True)
        writer.write_add_breakpoint(writer.get_line_index_with_content("break1"), "main")
        writer.write_add_breakpoint(writer.get_line_index_with_content("break2"), "main")
        writer.write_add_breakpoint(writer.get_line_index_with_content("break3"), "main")
        writer.write_add_breakpoint(writer.get_line_index_with_content("break4"), "main")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT)
        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit("107", line=2)
        # Should Skip step into properties setter
        writer.write_run_thread(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT)
        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit("107", line=2)
        # Should Skip step into properties setter
        writer.write_run_thread(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT)
        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit("107", line=2)
        # Should Skip step into properties setter
        writer.write_run_thread(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT)
        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit("107", line=2)
        # Should Skip step into properties setter
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_case_17a(case_setup):
    # Check dont trace return
    with case_setup.test_file("_debugger_case17a.py") as writer:
        writer.write_enable_dont_trace(True)
        break1_line = writer.get_line_index_with_content("break 1 here")
        writer.write_add_breakpoint(break1_line, "m1")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT, line=break1_line)

        writer.write_step_in(hit.thread_id)
        if PYDEVD_USE_SYS_MONITORING:
            break2_line = writer.get_line_index_with_content("break 2 on sys.monitoring here")
        else:
            break2_line = writer.get_line_index_with_content("break 2 here")
        hit = writer.wait_for_breakpoint_hit("107", line=break2_line)

        # Should Skip step into properties setter
        assert hit.name == "m3"
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_case_18(case_setup):
    # change local variable
    if IS_IRONPYTHON or IS_JYTHON:
        pytest.skip("Unsupported assign to local")

    with case_setup.test_file("_debugger_case18.py") as writer:
        writer.write_add_breakpoint(5, "m2")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT, line=5)

        writer.write_change_variable(hit.thread_id, hit.frame_id, "a", "40")
        writer.wait_for_var(
            '<xml><var name="" type="int" qualifier="{0}" value="int%253A 40" />%0A</xml>'.format(
                builtin_qualifier,
            )
        )
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_case_19(case_setup):
    # Check evaluate '__' attributes
    with case_setup.test_file("_debugger_case19.py") as writer:
        writer.write_add_breakpoint(8, None)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT, line=8)

        writer.write_evaluate_expression("%s\t%s\t%s" % (hit.thread_id, hit.frame_id, "LOCAL"), "a.__var")
        writer.wait_for_evaluation(
            [
                [
                    '<var name="a.__var" type="int" qualifier="{0}" value="int'.format(builtin_qualifier),
                    '<var name="a.__var" type="int"  value="int',  # jython
                ]
            ]
        )
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Monkey-patching related to starting threads not done on Jython.")
def test_case_20(case_setup):
    # Check that we were notified of threads creation before they started to run
    with case_setup.test_file("_debugger_case20.py") as writer:
        writer.write_make_initial_run()

        # We already check if it prints 'TEST SUCEEDED' by default, so, nothing
        # else should be needed in this test as it tests what's needed just by
        # running the module.
        writer.finished_ok = True


@pytest.mark.skipif(not TEST_FLASK, reason="No flask available")
def test_case_flask(case_setup_flask):
    with case_setup_flask.test_file(EXPECTED_RETURNCODE="any") as writer:
        writer.write_multi_threads_single_notification(True)
        writer.write_add_breakpoint_jinja2(5, None, "hello.html")
        writer.write_add_breakpoint_jinja2(8, None, "hello.html")
        writer.write_make_initial_run()

        t = writer.create_request_thread()
        time.sleep(2)  # Give flask some time to get to startup before requesting the page
        t.start()

        hit = writer.wait_for_single_notification_as_hit(line=5)
        writer.write_get_frame(hit.thread_id, hit.frame_id)
        writer.wait_for_vars(['<var name="content" type="str"'])
        writer.write_run_thread(hit.thread_id)

        hit = writer.wait_for_single_notification_as_hit(line=8)
        writer.write_get_frame(hit.thread_id, hit.frame_id)
        writer.wait_for_vars(['<var name="content" type="str"'])
        writer.write_run_thread(hit.thread_id)

        contents = t.wait_for_contents()

        assert "<title>Hello</title>" in contents
        assert "Flask-Jinja-Test" in contents

        writer.finished_ok = True


@pytest.mark.skipif(not TEST_DJANGO, reason="No django available")
def test_case_django_a(case_setup_django):
    def get_environ(writer):
        env = os.environ.copy()
        env.update(
            {
                "PYDEVD_FILTER_LIBRARIES": "1",  # Global setting for in project or not
                "IDE_PROJECT_ROOTS": debugger_unittest._get_debugger_test_file(writer.DJANGO_FOLDER),
            }
        )
        return env

    with case_setup_django.test_file(EXPECTED_RETURNCODE="any", get_environ=get_environ) as writer:
        writer.write_make_initial_run()

        # Wait for the first request that works...
        for i in range(4):
            try:
                t = writer.create_request_thread("my_app")
                t.start()
                contents = t.wait_for_contents()
                contents = contents.replace(" ", "").replace("\r", "").replace("\n", "")
                assert contents == "<ul><li>v1:v1</li><li>v2:v2</li></ul>"
                break
            except:
                if i == 3:
                    raise
                continue

        writer.write_add_breakpoint_django(5, None, "index.html")
        t = writer.create_request_thread("my_app")
        t.start()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT, line=5)
        writer.write_get_variable(hit.thread_id, hit.frame_id, "entry")
        writer.wait_for_vars(['<var name="key" type="str"', "v1"])

        writer.write_run_thread(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT, line=5)
        writer.write_get_variable(hit.thread_id, hit.frame_id, "entry")
        writer.wait_for_vars(['<var name="key" type="str"', "v2"])

        writer.write_run_thread(hit.thread_id)

        contents = t.wait_for_contents()

        contents = contents.replace(" ", "").replace("\r", "").replace("\n", "")
        if contents != "<ul><li>v1:v1</li><li>v2:v2</li></ul>":
            raise AssertionError("%s != <ul><li>v1:v1</li><li>v2:v2</li></ul>" % (contents,))

        writer.finished_ok = True


@pytest.mark.skipif(not TEST_DJANGO, reason="No django available")
def test_case_django_step_next(case_setup_django):
    def get_environ(writer):
        env = os.environ.copy()
        env.update(
            {
                "PYDEVD_FILTER_LIBRARIES": "1",  # Global setting for in project or not
                "IDE_PROJECT_ROOTS": debugger_unittest._get_debugger_test_file(writer.DJANGO_FOLDER),
            }
        )
        return env

    with case_setup_django.test_file(EXPECTED_RETURNCODE="any", get_environ=get_environ) as writer:
        writer.write_make_initial_run()

        # Wait for the first request that works...
        for i in range(4):
            try:
                t = writer.create_request_thread("my_app")
                t.start()
                contents = t.wait_for_contents()
                contents = contents.replace(" ", "").replace("\r", "").replace("\n", "")
                assert contents == "<ul><li>v1:v1</li><li>v2:v2</li></ul>"
                break
            except:
                if i == 3:
                    raise
                continue

        writer.write_add_breakpoint_django(3, None, "index.html")
        t = writer.create_request_thread("my_app")
        t.start()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT, line=3)

        writer.write_step_over(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(REASON_STEP_OVER, line=5)

        writer.write_step_in(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(REASON_STEP_INTO, line=7)

        writer.write_run_thread(hit.thread_id)

        contents = t.wait_for_contents()

        contents = contents.replace(" ", "").replace("\r", "").replace("\n", "")
        if contents != "<ul><li>v1:v1</li><li>v2:v2</li></ul>":
            raise AssertionError("%s != <ul><li>v1:v1</li><li>v2:v2</li></ul>" % (contents,))

        writer.finished_ok = True


@pytest.mark.skipif(not TEST_DJANGO, reason="No django available")
def test_case_django_b(case_setup_django):
    with case_setup_django.test_file(EXPECTED_RETURNCODE="any") as writer:
        writer.write_add_breakpoint_django(4, None, "name.html")
        writer.write_add_exception_breakpoint_django()
        writer.write_remove_exception_breakpoint_django()
        writer.write_make_initial_run()

        t = writer.create_request_thread("my_app/name")
        time.sleep(5)  # Give django some time to get to startup before requesting the page
        t.start()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT, line=4)

        writer.write_get_frame(hit.thread_id, hit.frame_id)
        writer.wait_for_var('<var name="form" type="NameForm" qualifier="my_app.forms" value="NameForm%253A')
        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.skipif(not TEST_DJANGO, reason="No django available")
def test_case_django_template_inherits_no_exception(case_setup_django):
    with case_setup_django.test_file(EXPECTED_RETURNCODE="any") as writer:
        # Check that it doesn't have issues with inherits + django exception breakpoints.
        writer.write_add_exception_breakpoint_django()

        writer.write_make_initial_run()

        t = writer.create_request_thread("my_app/inherits")
        time.sleep(5)  # Give django some time to get to startup before requesting the page
        t.start()
        contents = t.wait_for_contents()

        contents = contents.replace(" ", "").replace("\r", "").replace("\n", "")
        assert contents == '''"chat_mode=True""chat_mode=False"'''

        writer.finished_ok = True


@pytest.mark.skipif(not TEST_DJANGO, reason="No django available")
def test_case_django_no_var_error(case_setup_django):
    with case_setup_django.test_file(EXPECTED_RETURNCODE="any") as writer:
        # Check that it doesn't have issues with inherits + django exception breakpoints.
        writer.write_add_exception_breakpoint_django()

        writer.write_make_initial_run()

        t = writer.create_request_thread("my_app/no_var_error")
        time.sleep(5)  # Give django some time to get to startup before requesting the page
        t.start()
        contents = t.wait_for_contents()

        contents = contents.replace(" ", "").replace("\r", "").replace("\n", "")
        assert contents == """no_pat_name"""

        writer.finished_ok = True


@pytest.mark.skipif(not TEST_DJANGO, reason="No django available")
@pytest.mark.parametrize("jmc", [False, True])
def test_case_django_no_attribute_exception_breakpoint(case_setup_django, jmc):
    kwargs = {}
    if jmc:

        def get_environ(writer):
            env = os.environ.copy()
            env.update(
                {
                    "PYDEVD_FILTER_LIBRARIES": "1",  # Global setting for in project or not
                }
            )
            return env

        kwargs["get_environ"] = get_environ

    with case_setup_django.test_file(EXPECTED_RETURNCODE="any", **kwargs) as writer:
        writer.write_add_exception_breakpoint_django()

        writer.write_make_initial_run()

        t = writer.create_request_thread("my_app/template_error")
        time.sleep(5)  # Give django some time to get to startup before requesting the page
        t.start()

        hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION, line=7, file="template_error.html")

        writer.write_get_frame(hit.thread_id, hit.frame_id)
        writer.wait_for_var('<var name="entry" type="Entry" qualifier="my_app.views" value="Entry: v1:v1" isContainer="True"')

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.skipif(not TEST_DJANGO, reason="No django available")
def test_case_django_no_attribute_exception_breakpoint_and_regular_exceptions(case_setup_django):
    with case_setup_django.test_file(EXPECTED_RETURNCODE="any") as writer:
        writer.write_add_exception_breakpoint_django()

        # The django plugin has priority over the regular exception breakpoint.
        writer.write_add_exception_breakpoint_with_policy(
            "django.template.base.VariableDoesNotExist",
            notify_on_handled_exceptions=2,  # 2 means notify only on first raise.
            notify_on_unhandled_exceptions=0,
            ignore_libraries=0,
        )
        writer.write_make_initial_run()

        t = writer.create_request_thread("my_app/template_error")
        time.sleep(5)  # Give django some time to get to startup before requesting the page
        t.start()

        hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION, line=7, file="template_error.html")

        writer.write_get_frame(hit.thread_id, hit.frame_id)
        writer.wait_for_var('<var name="entry" type="Entry" qualifier="my_app.views" value="Entry: v1:v1" isContainer="True"')

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.skipif(not TEST_DJANGO, reason="No django available")
@pytest.mark.parametrize("jmc", [False, True])
def test_case_django_invalid_template_exception_breakpoint(case_setup_django, jmc):
    kwargs = {}
    if jmc:

        def get_environ(writer):
            env = os.environ.copy()
            env.update(
                {
                    "PYDEVD_FILTER_LIBRARIES": "1",  # Global setting for in project or not
                }
            )
            return env

        kwargs["get_environ"] = get_environ

    with case_setup_django.test_file(EXPECTED_RETURNCODE="any", **kwargs) as writer:
        writer.write_add_exception_breakpoint_django()
        writer.write_make_initial_run()

        t = writer.create_request_thread("my_app/template_error2")
        time.sleep(5)  # Give django some time to get to startup before requesting the page
        t.start()

        hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION, line=4, file="template_error2.html")

        writer.write_get_frame(hit.thread_id, hit.frame_id)
        writer.wait_for_var('<var name="token" type="Token" qualifier="django.template.base" value="Token:')

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.skipif(not TEST_CYTHON, reason="No cython available")
def test_cython(case_setup):
    from _pydevd_bundle import pydevd_cython

    assert pydevd_cython.trace_dispatch is not None


def _has_qt():
    try:
        try:
            from PySide import QtCore  # @UnresolvedImport

            return True
        except:
            from PySide2 import QtCore  # @UnresolvedImport

            return True
    except:
        try:
            from PyQt4 import QtCore  # @UnresolvedImport

            return True
        except:
            try:
                from PyQt5 import QtCore  # @UnresolvedImport

                return True
            except:
                pass
    return False


@pytest.mark.skipif(not _has_qt(), reason="No qt available")
def test_case_qthread1(case_setup):
    with case_setup.test_file("_debugger_case_qthread1.py") as writer:
        breakpoint_id = writer.write_add_breakpoint(writer.get_line_index_with_content("break here"), "run")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()

        writer.write_remove_breakpoint(breakpoint_id)
        writer.write_run_thread(hit.thread_id)

        writer.log.append("Checking sequence. Found: %s" % (writer._sequence))
        assert 9 == writer._sequence, "Expected 9. Had: %s" % writer._sequence

        writer.log.append("Marking finished ok.")
        writer.finished_ok = True


@pytest.mark.skipif(not _has_qt(), reason="No qt available")
def test_case_qthread2(case_setup):
    with case_setup.test_file("_debugger_case_qthread2.py") as writer:
        breakpoint_id = writer.write_add_breakpoint(writer.get_line_index_with_content("break here"), "long_running")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()
        thread_id = hit.thread_id

        writer.write_remove_breakpoint(breakpoint_id)
        writer.write_run_thread(thread_id)

        writer.log.append("Checking sequence. Found: %s" % (writer._sequence))
        assert 9 == writer._sequence, "Expected 9. Had: %s" % writer._sequence

        writer.log.append("Marking finished ok.")
        writer.finished_ok = True


@pytest.mark.skipif(not _has_qt(), reason="No qt available")
def test_case_qthread3(case_setup):
    with case_setup.test_file("_debugger_case_qthread3.py") as writer:
        breakpoint_id = writer.write_add_breakpoint(writer.get_line_index_with_content("break here"), "run")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()
        thread_id = hit.thread_id
        frame_id = hit.frame_id

        writer.write_remove_breakpoint(breakpoint_id)
        writer.write_run_thread(thread_id)

        writer.log.append("Checking sequence. Found: %s" % (writer._sequence))
        assert 9 == writer._sequence, "Expected 9. Had: %s" % writer._sequence

        writer.log.append("Marking finished ok.")
        writer.finished_ok = True


@pytest.mark.skipif(not _has_qt(), reason="No qt available")
def test_case_qthread4(case_setup):
    with case_setup.test_file("_debugger_case_qthread4.py") as writer:
        original_additional_output_checks = writer.additional_output_checks

        def additional_output_checks(stdout, stderr):
            original_additional_output_checks(stdout, stderr)
            if "On start called" not in stdout:
                raise AssertionError('Expected "On start called" to be in stdout:\n%s' % (stdout,))
            if "Done sleeping" not in stdout:
                raise AssertionError('Expected "Done sleeping" to be in stdout:\n%s' % (stdout,))
            if "native Qt signal is not callable" in stderr:
                raise AssertionError('Did not expect "native Qt signal is not callable" to be in stderr:\n%s' % (stderr,))

        breakpoint_id = writer.write_add_breakpoint(28, "on_start")  # breakpoint on print('On start called2').
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()

        writer.write_remove_breakpoint(breakpoint_id)
        writer.write_run_thread(hit.thread_id)

        writer.log.append("Checking sequence. Found: %s" % (writer._sequence))
        assert 9 == writer._sequence, "Expected 9. Had: %s" % writer._sequence

        writer.log.append("Marking finished ok.")
        writer.finished_ok = True


def test_m_switch(case_setup_m_switch):
    if TODO_PYPY:
        raise pytest.skip("Not ok in pypy")

    with case_setup_m_switch.test_file() as writer:
        writer.log.append("writing add breakpoint")
        breakpoint_id = writer.write_add_breakpoint(1, None)

        writer.log.append("making initial run")
        writer.write_make_initial_run()

        writer.log.append("waiting for breakpoint hit")
        hit = writer.wait_for_breakpoint_hit()

        writer.write_remove_breakpoint(breakpoint_id)

        writer.log.append("run thread")
        writer.write_run_thread(hit.thread_id)

        writer.log.append("asserting")
        try:
            assert 9 == writer._sequence, "Expected 9. Had: %s" % writer._sequence
        except:
            writer.log.append("assert failed!")
            raise
        writer.log.append("asserted")

        writer.finished_ok = True


def test_module_entry_point(case_setup_m_switch_entry_point):
    with case_setup_m_switch_entry_point.test_file() as writer:
        writer.log.append("writing add breakpoint")
        breakpoint_id = writer.write_add_breakpoint(1, None)

        writer.log.append("making initial run")
        writer.write_make_initial_run()

        writer.log.append("waiting for breakpoint hit")
        hit = writer.wait_for_breakpoint_hit()

        writer.write_remove_breakpoint(breakpoint_id)

        writer.log.append("run thread")
        writer.write_run_thread(hit.thread_id)

        writer.log.append("asserting")
        try:
            assert 9 == writer._sequence, "Expected 9. Had: %s" % writer._sequence
        except:
            writer.log.append("assert failed!")
            raise
        writer.log.append("asserted")

        writer.finished_ok = True


@pytest.mark.skipif(not IS_CPYTHON or PYDEVD_USE_SYS_MONITORING, reason="CPython only test.")
def test_check_tracer_with_exceptions(case_setup):
    def get_environ(writer):
        env = os.environ.copy()
        # This test requires regular tracing (without cython).
        env["PYDEVD_USE_CYTHON"] = "NO"
        env["PYDEVD_USE_FRAME_EVAL"] = "NO"
        return env

    with case_setup.test_file("_debugger_case_check_tracer.py", get_environ=get_environ) as writer:
        writer.write_add_exception_breakpoint_with_policy("IndexError", "1", "1", "1")
        writer.write_make_initial_run()
        writer.finished_ok = True


@pytest.mark.parametrize(
    "target_file",
    [
        "_debugger_case_unhandled_exceptions_generator.py",
        "_debugger_case_unhandled_exceptions_listcomp.py",
    ],
)
@pytest.mark.parametrize("unhandled", [False, True])
@pytest.mark.skipif(IS_JYTHON, reason="Not ok for Jython.")
def test_case_handled_and_unhandled_exception_generator(case_setup, target_file, unhandled):
    def check_test_suceeded_msg(writer, stdout, stderr):
        # Don't call super (we have an unhandled exception in the stack trace).
        return "TEST SUCEEDED" in "".join(stdout) and "TEST SUCEEDED" in "".join(stderr)

    def additional_output_checks(writer, stdout, stderr):
        if "ZeroDivisionError" not in stderr:
            raise AssertionError("Expected test to have an unhandled exception.\nstdout:\n%s\n\nstderr:\n%s" % (stdout, stderr))

    with case_setup.test_file(
        target_file,
        check_test_suceeded_msg=check_test_suceeded_msg,
        additional_output_checks=additional_output_checks,
        EXPECTED_RETURNCODE=1,
    ) as writer:
        if unhandled:
            writer.write_add_exception_breakpoint_with_policy("Exception", "0", "1", "0")
        else:
            writer.write_add_exception_breakpoint_with_policy("Exception", "1", "0", "1")

        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_UNCAUGHT_EXCEPTION if unhandled else REASON_CAUGHT_EXCEPTION)
        assert hit.line == writer.get_line_index_with_content("# exc line")

        if "generator" in target_file:
            expected_frame_names = ["<genexpr>", "f", "<module>"]
        else:
            if sys.version_info[:2] >= (3, 12):
                expected_frame_names = ["f", "<module>"]
            else:
                expected_frame_names = ["<listcomp>", "f", "<module>"]

        writer.write_get_current_exception(hit.thread_id)
        msg = writer.wait_for_message(accept_message=lambda msg: 'exc_type="' in msg and 'exc_desc="' in msg, unquote_msg=False)

        frame_names = [unquote(f["name"]).replace("&lt;", "<").replace("&gt;", ">") for f in msg.thread.frame]
        assert frame_names == expected_frame_names

        writer.write_run_thread(hit.thread_id)

        if not unhandled:
            if sys.version_info[:2] >= (3, 12) and "generator" not in target_file:
                expected_lines = [
                    writer.get_line_index_with_content("# call exc"),
                ]
            else:
                expected_lines = [
                    writer.get_line_index_with_content("# exc line"),
                    writer.get_line_index_with_content("# call exc"),
                ]

            for expected_line in expected_lines:
                hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION)
                assert hit.line == expected_line

                writer.write_get_current_exception(hit.thread_id)
                msg = writer.wait_for_message(accept_message=lambda msg: 'exc_type="' in msg and 'exc_desc="' in msg, unquote_msg=False)

                frame_names = [unquote(f["name"]).replace("&lt;", "<").replace("&gt;", ">") for f in msg.thread.frame]
                assert frame_names == expected_frame_names

                writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Failing on Jython -- needs to be investigated).")
def test_unhandled_exceptions_basic(case_setup):
    def check_test_suceeded_msg(writer, stdout, stderr):
        # Don't call super (we have an unhandled exception in the stack trace).
        return "TEST SUCEEDED" in "".join(stdout) and "TEST SUCEEDED" in "".join(stderr)

    def additional_output_checks(writer, stdout, stderr):
        if "raise Exception" not in stderr:
            raise AssertionError("Expected test to have an unhandled exception.\nstdout:\n%s\n\nstderr:\n%s" % (stdout, stderr))

    with case_setup.test_file(
        "_debugger_case_unhandled_exceptions.py",
        check_test_suceeded_msg=check_test_suceeded_msg,
        additional_output_checks=additional_output_checks,
        EXPECTED_RETURNCODE=1,
    ) as writer:
        writer.write_add_exception_breakpoint_with_policy("Exception", "0", "1", "0")
        writer.write_make_initial_run()

        def check(hit, exc_type, exc_desc):
            writer.write_get_current_exception(hit.thread_id)
            msg = writer.wait_for_message(
                accept_message=lambda msg: exc_type in msg and 'exc_type="' in msg and 'exc_desc="' in msg, unquote_msg=False
            )
            assert unquote(msg.thread["exc_desc"]) == exc_desc
            assert unquote(msg.thread["exc_type"]) in (
                "&lt;type 'exceptions.%s'&gt;" % (exc_type,),  # py2
                "&lt;class '%s'&gt;" % (exc_type,),  # py3
            )
            if len(msg.thread.frame) == 0:
                assert unquote(unquote(msg.thread.frame["file"])).endswith("_debugger_case_unhandled_exceptions.py")
            else:
                assert unquote(unquote(msg.thread.frame[0]["file"])).endswith("_debugger_case_unhandled_exceptions.py")
            writer.write_run_thread(hit.thread_id)

        # Will stop in 2 background threads
        hit0 = writer.wait_for_breakpoint_hit(REASON_UNCAUGHT_EXCEPTION)
        thread_id1 = hit0.thread_id

        hit1 = writer.wait_for_breakpoint_hit(REASON_UNCAUGHT_EXCEPTION)
        thread_id2 = hit1.thread_id

        if hit0.name == "thread_func2":
            check(hit0, "ValueError", "in thread 2")
            check(hit1, "Exception", "in thread 1")
        else:
            check(hit0, "Exception", "in thread 1")
            check(hit1, "ValueError", "in thread 2")

        writer.write_run_thread(thread_id1)
        writer.write_run_thread(thread_id2)

        # Will stop in main thread
        hit = writer.wait_for_breakpoint_hit(REASON_UNCAUGHT_EXCEPTION)
        assert hit.name == "<module>"
        thread_id3 = hit.thread_id

        # Requesting the stack in an unhandled exception should provide the stack of the exception,
        # not the current location of the program.
        writer.write_get_thread_stack(thread_id3)
        msg = writer.wait_for_message(CMD_GET_THREAD_STACK)
        assert len(msg.thread.frame) == 0  # In main thread (must have no back frames).
        assert msg.thread.frame["name"] == "<module>"
        check(hit, "IndexError", "in main")

        writer.log.append("Marking finished ok.")
        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Failing on Jython -- needs to be investigated).")
def test_unhandled_exceptions_in_top_level1(case_setup_unhandled_exceptions):
    with case_setup_unhandled_exceptions.test_file(
        "_debugger_case_unhandled_exceptions_on_top_level.py",
        EXPECTED_RETURNCODE=1,
    ) as writer:
        writer.write_add_exception_breakpoint_with_policy("Exception", "0", "1", "0")
        writer.write_make_initial_run()

        # Will stop in main thread
        hit = writer.wait_for_breakpoint_hit(REASON_UNCAUGHT_EXCEPTION)
        writer.write_run_thread(hit.thread_id)

        writer.log.append("Marking finished ok.")
        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Failing on Jython -- needs to be investigated).")
def test_unhandled_exceptions_in_top_level2(case_setup_unhandled_exceptions):
    # Note: expecting unhandled exception to be printed to stderr.

    def get_environ(writer):
        env = os.environ.copy()
        curr_pythonpath = env.get("PYTHONPATH", "")

        pydevd_dirname = os.path.dirname(writer.get_pydevd_file())

        curr_pythonpath = pydevd_dirname + os.pathsep + curr_pythonpath
        env["PYTHONPATH"] = curr_pythonpath
        return env

    def update_command_line_args(writer, args):
        # Start pydevd with '-m' to see how it deal with being called with
        # runpy at the start.
        assert args[0].endswith("pydevd.py")
        args = ["-m", "pydevd"] + args[1:]
        return args

    with case_setup_unhandled_exceptions.test_file(
        "_debugger_case_unhandled_exceptions_on_top_level.py",
        get_environ=get_environ,
        update_command_line_args=update_command_line_args,
        EXPECTED_RETURNCODE="any",
    ) as writer:
        writer.write_add_exception_breakpoint_with_policy("Exception", "0", "1", "0")
        writer.write_make_initial_run()

        # Should stop (only once) in the main thread.
        hit = writer.wait_for_breakpoint_hit(REASON_UNCAUGHT_EXCEPTION)
        writer.write_run_thread(hit.thread_id)

        writer.log.append("Marking finished ok.")
        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Failing on Jython -- needs to be investigated).")
def test_unhandled_exceptions_in_top_level3(case_setup_unhandled_exceptions):
    with case_setup_unhandled_exceptions.test_file("_debugger_case_unhandled_exceptions_on_top_level.py", EXPECTED_RETURNCODE=1) as writer:
        # Handled and unhandled
        # PySide2 has a bug in shibokensupport which will try to do: sys._getframe(1).
        # during the teardown (which will fail as there's no back frame in this case).
        # So, mark ignore libraries in this case.
        writer.write_add_exception_breakpoint_with_policy("Exception", "1", "1", ignore_libraries="1")
        writer.write_make_initial_run()

        # Will stop in main thread twice: once one we find that the exception is being
        # thrown and another in postmortem mode when we discover it's uncaught.
        hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION)
        writer.write_run_thread(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(REASON_UNCAUGHT_EXCEPTION)
        writer.write_run_thread(hit.thread_id)

        writer.log.append("Marking finished ok.")
        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Failing on Jython -- needs to be investigated).")
def test_unhandled_exceptions_in_top_level4(case_setup_unhandled_exceptions):
    # Note: expecting unhandled exception to be printed to stderr.
    with case_setup_unhandled_exceptions.test_file(
        "_debugger_case_unhandled_exceptions_on_top_level2.py",
        EXPECTED_RETURNCODE=1,
    ) as writer:
        # Handled and unhandled
        writer.write_add_exception_breakpoint_with_policy("Exception", "1", "1", "1")
        writer.write_make_initial_run()

        # We have an exception thrown and handled and another which is thrown and is then unhandled.
        hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION)
        writer.write_run_thread(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION)
        writer.write_run_thread(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(REASON_UNCAUGHT_EXCEPTION)
        writer.write_run_thread(hit.thread_id)

        writer.log.append("Marking finished ok.")
        writer.finished_ok = True


@pytest.mark.skipif(not IS_CPYTHON, reason="Only for Python.")
def test_case_set_next_statement(case_setup):
    with case_setup.test_file("_debugger_case_set_next_statement.py") as writer:
        breakpoint_id = writer.write_add_breakpoint(6, None)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT, line=6)  # Stop in line a=3 (before setting it)

        writer.write_evaluate_expression("%s\t%s\t%s" % (hit.thread_id, hit.frame_id, "LOCAL"), "a")
        writer.wait_for_evaluation('<var name="a" type="int" qualifier="{0}" value="int: 2"'.format(builtin_qualifier))
        writer.write_set_next_statement(hit.thread_id, 2, "method")
        hit = writer.wait_for_breakpoint_hit("127", line=2)

        # Check that it's still unchanged
        writer.write_evaluate_expression("%s\t%s\t%s" % (hit.thread_id, hit.frame_id, "LOCAL"), "a")
        writer.wait_for_evaluation('<var name="a" type="int" qualifier="{0}" value="int: 2"'.format(builtin_qualifier))

        # After a step over it should become 1 as we executed line which sets a = 1
        writer.write_step_over(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit("108")

        writer.write_evaluate_expression("%s\t%s\t%s" % (hit.thread_id, hit.frame_id, "LOCAL"), "a")
        writer.wait_for_evaluation('<var name="a" type="int" qualifier="{0}" value="int: 1"'.format(builtin_qualifier))

        writer.write_remove_breakpoint(breakpoint_id)
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_unhandled_exceptions_get_stack(case_setup_unhandled_exceptions):
    with case_setup_unhandled_exceptions.test_file(
        "_debugger_case_unhandled_exception_get_stack.py",
        EXPECTED_RETURNCODE="any",
    ) as writer:
        writer.write_add_exception_breakpoint_with_policy("Exception", "0", "1", "0")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_UNCAUGHT_EXCEPTION)
        writer.write_get_thread_stack(hit.thread_id)

        msg = writer.wait_for_get_thread_stack_message()
        files = [frame["file"] for frame in msg.thread.frame]
        assert msg.thread["id"] == hit.thread_id
        if not files[0].endswith("_debugger_case_unhandled_exception_get_stack.py"):
            raise AssertionError(
                "Expected to find _debugger_case_unhandled_exception_get_stack.py in files[0]. Found: %s" % ("\n".join(files),)
            )

        assert len(msg.thread.frame) == 0  # No back frames (stopped in main).
        assert msg.thread.frame["name"] == "<module>"
        assert msg.thread.frame["line"] == str(writer.get_line_index_with_content("break line on unhandled exception"))

        writer.write_run_thread(hit.thread_id)

        writer.log.append("Marking finished ok.")
        writer.finished_ok = True


@pytest.mark.skipif(not IS_PY36_OR_GREATER, reason="Requires Python 3.")
def test_case_throw_exc_reason_xml(case_setup):
    from _pydevd_bundle.pydevd_comm_constants import CMD_SEND_CURR_EXCEPTION_TRACE

    def check_test_suceeded_msg(self, stdout, stderr):
        return "TEST SUCEEDED" in "".join(stderr)

    def additional_output_checks(writer, stdout, stderr):
        assert "raise RuntimeError('TEST SUCEEDED')" in stderr
        assert "raise RuntimeError from e" in stderr
        assert "raise Exception('another while handling')" in stderr

    with case_setup.test_file(
        "_debugger_case_raise_with_cause.py",
        EXPECTED_RETURNCODE=1,
        check_test_suceeded_msg=check_test_suceeded_msg,
        additional_output_checks=additional_output_checks,
    ) as writer:
        writer.write_add_exception_breakpoint_with_policy("Exception", "0", "1", "0")
        writer.write_make_initial_run()

        def accept_message(msg):
            if msg.startswith("%s\t" % CMD_SEND_CURR_EXCEPTION_TRACE) or ('stop_reason="%s"' % REASON_UNCAUGHT_EXCEPTION) in msg:
                return True
            return False

        # Order is not guaranteed.
        msg1 = writer.wait_for_message(accept_message)
        msg2 = writer.wait_for_message(accept_message)
        try:
            hitmsg = msg1
            exctracemsg = msg2
            msg1.thread["stop_reason"]
        except:
            hitmsg = msg2
            exctracemsg = msg1
        hit = writer._get_stack_as_hit(hitmsg)

        name_and_lines = []
        for frame in exctracemsg.thread.frame:
            name_and_lines.append((frame["name"], frame["line"]))

        assert name_and_lines == [
            ("foobar", "20"),
            ("<module>", "23"),
            ("[Chained Exc: another while handling] foobar", "18"),
            ("[Chained Exc: another while handling] handle", "10"),
            ("[Chained Exc: TEST SUCEEDED] foobar", "16"),
            ("[Chained Exc: TEST SUCEEDED] method", "6"),
            ("[Chained Exc: TEST SUCEEDED] method2", "2"),
        ]

        writer.write_get_thread_stack(hit.thread_id)

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(not IS_CPYTHON, reason="Only for Python.")
def test_case_get_next_statement_targets(case_setup):
    with case_setup.test_file("_debugger_case_get_next_statement_targets.py") as writer:
        breakpoint_id = writer.write_add_breakpoint(21, None)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT, line=21)

        writer.write_get_next_statement_targets(hit.thread_id, hit.frame_id)
        targets = writer.wait_for_get_next_statement_targets()
        # Note: 20 may appear as a side-effect of the frame eval
        # mode (so, we have to ignore it here) -- this isn't ideal, but
        # it's also not that bad (that line has no code in the source and
        # executing it will just set the tracing for the method).
        targets.discard(20)
        # On Python 3.11 there's now a line 1 (which should be harmless).
        targets.discard(1)
        expected = set((2, 3, 5, 8, 9, 10, 12, 13, 14, 15, 17, 18, 19, 21))
        assert targets == expected, "Expected targets to be %s, was: %s" % (expected, targets)

        writer.write_remove_breakpoint(breakpoint_id)
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(IS_IRONPYTHON or IS_JYTHON, reason="Failing on IronPython and Jython (needs to be investigated).")
def test_case_type_ext(case_setup):
    # Custom type presentation extensions

    def get_environ(self):
        env = os.environ.copy()

        python_path = env.get("PYTHONPATH", "")
        ext_base = debugger_unittest._get_debugger_test_file("my_extensions")
        env["PYTHONPATH"] = ext_base + os.pathsep + python_path if python_path else ext_base
        return env

    with case_setup.test_file("_debugger_case_type_ext.py", get_environ=get_environ) as writer:
        writer.get_environ = get_environ

        writer.write_add_breakpoint(7, None)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit("111")
        writer.write_get_frame(hit.thread_id, hit.frame_id)
        assert writer.wait_for_var(
            [
                [
                    r'<var name="my_rect" type="Rect" qualifier="__main__" value="Rectangle%255BLength%253A 5%252C Width%253A 10 %252C Area%253A 50%255D" isContainer="True" />',
                    r'<var name="my_rect" type="Rect"  value="Rect: <__main__.Rect object at',  # Jython
                ]
            ]
        )
        writer.write_get_variable(hit.thread_id, hit.frame_id, "my_rect")
        assert writer.wait_for_var(r'<var name="area" type="int" qualifier="{0}" value="int%253A 50" />'.format(builtin_qualifier))
        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_case_variable_access(case_setup, pyfile, data_regression):
    @pyfile
    def case_custom():
        obj = [
            tuple(range(9)),
            [
                tuple(range(5)),
            ],
        ]

        print("TEST SUCEEDED")

    with case_setup.test_file(case_custom) as writer:
        line = writer.get_line_index_with_content("TEST SUCEEDED")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit("111")
        writer.write_get_frame(hit.thread_id, hit.frame_id)

        frame_vars = writer.wait_for_untangled_message(accept_message=lambda cmd_id, untangled: cmd_id == CMD_GET_FRAME)

        obj_var = [v for v in frame_vars.var if v["name"] == "obj"][0]
        assert obj_var["type"] == "list"
        assert unquote_plus(obj_var["value"]) == "<class 'list'>: [(0, 1, 2, 3, 4, 5, 6, 7, 8), [(0, 1, 2, 3, 4)]]"
        assert obj_var["isContainer"] == "True"

        def _skip_key_in_dict(key):
            try:
                int(key)
            except ValueError:
                if "more" in key or "[" in key:
                    return False
                return True
            return False

        def collect_vars(locator, level=0):
            writer.write("%s\t%s\t%s\t%s" % (CMD_GET_VARIABLE, writer.next_seq(), hit.thread_id, locator))
            obj_vars = writer.wait_for_untangled_message(accept_message=lambda cmd_id, _untangled: cmd_id == CMD_GET_VARIABLE)

            for v in obj_vars.var:
                if _skip_key_in_dict(v["name"]):
                    continue
                new_locator = locator + "\t" + v["name"]
                yield level, v, new_locator
                if v["isContainer"] == "True":
                    yield from collect_vars(new_locator, level + 1)

        found = []
        for level, val, _locator in collect_vars("%s\tFRAME\tobj" % hit.frame_id):
            found.append((("    " * level) + val["name"] + ": " + unquote_plus(val["value"])))

        data_regression.check(found)

        # Check referrers
        full_loc = "%s\t%s\t%s" % (hit.thread_id, hit.frame_id, "FRAME\tobj\t1\t0")
        writer.write_custom_operation(
            full_loc, "EXEC", "from _pydevd_bundle.pydevd_referrers import get_referrer_info", "get_referrer_info"
        )
        msg = writer.wait_for_untangled_message(
            double_unquote=True, accept_message=lambda cmd_id, _untangled: cmd_id == CMD_RUN_CUSTOM_OPERATION
        )

        msg_vars = msg.var
        try:
            msg_vars["found_as"]
            msg_vars = [msg_vars]
        except:
            pass  # it's a container.

        for v in msg_vars:
            if v["found_as"] == "list[0]":
                # In pypy we may have more than one reference, find out the one
                referrer_id = v["id"]
                assert int(referrer_id)
                assert unquote_plus(v["value"]) == "<class 'list'>: [(0, 1, 2, 3, 4)]"
                break
        else:
            raise AssertionError("Unable to find ref with list[0]. Found: %s" % (msg_vars,))

        found = []
        by_id_locator = "%s\t%s" % (referrer_id, "BY_ID")
        for level, val, _locator in collect_vars(by_id_locator):
            found.append((("    " * level) + val["name"] + ": " + unquote_plus(val["value"])))

        data_regression.check(found, basename="test_case_variable_access_by_id")

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.skipif(IS_IRONPYTHON or IS_JYTHON, reason="Failing on IronPython and Jython (needs to be investigated).")
def test_case_event_ext(case_setup):
    def get_environ(self):
        env = os.environ.copy()

        python_path = env.get("PYTHONPATH", "")
        ext_base = debugger_unittest._get_debugger_test_file("my_extensions")
        env["PYTHONPATH"] = ext_base + os.pathsep + python_path if python_path else ext_base
        env["VERIFY_EVENT_TEST"] = "1"
        return env

    # Test initialize event for extensions
    with case_setup.test_file("_debugger_case_event_ext.py", get_environ=get_environ) as writer:
        original_additional_output_checks = writer.additional_output_checks

        @overrides(writer.additional_output_checks)
        def additional_output_checks(stdout, stderr):
            original_additional_output_checks(stdout, stderr)
            if "INITIALIZE EVENT RECEIVED" not in stdout:
                raise AssertionError("No initialize event received")

        writer.additional_output_checks = additional_output_checks

        writer.write_make_initial_run()
        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Jython does not seem to be creating thread started inside tracing (investigate).")
def test_case_writer_creation_deadlock(case_setup):
    # check case where there was a deadlock evaluating expressions
    with case_setup.test_file("_debugger_case_thread_creation_deadlock.py") as writer:
        writer.write_add_breakpoint(26, None)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit("111")

        assert hit.line == 26, "Expected return to be in line 26, was: %s" % (hit.line,)

        writer.write_evaluate_expression("%s\t%s\t%s" % (hit.thread_id, hit.frame_id, "LOCAL"), "create_thread()")
        writer.wait_for_evaluation(
            '<var name="create_thread()" type="str" qualifier="{0}" value="str: create_thread:ok'.format(builtin_qualifier)
        )
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_case_skip_breakpoints_in_exceptions(case_setup):
    # Case where breakpoint is skipped after an exception is raised over it
    with case_setup.test_file("_debugger_case_skip_breakpoint_in_exceptions.py") as writer:
        writer.write_add_breakpoint(5, None)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit("111", line=5)
        writer.write_run_thread(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit("111", line=5)
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_case_handled_exceptions0(case_setup):
    # Stop only once per handled exception.
    with case_setup.test_file("_debugger_case_exceptions.py") as writer:
        writer.write_set_project_roots([os.path.dirname(writer.TEST_FILE)])
        writer.write_add_exception_breakpoint_with_policy(
            "IndexError",
            notify_on_handled_exceptions=2,  # Notify only once
            notify_on_unhandled_exceptions=0,
            ignore_libraries=1,
        )
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION, line=writer.get_line_index_with_content("raise indexerror line"))

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Not working on Jython (needs to be investigated).")
def test_case_handled_exceptions1(case_setup):
    # Stop multiple times for the same handled exception.
    def get_environ(self):
        env = os.environ.copy()

        env["IDE_PROJECT_ROOTS"] = os.path.dirname(self.TEST_FILE)
        return env

    with case_setup.test_file("_debugger_case_exceptions.py", get_environ=get_environ) as writer:
        writer.write_add_exception_breakpoint_with_policy(
            "IndexError",
            notify_on_handled_exceptions=1,  # Notify multiple times
            notify_on_unhandled_exceptions=0,
            ignore_libraries=1,
        )
        writer.write_make_initial_run()

        def check(hit):
            writer.write_get_frame(hit.thread_id, hit.frame_id)
            writer.wait_for_message(accept_message=lambda msg: "__exception__" in msg and "IndexError" in msg, unquote_msg=False)
            writer.write_get_current_exception(hit.thread_id)
            msg = writer.wait_for_message(
                accept_message=lambda msg: "IndexError" in msg and 'exc_type="' in msg and 'exc_desc="' in msg, unquote_msg=False
            )
            assert msg.thread["exc_desc"] == "foo"
            assert unquote(msg.thread["exc_type"]) in (
                "&lt;type 'exceptions.IndexError'&gt;",  # py2
                "&lt;class 'IndexError'&gt;",  # py3
            )

            assert unquote(unquote(msg.thread.frame[0]["file"])).endswith("_debugger_case_exceptions.py")
            writer.write_run_thread(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION, line=writer.get_line_index_with_content("raise indexerror line"))
        check(hit)

        hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION, line=writer.get_line_index_with_content("reraise on method2"))
        check(hit)

        hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION, line=writer.get_line_index_with_content("handle on method1"))
        check(hit)

        writer.finished_ok = True


def test_case_handled_exceptions2(case_setup):
    # No IDE_PROJECT_ROOTS set.
    def get_environ(self):
        env = os.environ.copy()

        # Don't stop anywhere (note: having IDE_PROJECT_ROOTS = '' will consider
        # having anything not under site-packages as being in the project).
        env["IDE_PROJECT_ROOTS"] = '["empty"]'
        return env

    with case_setup.test_file("_debugger_case_exceptions.py", get_environ=get_environ) as writer:
        writer.write_add_exception_breakpoint_with_policy(
            "IndexError",
            notify_on_handled_exceptions=1,  # Notify multiple times
            notify_on_unhandled_exceptions=0,
            ignore_libraries=1,
        )
        writer.write_make_initial_run()

        writer.finished_ok = True


def test_case_handled_exceptions3(case_setup):
    # Don't stop on exception thrown in the same context (only at caller).
    def get_environ(self):
        env = os.environ.copy()

        env["IDE_PROJECT_ROOTS"] = os.path.dirname(self.TEST_FILE)
        return env

    with case_setup.test_file("_debugger_case_exceptions.py", get_environ=get_environ) as writer:
        # Note: in this mode we'll only stop once.
        writer.write_set_py_exception_globals(
            break_on_uncaught=False,
            break_on_caught=True,
            skip_on_exceptions_thrown_in_same_context=False,
            ignore_exceptions_thrown_in_lines_with_ignore_exception=True,
            ignore_libraries=True,
            exceptions=("IndexError",),
        )

        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION, line=writer.get_line_index_with_content("raise indexerror line"))
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_case_handled_exceptions4(case_setup):
    # Don't stop on exception thrown in the same context (only at caller).
    def get_environ(self):
        env = os.environ.copy()

        env["IDE_PROJECT_ROOTS"] = os.path.dirname(self.TEST_FILE)
        return env

    with case_setup.test_file("_debugger_case_exceptions.py", get_environ=get_environ) as writer:
        # Note: in this mode we'll only stop once.
        writer.write_set_py_exception_globals(
            break_on_uncaught=False,
            break_on_caught=True,
            skip_on_exceptions_thrown_in_same_context=True,
            ignore_exceptions_thrown_in_lines_with_ignore_exception=True,
            ignore_libraries=True,
            exceptions=("IndexError",),
        )

        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION, line=writer.get_line_index_with_content("reraise on method2"))
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_case_settrace(case_setup):
    with case_setup.test_file("_debugger_case_settrace.py") as writer:
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(line=12)
        writer.write_run_thread(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(line=7)
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(True, reason="This is *very* flaky.")
def test_case_scapy(case_setup):
    with case_setup.test_file("_debugger_case_scapy.py") as writer:
        writer.FORCE_KILL_PROCESS_WHEN_FINISHED_OK = True
        writer.reader_thread.set_messages_timeout(30)  # Starting scapy may be slow (timed out with 15 seconds on appveyor).
        writer.write_add_breakpoint(2, None)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()
        thread_id = hit.thread_id
        frame_id = hit.frame_id

        writer.write_run_thread(thread_id)
        writer.finished_ok = True


@pytest.mark.skipif(IS_APPVEYOR or IS_JYTHON, reason="Flaky on appveyor / Jython encoding issues (needs investigation).")
def test_redirect_output(case_setup):
    def get_environ(writer):
        env = os.environ.copy()

        env["PYTHONIOENCODING"] = "utf-8"
        return env

    with case_setup.test_file("_debugger_case_redirect.py", get_environ=get_environ) as writer:
        original_ignore_stderr_line = writer._ignore_stderr_line

        @overrides(writer._ignore_stderr_line)
        def _ignore_stderr_line(line):
            if original_ignore_stderr_line(line):
                return True

            binary_junk = b"\xe8\xf0\x80\x80\x80"
            if sys.version_info[0] >= 3:
                binary_junk = binary_junk.decode("utf-8", "replace")

            return line.startswith(
                (
                    "text",
                    "binary",
                    "a",
                    binary_junk,
                )
            )

        writer._ignore_stderr_line = _ignore_stderr_line

        # Note: writes to stdout and stderr are now synchronous (so, the order
        # must always be consistent and there's a message for each write).
        expected = [
            "text\n",
            "binary or text\n",
            "ação1\n",
        ]

        if sys.version_info[0] >= 3:
            expected.extend(
                (
                    "binary\n",
                    "ação2\n".encode(encoding="latin1").decode("utf-8", "replace"),
                    "ação3\n",
                )
            )

        binary_junk = "\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\n\n"
        if sys.version_info[0] >= 3:
            binary_junk = "\ufffd\ufffd\ufffd\ufffd\ufffd\n\n"
        expected.append(binary_junk)

        new_expected = [(x, "stdout") for x in expected]
        new_expected.extend([(x, "stderr") for x in expected])

        writer.write_start_redirect()

        writer.write_make_initial_run()
        msgs = []
        ignored = []
        while len(msgs) < len(new_expected):
            try:
                msg = writer.wait_for_output()
            except AssertionError:
                for msg in msgs:
                    sys.stderr.write("Found: %s\n" % (msg,))
                for msg in new_expected:
                    sys.stderr.write("Expected: %s\n" % (msg,))
                for msg in ignored:
                    sys.stderr.write("Ignored: %s\n" % (msg,))
                raise
            if msg not in new_expected:
                ignored.append(msg)
                continue
            msgs.append(msg)

        if msgs != new_expected:
            print(msgs)
            print(new_expected)
        assert msgs == new_expected
        writer.finished_ok = True


def _path_equals(path1, path2):
    path1 = pydevd_file_utils.normcase(path1)
    path2 = pydevd_file_utils.normcase(path2)
    return path1 == path2


@pytest.mark.parametrize("mixed_case", [True, False] if sys.platform == "win32" else [False])
def test_path_translation(case_setup, mixed_case):
    def get_file_in_client(writer):
        # Instead of using: test_python/_debugger_case_path_translation.py
        # we'll set the breakpoints at foo/_debugger_case_path_translation.py
        file_in_client = os.path.dirname(os.path.dirname(writer.TEST_FILE))
        return os.path.join(os.path.dirname(file_in_client), "foo", "_debugger_case_path_translation.py")

    def get_environ(writer):
        import json

        env = os.environ.copy()

        env["PYTHONIOENCODING"] = "utf-8"

        assert writer.TEST_FILE.endswith("_debugger_case_path_translation.py")
        file_in_client = get_file_in_client(writer)
        if mixed_case:
            new_file_in_client = "".join(
                [file_in_client[i].upper() if i % 2 == 0 else file_in_client[i].lower() for i in range(len(file_in_client))]
            )
            assert _path_equals(file_in_client, new_file_in_client)
        env["PATHS_FROM_ECLIPSE_TO_PYTHON"] = json.dumps([(os.path.dirname(file_in_client), os.path.dirname(writer.TEST_FILE))])
        return env

    with case_setup.test_file("_debugger_case_path_translation.py", get_environ=get_environ) as writer:
        from tests_python.debugger_unittest import CMD_LOAD_SOURCE

        writer.write_start_redirect()

        file_in_client = get_file_in_client(writer)
        assert "tests_python" not in file_in_client
        writer.write_add_breakpoint(writer.get_line_index_with_content("break here"), "call_this", filename=file_in_client)
        writer.write_make_initial_run()

        xml = writer.wait_for_message(lambda msg: 'stop_reason="111"' in msg)
        assert xml.thread.frame[0]["file"] == file_in_client
        thread_id = xml.thread["id"]

        # Request a file that exists
        files_to_match = [file_in_client]
        if IS_WINDOWS:
            files_to_match.append(file_in_client.upper())
        for f in files_to_match:
            writer.write_load_source(f)
            writer.wait_for_message(
                lambda msg: "%s\t" % CMD_LOAD_SOURCE in msg
                and "def main():" in msg
                and "print('break here')" in msg
                and "print('TEST SUCEEDED!')" in msg,
                expect_xml=False,
            )

        # Request a file that does not exist
        writer.write_load_source(file_in_client + "not_existent.py")
        writer.wait_for_message(lambda msg: "901\t" in msg and ("FileNotFoundError" in msg or "IOError" in msg), expect_xml=False)

        writer.write_run_thread(thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(not IS_CPYTHON, reason="CPython only test.")
def test_linecache_xml(case_setup, tmpdir):
    from _pydevd_bundle.pydevd_comm_constants import CMD_LOAD_SOURCE_FROM_FRAME_ID

    with case_setup.test_file("_debugger_case_linecache.py") as writer:
        writer.write_add_breakpoint(writer.get_line_index_with_content("breakpoint"))
        writer.write_make_initial_run()

        # First hit is for breakpoint reached via a stack frame that doesn't have source.
        hit = writer.wait_for_breakpoint_hit()

        writer.write_get_thread_stack(hit.thread_id)
        msg = writer.wait_for_get_thread_stack_message()
        frame_ids = set()
        for frame in msg.thread.frame:
            if frame["file"] == "<foo bar>":
                frame_ids.add(frame["id"])

        assert len(frame_ids) == 2

        for frame_id in frame_ids:
            writer.write_load_source_from_frame_id(frame_id)
            writer.wait_for_message(
                lambda msg: "%s\t" % CMD_LOAD_SOURCE_FROM_FRAME_ID in msg
                and ("[x for x in range(10)]" in msg and "def somemethod():" in msg),
                expect_xml=False,
            )

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(not IS_CPYTHON, reason="CPython only test.")
def test_show_bytecode_xml(case_setup, tmpdir):
    from _pydevd_bundle.pydevd_comm_constants import CMD_LOAD_SOURCE_FROM_FRAME_ID

    with case_setup.test_file("_debugger_case_show_bytecode.py") as writer:
        writer.write_add_breakpoint(writer.get_line_index_with_content("breakpoint"))
        writer.write_make_initial_run()

        # First hit is for breakpoint reached via a stack frame that doesn't have source.
        hit = writer.wait_for_breakpoint_hit()

        writer.write_get_thread_stack(hit.thread_id)
        msg = writer.wait_for_get_thread_stack_message()
        frame_ids = set()
        for frame in msg.thread.frame:
            if frame["file"] == "<something>":
                frame_ids.add(frame["id"])

        assert len(frame_ids) == 2

        for frame_id in frame_ids:
            writer.write_load_source_from_frame_id(frame_id)
            writer.wait_for_message(
                lambda msg: "%s\t" % CMD_LOAD_SOURCE_FROM_FRAME_ID in msg and ("MyClass" in msg or "foo()" in msg), expect_xml=False
            )

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_evaluate_errors(case_setup):
    with case_setup.test_file("_debugger_case_local_variables.py") as writer:
        writer.write_add_breakpoint(writer.get_line_index_with_content("Break here"), "Call")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()
        thread_id = hit.thread_id
        frame_id = hit.frame_id

        writer.write_evaluate_expression("%s\t%s\t%s" % (thread_id, frame_id, "LOCAL"), "name_error")
        writer.wait_for_evaluation('<var name="name_error" type="NameError"')
        writer.write_run_thread(thread_id)
        writer.finished_ok = True


def test_list_threads(case_setup):
    with case_setup.test_file("_debugger_case_local_variables.py") as writer:
        writer.write_add_breakpoint(writer.get_line_index_with_content("Break here"), "Call")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()
        thread_id = hit.thread_id
        frame_id = hit.frame_id

        seq = writer.write_list_threads()
        msg = writer.wait_for_list_threads(seq)
        assert msg.thread["name"] == "MainThread"
        assert msg.thread["id"].startswith("pid")
        writer.write_run_thread(thread_id)
        writer.finished_ok = True


def test_case_print(case_setup):
    with case_setup.test_file("_debugger_case_print.py") as writer:
        writer.write_add_breakpoint(1, "None")
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()
        thread_id = hit.thread_id
        frame_id = hit.frame_id

        writer.write_run_thread(thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Not working on Jython (needs to be investigated).")
def test_case_lamdda(case_setup):
    if TODO_PYPY:
        raise pytest.skip("Not ok in pypy")
    with case_setup.test_file("_debugger_case_lamda.py") as writer:
        writer.write_add_breakpoint(writer.get_line_index_with_content("Break here"), "None")
        writer.write_make_initial_run()

        for _ in range(3):  # We'll hit the same breakpoint 3 times.
            hit = writer.wait_for_breakpoint_hit()

            writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Not working on Jython (needs to be investigated).")
def test_case_lamdda_multiline(case_setup):
    with case_setup.test_file("_debugger_case_lambda_multiline.py") as writer:
        writer.write_add_breakpoint(writer.get_line_index_with_content("Break here"), "None")
        writer.write_make_initial_run()

        for _ in range(2):  # We'll hit the same breakpoint 2 times.
            hit = writer.wait_for_breakpoint_hit()

            writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Not working on Jython (needs to be investigated).")
def test_case_method_single_line(case_setup):
    if TODO_PYPY:
        raise pytest.skip("Not ok in pypy")
    with case_setup.test_file("_debugger_case_method_single_line.py") as writer:
        writer.write_add_breakpoint(writer.get_line_index_with_content("Break here"), "None")
        writer.write_make_initial_run()

        for _ in range(3):
            # We'll hit the same breakpoint 3 times (method creation,
            # method line for each call).
            hit = writer.wait_for_breakpoint_hit()

            writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Not working properly on Jython (needs investigation).")
def test_case_suspension_policy(case_setup):
    with case_setup.test_file("_debugger_case_suspend_policy.py") as writer:
        writer.write_add_breakpoint(25, "", suspend_policy="ALL")
        writer.write_make_initial_run()

        thread_ids = []
        for i in range(3):
            writer.log.append("Waiting for thread %s of 3 to stop" % (i + 1,))
            # One thread is suspended with a breakpoint hit and the other 2 as thread suspended.
            hit = writer.wait_for_breakpoint_hit((REASON_STOP_ON_BREAKPOINT, REASON_THREAD_SUSPEND))
            thread_ids.append(hit.thread_id)

        for thread_id in thread_ids:
            writer.write_run_thread(thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Flaky on Jython (needs investigation).")
def test_case_get_thread_stack(case_setup):
    with case_setup.test_file("_debugger_case_get_thread_stack.py") as writer:
        original_ignore_stderr_line = writer._ignore_stderr_line

        @overrides(writer._ignore_stderr_line)
        def _ignore_stderr_line(line):
            if original_ignore_stderr_line(line):
                return True

            if IS_JYTHON:
                for expected in (
                    "RuntimeWarning: Parent module '_pydev_bundle' not found while handling absolute import",
                    "from java.lang import System",
                ):
                    if expected in line:
                        return True

            return False

        writer._ignore_stderr_line = _ignore_stderr_line
        writer.write_add_breakpoint(18, None)
        writer.write_make_initial_run()

        thread_created_msgs = [writer.wait_for_message(CMD_THREAD_CREATE)]
        thread_created_msgs.append(writer.wait_for_message(CMD_THREAD_CREATE))
        thread_id_to_name = {}
        for msg in thread_created_msgs:
            thread_id_to_name[msg.thread["id"]] = msg.thread["name"]
        assert len(thread_id_to_name) == 2

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT)
        assert hit.thread_id in thread_id_to_name

        for request_thread_id in thread_id_to_name:
            writer.write_get_thread_stack(request_thread_id)
            msg = writer.wait_for_get_thread_stack_message()
            files = [frame["file"] for frame in msg.thread.frame]
            assert msg.thread["id"] == request_thread_id
            if not files[0].endswith("_debugger_case_get_thread_stack.py"):
                raise AssertionError("Expected to find _debugger_case_get_thread_stack.py in files[0]. Found: %s" % ("\n".join(files),))

            if [filename for filename in files if filename.endswith("pydevd.py")]:
                raise AssertionError("Did not expect to find pydevd.py. Found: %s" % ("\n".join(files),))
            if request_thread_id == hit.thread_id:
                assert len(msg.thread.frame) == 0  # In main thread (must have no back frames).
                assert msg.thread.frame["name"] == "<module>"
            else:
                assert len(msg.thread.frame) > 1  # Stopped in threading (must have back frames).
                assert msg.thread.frame[0]["name"] == "method"

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_case_dump_threads_to_stderr(case_setup):
    from tests_python.debugger_unittest import wait_for_condition

    def additional_output_checks(writer, stdout, stderr):
        assert is_stderr_ok(stderr), make_error_msg(stderr)

    def make_error_msg(stderr):
        return "Did not find thread dump in stderr. stderr:\n%s" % (stderr,)

    def is_stderr_ok(stderr):
        return "Thread Dump" in stderr and "Thread pydevd.CommandThread  (daemon: True, pydevd thread: True)" in stderr

    with case_setup.test_file("_debugger_case_get_thread_stack.py", additional_output_checks=additional_output_checks) as writer:
        writer.write_add_breakpoint(12, None)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT)

        writer.write_dump_threads()
        wait_for_condition(lambda: is_stderr_ok(writer.get_stderr()), lambda: make_error_msg(writer.get_stderr()))
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_stop_on_start_regular(case_setup):
    if TODO_PYPY:
        raise pytest.skip("Not ok in pypy")

    with case_setup.test_file("_debugger_case_simple_calls.py") as writer:
        writer.write_stop_on_start()
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_START, file="_debugger_case_simple_calls.py", line=1)

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def _get_breakpoint_cases():
    if sys.version_info >= (3, 7):
        # Just check breakpoint()
        return ("_debugger_case_breakpoint.py",)
    else:
        # Check breakpoint() and sys.__breakpointhook__ replacement.
        return ("_debugger_case_breakpoint.py", "_debugger_case_breakpoint2.py")


@pytest.mark.parametrize("filename", _get_breakpoint_cases())
def test_py_37_breakpoint(case_setup, filename):
    with case_setup.test_file(filename) as writer:
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(file=filename)
        assert hit.line in (3, 6), "Expected hit in line 3 or 6. Found at: %s" % (hit.line,)

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def _get_generator_cases():
    # On py3 we should check both versions.
    return (
        "_debugger_case_generator_py2.py",
        "_debugger_case_generator_py3.py",
    )


@pytest.mark.parametrize("filename", _get_generator_cases())
def test_generator_cases(case_setup, filename):
    with case_setup.test_file(filename) as writer:
        writer.write_add_breakpoint(writer.get_line_index_with_content("break here"))
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_stop_on_start_m_switch(case_setup_m_switch):
    if TODO_PYPY:
        raise pytest.skip("Not ok in pypy")

    with case_setup_m_switch.test_file() as writer:
        writer.write_stop_on_start()
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_START, file="_debugger_case_m_switch.py", line=1)

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


def test_stop_on_start_entry_point(case_setup_m_switch_entry_point):
    with case_setup_m_switch_entry_point.test_file() as writer:
        writer.write_stop_on_start()
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_START, file="_debugger_case_module_entry_point.py", line=1)

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Not working properly on Jython (needs investigation).")
def test_debug_zip_files(case_setup, tmpdir):
    def get_environ(writer):
        env = os.environ.copy()
        curr_pythonpath = env.get("PYTHONPATH", "")

        curr_pythonpath = str(tmpdir.join("myzip.zip")) + os.pathsep + curr_pythonpath
        curr_pythonpath = str(tmpdir.join("myzip2.egg!")) + os.pathsep + curr_pythonpath
        env["PYTHONPATH"] = curr_pythonpath

        env["IDE_PROJECT_ROOTS"] = str(tmpdir.join("myzip.zip"))
        return env

    import zipfile

    zip_file = zipfile.ZipFile(str(tmpdir.join("myzip.zip")), "w")
    zip_file.writestr("zipped/__init__.py", "")
    zip_file.writestr("zipped/zipped_contents.py", "def call_in_zip():\n    return 1")
    zip_file.close()

    zip_file = zipfile.ZipFile(str(tmpdir.join("myzip2.egg!")), "w")
    zip_file.writestr("zipped2/__init__.py", "")
    zip_file.writestr("zipped2/zipped_contents2.py", "def call_in_zip2():\n    return 1")
    zip_file.close()

    with case_setup.test_file("_debugger_case_zip_files.py", get_environ=get_environ) as writer:
        writer.write_add_breakpoint(2, "None", filename=os.path.join(str(tmpdir.join("myzip.zip")), "zipped", "zipped_contents.py"))

        writer.write_add_breakpoint(2, "None", filename=os.path.join(str(tmpdir.join("myzip2.egg!")), "zipped2", "zipped_contents2.py"))

        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit()
        assert hit.name == "call_in_zip"
        writer.write_run_thread(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit()
        assert hit.name == "call_in_zip2"
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(not IS_CPYTHON, reason="CPython only test.")
@pytest.mark.parametrize(
    "file_to_check",
    [
        "_debugger_case_multiprocessing_2.py",
        "_debugger_case_multiprocessing.py",
        "_debugger_case_python_c.py",
        "_debugger_case_multiprocessing_pool.py",
    ],
)
def test_multiprocessing_simple(case_setup_multiprocessing, file_to_check):
    import threading
    from tests_python.debugger_unittest import AbstractWriterThread

    with case_setup_multiprocessing.test_file(file_to_check) as writer:
        break1_line = writer.get_line_index_with_content("break 1 here")
        break2_line = writer.get_line_index_with_content("break 2 here")

        writer.write_add_breakpoint(break1_line)
        writer.write_add_breakpoint(break2_line)

        server_socket = writer.server_socket

        class SecondaryProcessWriterThread(AbstractWriterThread):
            TEST_FILE = writer.get_main_filename()
            _sequence = -1

        class SecondaryProcessThreadCommunication(threading.Thread):
            def run(self):
                from tests_python.debugger_unittest import ReaderThread

                expected_connections = 2 if sys.platform == "darwin" else 1

                for _ in range(expected_connections):
                    server_socket.listen(1)
                    self.server_socket = server_socket
                    new_sock, addr = server_socket.accept()

                    reader_thread = ReaderThread(new_sock)
                    reader_thread.name = "  *** Multiprocess Reader Thread"
                    reader_thread.start()

                    writer2 = SecondaryProcessWriterThread()

                    writer2.reader_thread = reader_thread
                    writer2.sock = new_sock

                    writer2.write_version()
                    writer2.write_add_breakpoint(break1_line)
                    writer2.write_add_breakpoint(break2_line)
                    writer2.write_make_initial_run()

                hit = writer2.wait_for_breakpoint_hit()
                writer2.write_run_thread(hit.thread_id)

        secondary_process_thread_communication = SecondaryProcessThreadCommunication()
        secondary_process_thread_communication.start()
        writer.write_make_initial_run()
        hit2 = writer.wait_for_breakpoint_hit()
        secondary_process_thread_communication.join(10)
        if secondary_process_thread_communication.is_alive():
            raise AssertionError("The SecondaryProcessThreadCommunication did not finish")
        writer.write_run_thread(hit2.thread_id)
        writer.finished_ok = True


@pytest.mark.skipif(not IS_CPYTHON, reason="CPython only test.")
@pytest.mark.parametrize("count", range(5))  # Call multiple times to exercise timing issues.
def test_multiprocessing_with_stopped_breakpoints(case_setup_multiprocessing, count, debugger_runner_simple):
    import threading
    from tests_python.debugger_unittest import AbstractWriterThread

    with case_setup_multiprocessing.test_file("_debugger_case_multiprocessing_stopped_threads.py") as writer:
        break_main_line = writer.get_line_index_with_content("break in main here")
        break_thread_line = writer.get_line_index_with_content("break in thread here")
        break_process_line = writer.get_line_index_with_content("break in process here")

        writer.write_add_breakpoint(break_main_line)
        writer.write_add_breakpoint(break_thread_line)
        writer.write_add_breakpoint(break_process_line)

        server_socket = writer.server_socket
        listening_event = threading.Event()

        class SecondaryProcessWriterThread(AbstractWriterThread):
            TEST_FILE = writer.get_main_filename()
            _sequence = -1

        class SecondaryProcessThreadCommunication(threading.Thread):
            def run(self):
                from tests_python.debugger_unittest import ReaderThread

                server_socket.listen(1)
                self.server_socket = server_socket
                listening_event.set()
                writer.log.append("  *** Multiprocess waiting on server_socket.accept()")
                new_sock, addr = server_socket.accept()

                reader_thread = ReaderThread(new_sock)
                reader_thread.name = "  *** Multiprocess Reader Thread"
                reader_thread.start()
                writer.log.append("  *** Multiprocess started ReaderThread")

                writer2 = SecondaryProcessWriterThread()
                writer2._WRITE_LOG_PREFIX = "  *** Multiprocess write: "
                writer2.log = writer.log

                writer2.reader_thread = reader_thread
                writer2.sock = new_sock

                writer2.write_version()
                writer2.write_add_breakpoint(break_main_line)
                writer2.write_add_breakpoint(break_thread_line)
                writer2.write_add_breakpoint(break_process_line)
                writer2.write_make_initial_run()
                hit = writer2.wait_for_breakpoint_hit()
                writer2.write_run_thread(hit.thread_id)

        secondary_process_thread_communication = SecondaryProcessThreadCommunication()
        secondary_process_thread_communication.start()

        ok = listening_event.wait(timeout=10)
        assert ok
        writer.write_make_initial_run()
        hit2 = writer.wait_for_breakpoint_hit()  # Breaks in thread.
        writer.write_step_over(hit2.thread_id)

        hit2 = writer.wait_for_breakpoint_hit(REASON_STEP_OVER)  # line == event.set()

        # paused on breakpoint, will start process and pause on main thread
        # in the main process too.
        writer.write_step_over(hit2.thread_id)

        # Note: ignore the step over hit (go only for the breakpoint hit).
        main_hit = writer.wait_for_breakpoint_hit(REASON_STOP_ON_BREAKPOINT)

        secondary_process_thread_communication.join(10)
        if secondary_process_thread_communication.is_alive():
            raise AssertionError("The SecondaryProcessThreadCommunication did not finish")

        writer.write_run_thread(hit2.thread_id)
        writer.write_run_thread(main_hit.thread_id)

        # We must have found at least 2 debug files when doing multiprocessing (one for
        # each pid).
        assert len(pydev_log.list_log_files(debugger_runner_simple.pydevd_debug_file)) == 2
        writer.finished_ok = True


@pytest.mark.skipif(not IS_CPYTHON, reason="CPython only test.")
@pytest.mark.parametrize("target", ["_debugger_case_quoting.py", "_debugger_case_subprocess_zip.py"])
def test_subprocess_quoted_args(case_setup_multiprocessing, target):
    from tests_python.debugger_unittest import AbstractWriterThread

    with case_setup_multiprocessing.test_file(target) as writer:
        break_subprocess_line = writer.get_line_index_with_content("break here")

        writer.write_add_breakpoint(break_subprocess_line)

        server_socket = writer.server_socket

        class SecondaryProcessWriterThread(AbstractWriterThread):
            TEST_FILE = writer.get_main_filename()
            _sequence = -1

        class SecondaryProcessThreadCommunication(threading.Thread):
            def run(self):
                from tests_python.debugger_unittest import ReaderThread
                # Note: on linux on Python 2 because on Python 2 CPython subprocess.call will actually
                # create a fork first (at which point it'll connect) and then, later on it'll
                # call the main (as if it was a clean process as if PyDB wasn't created
                # the first time -- the debugger will still work, but it'll do an additional
                # connection.

                expected_connections = 1

                for _ in range(expected_connections):
                    server_socket.listen(1)
                    self.server_socket = server_socket
                    new_sock, addr = server_socket.accept()

                    reader_thread = ReaderThread(new_sock)
                    reader_thread.name = "  *** Multiprocess Reader Thread"
                    reader_thread.start()

                    writer2 = SecondaryProcessWriterThread()

                    writer2.reader_thread = reader_thread
                    writer2.sock = new_sock

                    writer2.write_version()
                    writer2.write_add_breakpoint(break_subprocess_line)
                    writer2.write_make_initial_run()
                hit = writer2.wait_for_breakpoint_hit()
                writer2.write_run_thread(hit.thread_id)

        secondary_process_thread_communication = SecondaryProcessThreadCommunication()
        secondary_process_thread_communication.start()
        writer.write_make_initial_run()

        secondary_process_thread_communication.join(10)
        if secondary_process_thread_communication.is_alive():
            raise AssertionError("The SecondaryProcessThreadCommunication did not finish")

        writer.finished_ok = True


def _attach_to_writer_pid(writer):
    import pydevd

    assert writer.process is not None

    def attach():
        attach_pydevd_file = os.path.join(os.path.dirname(pydevd.__file__), "pydevd_attach_to_process", "attach_pydevd.py")
        subprocess.call([sys.executable, attach_pydevd_file, "--pid", str(writer.process.pid), "--port", str(writer.port)])

    threading.Thread(target=attach).start()

    wait_for_condition(lambda: writer.finished_initialization)


@pytest.mark.skipif(not IS_CPYTHON or IS_MAC or not SUPPORT_ATTACH_TO_PID, reason="CPython only test (brittle on Mac).")
@pytest.mark.parametrize("reattach", [True, False])
def test_attach_to_pid_no_threads(case_setup_remote, reattach):
    with case_setup_remote.test_file("_debugger_case_attach_to_pid_simple.py", wait_for_port=False) as writer:
        time.sleep(1)  # Give it some time to initialize to get to the while loop.
        _attach_to_writer_pid(writer)

        bp_line = writer.get_line_index_with_content("break here")
        bp_id = writer.write_add_breakpoint(bp_line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(line=bp_line)

        if reattach:
            # This would be the same as a second attach to pid, so, the idea is closing the current
            # connection and then doing a new attach to pid.
            writer.write_remove_breakpoint(bp_id)
            writer.write_run_thread(hit.thread_id)

            writer.do_kill()  # This will simply close the open sockets without doing anything else.
            time.sleep(1)

            t = threading.Thread(target=writer.start_socket)
            t.start()
            wait_for_condition(lambda: hasattr(writer, "port"))
            time.sleep(1)
            writer.process = writer.process
            _attach_to_writer_pid(writer)
            wait_for_condition(lambda: hasattr(writer, "reader_thread"))
            time.sleep(1)

            bp_id = writer.write_add_breakpoint(bp_line)
            writer.write_make_initial_run()

            hit = writer.wait_for_breakpoint_hit(line=bp_line)

        writer.write_change_variable(hit.thread_id, hit.frame_id, "wait", "False")
        writer.wait_for_var('<xml><var name="" type="bool"')

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(
    not IS_CPYTHON or IS_MAC or not SUPPORT_ATTACH_TO_PID or IS_PY312_OR_GREATER, reason="CPython only test (brittle on Mac)."
)
def test_attach_to_pid_halted(case_setup_remote):
    with case_setup_remote.test_file("_debugger_case_attach_to_pid_multiple_threads.py", wait_for_port=False) as writer:
        time.sleep(1)  # Give it some time to initialize and get to the proper halting condition
        _attach_to_writer_pid(writer)

        bp_line = writer.get_line_index_with_content("break thread here")
        writer.write_add_breakpoint(bp_line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(line=bp_line)

        writer.write_change_variable(hit.thread_id, hit.frame_id, "wait", "False")
        writer.wait_for_var('<xml><var name="" type="bool"')

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(not IS_CPYTHON, reason="CPython only test.")
def test_remote_debugger_basic(case_setup_remote):
    with case_setup_remote.test_file("_debugger_case_remote.py") as writer:
        writer.log.append("making initial run")
        writer.write_make_initial_run()

        writer.log.append("waiting for breakpoint hit")
        hit = writer.wait_for_breakpoint_hit()

        writer.log.append("run thread")
        writer.write_run_thread(hit.thread_id)

        writer.log.append("asserting")
        try:
            assert 5 == writer._sequence, "Expected 5. Had: %s" % writer._sequence
        except:
            writer.log.append("assert failed!")
            raise
        writer.log.append("asserted")

        writer.finished_ok = True


@pytest.mark.skipif(not IS_CPYTHON, reason="CPython only test.")
def test_remote_debugger_threads(case_setup_remote):
    with case_setup_remote.test_file("_debugger_case_remote_threads.py") as writer:
        writer.write_make_initial_run()

        hit_in_main = writer.wait_for_breakpoint_hit()

        bp_line = writer.get_line_index_with_content("break here")
        writer.write_add_breakpoint(bp_line)

        # Break in the 2 threads.
        hit_in_thread1 = writer.wait_for_breakpoint_hit(line=bp_line)
        hit_in_thread2 = writer.wait_for_breakpoint_hit(line=bp_line)

        writer.write_change_variable(hit_in_thread1.thread_id, hit_in_thread1.frame_id, "wait", "False")
        writer.wait_for_var('<xml><var name="" type="bool"')
        writer.write_change_variable(hit_in_thread2.thread_id, hit_in_thread2.frame_id, "wait", "False")
        writer.wait_for_var('<xml><var name="" type="bool"')

        writer.write_run_thread(hit_in_main.thread_id)
        writer.write_run_thread(hit_in_thread1.thread_id)
        writer.write_run_thread(hit_in_thread2.thread_id)
        writer.finished_ok = True


@pytest.mark.skipif(not IS_CPYTHON, reason="CPython only test.")
def test_py_37_breakpoint_remote(case_setup_remote):
    with case_setup_remote.test_file("_debugger_case_breakpoint_remote.py") as writer:
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(
            filename="_debugger_case_breakpoint_remote.py",
            line=13,
        )

        writer.write_run_thread(hit.thread_id)

        try:
            assert 5 == writer._sequence, "Expected 5. Had: %s" % writer._sequence
        except:
            writer.log.append("assert failed!")
            raise
        writer.log.append("asserted")

        writer.finished_ok = True


@pytest.mark.skipif(not IS_CPYTHON, reason="CPython only test.")
def test_py_37_breakpoint_remote_no_import(case_setup_remote):
    def get_environ(writer):
        env = os.environ.copy()
        curr_pythonpath = env.get("PYTHONPATH", "")

        pydevd_dirname = os.path.join(os.path.dirname(writer.get_pydevd_file()), "pydev_sitecustomize")

        curr_pythonpath = pydevd_dirname + os.pathsep + curr_pythonpath
        env["PYTHONPATH"] = curr_pythonpath
        return env

    with case_setup_remote.test_file("_debugger_case_breakpoint_remote_no_import.py", get_environ=get_environ) as writer:
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(
            filename="_debugger_case_breakpoint_remote_no_import.py",
            line=12,
        )

        writer.write_run_thread(hit.thread_id)

        try:
            assert 5 == writer._sequence, "Expected 5. Had: %s" % writer._sequence
        except:
            writer.log.append("assert failed!")
            raise
        writer.log.append("asserted")

        writer.finished_ok = True


@pytest.mark.skipif(not IS_CPYTHON, reason="CPython only test.")
@pytest.mark.parametrize("authenticate", [True, False])
def test_remote_debugger_multi_proc(case_setup_remote, authenticate):
    access_token = None
    client_access_token = None
    if authenticate:
        access_token = "tok123"
        client_access_token = "tok456"

    class _SecondaryMultiProcProcessWriterThread(debugger_unittest.AbstractWriterThread):
        FORCE_KILL_PROCESS_WHEN_FINISHED_OK = True

        def __init__(self, server_socket):
            debugger_unittest.AbstractWriterThread.__init__(self)
            self.server_socket = server_socket

        def run(self):
            print("waiting for second process")
            self.sock, addr = self.server_socket.accept()
            print("accepted second process")

            from tests_python.debugger_unittest import ReaderThread

            self.reader_thread = ReaderThread(self.sock)
            self.reader_thread.name = "Secondary Reader Thread"
            self.reader_thread.start()

            self._sequence = -1
            # initial command is always the version
            self.write_version()

            if authenticate:
                self.wait_for_message(lambda msg: "Client not authenticated." in msg, expect_xml=False)
                self.write_authenticate(access_token=access_token, client_access_token=client_access_token)
                self.write_version()

            self.log.append("start_socket")
            self.write_make_initial_run()
            time.sleep(0.5)
            self.finished_ok = True

    def do_kill(writer):
        debugger_unittest.AbstractWriterThread.do_kill(writer)
        if hasattr(writer, "secondary_multi_proc_process_writer"):
            writer.secondary_multi_proc_process_writer.do_kill()

    with case_setup_remote.test_file(
        "_debugger_case_remote_1.py",
        do_kill=do_kill,
        EXPECTED_RETURNCODE="any",
        access_token=access_token,
        client_access_token=client_access_token,
    ) as writer:
        # It seems sometimes it becomes flaky on the ci because the process outlives the writer thread...
        # As we're only interested in knowing if a second connection was received, just kill the related
        # process.
        assert hasattr(writer, "FORCE_KILL_PROCESS_WHEN_FINISHED_OK")
        writer.FORCE_KILL_PROCESS_WHEN_FINISHED_OK = True

        writer.log.append("making initial run")
        writer.write_make_initial_run()

        if authenticate:
            writer.wait_for_message(lambda msg: "Client not authenticated." in msg, expect_xml=False)
            writer.write_authenticate(access_token=access_token, client_access_token=client_access_token)
            writer.write_make_initial_run()

        writer.log.append("waiting for breakpoint hit")
        hit = writer.wait_for_breakpoint_hit()

        writer.secondary_multi_proc_process_writer = secondary_multi_proc_process_writer = _SecondaryMultiProcProcessWriterThread(
            writer.server_socket
        )
        secondary_multi_proc_process_writer.start()

        writer.log.append("run thread")
        writer.write_run_thread(hit.thread_id)

        for _i in range(400):
            if secondary_multi_proc_process_writer.finished_ok:
                break
            time.sleep(0.1)
        else:
            writer.log.append("Secondary process not finished ok!")
            raise AssertionError("Secondary process not finished ok!")

        writer.log.append("Secondary process finished!")
        try:
            assert writer._sequence == 5 if not authenticate else 9, "Found: %s" % writer._sequence
        except:
            writer.log.append("assert failed!")
            raise
        writer.log.append("asserted")

        writer.finished_ok = True


@pytest.mark.parametrize("handle", [True, False])
@pytest.mark.skipif(not IS_CPYTHON, reason="CPython only test.")
def test_remote_unhandled_exceptions(case_setup_remote, handle):
    def check_test_suceeded_msg(writer, stdout, stderr):
        return "TEST SUCEEDED" in "".join(stderr)

    def additional_output_checks(writer, stdout, stderr):
        # Don't call super as we have an expected exception
        assert "ValueError: TEST SUCEEDED" in stderr

    with case_setup_remote.test_file(
        "_debugger_case_remote_unhandled_exceptions.py",
        additional_output_checks=additional_output_checks,
        check_test_suceeded_msg=check_test_suceeded_msg,
        EXPECTED_RETURNCODE=1,
    ) as writer:
        writer.log.append("making initial run")
        writer.write_make_initial_run()

        writer.log.append("waiting for breakpoint hit")
        hit = writer.wait_for_breakpoint_hit()

        # Add, remove and add back
        writer.write_add_exception_breakpoint_with_policy("Exception", "0", "1", "0")
        writer.write_remove_exception_breakpoint("Exception")
        writer.write_add_exception_breakpoint_with_policy("Exception", "0", "1", "0")

        if not handle:
            writer.write_remove_exception_breakpoint("Exception")

        writer.log.append("run thread")
        writer.write_run_thread(hit.thread_id)

        if handle:
            writer.log.append("waiting for uncaught exception")
            hit = writer.wait_for_breakpoint_hit(REASON_UNCAUGHT_EXCEPTION)
            writer.write_run_thread(hit.thread_id)

        writer.log.append("finished ok")
        writer.finished_ok = True


def test_trace_dispatch_correct(case_setup):
    def get_environ(writer):
        env = os.environ.copy()
        env["PYDEVD_USE_FRAME_EVAL"] = "NO"  # This test checks trace dispatch (so, disable frame eval).
        return env

    with case_setup.test_file("_debugger_case_trace_dispatch.py", get_environ=get_environ) as writer:
        breakpoint_id = writer.write_add_breakpoint(5, "method")
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit()
        writer.write_remove_breakpoint(breakpoint_id)
        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_case_single_notification_on_step(case_setup):
    if TODO_PYPY:
        raise pytest.skip("Not ok in pypy")

    with case_setup.test_file("_debugger_case_import_main.py") as writer:
        writer.write_multi_threads_single_notification(True)
        writer.write_add_breakpoint(writer.get_line_index_with_content("break here"), "")
        writer.write_make_initial_run()

        hit = writer.wait_for_single_notification_as_hit()

        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_single_notification_as_hit(reason=REASON_STEP_INTO)

        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_single_notification_as_hit(reason=REASON_STEP_INTO)

        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_single_notification_as_hit(reason=REASON_STEP_INTO)

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Not ok for Jython.")
def test_reload(case_setup, tmpdir):
    path = tmpdir.join("my_temp.py")
    path.write(
        """
import my_temp2
assert my_temp2.call() == 1
a = 10 # break here
assert my_temp2.call() == 2
print('TEST SUCEEDED!')
"""
    )

    path2 = tmpdir.join("my_temp2.py")
    path2.write(
        """
def call():
    return 1
"""
    )
    with case_setup.test_file(str(path)) as writer:
        break_line = writer.get_line_index_with_content("break here")
        writer.write_add_breakpoint(break_line, "")
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit()

        path2 = tmpdir.join("my_temp2.py")
        path2.write(
            """
def call():
    return 2
"""
        )

        writer.write_reload("my_temp2")
        output = writer.wait_for_output()
        output2 = writer.wait_for_output()
        output3 = writer.wait_for_output()

        assert output[0].startswith('code reload: Start reloading module: "my_temp2"')
        assert output2[0].startswith("code reload: Updated function code:")
        assert output3[0].startswith("code reload: reload finished")
        assert output[1] == "stderr"
        assert output2[1] == "stderr"
        assert output3[1] == "stderr"

        writer.wait_for_message(CMD_RELOAD_CODE)
        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Not working with Jython on ci (needs investigation).")
def test_custom_frames(case_setup):
    with case_setup.test_file("_debugger_case_custom_frames.py") as writer:
        writer.write_add_breakpoint(writer.get_line_index_with_content("break here"))
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()

        for i in range(3):
            writer.write_step_over(hit.thread_id)

            # Check that the frame-related threads have been killed.
            for _ in range(i):
                writer.wait_for_message(CMD_THREAD_KILL, expect_xml=False)

            # Main thread stopped
            writer.wait_for_breakpoint_hit(REASON_STEP_OVER)

            # At each time we have an additional custom frame (which is shown as if it
            # was a thread which is created and then suspended).
            for _ in range(i):
                writer.wait_for_message(CMD_THREAD_CREATE)
                writer.wait_for_breakpoint_hit(REASON_THREAD_SUSPEND)

        writer.write_run_thread(hit.thread_id)

        # Check that the frame-related threads have been killed.
        for _ in range(i):
            try:
                writer.wait_for_message(CMD_THREAD_KILL, expect_xml=False, timeout=1)
            except debugger_unittest.TimeoutError:
                # Flaky: sometimes the thread kill is not received because
                # the process exists before the message is sent.
                break

        writer.finished_ok = True


@pytest.mark.skipif(not TEST_GEVENT, reason="Gevent not installed.")
def test_gevent(case_setup):
    def get_environ(writer):
        env = os.environ.copy()
        env["GEVENT_SUPPORT"] = "True"
        return env

    with case_setup.test_file("_debugger_case_gevent.py", get_environ=get_environ) as writer:
        writer.write_add_breakpoint(writer.get_line_index_with_content("break here"))
        writer.write_make_initial_run()
        for _i in range(10):
            hit = writer.wait_for_breakpoint_hit(name="run")
            writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.skipif(
    not TEST_GEVENT or True,  # Skipping as it can be flaky!
    reason="Gevent not installed.",
)
@pytest.mark.parametrize("show", [True, False])
def test_gevent_show_paused_greenlets(case_setup, show):
    def get_environ(writer):
        env = os.environ.copy()
        env["GEVENT_SUPPORT"] = "True"
        if show:
            env["GEVENT_SHOW_PAUSED_GREENLETS"] = "True"
        else:
            env["GEVENT_SHOW_PAUSED_GREENLETS"] = "False"
        return env

    with case_setup.test_file("_debugger_case_gevent_simple.py", get_environ=get_environ) as writer:
        writer.write_add_breakpoint(writer.get_line_index_with_content("break here"))
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit(name="bar")
        writer.write_run_thread(hit.thread_id)

        seq = writer.write_list_threads()
        msg = writer.wait_for_list_threads(seq)

        if show:
            assert len(msg) > 1
        else:
            assert len(msg) == 1

        writer.finished_ok = True


@pytest.mark.skipif(not TEST_GEVENT, reason="Gevent not installed.")
def test_gevent_remote(case_setup_remote):
    def get_environ(writer):
        env = os.environ.copy()
        env["GEVENT_SUPPORT"] = "True"
        return env

    with case_setup_remote.test_file("_debugger_case_gevent.py", get_environ=get_environ, append_command_line_args=["remote"]) as writer:
        writer.write_add_breakpoint(writer.get_line_index_with_content("break here"))
        writer.write_make_initial_run()
        for _i in range(10):
            hit = writer.wait_for_breakpoint_hit(name="run")
            writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_return_value(case_setup):
    with case_setup.test_file("_debugger_case_return_value.py") as writer:
        break_line = writer.get_line_index_with_content("break here")
        writer.write_add_breakpoint(break_line)
        writer.write_show_return_vars()
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit(name="main", line=break_line)

        writer.write_step_over(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(REASON_STEP_OVER, name="main", line=break_line + 1)
        writer.write_get_frame(hit.thread_id, hit.frame_id)
        writer.wait_for_vars(
            [
                [
                    '<var name="method1" type="int" qualifier="%s" value="int: 1" isRetVal="True"' % (builtin_qualifier,),
                    '<var name="method1" type="int"  value="int%253A 1" isRetVal="True"',
                ],
            ]
        )

        writer.write_step_over(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(REASON_STEP_OVER, name="main", line=break_line + 2)
        writer.write_get_frame(hit.thread_id, hit.frame_id)
        writer.wait_for_vars(
            [
                [
                    '<var name="method2" type="int" qualifier="%s" value="int: 2" isRetVal="True"' % (builtin_qualifier,),
                    '<var name="method2" type="int"  value="int%253A 2" isRetVal="True"',
                ],
            ]
        )

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_gettr_warning(case_setup):
    with case_setup.test_file("_debugger_case_warnings.py") as writer:
        break_line = writer.get_line_index_with_content("break here")
        writer.write_add_breakpoint(break_line)
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit(line=break_line)

        writer.write_get_frame(hit.thread_id, hit.frame_id)
        writer.wait_for_vars(
            [
                ['<var name="obj'],
            ]
        )

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Jython can only have one thread stopped at each time.")
@pytest.mark.parametrize("check_single_notification", [True, False])
def test_run_pause_all_threads_single_notification(case_setup, check_single_notification):
    from tests_python.debugger_unittest import TimeoutError

    with case_setup.test_file("_debugger_case_multiple_threads.py") as writer:
        # : :type writer: AbstractWriterThread
        writer.write_multi_threads_single_notification(True)
        writer.write_make_initial_run()

        main_thread_id = writer.wait_for_new_thread()
        thread_id1 = writer.wait_for_new_thread()
        thread_id2 = writer.wait_for_new_thread()

        # Ok, all threads created, let's wait for the main thread to get to the join.
        writer.wait_for_thread_join(main_thread_id)

        writer.write_suspend_thread("*")

        if check_single_notification:
            dct = writer.wait_for_json_message(CMD_THREAD_SUSPEND_SINGLE_NOTIFICATION)
            assert dct["thread_id"] in (thread_id1, thread_id2)
            assert dct["stop_reason"] == REASON_THREAD_SUSPEND
        else:
            # We should have a single thread suspended event for both threads.
            hit0 = writer.wait_for_breakpoint_hit(REASON_THREAD_SUSPEND)
            assert hit0.thread_id in (thread_id1, thread_id2)

            hit1 = writer.wait_for_breakpoint_hit(REASON_THREAD_SUSPEND)
            assert hit1.thread_id in (thread_id1, thread_id2)

            with pytest.raises(TimeoutError):
                # The main thread should not receive a hit as it's effectively deadlocked until other
                # threads finish.
                writer.wait_for_breakpoint_hit(REASON_THREAD_SUSPEND, timeout=1)

        # Doing a step in in one thread, when paused should notify on both threads.
        writer.write_step_over(thread_id1)

        if check_single_notification:
            dct = writer.wait_for_json_message(CMD_THREAD_RESUME_SINGLE_NOTIFICATION)  # Note: prefer wait_for_single_notification_as_hit
            assert dct["thread_id"] == thread_id1

            dct = writer.wait_for_json_message(CMD_THREAD_SUSPEND_SINGLE_NOTIFICATION)  # Note: prefer wait_for_single_notification_as_hit
            assert dct["thread_id"] == thread_id1
            assert dct["stop_reason"] == REASON_STEP_OVER

            hit = writer.get_current_stack_hit(thread_id1)

        else:
            hit = writer.wait_for_breakpoint_hit(CMD_STEP_OVER)

        writer.write_evaluate_expression("%s\t%s\t%s" % (hit.thread_id, hit.frame_id, "LOCAL"), "stop_loop()")
        writer.wait_for_evaluation('<var name="stop_loop()" type="str" qualifier="{0}" value="str: stopped_loop'.format(builtin_qualifier))

        writer.write_run_thread("*")
        writer.finished_ok = True


def scenario_uncaught(writer):
    hit = writer.wait_for_breakpoint_hit()
    writer.write_add_exception_breakpoint_with_policy("ValueError", "0", "1", "0")
    writer.write_run_thread(hit.thread_id)

    hit = writer.wait_for_breakpoint_hit(REASON_UNCAUGHT_EXCEPTION)
    writer.write_run_thread(hit.thread_id)


def scenario_caught(writer):
    hit = writer.wait_for_breakpoint_hit()
    writer.write_add_exception_breakpoint_with_policy("ValueError", "1", "0", "0")
    writer.write_run_thread(hit.thread_id)

    for _ in range(2):
        hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION)
        writer.write_run_thread(hit.thread_id)

    # Note: the one in the top-level will be hit once as caught (but not another time
    # in postmortem mode).
    hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION)
    writer.write_run_thread(hit.thread_id)


def scenario_caught_and_uncaught(writer):
    hit = writer.wait_for_breakpoint_hit()
    writer.write_add_exception_breakpoint_with_policy("ValueError", "1", "1", "0")
    writer.write_run_thread(hit.thread_id)

    for _ in range(2):
        hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION)
        writer.write_run_thread(hit.thread_id)

    # Note: the one in the top-level will be hit once as caught and another in postmortem mode.
    hit = writer.wait_for_breakpoint_hit(REASON_CAUGHT_EXCEPTION)
    writer.write_run_thread(hit.thread_id)

    hit = writer.wait_for_breakpoint_hit(REASON_UNCAUGHT_EXCEPTION)
    writer.write_run_thread(hit.thread_id)


@pytest.mark.skipif(not IS_CPYTHON, reason="CPython only test.")
@pytest.mark.parametrize(
    "check_scenario",
    [
        scenario_uncaught,
        scenario_caught,
        scenario_caught_and_uncaught,
    ],
)
def test_top_level_exceptions_on_attach(case_setup_remote, check_scenario):
    def check_test_suceeded_msg(writer, stdout, stderr):
        return "TEST SUCEEDED" in "".join(stderr)

    def additional_output_checks(writer, stdout, stderr):
        # Don't call super as we have an expected exception
        assert "ValueError: TEST SUCEEDED" in stderr

    with case_setup_remote.test_file(
        "_debugger_case_remote_unhandled_exceptions2.py",
        additional_output_checks=additional_output_checks,
        check_test_suceeded_msg=check_test_suceeded_msg,
        EXPECTED_RETURNCODE=1,
    ) as writer:
        writer.log.append("making initial run")
        writer.write_make_initial_run()

        check_scenario(writer)

        writer.log.append("finished ok")
        writer.finished_ok = True


@pytest.mark.parametrize(
    "filename, break_at_lines",
    [
        ("_debugger_case_tracing.py", {2: "frame_eval"}),
        ("_debugger_case_tracing.py", {3: "frame_eval"}),
        ("_debugger_case_tracing.py", {4: "frame_eval"}),
        ("_debugger_case_tracing.py", {2: "frame_eval", 4: "trace"}),
        ("_debugger_case_tracing.py", {8: "frame_eval"}),
        ("_debugger_case_tracing.py", {9: "frame_eval"}),
        ("_debugger_case_tracing.py", {10: "frame_eval"}),
        # Note: second frame eval hit is actually a trace because after we
        # hit the first frame eval we don't actually stop tracing a given
        # frame (known limitation to be fixed in the future).
        # -- needs a better test
        ("_debugger_case_tracing.py", {8: "frame_eval", 10: "trace"}),
    ],
)
def test_frame_eval_limitations(case_setup, filename, break_at_lines):
    """
    Test with limitations to be addressed in the future.
    """
    with case_setup.test_file(filename) as writer:
        for break_at_line in break_at_lines:
            writer.write_add_breakpoint(break_at_line)

        writer.log.append("making initial run")
        writer.write_make_initial_run()

        for break_at_line, break_mode in break_at_lines.items():
            writer.log.append("waiting for breakpoint hit")
            hit = writer.wait_for_breakpoint_hit()
            thread_id = hit.thread_id

            if IS_PY312_OR_GREATER:
                assert hit.suspend_type == "sys_monitor"
            elif (IS_PY36_OR_GREATER and TEST_CYTHON) and not IS_PY311_OR_GREATER:
                assert hit.suspend_type == break_mode
            else:
                # Before 3.6 frame eval is not available.
                assert hit.suspend_type == "trace"

            writer.log.append("run thread")
            writer.write_run_thread(thread_id)

        writer.finished_ok = True


def test_step_return_my_code(case_setup):
    with case_setup.test_file("my_code/my_code.py") as writer:
        writer.write_set_project_roots([debugger_unittest._get_debugger_test_file("my_code")])
        writer.write_add_breakpoint(writer.get_line_index_with_content("break here"))
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit()

        writer.write_step_in_my_code(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_INTO_MY_CODE)
        assert hit.name == "callback1"

        writer.write_step_in_my_code(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_INTO_MY_CODE)
        assert hit.name == "callback2"

        writer.write_step_return_my_code(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_RETURN_MY_CODE)
        assert hit.name == "callback1"

        writer.write_step_return_my_code(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_RETURN_MY_CODE)
        assert hit.name == "<module>"

        writer.write_step_return_my_code(hit.thread_id)
        writer.finished_ok = True


def test_smart_step_into_case1(case_setup):
    with case_setup.test_file("_debugger_case_smart_step_into.py") as writer:
        line = writer.get_line_index_with_content("break here")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit(line=line)

        found = writer.get_step_into_variants(hit.thread_id, hit.frame_id, line, line)

        # Remove the offset/childOffset to compare (as it changes for each python version)
        found_info = [x[:-2] for x in found]
        if IS_PY311_OR_GREATER:
            assert found_info == [
                ("bar()", "false", "14", "1"),
                ("foo(bar())", "false", "14", "1"),
                ("call_outer(foo(bar()))", "false", "14", "1"),
            ]
        else:
            assert found_info == [("bar", "false", "14", "1"), ("foo", "false", "14", "1"), ("call_outer", "false", "14", "1")]

        # Note: this is just using the name, not really taking using the context.
        writer.write_smart_step_into(hit.thread_id, line, "foo")
        hit = writer.wait_for_breakpoint_hit(reason=CMD_SMART_STEP_INTO)
        assert hit.line == writer.get_line_index_with_content("on foo mark")

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_smart_step_into_case2(case_setup):
    with case_setup.test_file("_debugger_case_smart_step_into2.py") as writer:
        line = writer.get_line_index_with_content("break here")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit(line=line)

        found = writer.get_step_into_variants(hit.thread_id, hit.frame_id, line, line)

        # Note: we have multiple 'foo' calls, so, we have to differentiate to
        # know in which one we want to stop.
        OFFSET_POS = 4
        writer.write_smart_step_into(hit.thread_id, "offset=" + found[2][OFFSET_POS], "foo")
        hit = writer.wait_for_breakpoint_hit(reason=CMD_SMART_STEP_INTO)
        assert hit.line == writer.get_line_index_with_content("on foo mark")

        writer.write_get_frame(hit.thread_id, hit.frame_id)
        writer.wait_for_var(
            [
                (
                    '<var name="arg" type="int" qualifier="__builtin__" value="int: 3"',
                    '<var name="arg" type="int" qualifier="builtins" value="int: 3"',
                )
            ]
        )

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_smart_step_into_case3(case_setup):
    with case_setup.test_file("_debugger_case_smart_step_into3.py") as writer:
        line = writer.get_line_index_with_content("break here")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit(line=line)

        found = writer.get_step_into_variants(hit.thread_id, hit.frame_id, 0, 9999)

        # Note: we have multiple 'foo' calls, so, we have to differentiate to
        # know in which one we want to stop.
        NAME_POS = 0
        OFFSET_POS = 4
        CHILD_OFFSET_POS = 5

        f = [x for x in found if x[NAME_POS] in ("foo", "foo(arg)")]  # Python 3.11 uses foo(arg)
        assert len(f) == 1

        writer.write_smart_step_into(hit.thread_id, "offset=" + f[0][OFFSET_POS] + ";" + f[0][CHILD_OFFSET_POS], f[0][NAME_POS])
        hit = writer.wait_for_breakpoint_hit(reason=CMD_SMART_STEP_INTO)
        assert hit.line == writer.get_line_index_with_content("on foo mark")

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_step_over_my_code(case_setup):
    with case_setup.test_file("my_code/my_code.py") as writer:
        writer.write_set_project_roots([debugger_unittest._get_debugger_test_file("my_code")])
        writer.write_add_breakpoint(writer.get_line_index_with_content("break here"))
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit()

        writer.write_step_in_my_code(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_INTO_MY_CODE)
        assert hit.name == "callback1"

        writer.write_step_in_my_code(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_INTO_MY_CODE)
        assert hit.name == "callback2"

        writer.write_step_over_my_code(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_OVER_MY_CODE)  # Note: goes from step over to step into
        assert hit.name == "callback1"

        writer.write_step_over_my_code(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_OVER_MY_CODE)  # Note: goes from step over to step into
        assert hit.name == "<module>"

        writer.write_step_over_my_code(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_OVER_MY_CODE)
        assert hit.name == "<module>"

        writer.write_step_over_my_code(hit.thread_id)
        writer.finished_ok = True


@pytest.fixture(
    params=[
        "step_over",
        "step_return",
        "step_in",
    ]
)
def step_method(request):
    return request.param


def test_sysexit_on_filtered_file(case_setup):
    def get_environ(writer):
        env = os.environ.copy()
        env.update({"PYDEVD_FILTERS": json.dumps({"**/_debugger_case_sysexit.py": True})})
        return env

    with case_setup.test_file("_debugger_case_sysexit.py", get_environ=get_environ, EXPECTED_RETURNCODE=1) as writer:
        writer.write_add_exception_breakpoint_with_policy(
            "SystemExit",
            notify_on_handled_exceptions=1,  # Notify multiple times
            notify_on_unhandled_exceptions=1,
            ignore_libraries=0,
        )

        writer.write_make_initial_run()
        writer.finished_ok = True


@pytest.mark.parametrize(
    "scenario",
    [
        "handled_once",
        "handled_multiple",
        "unhandled",
    ],
)
def test_exception_not_on_filtered_file(case_setup, scenario):
    def get_environ(writer):
        env = os.environ.copy()
        env.update({"PYDEVD_FILTERS": json.dumps({"**/other.py": True})})
        return env

    def check_test_suceeded_msg(writer, stdout, stderr):
        return "TEST SUCEEDED" in "".join(stderr)

    def additional_output_checks(writer, stdout, stderr):
        if "raise RuntimeError" not in stderr:
            raise AssertionError("Expected test to have an unhandled exception.\nstdout:\n%s\n\nstderr:\n%s" % (stdout, stderr))

    with case_setup.test_file(
        "my_code/my_code_exception.py",
        get_environ=get_environ,
        EXPECTED_RETURNCODE="any",
        check_test_suceeded_msg=check_test_suceeded_msg,
        additional_output_checks=additional_output_checks,
    ) as writer:
        if scenario == "handled_once":
            writer.write_add_exception_breakpoint_with_policy(
                "RuntimeError",
                notify_on_handled_exceptions=2,  # Notify only once
                notify_on_unhandled_exceptions=0,
                ignore_libraries=0,
            )
        elif scenario == "handled_multiple":
            writer.write_add_exception_breakpoint_with_policy(
                "RuntimeError",
                notify_on_handled_exceptions=1,  # Notify multiple times
                notify_on_unhandled_exceptions=0,
                ignore_libraries=0,
            )
        elif scenario == "unhandled":
            writer.write_add_exception_breakpoint_with_policy(
                "RuntimeError", notify_on_handled_exceptions=0, notify_on_unhandled_exceptions=1, ignore_libraries=0
            )

        writer.write_make_initial_run()
        for _i in range(3 if scenario == "handled_multiple" else 1):
            hit = writer.wait_for_breakpoint_hit(REASON_UNCAUGHT_EXCEPTION if scenario == "unhandled" else REASON_CAUGHT_EXCEPTION)
            writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_exception_on_filtered_file(case_setup):
    def get_environ(writer):
        env = os.environ.copy()
        env.update({"PYDEVD_FILTERS": json.dumps({"**/other.py": True})})
        return env

    def check_test_suceeded_msg(writer, stdout, stderr):
        return "TEST SUCEEDED" in "".join(stderr)

    def additional_output_checks(writer, stdout, stderr):
        if "raise RuntimeError" not in stderr:
            raise AssertionError("Expected test to have an unhandled exception.\nstdout:\n%s\n\nstderr:\n%s" % (stdout, stderr))

    with case_setup.test_file(
        "my_code/my_code_exception_on_other.py",
        get_environ=get_environ,
        EXPECTED_RETURNCODE="any",
        check_test_suceeded_msg=check_test_suceeded_msg,
        additional_output_checks=additional_output_checks,
    ) as writer:
        writer.write_add_exception_breakpoint_with_policy(
            "RuntimeError",
            notify_on_handled_exceptions=2,  # Notify only once
            notify_on_unhandled_exceptions=1,
            ignore_libraries=0,
        )

        writer.write_make_initial_run()

        # Note: the unhandled exception was initially raised in a file which is filtered out, but we
        # should be able to see the frames which are part of the project.
        hit = writer.wait_for_breakpoint_hit(
            REASON_UNCAUGHT_EXCEPTION,
            file="my_code_exception_on_other.py",
            line=writer.get_line_index_with_content("other.raise_exception()"),
        )
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.parametrize(
    "environ",
    [
        {"PYDEVD_FILTER_LIBRARIES": "1"},  # Global setting for step over
        {"PYDEVD_FILTERS": json.dumps({"**/other.py": True})},  # specify as json
        {"PYDEVD_FILTERS": "**/other.py"},  # specify ';' separated list
    ],
)
@pytest.mark.skipif(IS_JYTHON, reason="Flaky on Jython.")
def test_step_over_my_code_global_settings(case_setup, environ, step_method):
    def get_environ(writer):
        env = os.environ.copy()
        env.update(environ)
        return env

    def do_step():
        if step_method == "step_over":
            writer.write_step_over(hit.thread_id)
            return REASON_STEP_OVER  # Note: goes from step over to step into
        elif step_method == "step_return":
            writer.write_step_return(hit.thread_id)
            return REASON_STEP_RETURN
        else:
            assert step_method == "step_in"
            writer.write_step_in(hit.thread_id)
            return REASON_STEP_INTO

    with case_setup.test_file("my_code/my_code.py", get_environ=get_environ) as writer:
        writer.write_set_project_roots([debugger_unittest._get_debugger_test_file("my_code")])
        writer.write_add_breakpoint(writer.get_line_index_with_content("break here"))
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit()

        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_INTO)
        assert hit.name == "callback1"

        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_INTO)
        assert hit.name == "callback2"

        stop_reason = do_step()
        hit = writer.wait_for_breakpoint_hit(reason=stop_reason)
        assert hit.name == "callback1"

        stop_reason = do_step()
        hit = writer.wait_for_breakpoint_hit(reason=stop_reason)
        assert hit.name == "<module>"

        if IS_JYTHON:
            # Jython may get to exit functions, so, just resume the thread.
            writer.write_run_thread(hit.thread_id)

        else:
            stop_reason = do_step()

            if step_method != "step_return":
                stop_reason = do_step()
                if step_method == "step_over":
                    stop_reason = REASON_STEP_OVER

                hit = writer.wait_for_breakpoint_hit(reason=stop_reason)
                assert hit.name == "<module>"

                writer.write_step_over(hit.thread_id)

        writer.finished_ok = True


def test_step_over_my_code_global_setting_and_explicit_include(case_setup):
    def get_environ(writer):
        env = os.environ.copy()
        env.update(
            {
                "PYDEVD_FILTER_LIBRARIES": "1",  # Global setting for in project or not
                # specify as json (force include).
                "PYDEVD_FILTERS": json.dumps({"**/other.py": False}),
            }
        )
        return env

    with case_setup.test_file("my_code/my_code.py", get_environ=get_environ) as writer:
        writer.write_set_project_roots([debugger_unittest._get_debugger_test_file("my_code")])
        writer.write_add_breakpoint(writer.get_line_index_with_content("break here"))
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit()

        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_INTO)

        # Although we filtered out non-project files, other.py is explicitly included.
        assert hit.name == "call_me_back1"

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_access_token(case_setup):
    def update_command_line_args(self, args):
        i = args.index("--client")
        assert i > 0
        args.insert(i, "--access-token")
        args.insert(i + 1, "bar123")
        args.insert(i, "--client-access-token")
        args.insert(i + 1, "foo234")
        return args

    with case_setup.test_file("_debugger_case_print.py", update_command_line_args=update_command_line_args) as writer:
        writer.write_add_breakpoint(1, "None")  # I.e.: should not work (not authenticated).

        writer.wait_for_message(lambda msg: "Client not authenticated." in msg, expect_xml=False)

        writer.write_authenticate(access_token="bar123", client_access_token="foo234")

        writer.write_version()

        writer.write_make_initial_run()

        writer.finished_ok = True


def test_namedtuple(case_setup):
    """
    Check that we don't step into <string> in the namedtuple constructor.
    """
    with case_setup.test_file("_debugger_case_namedtuple.py") as writer:
        line = writer.get_line_index_with_content("break here")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()

        expected_line = line

        for _ in range(2):
            expected_line += 1
            writer.write_step_in(hit.thread_id)
            hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_INTO, file="_debugger_case_namedtuple.py", line=expected_line)

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_matplotlib_activation(case_setup):
    try:
        import matplotlib
    except ImportError:
        return

    def get_environ(writer):
        env = os.environ.copy()
        env.update(
            {
                "IPYTHONENABLE": "True",
            }
        )
        return env

    with case_setup.test_file("_debugger_case_matplotlib.py", get_environ=get_environ) as writer:
        writer.write_add_breakpoint(writer.get_line_index_with_content("break here"))
        writer.write_make_initial_run()
        for _ in range(3):
            hit = writer.wait_for_breakpoint_hit()
            writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


_GENERATOR_FILES = [
    "_debugger_case_generator3.py",
    "_debugger_case_generator.py",
    "_debugger_case_generator2.py",
]


@pytest.mark.parametrize("target_filename", _GENERATOR_FILES)
@pytest.mark.skipif(IS_JYTHON, reason="We do not detect generator returns on Jython.")
def test_generator_step_over_basic(case_setup, target_filename):
    with case_setup.test_file(target_filename) as writer:
        line = writer.get_line_index_with_content("break here")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()

        # Note: not using for so that we know which step failed in the ci if it fails.
        writer.write_step_over(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(
            reason=REASON_STEP_OVER, file=target_filename, line=writer.get_line_index_with_content("step 1")
        )

        writer.write_step_over(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(
            reason=REASON_STEP_OVER, file=target_filename, line=writer.get_line_index_with_content("step 2")
        )

        if IS_PY38_OR_GREATER and target_filename == "_debugger_case_generator2.py":
            # On py 3.8 it goes back to the return line.
            writer.write_step_over(hit.thread_id)
            hit = writer.wait_for_breakpoint_hit(
                reason=REASON_STEP_OVER, file=target_filename, line=writer.get_line_index_with_content("return \\")
            )

        if PYDEVD_USE_SYS_MONITORING:
            writer.write_step_over(hit.thread_id)
            hit = writer.wait_for_breakpoint_hit(
                reason=REASON_STEP_OVER, file=target_filename, line=writer.get_line_index_with_content("generator return")
            )

        writer.write_step_over(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(
            reason=REASON_STEP_OVER, file=target_filename, line=writer.get_line_index_with_content("step 3")
        )

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.parametrize("target_filename", _GENERATOR_FILES)
@pytest.mark.skipif(IS_JYTHON, reason="We do not detect generator returns on Jython.")
def test_generator_step_return(case_setup, target_filename):
    with case_setup.test_file(target_filename) as writer:
        line = writer.get_line_index_with_content("break here")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()

        # Note: not using for so that we know which step failed in the ci if it fails.
        writer.write_step_return(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(
            reason=REASON_STEP_RETURN, file=target_filename, line=writer.get_line_index_with_content("generator return")
        )

        writer.write_step_over(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(
            reason=REASON_STEP_OVER, file=target_filename, line=writer.get_line_index_with_content("step 3")
        )

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.skipif(not IS_PY36_OR_GREATER, reason="Only CPython 3.6 onwards")
def test_stepin_not_my_code_coroutine(case_setup):
    def get_environ(writer):
        environ = {"PYDEVD_FILTERS": '{"**/not_my_coroutine.py": true}'}
        env = os.environ.copy()
        env.update(environ)
        return env

    with case_setup.test_file("my_code/my_code_coroutine.py", get_environ=get_environ) as writer:
        writer.write_set_project_roots([debugger_unittest._get_debugger_test_file("my_code")])
        writer.write_add_breakpoint(writer.get_line_index_with_content("break here"))
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit()

        writer.write_step_in(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_INTO)
        assert hit.name == "main"

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.skipif(IS_JYTHON, reason="Flaky on Jython")
def test_generator_step_in(case_setup):
    with case_setup.test_file("_debugger_case_generator_step_in.py") as writer:
        line = writer.get_line_index_with_content("stop 1")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()

        for i in range(2, 5):
            writer.write_step_in(hit.thread_id)
            kwargs = {}
            if not IS_JYTHON:
                kwargs["line"] = writer.get_line_index_with_content("stop %s" % (i,))
            hit = writer.wait_for_breakpoint_hit(reason=REASON_STEP_INTO, file="_debugger_case_generator_step_in.py", **kwargs)

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.parametrize(
    "target_filename",
    [
        "_debugger_case_asyncio.py",
        "_debugger_case_trio.py",
    ],
)
@pytest.mark.skipif(not IS_CPYTHON or not IS_PY36_OR_GREATER, reason="Only CPython 3.6 onwards")
def test_asyncio_step_over_basic(case_setup, target_filename):
    with case_setup.test_file(target_filename) as writer:
        line = writer.get_line_index_with_content("break main")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()

        writer.write_step_over(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(
            reason=REASON_STEP_OVER, file=target_filename, line=writer.get_line_index_with_content("step main")
        )

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.parametrize(
    "target_filename",
    [
        "_debugger_case_asyncio.py",
        "_debugger_case_trio.py",
    ],
)
@pytest.mark.skipif(not IS_CPYTHON or not IS_PY36_OR_GREATER, reason="Only CPython 3.6 onwards")
def test_asyncio_step_over_end_of_function(case_setup, target_filename):
    with case_setup.test_file(target_filename) as writer:
        line = writer.get_line_index_with_content("break count 2")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()

        writer.write_step_over(hit.thread_id)
        names = ("sleep", "wait_task_rescheduled")
        if PYDEVD_USE_SYS_MONITORING:
            names = ("main",)
        hit = writer.wait_for_breakpoint_hit(
            reason=REASON_STEP_OVER,
            name=names,
        )
        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.parametrize(
    "target_filename",
    [
        "_debugger_case_asyncio.py",
        "_debugger_case_trio.py",
    ],
)
@pytest.mark.skipif(not IS_CPYTHON or not IS_PY36_OR_GREATER, reason="Only CPython 3.6 onwards")
def test_asyncio_step_in(case_setup, target_filename):
    with case_setup.test_file(target_filename) as writer:
        line = writer.get_line_index_with_content("break count 1")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()

        writer.write_step_return(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(
            reason=REASON_STEP_RETURN, file=target_filename, line=writer.get_line_index_with_content("break main")
        )

        writer.write_step_in(hit.thread_id)
        names = ("sleep", "wait_task_rescheduled")
        if PYDEVD_USE_SYS_MONITORING:
            names = ("main",)

        hit = writer.wait_for_breakpoint_hit(
            reason=REASON_STEP_INTO,
            name=names,
        )

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


@pytest.mark.parametrize(
    "target_filename",
    [
        "_debugger_case_asyncio.py",
        "_debugger_case_trio.py",
    ],
)
@pytest.mark.skipif(not IS_CPYTHON or not IS_PY36_OR_GREATER, reason="Only CPython 3.6 onwards")
def test_asyncio_step_return(case_setup, target_filename):
    with case_setup.test_file(target_filename) as writer:
        line = writer.get_line_index_with_content("break count 1")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit()

        writer.write_step_return(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(
            reason=REASON_STEP_RETURN, file=target_filename, line=writer.get_line_index_with_content("break main")
        )

        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_notify_stdin(case_setup, pyfile):
    @pyfile
    def case_stdin():
        import sys

        print("Write something:")
        contents = sys.stdin.readline()
        print("Found: " + contents)

        print("TEST SUCEEDED")

    def additional_output_checks(writer, stdout, stderr):
        assert "Found: foo" in stdout

    with case_setup.test_file(
        case_stdin,
        additional_output_checks=additional_output_checks,
    ) as writer:
        writer.write_make_initial_run()
        msg = writer.wait_for_message(CMD_INPUT_REQUESTED, expect_xml=False)
        assert msg.split("\t")[-1] == "True"
        process = writer.process
        process.stdin.write(b"foo\n")
        process.stdin.flush()
        msg = writer.wait_for_message(CMD_INPUT_REQUESTED, expect_xml=False)
        assert msg.split("\t")[-1] == "False"

        writer.finished_ok = True


def test_frame_eval_mode_corner_case_01(case_setup):
    with case_setup.test_file(
        "wrong_bytecode/_debugger_case_wrong_bytecode.py",
    ) as writer:
        line = writer.get_line_index_with_content("break here")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()
        hit = writer.wait_for_breakpoint_hit(line=writer.get_line_index_with_content("break here"), file="_debugger_case_wrong_bytecode.py")
        writer.write_step_over(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(
            line=writer.get_line_index_with_content("step 1"), file="_debugger_case_wrong_bytecode.py", reason=REASON_STEP_OVER
        )
        writer.write_step_over(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(
            line=writer.get_line_index_with_content("step 2"), file="_debugger_case_wrong_bytecode.py", reason=REASON_STEP_OVER
        )
        writer.write_step_over(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(
            line=writer.get_line_index_with_content("step 3"), file="_debugger_case_wrong_bytecode.py", reason=REASON_STEP_OVER
        )
        writer.write_step_over(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(
            line=writer.get_line_index_with_content("step 4"), file="_debugger_case_wrong_bytecode.py", reason=REASON_STEP_OVER
        )
        writer.write_step_over(hit.thread_id)

        hit = writer.wait_for_breakpoint_hit(
            line=writer.get_line_index_with_content("step 5"), file="_debugger_case_wrong_bytecode.py", reason=REASON_STEP_OVER
        )
        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(not IS_PY36_OR_GREATER, reason="Only CPython 3.6 onwards")
def test_frame_eval_mode_corner_case_02(case_setup):
    with case_setup.test_file(
        "_bytecode_super.py",
    ) as writer:
        line = writer.get_line_index_with_content("break here")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(line=line, file="_bytecode_super.py")

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(not IS_PY36_OR_GREATER, reason="Only CPython 3.6 onwards")
def test_frame_eval_mode_corner_case_03(case_setup):
    with case_setup.test_file(
        "_bytecode_constructs.py",
    ) as writer:
        line = writer.get_line_index_with_content("break while")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(line=line)

        writer.write_step_over(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(line=line + 1, reason=REASON_STEP_OVER)

        writer.write_step_over(hit.thread_id)  # i.e.: check that the jump target is still ok.
        hit = writer.wait_for_breakpoint_hit(line=line, reason=REASON_STOP_ON_BREAKPOINT)

        writer.write_step_over(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(line=line + 1, reason=REASON_STEP_OVER)

        writer.write_step_over(hit.thread_id)
        hit = writer.wait_for_breakpoint_hit(line=line, reason=REASON_STOP_ON_BREAKPOINT)

        writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(not IS_PY36_OR_GREATER, reason="Only CPython 3.6 onwards")
def test_frame_eval_mode_corner_case_04(case_setup):
    with case_setup.test_file(
        "_bytecode_constructs.py",
    ) as writer:
        line = writer.get_line_index_with_content("break for")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()

        for i in range(3):
            hit = writer.wait_for_breakpoint_hit(line=line)
            writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


@pytest.mark.skipif(not IS_PY36_OR_GREATER, reason="Only CPython 3.6 onwards")
@pytest.mark.parametrize(
    "break_name",
    [
        "break except",
        "break with",
        "break try 1",
        "break try 2",
        "break finally 1",
        "break except 2",
        "break finally 2",
        "break finally 3",
        "break finally 4",
        "break in dict",
        "break else",
    ],
)
def test_frame_eval_mode_corner_case_many(case_setup, break_name):
    if break_name == "break finally 4" and sys.version_info[:2] == (3, 9):
        # This case is currently failing in Python 3.9
        return

    # Check the constructs where we stop only once and proceed.
    with case_setup.test_file(
        "_bytecode_constructs.py",
    ) as writer:
        line = writer.get_line_index_with_content(break_name)
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(line=line)
        writer.write_run_thread(hit.thread_id)

        if break_name == "break with":
            if sys.version_info[:2] >= (3, 10):
                # On Python 3.10 it'll actually backtrack for the
                # with and thus will execute the line where the
                # 'with' statement was started again.
                hit = writer.wait_for_breakpoint_hit(line=line)
                writer.write_run_thread(hit.thread_id)

        writer.finished_ok = True


check_shadowed = [
    (
        """
if __name__ == '__main__':
    import queue
    print(queue)
""",
        "queue.py",
        "shadowed = True\n",
    ),
    (
        """
if __name__ == '__main__':
    import queue
    print(queue)
""",
        "queue.py",
        'raise AssertionError("error on import")',
    ),
]


@pytest.mark.parametrize("module_name_and_content", check_shadowed)
def test_debugger_shadowed_imports(case_setup, tmpdir, module_name_and_content):
    main_content, module_name, content = module_name_and_content
    target = tmpdir.join("main.py")
    shadowed = tmpdir.join(module_name)

    target.write_text(main_content, encoding="utf-8")

    shadowed.write_text(content, encoding="utf-8")

    def get_environ(writer):
        env = os.environ.copy()
        env.update(
            {
                "PYTHONPATH": str(tmpdir),
            }
        )
        return env

    try:
        with case_setup.test_file(
            str(target),
            get_environ=get_environ,
            wait_for_initialization=False,
        ) as writer:
            writer.write_make_initial_run()
    except AssertionError:
        pass  # This is expected as pydevd didn't start-up.

    assert ('the module "%s" could not be imported because it is shadowed by:' % (module_name.split(".")[0])) in writer.get_stderr()


def test_debugger_hide_pydevd_threads(case_setup, pyfile):
    if TODO_PYPY:
        raise pytest.skip("Not ok in pypy")

    @pyfile
    def target_file():
        import threading
        from _pydevd_bundle import pydevd_constants

        found_pydevd_thread = False
        for t in threading.enumerate():
            if getattr(t, "is_pydev_daemon_thread", False):
                found_pydevd_thread = True

        if pydevd_constants.IS_CPYTHON:
            assert not found_pydevd_thread
        else:
            assert found_pydevd_thread
        print("TEST SUCEEDED")

    with case_setup.test_file(target_file) as writer:
        line = writer.get_line_index_with_content("TEST SUCEEDED")
        writer.write_add_breakpoint(line)
        writer.write_make_initial_run()

        hit = writer.wait_for_breakpoint_hit(line=line)
        writer.write_run_thread(hit.thread_id)
        writer.finished_ok = True


def test_multiple_threads_same_code(case_setup, pyfile):
    with case_setup.test_file("_debugger_case_multiple_threads_same_code.py") as writer:
        line = writer.get_line_index_with_content("break on main")
        bpid_main = writer.write_add_breakpoint(line)
        writer.write_make_initial_run()
        hit_main = writer.wait_for_breakpoint_hit(line=line)

        line = writer.get_line_index_with_content("break on thread")
        bpid_thread = writer.write_add_breakpoint(line)

        hit_thread = writer.wait_for_breakpoint_hit(line=line)
        writer.write_run_thread(hit_thread.thread_id)

        writer.write_step_return(hit_thread.thread_id)
        hit_thread = writer.wait_for_breakpoint_hit(REASON_STEP_RETURN)

        # Multiple steps in that thread.
        for _i in range(2):
            writer.write_step_over(hit_thread.thread_id)
            hit_thread = writer.wait_for_breakpoint_hit((REASON_STEP_OVER, REASON_STOP_ON_BREAKPOINT))

        # Remove breakpoint from thread so that it can finish cleanly.
        writer.write_remove_breakpoint(bpid_thread)
        writer.write_run_thread(hit_thread.thread_id)

        # Ok, resume main
        writer.write_run_thread(hit_main.thread_id)

        writer.finished_ok = True


# Jython needs some vars to be set locally.
# set JAVA_HOME=c:\bin\jdk1.8.0_172
# set PATH=%PATH%;C:\bin\jython2.7.0\bin
# set PATH=%PATH%;%JAVA_HOME%\bin
# c:\bin\jython2.7.0\bin\jython.exe -m py.test tests_python


if __name__ == "__main__":
    pytest.main(["-k", "test_case_12"])