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
|
import builtins
import errno
import glob
import json
import importlib.util
from importlib._bootstrap_external import _get_sourcefile
from importlib.machinery import (
AppleFrameworkLoader,
BuiltinImporter,
ExtensionFileLoader,
FrozenImporter,
SourceFileLoader,
)
import marshal
import os
import py_compile
import random
import shutil
import stat
import subprocess
import sys
import textwrap
import threading
import time
import types
import unittest
from unittest import mock
import _imp
from test.support import os_helper
from test.support import (
STDLIB_DIR, swap_attr, swap_item, cpython_only, is_apple_mobile, is_emscripten,
is_wasi, run_in_subinterp, run_in_subinterp_with_config, Py_TRACE_REFS,
requires_gil_enabled, Py_GIL_DISABLED, no_rerun)
from test.support.import_helper import (
forget, make_legacy_pyc, unlink, unload, ready_to_import,
DirsOnSysPath, CleanImport, import_module)
from test.support.os_helper import (
TESTFN, rmtree, temp_umask, TESTFN_UNENCODABLE)
from test.support import script_helper
from test.support import threading_helper
from test.test_importlib.util import uncache
from types import ModuleType
try:
import _testsinglephase
except ImportError:
_testsinglephase = None
try:
import _testmultiphase
except ImportError:
_testmultiphase = None
try:
import _interpreters
except ModuleNotFoundError:
_interpreters = None
try:
import _testinternalcapi
except ImportError:
_testinternalcapi = None
skip_if_dont_write_bytecode = unittest.skipIf(
sys.dont_write_bytecode,
"test meaningful only when writing bytecode")
def _require_loader(module, loader, skip):
if isinstance(module, str):
module = __import__(module)
MODULE_KINDS = {
BuiltinImporter: 'built-in',
ExtensionFileLoader: 'extension',
AppleFrameworkLoader: 'framework extension',
FrozenImporter: 'frozen',
SourceFileLoader: 'pure Python',
}
expected = loader
assert isinstance(expected, type), expected
expected = MODULE_KINDS[expected]
actual = module.__spec__.loader
if not isinstance(actual, type):
actual = type(actual)
actual = MODULE_KINDS[actual]
if actual != expected:
err = f'expected module to be {expected}, got {module.__spec__}'
if skip:
raise unittest.SkipTest(err)
raise Exception(err)
return module
def require_builtin(module, *, skip=False):
module = _require_loader(module, BuiltinImporter, skip)
assert module.__spec__.origin == 'built-in', module.__spec__
def require_extension(module, *, skip=False):
# Apple extensions must be distributed as frameworks. This requires
# a specialist loader.
if is_apple_mobile:
_require_loader(module, AppleFrameworkLoader, skip)
else:
_require_loader(module, ExtensionFileLoader, skip)
def require_frozen(module, *, skip=True):
module = _require_loader(module, FrozenImporter, skip)
assert module.__spec__.origin == 'frozen', module.__spec__
def require_pure_python(module, *, skip=False):
_require_loader(module, SourceFileLoader, skip)
def create_extension_loader(modname, filename):
# Apple extensions must be distributed as frameworks. This requires
# a specialist loader.
if is_apple_mobile:
return AppleFrameworkLoader(modname, filename)
else:
return ExtensionFileLoader(modname, filename)
def import_extension_from_file(modname, filename, *, put_in_sys_modules=True):
loader = create_extension_loader(modname, filename)
spec = importlib.util.spec_from_loader(modname, loader)
module = importlib.util.module_from_spec(spec)
loader.exec_module(module)
if put_in_sys_modules:
sys.modules[modname] = module
return module
def remove_files(name):
for f in (name + ".py",
name + ".pyc",
name + ".pyw",
name + "$py.class"):
unlink(f)
rmtree('__pycache__')
if _testsinglephase is not None:
def restore__testsinglephase(*, _orig=_testsinglephase):
# We started with the module imported and want to restore
# it to its nominal state.
sys.modules.pop('_testsinglephase', None)
_orig._clear_globals()
origin = _orig.__spec__.origin
_testinternalcapi.clear_extension('_testsinglephase', origin)
import _testsinglephase
def requires_singlephase_init(meth):
"""Decorator to skip if single-phase init modules are not supported."""
if not isinstance(meth, type):
def meth(self, _meth=meth):
try:
return _meth(self)
finally:
restore__testsinglephase()
meth = cpython_only(meth)
msg = "gh-117694: free-threaded build does not currently support single-phase init modules in sub-interpreters"
meth = requires_gil_enabled(msg)(meth)
return unittest.skipIf(_testsinglephase is None,
'test requires _testsinglephase module')(meth)
def requires_subinterpreters(meth):
"""Decorator to skip a test if subinterpreters are not supported."""
return unittest.skipIf(_interpreters is None,
'subinterpreters required')(meth)
class ModuleSnapshot(types.SimpleNamespace):
"""A representation of a module for testing.
Fields:
* id - the module's object ID
* module - the actual module or an adequate substitute
* __file__
* __spec__
* name
* origin
* ns - a copy (dict) of the module's __dict__ (or None)
* ns_id - the object ID of the module's __dict__
* cached - the sys.modules[mod.__spec__.name] entry (or None)
* cached_id - the object ID of the sys.modules entry (or None)
In cases where the value is not available (e.g. due to serialization),
the value will be None.
"""
_fields = tuple('id module ns ns_id cached cached_id'.split())
@classmethod
def from_module(cls, mod):
name = mod.__spec__.name
cached = sys.modules.get(name)
return cls(
id=id(mod),
module=mod,
ns=types.SimpleNamespace(**mod.__dict__),
ns_id=id(mod.__dict__),
cached=cached,
cached_id=id(cached),
)
SCRIPT = textwrap.dedent('''
{imports}
name = {name!r}
{prescript}
mod = {name}
{body}
{postscript}
''')
IMPORTS = textwrap.dedent('''
import sys
''').strip()
SCRIPT_BODY = textwrap.dedent('''
# Capture the snapshot data.
cached = sys.modules.get(name)
snapshot = dict(
id=id(mod),
module=dict(
__file__=mod.__file__,
__spec__=dict(
name=mod.__spec__.name,
origin=mod.__spec__.origin,
),
),
ns=None,
ns_id=id(mod.__dict__),
cached=None,
cached_id=id(cached) if cached else None,
)
''').strip()
CLEANUP_SCRIPT = textwrap.dedent('''
# Clean up the module.
sys.modules.pop(name, None)
''').strip()
@classmethod
def build_script(cls, name, *,
prescript=None,
import_first=False,
postscript=None,
postcleanup=False,
):
if postcleanup is True:
postcleanup = cls.CLEANUP_SCRIPT
elif isinstance(postcleanup, str):
postcleanup = textwrap.dedent(postcleanup).strip()
postcleanup = cls.CLEANUP_SCRIPT + os.linesep + postcleanup
else:
postcleanup = ''
prescript = textwrap.dedent(prescript).strip() if prescript else ''
postscript = textwrap.dedent(postscript).strip() if postscript else ''
if postcleanup:
if postscript:
postscript = postscript + os.linesep * 2 + postcleanup
else:
postscript = postcleanup
if import_first:
prescript += textwrap.dedent(f'''
# Now import the module.
assert name not in sys.modules
import {name}''')
return cls.SCRIPT.format(
imports=cls.IMPORTS.strip(),
name=name,
prescript=prescript.strip(),
body=cls.SCRIPT_BODY.strip(),
postscript=postscript,
)
@classmethod
def parse(cls, text):
raw = json.loads(text)
mod = raw['module']
mod['__spec__'] = types.SimpleNamespace(**mod['__spec__'])
raw['module'] = types.SimpleNamespace(**mod)
return cls(**raw)
@classmethod
def from_subinterp(cls, name, interpid=None, *, pipe=None, **script_kwds):
if pipe is not None:
return cls._from_subinterp(name, interpid, pipe, script_kwds)
pipe = os.pipe()
try:
return cls._from_subinterp(name, interpid, pipe, script_kwds)
finally:
r, w = pipe
os.close(r)
os.close(w)
@classmethod
def _from_subinterp(cls, name, interpid, pipe, script_kwargs):
r, w = pipe
# Build the script.
postscript = textwrap.dedent(f'''
# Send the result over the pipe.
import json
import os
os.write({w}, json.dumps(snapshot).encode())
''')
_postscript = script_kwargs.get('postscript')
if _postscript:
_postscript = textwrap.dedent(_postscript).lstrip()
postscript += _postscript
script_kwargs['postscript'] = postscript.strip()
script = cls.build_script(name, **script_kwargs)
# Run the script.
if interpid is None:
ret = run_in_subinterp(script)
if ret != 0:
raise AssertionError(f'{ret} != 0')
else:
_interpreters.run_string(interpid, script)
# Parse the results.
text = os.read(r, 1000)
return cls.parse(text.decode())
class ImportTests(unittest.TestCase):
def setUp(self):
remove_files(TESTFN)
importlib.invalidate_caches()
def tearDown(self):
unload(TESTFN)
def test_import_raises_ModuleNotFoundError(self):
with self.assertRaises(ModuleNotFoundError):
import something_that_should_not_exist_anywhere
def test_from_import_missing_module_raises_ModuleNotFoundError(self):
with self.assertRaises(ModuleNotFoundError):
from something_that_should_not_exist_anywhere import blah
def test_from_import_missing_attr_raises_ImportError(self):
with self.assertRaises(ImportError):
from importlib import something_that_should_not_exist_anywhere
def test_from_import_missing_attr_has_name_and_path(self):
with CleanImport('os'):
import os
with self.assertRaises(ImportError) as cm:
from os import i_dont_exist
self.assertEqual(cm.exception.name, 'os')
self.assertEqual(cm.exception.path, os.__file__)
self.assertRegex(str(cm.exception), r"cannot import name 'i_dont_exist' from 'os' \(.*os.py\)")
@cpython_only
def test_from_import_missing_attr_has_name_and_so_path(self):
_testcapi = import_module("_testcapi")
with self.assertRaises(ImportError) as cm:
from _testcapi import i_dont_exist
self.assertEqual(cm.exception.name, '_testcapi')
if hasattr(_testcapi, "__file__"):
# The path on the exception is strictly the spec origin, not the
# module's __file__. For most cases, these are the same; but on
# iOS, the Framework relocation process results in the exception
# being raised from the spec location.
self.assertEqual(cm.exception.path, _testcapi.__spec__.origin)
self.assertRegex(
str(cm.exception),
r"cannot import name 'i_dont_exist' from '_testcapi' \(.*(\.(so|pyd))?\)"
)
else:
self.assertEqual(
str(cm.exception),
"cannot import name 'i_dont_exist' from '_testcapi' (unknown location)"
)
def test_from_import_missing_attr_has_name(self):
with self.assertRaises(ImportError) as cm:
# _warning has no path as it's a built-in module.
from _warning import i_dont_exist
self.assertEqual(cm.exception.name, '_warning')
self.assertIsNone(cm.exception.path)
def test_from_import_missing_attr_path_is_canonical(self):
with self.assertRaises(ImportError) as cm:
from os.path import i_dont_exist
self.assertIn(cm.exception.name, {'posixpath', 'ntpath'})
self.assertIsNotNone(cm.exception)
def test_from_import_star_invalid_type(self):
import re
with ready_to_import() as (name, path):
with open(path, 'w', encoding='utf-8') as f:
f.write("__all__ = [b'invalid_type']")
globals = {}
with self.assertRaisesRegex(
TypeError, f"{re.escape(name)}\\.__all__ must be str"
):
exec(f"from {name} import *", globals)
self.assertNotIn(b"invalid_type", globals)
with ready_to_import() as (name, path):
with open(path, 'w', encoding='utf-8') as f:
f.write("globals()[b'invalid_type'] = object()")
globals = {}
with self.assertRaisesRegex(
TypeError, f"{re.escape(name)}\\.__dict__ must be str"
):
exec(f"from {name} import *", globals)
self.assertNotIn(b"invalid_type", globals)
def test_case_sensitivity(self):
# Brief digression to test that import is case-sensitive: if we got
# this far, we know for sure that "random" exists.
with self.assertRaises(ImportError):
import RAnDoM
def test_double_const(self):
# Importing double_const checks that float constants
# serialized by marshal as PYC files don't lose precision
# (SF bug 422177).
from test.test_import.data import double_const
unload('test.test_import.data.double_const')
from test.test_import.data import double_const
def test_import(self):
def test_with_extension(ext):
# The extension is normally ".py", perhaps ".pyw".
source = TESTFN + ext
pyc = TESTFN + ".pyc"
with open(source, "w", encoding='utf-8') as f:
print("# This tests Python's ability to import a",
ext, "file.", file=f)
a = random.randrange(1000)
b = random.randrange(1000)
print("a =", a, file=f)
print("b =", b, file=f)
if TESTFN in sys.modules:
del sys.modules[TESTFN]
importlib.invalidate_caches()
try:
try:
mod = __import__(TESTFN)
except ImportError as err:
self.fail("import from %s failed: %s" % (ext, err))
self.assertEqual(mod.a, a,
"module loaded (%s) but contents invalid" % mod)
self.assertEqual(mod.b, b,
"module loaded (%s) but contents invalid" % mod)
finally:
forget(TESTFN)
unlink(source)
unlink(pyc)
sys.path.insert(0, os.curdir)
try:
test_with_extension(".py")
if sys.platform.startswith("win"):
for ext in [".PY", ".Py", ".pY", ".pyw", ".PYW", ".pYw"]:
test_with_extension(ext)
finally:
del sys.path[0]
def test_module_with_large_stack(self, module='longlist'):
# Regression test for http://bugs.python.org/issue561858.
filename = module + '.py'
# Create a file with a list of 65000 elements.
with open(filename, 'w', encoding='utf-8') as f:
f.write('d = [\n')
for i in range(65000):
f.write('"",\n')
f.write(']')
try:
# Compile & remove .py file; we only need .pyc.
# Bytecode must be relocated from the PEP 3147 bytecode-only location.
py_compile.compile(filename)
finally:
unlink(filename)
# Need to be able to load from current dir.
sys.path.append('')
importlib.invalidate_caches()
namespace = {}
try:
make_legacy_pyc(filename)
# This used to crash.
exec('import ' + module, None, namespace)
finally:
# Cleanup.
del sys.path[-1]
unlink(filename + 'c')
unlink(filename + 'o')
# Remove references to the module (unload the module)
namespace.clear()
try:
del sys.modules[module]
except KeyError:
pass
def test_failing_import_sticks(self):
source = TESTFN + ".py"
with open(source, "w", encoding='utf-8') as f:
print("a = 1/0", file=f)
# New in 2.4, we shouldn't be able to import that no matter how often
# we try.
sys.path.insert(0, os.curdir)
importlib.invalidate_caches()
if TESTFN in sys.modules:
del sys.modules[TESTFN]
try:
for i in [1, 2, 3]:
self.assertRaises(ZeroDivisionError, __import__, TESTFN)
self.assertNotIn(TESTFN, sys.modules,
"damaged module in sys.modules on %i try" % i)
finally:
del sys.path[0]
remove_files(TESTFN)
def test_import_name_binding(self):
# import x.y.z binds x in the current namespace
import test as x
import test.support
self.assertIs(x, test, x.__name__)
self.assertTrue(hasattr(test.support, "__file__"))
# import x.y.z as w binds z as w
import test.support as y
self.assertIs(y, test.support, y.__name__)
def test_issue31286(self):
# import in a 'finally' block resulted in SystemError
try:
x = ...
finally:
import test.support.script_helper as x
# import in a 'while' loop resulted in stack overflow
i = 0
while i < 10:
import test.support.script_helper as x
i += 1
# import in a 'for' loop resulted in segmentation fault
for i in range(2):
import test.support.script_helper as x
def test_failing_reload(self):
# A failing reload should leave the module object in sys.modules.
source = TESTFN + os.extsep + "py"
with open(source, "w", encoding='utf-8') as f:
f.write("a = 1\nb=2\n")
sys.path.insert(0, os.curdir)
try:
mod = __import__(TESTFN)
self.assertIn(TESTFN, sys.modules)
self.assertEqual(mod.a, 1, "module has wrong attribute values")
self.assertEqual(mod.b, 2, "module has wrong attribute values")
# On WinXP, just replacing the .py file wasn't enough to
# convince reload() to reparse it. Maybe the timestamp didn't
# move enough. We force it to get reparsed by removing the
# compiled file too.
remove_files(TESTFN)
# Now damage the module.
with open(source, "w", encoding='utf-8') as f:
f.write("a = 10\nb=20//0\n")
self.assertRaises(ZeroDivisionError, importlib.reload, mod)
# But we still expect the module to be in sys.modules.
mod = sys.modules.get(TESTFN)
self.assertIsNotNone(mod, "expected module to be in sys.modules")
# We should have replaced a w/ 10, but the old b value should
# stick.
self.assertEqual(mod.a, 10, "module has wrong attribute values")
self.assertEqual(mod.b, 2, "module has wrong attribute values")
finally:
del sys.path[0]
remove_files(TESTFN)
unload(TESTFN)
@skip_if_dont_write_bytecode
def test_file_to_source(self):
# check if __file__ points to the source file where available
source = TESTFN + ".py"
with open(source, "w", encoding='utf-8') as f:
f.write("test = None\n")
sys.path.insert(0, os.curdir)
try:
mod = __import__(TESTFN)
self.assertTrue(mod.__file__.endswith('.py'))
os.remove(source)
del sys.modules[TESTFN]
make_legacy_pyc(source)
importlib.invalidate_caches()
mod = __import__(TESTFN)
base, ext = os.path.splitext(mod.__file__)
self.assertEqual(ext, '.pyc')
finally:
del sys.path[0]
remove_files(TESTFN)
if TESTFN in sys.modules:
del sys.modules[TESTFN]
def test_import_by_filename(self):
path = os.path.abspath(TESTFN)
encoding = sys.getfilesystemencoding()
try:
path.encode(encoding)
except UnicodeEncodeError:
self.skipTest('path is not encodable to {}'.format(encoding))
with self.assertRaises(ImportError) as c:
__import__(path)
def test_import_in_del_does_not_crash(self):
# Issue 4236
testfn = script_helper.make_script('', TESTFN, textwrap.dedent("""\
import sys
class C:
def __del__(self):
import importlib
sys.argv.insert(0, C())
"""))
script_helper.assert_python_ok(testfn)
@skip_if_dont_write_bytecode
def test_timestamp_overflow(self):
# A modification timestamp larger than 2**32 should not be a problem
# when importing a module (issue #11235).
sys.path.insert(0, os.curdir)
try:
source = TESTFN + ".py"
compiled = importlib.util.cache_from_source(source)
with open(source, 'w', encoding='utf-8') as f:
pass
try:
os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
except OverflowError:
self.skipTest("cannot set modification time to large integer")
except OSError as e:
if e.errno not in (getattr(errno, 'EOVERFLOW', None),
getattr(errno, 'EINVAL', None)):
raise
self.skipTest("cannot set modification time to large integer ({})".format(e))
__import__(TESTFN)
# The pyc file was created.
os.stat(compiled)
finally:
del sys.path[0]
remove_files(TESTFN)
def test_bogus_fromlist(self):
try:
__import__('http', fromlist=['blah'])
except ImportError:
self.fail("fromlist must allow bogus names")
@cpython_only
def test_delete_builtins_import(self):
args = ["-c", "del __builtins__.__import__; import os"]
popen = script_helper.spawn_python(*args)
stdout, stderr = popen.communicate()
self.assertIn(b"ImportError", stdout)
def test_from_import_message_for_nonexistent_module(self):
with self.assertRaisesRegex(ImportError, "^No module named 'bogus'"):
from bogus import foo
def test_from_import_message_for_existing_module(self):
with self.assertRaisesRegex(ImportError, "^cannot import name 'bogus'"):
from re import bogus
def test_from_import_AttributeError(self):
# Issue #24492: trying to import an attribute that raises an
# AttributeError should lead to an ImportError.
class AlwaysAttributeError:
def __getattr__(self, _):
raise AttributeError
module_name = 'test_from_import_AttributeError'
self.addCleanup(unload, module_name)
sys.modules[module_name] = AlwaysAttributeError()
with self.assertRaises(ImportError) as cm:
from test_from_import_AttributeError import does_not_exist
self.assertEqual(str(cm.exception),
"cannot import name 'does_not_exist' from '<unknown module name>' (unknown location)")
@cpython_only
def test_issue31492(self):
# There shouldn't be an assertion failure in case of failing to import
# from a module with a bad __name__ attribute, or in case of failing
# to access an attribute of such a module.
with swap_attr(os, '__name__', None):
with self.assertRaises(ImportError):
from os import does_not_exist
with self.assertRaises(AttributeError):
os.does_not_exist
@threading_helper.requires_working_threading()
def test_concurrency(self):
# bpo 38091: this is a hack to slow down the code that calls
# has_deadlock(); the logic was itself sometimes deadlocking.
def delay_has_deadlock(frame, event, arg):
if event == 'call' and frame.f_code.co_name == 'has_deadlock':
time.sleep(0.1)
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'data'))
try:
exc = None
def run():
sys.settrace(delay_has_deadlock)
event.wait()
try:
import package
except BaseException as e:
nonlocal exc
exc = e
sys.settrace(None)
for i in range(10):
event = threading.Event()
threads = [threading.Thread(target=run) for x in range(2)]
try:
with threading_helper.start_threads(threads, event.set):
time.sleep(0)
finally:
sys.modules.pop('package', None)
sys.modules.pop('package.submodule', None)
if exc is not None:
raise exc
finally:
del sys.path[0]
@unittest.skipUnless(sys.platform == "win32", "Windows-specific")
def test_dll_dependency_import(self):
from _winapi import GetModuleFileName
dllname = GetModuleFileName(sys.dllhandle)
pydname = importlib.util.find_spec("_sqlite3").origin
depname = os.path.join(
os.path.dirname(pydname),
"sqlite3{}.dll".format("_d" if "_d" in pydname else ""))
with os_helper.temp_dir() as tmp:
tmp2 = os.path.join(tmp, "DLLs")
os.mkdir(tmp2)
pyexe = os.path.join(tmp, os.path.basename(sys.executable))
shutil.copy(sys.executable, pyexe)
shutil.copy(dllname, tmp)
for f in glob.glob(os.path.join(glob.escape(sys.prefix), "vcruntime*.dll")):
shutil.copy(f, tmp)
shutil.copy(pydname, tmp2)
env = None
env = {k.upper(): os.environ[k] for k in os.environ}
env["PYTHONPATH"] = tmp2 + ";" + STDLIB_DIR
# Test 1: import with added DLL directory
subprocess.check_call([
pyexe, "-Sc", ";".join([
"import os",
"p = os.add_dll_directory({!r})".format(
os.path.dirname(depname)),
"import _sqlite3",
"p.close"
])],
stderr=subprocess.STDOUT,
env=env,
cwd=os.path.dirname(pyexe))
# Test 2: import with DLL adjacent to PYD
shutil.copy(depname, tmp2)
subprocess.check_call([pyexe, "-Sc", "import _sqlite3"],
stderr=subprocess.STDOUT,
env=env,
cwd=os.path.dirname(pyexe))
def test_issue105979(self):
# this used to crash
with self.assertRaises(ImportError) as cm:
_imp.get_frozen_object("x", b"6\'\xd5Cu\x12")
self.assertIn("Frozen object named 'x' is invalid",
str(cm.exception))
def test_frozen_module_from_import_error(self):
with self.assertRaises(ImportError) as cm:
from os import this_will_never_exist
self.assertIn(
f"cannot import name 'this_will_never_exist' from 'os' ({os.__file__})",
str(cm.exception),
)
with self.assertRaises(ImportError) as cm:
from sys import this_will_never_exist
self.assertIn(
"cannot import name 'this_will_never_exist' from 'sys' (unknown location)",
str(cm.exception),
)
scripts = [
"""
import os
os.__spec__.has_location = False
os.__file__ = []
from os import this_will_never_exist
""",
"""
import os
os.__spec__.has_location = False
del os.__file__
from os import this_will_never_exist
""",
"""
import os
os.__spec__.origin = []
os.__file__ = []
from os import this_will_never_exist
"""
]
for script in scripts:
with self.subTest(script=script):
expected_error = (
b"cannot import name 'this_will_never_exist' "
b"from 'os' (unknown location)"
)
popen = script_helper.spawn_python("-c", script)
stdout, stderr = popen.communicate()
self.assertIn(expected_error, stdout)
def test_script_shadowing_stdlib(self):
script_errors = [
(
"import fractions\nfractions.Fraction",
rb"AttributeError: module 'fractions' has no attribute 'Fraction'"
),
(
"from fractions import Fraction",
rb"ImportError: cannot import name 'Fraction' from 'fractions'"
)
]
for script, error in script_errors:
with self.subTest(script=script), os_helper.temp_dir() as tmp:
with open(os.path.join(tmp, "fractions.py"), "w", encoding='utf-8') as f:
f.write(script)
expected_error = error + (
rb" \(consider renaming '.*fractions.py' since it has the "
rb"same name as the standard library module named 'fractions' "
rb"and prevents importing that standard library module\)"
)
popen = script_helper.spawn_python(os.path.join(tmp, "fractions.py"), cwd=tmp)
stdout, stderr = popen.communicate()
self.assertRegex(stdout, expected_error)
popen = script_helper.spawn_python('-m', 'fractions', cwd=tmp)
stdout, stderr = popen.communicate()
self.assertRegex(stdout, expected_error)
popen = script_helper.spawn_python('-c', 'import fractions', cwd=tmp)
stdout, stderr = popen.communicate()
self.assertRegex(stdout, expected_error)
# and there's no error at all when using -P
popen = script_helper.spawn_python('-P', 'fractions.py', cwd=tmp)
stdout, stderr = popen.communicate()
self.assertEqual(stdout, b'')
tmp_child = os.path.join(tmp, "child")
os.mkdir(tmp_child)
# test the logic with different cwd
popen = script_helper.spawn_python(os.path.join(tmp, "fractions.py"), cwd=tmp_child)
stdout, stderr = popen.communicate()
self.assertRegex(stdout, expected_error)
popen = script_helper.spawn_python('-m', 'fractions', cwd=tmp_child)
stdout, stderr = popen.communicate()
self.assertEqual(stdout, b'') # no error
popen = script_helper.spawn_python('-c', 'import fractions', cwd=tmp_child)
stdout, stderr = popen.communicate()
self.assertEqual(stdout, b'') # no error
def test_package_shadowing_stdlib_module(self):
script_errors = [
(
"fractions.Fraction",
rb"AttributeError: module 'fractions' has no attribute 'Fraction'"
),
(
"from fractions import Fraction",
rb"ImportError: cannot import name 'Fraction' from 'fractions'"
)
]
for script, error in script_errors:
with self.subTest(script=script), os_helper.temp_dir() as tmp:
os.mkdir(os.path.join(tmp, "fractions"))
with open(
os.path.join(tmp, "fractions", "__init__.py"), "w", encoding='utf-8'
) as f:
f.write("shadowing_module = True")
with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
f.write("import fractions; fractions.shadowing_module\n")
f.write(script)
expected_error = error + (
rb" \(consider renaming '.*[\\/]fractions[\\/]+__init__.py' since it has the "
rb"same name as the standard library module named 'fractions' "
rb"and prevents importing that standard library module\)"
)
popen = script_helper.spawn_python(os.path.join(tmp, "main.py"), cwd=tmp)
stdout, stderr = popen.communicate()
self.assertRegex(stdout, expected_error)
popen = script_helper.spawn_python('-m', 'main', cwd=tmp)
stdout, stderr = popen.communicate()
self.assertRegex(stdout, expected_error)
# and there's no shadowing at all when using -P
popen = script_helper.spawn_python('-P', 'main.py', cwd=tmp)
stdout, stderr = popen.communicate()
self.assertRegex(stdout, b"module 'fractions' has no attribute 'shadowing_module'")
def test_script_shadowing_third_party(self):
script_errors = [
(
"import numpy\nnumpy.array",
rb"AttributeError: module 'numpy' has no attribute 'array'"
),
(
"from numpy import array",
rb"ImportError: cannot import name 'array' from 'numpy'"
)
]
for script, error in script_errors:
with self.subTest(script=script), os_helper.temp_dir() as tmp:
with open(os.path.join(tmp, "numpy.py"), "w", encoding='utf-8') as f:
f.write(script)
expected_error = error + (
rb" \(consider renaming '.*numpy.py' if it has the "
rb"same name as a library you intended to import\)\s+\Z"
)
popen = script_helper.spawn_python(os.path.join(tmp, "numpy.py"))
stdout, stderr = popen.communicate()
self.assertRegex(stdout, expected_error)
popen = script_helper.spawn_python('-m', 'numpy', cwd=tmp)
stdout, stderr = popen.communicate()
self.assertRegex(stdout, expected_error)
popen = script_helper.spawn_python('-c', 'import numpy', cwd=tmp)
stdout, stderr = popen.communicate()
self.assertRegex(stdout, expected_error)
def test_script_maybe_not_shadowing_third_party(self):
with os_helper.temp_dir() as tmp:
with open(os.path.join(tmp, "numpy.py"), "w", encoding='utf-8') as f:
f.write("this_script_does_not_attempt_to_import_numpy = True")
expected_error = (
rb"AttributeError: module 'numpy' has no attribute 'attr'\s+\Z"
)
popen = script_helper.spawn_python('-c', 'import numpy; numpy.attr', cwd=tmp)
stdout, stderr = popen.communicate()
self.assertRegex(stdout, expected_error)
expected_error = (
rb"ImportError: cannot import name 'attr' from 'numpy' \(.*\)\s+\Z"
)
popen = script_helper.spawn_python('-c', 'from numpy import attr', cwd=tmp)
stdout, stderr = popen.communicate()
self.assertRegex(stdout, expected_error)
def test_script_shadowing_stdlib_edge_cases(self):
with os_helper.temp_dir() as tmp:
with open(os.path.join(tmp, "fractions.py"), "w", encoding='utf-8') as f:
f.write("shadowing_module = True")
# Unhashable str subclass
with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
f.write("""
import fractions
fractions.shadowing_module
class substr(str):
__hash__ = None
fractions.__name__ = substr('fractions')
try:
fractions.Fraction
except TypeError as e:
print(str(e))
""")
popen = script_helper.spawn_python("main.py", cwd=tmp)
stdout, stderr = popen.communicate()
self.assertEqual(stdout.rstrip(), b"unhashable type: 'substr'")
with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
f.write("""
import fractions
fractions.shadowing_module
class substr(str):
__hash__ = None
fractions.__name__ = substr('fractions')
try:
from fractions import Fraction
except TypeError as e:
print(str(e))
""")
popen = script_helper.spawn_python("main.py", cwd=tmp)
stdout, stderr = popen.communicate()
self.assertEqual(stdout.rstrip(), b"unhashable type: 'substr'")
# Various issues with sys module
with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
f.write("""
import fractions
fractions.shadowing_module
import sys
sys.stdlib_module_names = None
try:
fractions.Fraction
except AttributeError as e:
print(str(e))
del sys.stdlib_module_names
try:
fractions.Fraction
except AttributeError as e:
print(str(e))
sys.path = [0]
try:
fractions.Fraction
except AttributeError as e:
print(str(e))
""")
popen = script_helper.spawn_python("main.py", cwd=tmp)
stdout, stderr = popen.communicate()
lines = stdout.splitlines()
self.assertEqual(len(lines), 3)
for line in lines:
self.assertEqual(line, b"module 'fractions' has no attribute 'Fraction'")
with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
f.write("""
import fractions
fractions.shadowing_module
import sys
sys.stdlib_module_names = None
try:
from fractions import Fraction
except ImportError as e:
print(str(e))
del sys.stdlib_module_names
try:
from fractions import Fraction
except ImportError as e:
print(str(e))
sys.path = [0]
try:
from fractions import Fraction
except ImportError as e:
print(str(e))
""")
popen = script_helper.spawn_python("main.py", cwd=tmp)
stdout, stderr = popen.communicate()
lines = stdout.splitlines()
self.assertEqual(len(lines), 3)
for line in lines:
self.assertRegex(line, rb"cannot import name 'Fraction' from 'fractions' \(.*\)")
# Various issues with origin
with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
f.write("""
import fractions
fractions.shadowing_module
del fractions.__spec__.origin
try:
fractions.Fraction
except AttributeError as e:
print(str(e))
fractions.__spec__.origin = []
try:
fractions.Fraction
except AttributeError as e:
print(str(e))
""")
popen = script_helper.spawn_python("main.py", cwd=tmp)
stdout, stderr = popen.communicate()
lines = stdout.splitlines()
self.assertEqual(len(lines), 2)
for line in lines:
self.assertEqual(line, b"module 'fractions' has no attribute 'Fraction'")
with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
f.write("""
import fractions
fractions.shadowing_module
del fractions.__spec__.origin
try:
from fractions import Fraction
except ImportError as e:
print(str(e))
fractions.__spec__.origin = []
try:
from fractions import Fraction
except ImportError as e:
print(str(e))
""")
popen = script_helper.spawn_python("main.py", cwd=tmp)
stdout, stderr = popen.communicate()
lines = stdout.splitlines()
self.assertEqual(len(lines), 2)
for line in lines:
self.assertRegex(line, rb"cannot import name 'Fraction' from 'fractions' \(.*\)")
def test_script_shadowing_stdlib_sys_path_modification(self):
script_errors = [
(
"import fractions\nfractions.Fraction",
rb"AttributeError: module 'fractions' has no attribute 'Fraction'"
),
(
"from fractions import Fraction",
rb"ImportError: cannot import name 'Fraction' from 'fractions'"
)
]
for script, error in script_errors:
with self.subTest(script=script), os_helper.temp_dir() as tmp:
with open(os.path.join(tmp, "fractions.py"), "w", encoding='utf-8') as f:
f.write("shadowing_module = True")
with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
f.write('import sys; sys.path.insert(0, "this_folder_does_not_exist")\n')
f.write(script)
expected_error = error + (
rb" \(consider renaming '.*fractions.py' since it has the "
rb"same name as the standard library module named 'fractions' "
rb"and prevents importing that standard library module\)"
)
popen = script_helper.spawn_python("main.py", cwd=tmp)
stdout, stderr = popen.communicate()
self.assertRegex(stdout, expected_error)
def test_create_dynamic_null(self):
with self.assertRaisesRegex(ValueError, 'embedded null character'):
class Spec:
name = "a\x00b"
origin = "abc"
_imp.create_dynamic(Spec())
with self.assertRaisesRegex(ValueError, 'embedded null character'):
class Spec2:
name = "abc"
origin = "a\x00b"
_imp.create_dynamic(Spec2())
@skip_if_dont_write_bytecode
class FilePermissionTests(unittest.TestCase):
# tests for file mode on cached .pyc files
@unittest.skipUnless(os.name == 'posix',
"test meaningful only on posix systems")
@unittest.skipIf(
is_emscripten or is_wasi,
"Emscripten's/WASI's umask is a stub."
)
def test_creation_mode(self):
mask = 0o022
with temp_umask(mask), ready_to_import() as (name, path):
cached_path = importlib.util.cache_from_source(path)
module = __import__(name)
if not os.path.exists(cached_path):
self.fail("__import__ did not result in creation of "
"a .pyc file")
stat_info = os.stat(cached_path)
# Check that the umask is respected, and the executable bits
# aren't set.
self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)),
oct(0o666 & ~mask))
@unittest.skipUnless(os.name == 'posix',
"test meaningful only on posix systems")
@os_helper.skip_unless_working_chmod
def test_cached_mode_issue_2051(self):
# permissions of .pyc should match those of .py, regardless of mask
mode = 0o600
with temp_umask(0o022), ready_to_import() as (name, path):
cached_path = importlib.util.cache_from_source(path)
os.chmod(path, mode)
__import__(name)
if not os.path.exists(cached_path):
self.fail("__import__ did not result in creation of "
"a .pyc file")
stat_info = os.stat(cached_path)
self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)), oct(mode))
@unittest.skipUnless(os.name == 'posix',
"test meaningful only on posix systems")
@os_helper.skip_unless_working_chmod
def test_cached_readonly(self):
mode = 0o400
with temp_umask(0o022), ready_to_import() as (name, path):
cached_path = importlib.util.cache_from_source(path)
os.chmod(path, mode)
__import__(name)
if not os.path.exists(cached_path):
self.fail("__import__ did not result in creation of "
"a .pyc file")
stat_info = os.stat(cached_path)
expected = mode | 0o200 # Account for fix for issue #6074
self.assertEqual(oct(stat.S_IMODE(stat_info.st_mode)), oct(expected))
def test_pyc_always_writable(self):
# Initially read-only .pyc files on Windows used to cause problems
# with later updates, see issue #6074 for details
with ready_to_import() as (name, path):
# Write a Python file, make it read-only and import it
with open(path, 'w', encoding='utf-8') as f:
f.write("x = 'original'\n")
# Tweak the mtime of the source to ensure pyc gets updated later
s = os.stat(path)
os.utime(path, (s.st_atime, s.st_mtime-100000000))
os.chmod(path, 0o400)
m = __import__(name)
self.assertEqual(m.x, 'original')
# Change the file and then reimport it
os.chmod(path, 0o600)
with open(path, 'w', encoding='utf-8') as f:
f.write("x = 'rewritten'\n")
unload(name)
importlib.invalidate_caches()
m = __import__(name)
self.assertEqual(m.x, 'rewritten')
# Now delete the source file and check the pyc was rewritten
unlink(path)
unload(name)
importlib.invalidate_caches()
bytecode_only = path + "c"
os.rename(importlib.util.cache_from_source(path), bytecode_only)
m = __import__(name)
self.assertEqual(m.x, 'rewritten')
class PycRewritingTests(unittest.TestCase):
# Test that the `co_filename` attribute on code objects always points
# to the right file, even when various things happen (e.g. both the .py
# and the .pyc file are renamed).
module_name = "unlikely_module_name"
module_source = """
import sys
code_filename = sys._getframe().f_code.co_filename
module_filename = __file__
constant = 1000
def func():
pass
func_filename = func.__code__.co_filename
"""
dir_name = os.path.abspath(TESTFN)
file_name = os.path.join(dir_name, module_name) + os.extsep + "py"
compiled_name = importlib.util.cache_from_source(file_name)
def setUp(self):
self.sys_path = sys.path[:]
self.orig_module = sys.modules.pop(self.module_name, None)
os.mkdir(self.dir_name)
with open(self.file_name, "w", encoding='utf-8') as f:
f.write(self.module_source)
sys.path.insert(0, self.dir_name)
importlib.invalidate_caches()
def tearDown(self):
sys.path[:] = self.sys_path
if self.orig_module is not None:
sys.modules[self.module_name] = self.orig_module
else:
unload(self.module_name)
unlink(self.file_name)
unlink(self.compiled_name)
rmtree(self.dir_name)
def import_module(self):
ns = globals()
__import__(self.module_name, ns, ns)
return sys.modules[self.module_name]
def test_basics(self):
mod = self.import_module()
self.assertEqual(mod.module_filename, self.file_name)
self.assertEqual(mod.code_filename, self.file_name)
self.assertEqual(mod.func_filename, self.file_name)
del sys.modules[self.module_name]
mod = self.import_module()
self.assertEqual(mod.module_filename, self.file_name)
self.assertEqual(mod.code_filename, self.file_name)
self.assertEqual(mod.func_filename, self.file_name)
def test_incorrect_code_name(self):
py_compile.compile(self.file_name, dfile="another_module.py")
mod = self.import_module()
self.assertEqual(mod.module_filename, self.file_name)
self.assertEqual(mod.code_filename, self.file_name)
self.assertEqual(mod.func_filename, self.file_name)
def test_module_without_source(self):
target = "another_module.py"
py_compile.compile(self.file_name, dfile=target)
os.remove(self.file_name)
pyc_file = make_legacy_pyc(self.file_name)
importlib.invalidate_caches()
mod = self.import_module()
self.assertEqual(mod.module_filename, pyc_file)
self.assertEqual(mod.code_filename, target)
self.assertEqual(mod.func_filename, target)
def test_foreign_code(self):
py_compile.compile(self.file_name)
with open(self.compiled_name, "rb") as f:
header = f.read(16)
code = marshal.load(f)
constants = list(code.co_consts)
foreign_code = importlib.import_module.__code__
pos = constants.index(1000)
constants[pos] = foreign_code
code = code.replace(co_consts=tuple(constants))
with open(self.compiled_name, "wb") as f:
f.write(header)
marshal.dump(code, f)
mod = self.import_module()
self.assertEqual(mod.constant.co_filename, foreign_code.co_filename)
class PathsTests(unittest.TestCase):
SAMPLES = ('test', 'test\u00e4\u00f6\u00fc\u00df', 'test\u00e9\u00e8',
'test\u00b0\u00b3\u00b2')
path = TESTFN
def setUp(self):
os.mkdir(self.path)
self.syspath = sys.path[:]
def tearDown(self):
rmtree(self.path)
sys.path[:] = self.syspath
# Regression test for http://bugs.python.org/issue1293.
def test_trailing_slash(self):
with open(os.path.join(self.path, 'test_trailing_slash.py'),
'w', encoding='utf-8') as f:
f.write("testdata = 'test_trailing_slash'")
sys.path.append(self.path+'/')
mod = __import__("test_trailing_slash")
self.assertEqual(mod.testdata, 'test_trailing_slash')
unload("test_trailing_slash")
# Regression test for http://bugs.python.org/issue3677.
@unittest.skipUnless(sys.platform == 'win32', 'Windows-specific')
def test_UNC_path(self):
with open(os.path.join(self.path, 'test_unc_path.py'), 'w') as f:
f.write("testdata = 'test_unc_path'")
importlib.invalidate_caches()
# Create the UNC path, like \\myhost\c$\foo\bar.
path = os.path.abspath(self.path)
import socket
hn = socket.gethostname()
drive = path[0]
unc = "\\\\%s\\%s$"%(hn, drive)
unc += path[2:]
try:
os.listdir(unc)
except OSError as e:
if e.errno in (errno.EPERM, errno.EACCES, errno.ENOENT):
# See issue #15338
self.skipTest("cannot access administrative share %r" % (unc,))
raise
sys.path.insert(0, unc)
try:
mod = __import__("test_unc_path")
except ImportError as e:
self.fail("could not import 'test_unc_path' from %r: %r"
% (unc, e))
self.assertEqual(mod.testdata, 'test_unc_path')
self.assertTrue(mod.__file__.startswith(unc), mod.__file__)
unload("test_unc_path")
class RelativeImportTests(unittest.TestCase):
def tearDown(self):
unload("test.relimport")
setUp = tearDown
def test_relimport_star(self):
# This will import * from .test_import.
from .. import relimport
self.assertTrue(hasattr(relimport, "RelativeImportTests"))
def test_issue3221(self):
# Note for mergers: the 'absolute' tests from the 2.x branch
# are missing in Py3k because implicit relative imports are
# a thing of the past
#
# Regression test for http://bugs.python.org/issue3221.
def check_relative():
exec("from . import relimport", ns)
# Check relative import OK with __package__ and __name__ correct
ns = dict(__package__='test', __name__='test.notarealmodule')
check_relative()
# Check relative import OK with only __name__ wrong
ns = dict(__package__='test', __name__='notarealpkg.notarealmodule')
check_relative()
# Check relative import fails with only __package__ wrong
ns = dict(__package__='foo', __name__='test.notarealmodule')
self.assertRaises(ModuleNotFoundError, check_relative)
# Check relative import fails with __package__ and __name__ wrong
ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule')
self.assertRaises(ModuleNotFoundError, check_relative)
# Check relative import fails with package set to a non-string
ns = dict(__package__=object())
self.assertRaises(TypeError, check_relative)
def test_parentless_import_shadowed_by_global(self):
# Test as if this were done from the REPL where this error most commonly occurs (bpo-37409).
script_helper.assert_python_failure('-W', 'ignore', '-c',
"foo = 1; from . import foo")
def test_absolute_import_without_future(self):
# If explicit relative import syntax is used, then do not try
# to perform an absolute import in the face of failure.
# Issue #7902.
with self.assertRaises(ImportError):
from .os import sep
self.fail("explicit relative import triggered an "
"implicit absolute import")
def test_import_from_non_package(self):
path = os.path.join(os.path.dirname(__file__), 'data', 'package2')
with uncache('submodule1', 'submodule2'), DirsOnSysPath(path):
with self.assertRaises(ImportError):
import submodule1
self.assertNotIn('submodule1', sys.modules)
self.assertNotIn('submodule2', sys.modules)
def test_import_from_unloaded_package(self):
with uncache('package2', 'package2.submodule1', 'package2.submodule2'), \
DirsOnSysPath(os.path.join(os.path.dirname(__file__), 'data')):
import package2.submodule1
package2.submodule1.submodule2
def test_rebinding(self):
# The same data is also used for testing pkgutil.resolve_name()
# in test_pkgutil and mock.patch in test_unittest.
path = os.path.join(os.path.dirname(__file__), 'data')
with uncache('package3', 'package3.submodule'), DirsOnSysPath(path):
from package3 import submodule
self.assertEqual(submodule.attr, 'rebound')
import package3.submodule as submodule
self.assertEqual(submodule.attr, 'rebound')
with uncache('package3', 'package3.submodule'), DirsOnSysPath(path):
import package3.submodule as submodule
self.assertEqual(submodule.attr, 'rebound')
from package3 import submodule
self.assertEqual(submodule.attr, 'rebound')
def test_rebinding2(self):
path = os.path.join(os.path.dirname(__file__), 'data')
with uncache('package4', 'package4.submodule'), DirsOnSysPath(path):
import package4.submodule as submodule
self.assertEqual(submodule.attr, 'submodule')
from package4 import submodule
self.assertEqual(submodule.attr, 'submodule')
with uncache('package4', 'package4.submodule'), DirsOnSysPath(path):
from package4 import submodule
self.assertEqual(submodule.attr, 'origin')
import package4.submodule as submodule
self.assertEqual(submodule.attr, 'submodule')
class OverridingImportBuiltinTests(unittest.TestCase):
def test_override_builtin(self):
# Test that overriding builtins.__import__ can bypass sys.modules.
import os
def foo():
import os
return os
self.assertEqual(foo(), os) # Quick sanity check.
with swap_attr(builtins, "__import__", lambda *x: 5):
self.assertEqual(foo(), 5)
# Test what happens when we shadow __import__ in globals(); this
# currently does not impact the import process, but if this changes,
# other code will need to change, so keep this test as a tripwire.
with swap_item(globals(), "__import__", lambda *x: 5):
self.assertEqual(foo(), os)
class PycacheTests(unittest.TestCase):
# Test the various PEP 3147/488-related behaviors.
def _clean(self):
forget(TESTFN)
rmtree('__pycache__')
unlink(self.source)
def setUp(self):
self.source = TESTFN + '.py'
self._clean()
with open(self.source, 'w', encoding='utf-8') as fp:
print('# This is a test file written by test_import.py', file=fp)
sys.path.insert(0, os.curdir)
importlib.invalidate_caches()
def tearDown(self):
assert sys.path[0] == os.curdir, 'Unexpected sys.path[0]'
del sys.path[0]
self._clean()
@skip_if_dont_write_bytecode
def test_import_pyc_path(self):
self.assertFalse(os.path.exists('__pycache__'))
__import__(TESTFN)
self.assertTrue(os.path.exists('__pycache__'))
pyc_path = importlib.util.cache_from_source(self.source)
self.assertTrue(os.path.exists(pyc_path),
'bytecode file {!r} for {!r} does not '
'exist'.format(pyc_path, TESTFN))
@unittest.skipUnless(os.name == 'posix',
"test meaningful only on posix systems")
@skip_if_dont_write_bytecode
@os_helper.skip_unless_working_chmod
@os_helper.skip_if_dac_override
@unittest.skipIf(is_emscripten, "umask is a stub")
def test_unwritable_directory(self):
# When the umask causes the new __pycache__ directory to be
# unwritable, the import still succeeds but no .pyc file is written.
with temp_umask(0o222):
__import__(TESTFN)
self.assertTrue(os.path.exists('__pycache__'))
pyc_path = importlib.util.cache_from_source(self.source)
self.assertFalse(os.path.exists(pyc_path),
'bytecode file {!r} for {!r} '
'exists'.format(pyc_path, TESTFN))
@skip_if_dont_write_bytecode
def test_missing_source(self):
# With PEP 3147 cache layout, removing the source but leaving the pyc
# file does not satisfy the import.
__import__(TESTFN)
pyc_file = importlib.util.cache_from_source(self.source)
self.assertTrue(os.path.exists(pyc_file))
os.remove(self.source)
forget(TESTFN)
importlib.invalidate_caches()
self.assertRaises(ImportError, __import__, TESTFN)
@skip_if_dont_write_bytecode
def test_missing_source_legacy(self):
# Like test_missing_source() except that for backward compatibility,
# when the pyc file lives where the py file would have been (and named
# without the tag), it is importable. The __file__ of the imported
# module is the pyc location.
__import__(TESTFN)
# pyc_file gets removed in _clean() via tearDown().
pyc_file = make_legacy_pyc(self.source)
os.remove(self.source)
unload(TESTFN)
importlib.invalidate_caches()
m = __import__(TESTFN)
try:
self.assertEqual(m.__file__,
os.path.join(os.getcwd(), os.path.relpath(pyc_file)))
finally:
os.remove(pyc_file)
def test___cached__(self):
# Modules now also have an __cached__ that points to the pyc file.
m = __import__(TESTFN)
pyc_file = importlib.util.cache_from_source(TESTFN + '.py')
self.assertEqual(m.__cached__, os.path.join(os.getcwd(), pyc_file))
@skip_if_dont_write_bytecode
def test___cached___legacy_pyc(self):
# Like test___cached__() except that for backward compatibility,
# when the pyc file lives where the py file would have been (and named
# without the tag), it is importable. The __cached__ of the imported
# module is the pyc location.
__import__(TESTFN)
# pyc_file gets removed in _clean() via tearDown().
pyc_file = make_legacy_pyc(self.source)
os.remove(self.source)
unload(TESTFN)
importlib.invalidate_caches()
m = __import__(TESTFN)
self.assertEqual(m.__cached__,
os.path.join(os.getcwd(), os.path.relpath(pyc_file)))
@skip_if_dont_write_bytecode
def test_package___cached__(self):
# Like test___cached__ but for packages.
def cleanup():
rmtree('pep3147')
unload('pep3147.foo')
unload('pep3147')
os.mkdir('pep3147')
self.addCleanup(cleanup)
# Touch the __init__.py
with open(os.path.join('pep3147', '__init__.py'), 'wb'):
pass
with open(os.path.join('pep3147', 'foo.py'), 'wb'):
pass
importlib.invalidate_caches()
m = __import__('pep3147.foo')
init_pyc = importlib.util.cache_from_source(
os.path.join('pep3147', '__init__.py'))
self.assertEqual(m.__cached__, os.path.join(os.getcwd(), init_pyc))
foo_pyc = importlib.util.cache_from_source(os.path.join('pep3147', 'foo.py'))
self.assertEqual(sys.modules['pep3147.foo'].__cached__,
os.path.join(os.getcwd(), foo_pyc))
def test_package___cached___from_pyc(self):
# Like test___cached__ but ensuring __cached__ when imported from a
# PEP 3147 pyc file.
def cleanup():
rmtree('pep3147')
unload('pep3147.foo')
unload('pep3147')
os.mkdir('pep3147')
self.addCleanup(cleanup)
# Touch the __init__.py
with open(os.path.join('pep3147', '__init__.py'), 'wb'):
pass
with open(os.path.join('pep3147', 'foo.py'), 'wb'):
pass
importlib.invalidate_caches()
m = __import__('pep3147.foo')
unload('pep3147.foo')
unload('pep3147')
importlib.invalidate_caches()
m = __import__('pep3147.foo')
init_pyc = importlib.util.cache_from_source(
os.path.join('pep3147', '__init__.py'))
self.assertEqual(m.__cached__, os.path.join(os.getcwd(), init_pyc))
foo_pyc = importlib.util.cache_from_source(os.path.join('pep3147', 'foo.py'))
self.assertEqual(sys.modules['pep3147.foo'].__cached__,
os.path.join(os.getcwd(), foo_pyc))
def test_recompute_pyc_same_second(self):
# Even when the source file doesn't change timestamp, a change in
# source size is enough to trigger recomputation of the pyc file.
__import__(TESTFN)
unload(TESTFN)
with open(self.source, 'a', encoding='utf-8') as fp:
print("x = 5", file=fp)
m = __import__(TESTFN)
self.assertEqual(m.x, 5)
class TestSymbolicallyLinkedPackage(unittest.TestCase):
package_name = 'sample'
tagged = package_name + '-tagged'
def setUp(self):
os_helper.rmtree(self.tagged)
os_helper.rmtree(self.package_name)
self.orig_sys_path = sys.path[:]
# create a sample package; imagine you have a package with a tag and
# you want to symbolically link it from its untagged name.
os.mkdir(self.tagged)
self.addCleanup(os_helper.rmtree, self.tagged)
init_file = os.path.join(self.tagged, '__init__.py')
os_helper.create_empty_file(init_file)
assert os.path.exists(init_file)
# now create a symlink to the tagged package
# sample -> sample-tagged
os.symlink(self.tagged, self.package_name, target_is_directory=True)
self.addCleanup(os_helper.unlink, self.package_name)
importlib.invalidate_caches()
self.assertEqual(os.path.isdir(self.package_name), True)
assert os.path.isfile(os.path.join(self.package_name, '__init__.py'))
def tearDown(self):
sys.path[:] = self.orig_sys_path
# regression test for issue6727
@unittest.skipUnless(
not hasattr(sys, 'getwindowsversion')
or sys.getwindowsversion() >= (6, 0),
"Windows Vista or later required")
@os_helper.skip_unless_symlink
def test_symlinked_dir_importable(self):
# make sure sample can only be imported from the current directory.
sys.path[:] = ['.']
assert os.path.exists(self.package_name)
assert os.path.exists(os.path.join(self.package_name, '__init__.py'))
# Try to import the package
importlib.import_module(self.package_name)
@cpython_only
class ImportlibBootstrapTests(unittest.TestCase):
# These tests check that importlib is bootstrapped.
def test_frozen_importlib(self):
mod = sys.modules['_frozen_importlib']
self.assertTrue(mod)
def test_frozen_importlib_is_bootstrap(self):
from importlib import _bootstrap
mod = sys.modules['_frozen_importlib']
self.assertIs(mod, _bootstrap)
self.assertEqual(mod.__name__, 'importlib._bootstrap')
self.assertEqual(mod.__package__, 'importlib')
self.assertTrue(mod.__file__.endswith('_bootstrap.py'), mod.__file__)
def test_frozen_importlib_external_is_bootstrap_external(self):
from importlib import _bootstrap_external
mod = sys.modules['_frozen_importlib_external']
self.assertIs(mod, _bootstrap_external)
self.assertEqual(mod.__name__, 'importlib._bootstrap_external')
self.assertEqual(mod.__package__, 'importlib')
self.assertTrue(mod.__file__.endswith('_bootstrap_external.py'), mod.__file__)
def test_there_can_be_only_one(self):
# Issue #15386 revealed a tricky loophole in the bootstrapping
# This test is technically redundant, since the bug caused importing
# this test module to crash completely, but it helps prove the point
from importlib import machinery
mod = sys.modules['_frozen_importlib']
self.assertIs(machinery.ModuleSpec, mod.ModuleSpec)
@cpython_only
class GetSourcefileTests(unittest.TestCase):
"""Test importlib._bootstrap_external._get_sourcefile() as used by the C API.
Because of the peculiarities of the need of this function, the tests are
knowingly whitebox tests.
"""
def test_get_sourcefile(self):
# Given a valid bytecode path, return the path to the corresponding
# source file if it exists.
with mock.patch('importlib._bootstrap_external._path_isfile') as _path_isfile:
_path_isfile.return_value = True
path = TESTFN + '.pyc'
expect = TESTFN + '.py'
self.assertEqual(_get_sourcefile(path), expect)
def test_get_sourcefile_no_source(self):
# Given a valid bytecode path without a corresponding source path,
# return the original bytecode path.
with mock.patch('importlib._bootstrap_external._path_isfile') as _path_isfile:
_path_isfile.return_value = False
path = TESTFN + '.pyc'
self.assertEqual(_get_sourcefile(path), path)
def test_get_sourcefile_bad_ext(self):
# Given a path with an invalid bytecode extension, return the
# bytecode path passed as the argument.
path = TESTFN + '.bad_ext'
self.assertEqual(_get_sourcefile(path), path)
class ImportTracebackTests(unittest.TestCase):
def setUp(self):
os.mkdir(TESTFN)
self.old_path = sys.path[:]
sys.path.insert(0, TESTFN)
def tearDown(self):
sys.path[:] = self.old_path
rmtree(TESTFN)
def create_module(self, mod, contents, ext=".py"):
fname = os.path.join(TESTFN, mod + ext)
with open(fname, "w", encoding='utf-8') as f:
f.write(contents)
self.addCleanup(unload, mod)
importlib.invalidate_caches()
return fname
def assert_traceback(self, tb, files):
deduped_files = []
while tb:
code = tb.tb_frame.f_code
fn = code.co_filename
if not deduped_files or fn != deduped_files[-1]:
deduped_files.append(fn)
tb = tb.tb_next
self.assertEqual(len(deduped_files), len(files), deduped_files)
for fn, pat in zip(deduped_files, files):
self.assertIn(pat, fn)
def test_nonexistent_module(self):
try:
# assertRaises() clears __traceback__
import nonexistent_xyzzy
except ImportError as e:
tb = e.__traceback__
else:
self.fail("ImportError should have been raised")
self.assert_traceback(tb, [__file__])
def test_nonexistent_module_nested(self):
self.create_module("foo", "import nonexistent_xyzzy")
try:
import foo
except ImportError as e:
tb = e.__traceback__
else:
self.fail("ImportError should have been raised")
self.assert_traceback(tb, [__file__, 'foo.py'])
def test_exec_failure(self):
self.create_module("foo", "1/0")
try:
import foo
except ZeroDivisionError as e:
tb = e.__traceback__
else:
self.fail("ZeroDivisionError should have been raised")
self.assert_traceback(tb, [__file__, 'foo.py'])
def test_exec_failure_nested(self):
self.create_module("foo", "import bar")
self.create_module("bar", "1/0")
try:
import foo
except ZeroDivisionError as e:
tb = e.__traceback__
else:
self.fail("ZeroDivisionError should have been raised")
self.assert_traceback(tb, [__file__, 'foo.py', 'bar.py'])
# A few more examples from issue #15425
def test_syntax_error(self):
self.create_module("foo", "invalid syntax is invalid")
try:
import foo
except SyntaxError as e:
tb = e.__traceback__
else:
self.fail("SyntaxError should have been raised")
self.assert_traceback(tb, [__file__])
def _setup_broken_package(self, parent, child):
pkg_name = "_parent_foo"
self.addCleanup(unload, pkg_name)
pkg_path = os.path.join(TESTFN, pkg_name)
os.mkdir(pkg_path)
# Touch the __init__.py
init_path = os.path.join(pkg_path, '__init__.py')
with open(init_path, 'w', encoding='utf-8') as f:
f.write(parent)
bar_path = os.path.join(pkg_path, 'bar.py')
with open(bar_path, 'w', encoding='utf-8') as f:
f.write(child)
importlib.invalidate_caches()
return init_path, bar_path
def test_broken_submodule(self):
init_path, bar_path = self._setup_broken_package("", "1/0")
try:
import _parent_foo.bar
except ZeroDivisionError as e:
tb = e.__traceback__
else:
self.fail("ZeroDivisionError should have been raised")
self.assert_traceback(tb, [__file__, bar_path])
def test_broken_from(self):
init_path, bar_path = self._setup_broken_package("", "1/0")
try:
from _parent_foo import bar
except ZeroDivisionError as e:
tb = e.__traceback__
else:
self.fail("ImportError should have been raised")
self.assert_traceback(tb, [__file__, bar_path])
def test_broken_parent(self):
init_path, bar_path = self._setup_broken_package("1/0", "")
try:
import _parent_foo.bar
except ZeroDivisionError as e:
tb = e.__traceback__
else:
self.fail("ZeroDivisionError should have been raised")
self.assert_traceback(tb, [__file__, init_path])
def test_broken_parent_from(self):
init_path, bar_path = self._setup_broken_package("1/0", "")
try:
from _parent_foo import bar
except ZeroDivisionError as e:
tb = e.__traceback__
else:
self.fail("ZeroDivisionError should have been raised")
self.assert_traceback(tb, [__file__, init_path])
@cpython_only
def test_import_bug(self):
# We simulate a bug in importlib and check that it's not stripped
# away from the traceback.
self.create_module("foo", "")
importlib = sys.modules['_frozen_importlib_external']
if 'load_module' in vars(importlib.SourceLoader):
old_exec_module = importlib.SourceLoader.exec_module
else:
old_exec_module = None
try:
def exec_module(*args):
1/0
importlib.SourceLoader.exec_module = exec_module
try:
import foo
except ZeroDivisionError as e:
tb = e.__traceback__
else:
self.fail("ZeroDivisionError should have been raised")
self.assert_traceback(tb, [__file__, '<frozen importlib', __file__])
finally:
if old_exec_module is None:
del importlib.SourceLoader.exec_module
else:
importlib.SourceLoader.exec_module = old_exec_module
@unittest.skipUnless(TESTFN_UNENCODABLE, 'need TESTFN_UNENCODABLE')
def test_unencodable_filename(self):
# Issue #11619: The Python parser and the import machinery must not
# encode filenames, especially on Windows
pyname = script_helper.make_script('', TESTFN_UNENCODABLE, 'pass')
self.addCleanup(unlink, pyname)
name = pyname[:-3]
script_helper.assert_python_ok("-c", "mod = __import__(%a)" % name,
__isolated=False)
class CircularImportTests(unittest.TestCase):
"""See the docstrings of the modules being imported for the purpose of the
test."""
def tearDown(self):
"""Make sure no modules pre-exist in sys.modules which are being used to
test."""
for key in list(sys.modules.keys()):
if key.startswith('test.test_import.data.circular_imports'):
del sys.modules[key]
def test_direct(self):
try:
import test.test_import.data.circular_imports.basic
except ImportError:
self.fail('circular import through relative imports failed')
def test_indirect(self):
try:
import test.test_import.data.circular_imports.indirect
except ImportError:
self.fail('relative import in module contributing to circular '
'import failed')
def test_subpackage(self):
try:
import test.test_import.data.circular_imports.subpackage
except ImportError:
self.fail('circular import involving a subpackage failed')
def test_rebinding(self):
try:
import test.test_import.data.circular_imports.rebinding as rebinding
except ImportError:
self.fail('circular import with rebinding of module attribute failed')
from test.test_import.data.circular_imports.subpkg import util
self.assertIs(util.util, rebinding.util)
def test_binding(self):
try:
import test.test_import.data.circular_imports.binding
except ImportError:
self.fail('circular import with binding a submodule to a name failed')
def test_crossreference1(self):
import test.test_import.data.circular_imports.use
import test.test_import.data.circular_imports.source
def test_crossreference2(self):
with self.assertRaises(AttributeError) as cm:
import test.test_import.data.circular_imports.source
errmsg = str(cm.exception)
self.assertIn('test.test_import.data.circular_imports.source', errmsg)
self.assertIn('spam', errmsg)
self.assertIn('partially initialized module', errmsg)
self.assertIn('circular import', errmsg)
def test_circular_from_import(self):
with self.assertRaises(ImportError) as cm:
import test.test_import.data.circular_imports.from_cycle1
self.assertIn(
"cannot import name 'b' from partially initialized module "
"'test.test_import.data.circular_imports.from_cycle1' "
"(most likely due to a circular import)",
str(cm.exception),
)
def test_circular_import(self):
with self.assertRaisesRegex(
AttributeError,
r"partially initialized module 'test.test_import.data.circular_imports.import_cycle' "
r"from '.*' has no attribute 'some_attribute' \(most likely due to a circular import\)"
):
import test.test_import.data.circular_imports.import_cycle
def test_absolute_circular_submodule(self):
with self.assertRaises(AttributeError) as cm:
import test.test_import.data.circular_imports.subpkg2.parent
self.assertIn(
"cannot access submodule 'parent' of module "
"'test.test_import.data.circular_imports.subpkg2' "
"(most likely due to a circular import)",
str(cm.exception),
)
@requires_singlephase_init
@unittest.skipIf(_testsinglephase is None, "test requires _testsinglephase module")
def test_singlephase_circular(self):
"""Regression test for gh-123950
Import a single-phase-init module that imports itself
from the PyInit_* function (before it's added to sys.modules).
Manages its own cache (which is `static`, and so incompatible
with multiple interpreters or interpreter reset).
"""
name = '_testsinglephase_circular'
helper_name = 'test.test_import.data.circular_imports.singlephase'
with uncache(name, helper_name):
filename = _testsinglephase.__file__
# We don't put the module in sys.modules: that the *inner*
# import should do that.
mod = import_extension_from_file(name, filename,
put_in_sys_modules=False)
self.assertEqual(mod.helper_mod_name, helper_name)
self.assertIn(name, sys.modules)
self.assertIn(helper_name, sys.modules)
self.assertIn(name, sys.modules)
self.assertIn(helper_name, sys.modules)
self.assertNotIn(name, sys.modules)
self.assertNotIn(helper_name, sys.modules)
self.assertIs(mod.clear_static_var(), mod)
_testinternalcapi.clear_extension('_testsinglephase_circular',
mod.__spec__.origin)
def test_unwritable_module(self):
self.addCleanup(unload, "test.test_import.data.unwritable")
self.addCleanup(unload, "test.test_import.data.unwritable.x")
import test.test_import.data.unwritable as unwritable
with self.assertWarns(ImportWarning):
from test.test_import.data.unwritable import x
self.assertNotEqual(type(unwritable), ModuleType)
self.assertEqual(type(x), ModuleType)
with self.assertRaises(AttributeError):
unwritable.x = 42
class SubinterpImportTests(unittest.TestCase):
RUN_KWARGS = dict(
allow_fork=False,
allow_exec=False,
allow_threads=True,
allow_daemon_threads=False,
# Isolation-related config values aren't included here.
)
ISOLATED = dict(
use_main_obmalloc=False,
gil=2,
)
NOT_ISOLATED = {k: not v for k, v in ISOLATED.items()}
NOT_ISOLATED['gil'] = 1
@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
def pipe(self):
r, w = os.pipe()
self.addCleanup(os.close, r)
self.addCleanup(os.close, w)
if hasattr(os, 'set_blocking'):
os.set_blocking(r, False)
return (r, w)
def import_script(self, name, fd, filename=None, check_override=None):
override_text = ''
if check_override is not None:
override_text = f'''
import _imp
_imp._override_multi_interp_extensions_check({check_override})
'''
if filename:
# Apple extensions must be distributed as frameworks. This requires
# a specialist loader.
if is_apple_mobile:
loader = "AppleFrameworkLoader"
else:
loader = "ExtensionFileLoader"
return textwrap.dedent(f'''
from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import {loader}
import os, sys
{override_text}
loader = {loader}({name!r}, {filename!r})
spec = spec_from_loader({name!r}, loader)
try:
module = module_from_spec(spec)
loader.exec_module(module)
except ImportError as exc:
text = 'ImportError: ' + str(exc)
else:
text = 'okay'
os.write({fd}, text.encode('utf-8'))
''')
else:
return textwrap.dedent(f'''
import os, sys
{override_text}
try:
import {name}
except ImportError as exc:
text = 'ImportError: ' + str(exc)
else:
text = 'okay'
os.write({fd}, text.encode('utf-8'))
''')
def run_here(self, name, filename=None, *,
check_singlephase_setting=False,
check_singlephase_override=None,
isolated=False,
):
"""
Try importing the named module in a subinterpreter.
The subinterpreter will be in the current process.
The module will have already been imported in the main interpreter.
Thus, for extension/builtin modules, the module definition will
have been loaded already and cached globally.
"check_singlephase_setting" determines whether or not
the interpreter will be configured to check for modules
that are not compatible with use in multiple interpreters.
This should always return "okay" for all modules if the
setting is False (with no override).
"""
__import__(name)
kwargs = dict(
**self.RUN_KWARGS,
**(self.ISOLATED if isolated else self.NOT_ISOLATED),
check_multi_interp_extensions=check_singlephase_setting,
)
r, w = self.pipe()
script = self.import_script(name, w, filename,
check_singlephase_override)
ret = run_in_subinterp_with_config(script, **kwargs)
self.assertEqual(ret, 0)
return os.read(r, 100)
def check_compatible_here(self, name, filename=None, *,
strict=False,
isolated=False,
):
# Verify that the named module may be imported in a subinterpreter.
# (See run_here() for more info.)
out = self.run_here(name, filename,
check_singlephase_setting=strict,
isolated=isolated,
)
self.assertEqual(out, b'okay')
def check_incompatible_here(self, name, filename=None, *, isolated=False):
# Differences from check_compatible_here():
# * verify that import fails
# * "strict" is always True
out = self.run_here(name, filename,
check_singlephase_setting=True,
isolated=isolated,
)
self.assertEqual(
out.decode('utf-8'),
f'ImportError: module {name} does not support loading in subinterpreters',
)
def check_compatible_fresh(self, name, *, strict=False, isolated=False):
# Differences from check_compatible_here():
# * subinterpreter in a new process
# * module has never been imported before in that process
# * this tests importing the module for the first time
kwargs = dict(
**self.RUN_KWARGS,
**(self.ISOLATED if isolated else self.NOT_ISOLATED),
check_multi_interp_extensions=strict,
)
gil = kwargs['gil']
kwargs['gil'] = 'default' if gil == 0 else (
'shared' if gil == 1 else 'own' if gil == 2 else gil)
_, out, err = script_helper.assert_python_ok('-c', textwrap.dedent(f'''
import _testinternalcapi, sys
assert (
{name!r} in sys.builtin_module_names or
{name!r} not in sys.modules
), repr({name!r})
config = type(sys.implementation)(**{kwargs})
ret = _testinternalcapi.run_in_subinterp_with_config(
{self.import_script(name, "sys.stdout.fileno()")!r},
config,
)
assert ret == 0, ret
'''))
self.assertEqual(err, b'')
self.assertEqual(out, b'okay')
def check_incompatible_fresh(self, name, *, isolated=False):
# Differences from check_compatible_fresh():
# * verify that import fails
# * "strict" is always True
kwargs = dict(
**self.RUN_KWARGS,
**(self.ISOLATED if isolated else self.NOT_ISOLATED),
check_multi_interp_extensions=True,
)
gil = kwargs['gil']
kwargs['gil'] = 'default' if gil == 0 else (
'shared' if gil == 1 else 'own' if gil == 2 else gil)
_, out, err = script_helper.assert_python_ok('-c', textwrap.dedent(f'''
import _testinternalcapi, sys
assert {name!r} not in sys.modules, {name!r}
config = type(sys.implementation)(**{kwargs})
ret = _testinternalcapi.run_in_subinterp_with_config(
{self.import_script(name, "sys.stdout.fileno()")!r},
config,
)
assert ret == 0, ret
'''))
self.assertEqual(err, b'')
self.assertEqual(
out.decode('utf-8'),
f'ImportError: module {name} does not support loading in subinterpreters',
)
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
def test_builtin_compat(self):
# For now we avoid using sys or builtins
# since they still don't implement multi-phase init.
module = '_imp'
require_builtin(module)
if not Py_GIL_DISABLED:
with self.subTest(f'{module}: not strict'):
self.check_compatible_here(module, strict=False)
with self.subTest(f'{module}: strict, not fresh'):
self.check_compatible_here(module, strict=True)
@cpython_only
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
def test_frozen_compat(self):
module = '_frozen_importlib'
require_frozen(module, skip=True)
if __import__(module).__spec__.origin != 'frozen':
raise unittest.SkipTest(f'{module} is unexpectedly not frozen')
if not Py_GIL_DISABLED:
with self.subTest(f'{module}: not strict'):
self.check_compatible_here(module, strict=False)
with self.subTest(f'{module}: strict, not fresh'):
self.check_compatible_here(module, strict=True)
@requires_singlephase_init
def test_single_init_extension_compat(self):
module = '_testsinglephase'
require_extension(module)
with self.subTest(f'{module}: not strict'):
self.check_compatible_here(module, strict=False)
with self.subTest(f'{module}: strict, not fresh'):
self.check_incompatible_here(module)
with self.subTest(f'{module}: strict, fresh'):
self.check_incompatible_fresh(module)
with self.subTest(f'{module}: isolated, fresh'):
self.check_incompatible_fresh(module, isolated=True)
@unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
def test_multi_init_extension_compat(self):
module = '_testmultiphase'
require_extension(module)
if not Py_GIL_DISABLED:
with self.subTest(f'{module}: not strict'):
self.check_compatible_here(module, strict=False)
with self.subTest(f'{module}: strict, not fresh'):
self.check_compatible_here(module, strict=True)
with self.subTest(f'{module}: strict, fresh'):
self.check_compatible_fresh(module, strict=True)
@unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
def test_multi_init_extension_non_isolated_compat(self):
modname = '_test_non_isolated'
filename = _testmultiphase.__file__
module = import_extension_from_file(modname, filename)
require_extension(module)
with self.subTest(f'{modname}: isolated'):
self.check_incompatible_here(modname, filename, isolated=True)
with self.subTest(f'{modname}: not isolated'):
self.check_incompatible_here(modname, filename, isolated=False)
if not Py_GIL_DISABLED:
with self.subTest(f'{modname}: not strict'):
self.check_compatible_here(modname, filename, strict=False)
@unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
def test_multi_init_extension_per_interpreter_gil_compat(self):
modname = '_test_shared_gil_only'
filename = _testmultiphase.__file__
module = import_extension_from_file(modname, filename)
require_extension(module)
with self.subTest(f'{modname}: isolated, strict'):
self.check_incompatible_here(modname, filename, isolated=True)
with self.subTest(f'{modname}: not isolated, strict'):
self.check_compatible_here(modname, filename,
strict=True, isolated=False)
if not Py_GIL_DISABLED:
with self.subTest(f'{modname}: not isolated, not strict'):
self.check_compatible_here(modname, filename,
strict=False, isolated=False)
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
def test_python_compat(self):
module = 'threading'
require_pure_python(module)
if not Py_GIL_DISABLED:
with self.subTest(f'{module}: not strict'):
self.check_compatible_here(module, strict=False)
with self.subTest(f'{module}: strict, not fresh'):
self.check_compatible_here(module, strict=True)
with self.subTest(f'{module}: strict, fresh'):
self.check_compatible_fresh(module, strict=True)
@requires_singlephase_init
def test_singlephase_check_with_setting_and_override(self):
module = '_testsinglephase'
require_extension(module)
def check_compatible(setting, override):
out = self.run_here(
module,
check_singlephase_setting=setting,
check_singlephase_override=override,
)
self.assertEqual(out, b'okay')
def check_incompatible(setting, override):
out = self.run_here(
module,
check_singlephase_setting=setting,
check_singlephase_override=override,
)
self.assertNotEqual(out, b'okay')
with self.subTest('config: check enabled; override: enabled'):
check_incompatible(True, 1)
with self.subTest('config: check enabled; override: use config'):
check_incompatible(True, 0)
with self.subTest('config: check enabled; override: disabled'):
check_compatible(True, -1)
with self.subTest('config: check disabled; override: enabled'):
check_incompatible(False, 1)
with self.subTest('config: check disabled; override: use config'):
check_compatible(False, 0)
with self.subTest('config: check disabled; override: disabled'):
check_compatible(False, -1)
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
def test_isolated_config(self):
module = 'threading'
require_pure_python(module)
with self.subTest(f'{module}: strict, not fresh'):
self.check_compatible_here(module, strict=True, isolated=True)
with self.subTest(f'{module}: strict, fresh'):
self.check_compatible_fresh(module, strict=True, isolated=True)
@requires_subinterpreters
@requires_singlephase_init
def test_disallowed_reimport(self):
# See https://github.com/python/cpython/issues/104621.
script = textwrap.dedent('''
import _testsinglephase
print(_testsinglephase)
''')
interpid = _interpreters.create()
self.addCleanup(lambda: _interpreters.destroy(interpid))
excsnap = _interpreters.run_string(interpid, script)
self.assertIsNot(excsnap, None)
excsnap = _interpreters.run_string(interpid, script)
self.assertIsNot(excsnap, None)
class TestSinglePhaseSnapshot(ModuleSnapshot):
"""A representation of a single-phase init module for testing.
Fields from ModuleSnapshot:
* id - id(mod)
* module - mod or a SimpleNamespace with __file__ & __spec__
* ns - a shallow copy of mod.__dict__
* ns_id - id(mod.__dict__)
* cached - sys.modules[name] (or None if not there or not snapshotable)
* cached_id - id(sys.modules[name]) (or None if not there)
Extra fields:
* summed - the result of calling "mod.sum(1, 2)"
* lookedup - the result of calling "mod.look_up_self()"
* lookedup_id - the object ID of self.lookedup
* state_initialized - the result of calling "mod.state_initialized()"
* init_count - (optional) the result of calling "mod.initialized_count()"
Overridden methods from ModuleSnapshot:
* from_module()
* parse()
Other methods from ModuleSnapshot:
* build_script()
* from_subinterp()
----
There are 5 modules in Modules/_testsinglephase.c:
* _testsinglephase
* has global state
* extra loads skip the init function, copy def.m_base.m_copy
* counts calls to init function
* _testsinglephase_basic_wrapper
* _testsinglephase by another name (and separate init function symbol)
* _testsinglephase_basic_copy
* same as _testsinglephase but with own def (and init func)
* _testsinglephase_with_reinit
* has no global or module state
* mod.state_initialized returns None
* an extra load in the main interpreter calls the cached init func
* an extra load in legacy subinterpreters does a full load
* _testsinglephase_with_state
* has module state
* an extra load in the main interpreter calls the cached init func
* an extra load in legacy subinterpreters does a full load
(See Modules/_testsinglephase.c for more info.)
For all those modules, the snapshot after the initial load (not in
the global extensions cache) would look like the following:
* initial load
* id: ID of nww module object
* ns: exactly what the module init put there
* ns_id: ID of new module's __dict__
* cached_id: same as self.id
* summed: 3 (never changes)
* lookedup_id: same as self.id
* state_initialized: a timestamp between the time of the load
and the time of the snapshot
* init_count: 1 (None for _testsinglephase_with_reinit)
For the other scenarios it varies.
For the _testsinglephase, _testsinglephase_basic_wrapper, and
_testsinglephase_basic_copy modules, the snapshot should look
like the following:
* reloaded
* id: no change
* ns: matches what the module init function put there,
including the IDs of all contained objects,
plus any extra attributes added before the reload
* ns_id: no change
* cached_id: no change
* lookedup_id: no change
* state_initialized: no change
* init_count: no change
* already loaded
* (same as initial load except for ns and state_initialized)
* ns: matches the initial load, incl. IDs of contained objects
* state_initialized: no change from initial load
For _testsinglephase_with_reinit:
* reloaded: same as initial load (old module & ns is discarded)
* already loaded: same as initial load (old module & ns is discarded)
For _testsinglephase_with_state:
* reloaded
* (same as initial load (old module & ns is discarded),
except init_count)
* init_count: increase by 1
* already loaded: same as reloaded
"""
@classmethod
def from_module(cls, mod):
self = super().from_module(mod)
self.summed = mod.sum(1, 2)
self.lookedup = mod.look_up_self()
self.lookedup_id = id(self.lookedup)
self.state_initialized = mod.state_initialized()
if hasattr(mod, 'initialized_count'):
self.init_count = mod.initialized_count()
return self
SCRIPT_BODY = ModuleSnapshot.SCRIPT_BODY + textwrap.dedent('''
snapshot['module'].update(dict(
int_const=mod.int_const,
str_const=mod.str_const,
_module_initialized=mod._module_initialized,
))
snapshot.update(dict(
summed=mod.sum(1, 2),
lookedup_id=id(mod.look_up_self()),
state_initialized=mod.state_initialized(),
init_count=mod.initialized_count(),
has_spam=hasattr(mod, 'spam'),
spam=getattr(mod, 'spam', None),
))
''').rstrip()
@classmethod
def parse(cls, text):
self = super().parse(text)
if not self.has_spam:
del self.spam
del self.has_spam
return self
@requires_singlephase_init
class SinglephaseInitTests(unittest.TestCase):
NAME = '_testsinglephase'
@classmethod
def setUpClass(cls):
spec = importlib.util.find_spec(cls.NAME)
cls.LOADER = type(spec.loader)
# Apple extensions must be distributed as frameworks. This requires
# a specialist loader, and we need to differentiate between the
# spec.origin and the original file location.
if is_apple_mobile:
assert cls.LOADER is AppleFrameworkLoader
cls.ORIGIN = spec.origin
with open(spec.origin + ".origin", "r") as f:
cls.FILE = os.path.join(
os.path.dirname(sys.executable),
f.read().strip()
)
else:
assert cls.LOADER is ExtensionFileLoader
cls.ORIGIN = spec.origin
cls.FILE = spec.origin
# Start fresh.
cls.clean_up()
def tearDown(self):
# Clean up the module.
self.clean_up()
@classmethod
def clean_up(cls):
name = cls.NAME
if name in sys.modules:
if hasattr(sys.modules[name], '_clear_globals'):
assert sys.modules[name].__file__ == cls.FILE, \
f"{sys.modules[name].__file__} != {cls.FILE}"
sys.modules[name]._clear_globals()
del sys.modules[name]
# Clear all internally cached data for the extension.
_testinternalcapi.clear_extension(name, cls.ORIGIN)
#########################
# helpers
def add_module_cleanup(self, name):
def clean_up():
# Clear all internally cached data for the extension.
_testinternalcapi.clear_extension(name, self.ORIGIN)
self.addCleanup(clean_up)
def _load_dynamic(self, name, path):
"""
Load an extension module.
"""
# This is essentially copied from the old imp module.
from importlib._bootstrap import _load
loader = self.LOADER(name, path)
# Issue bpo-24748: Skip the sys.modules check in _load_module_shim;
# always load new extension.
spec = importlib.util.spec_from_file_location(name, path,
loader=loader)
return _load(spec)
def load(self, name):
try:
already_loaded = self.already_loaded
except AttributeError:
already_loaded = self.already_loaded = {}
assert name not in already_loaded
mod = self._load_dynamic(name, self.ORIGIN)
self.assertNotIn(mod, already_loaded.values())
already_loaded[name] = mod
return types.SimpleNamespace(
name=name,
module=mod,
snapshot=TestSinglePhaseSnapshot.from_module(mod),
)
def re_load(self, name, mod):
assert sys.modules[name] is mod
assert mod.__dict__ == mod.__dict__
reloaded = self._load_dynamic(name, self.ORIGIN)
return types.SimpleNamespace(
name=name,
module=reloaded,
snapshot=TestSinglePhaseSnapshot.from_module(reloaded),
)
# subinterpreters
def add_subinterpreter(self):
interpid = _interpreters.create('legacy')
def ensure_destroyed():
try:
_interpreters.destroy(interpid)
except _interpreters.InterpreterNotFoundError:
pass
self.addCleanup(ensure_destroyed)
_interpreters.exec(interpid, textwrap.dedent('''
import sys
import _testinternalcapi
'''))
def clean_up():
_interpreters.exec(interpid, textwrap.dedent(f'''
name = {self.NAME!r}
if name in sys.modules:
sys.modules.pop(name)._clear_globals()
_testinternalcapi.clear_extension(name, {self.ORIGIN!r})
'''))
_interpreters.destroy(interpid)
self.addCleanup(clean_up)
return interpid
def import_in_subinterp(self, interpid=None, *,
postscript=None,
postcleanup=False,
):
name = self.NAME
if postcleanup:
import_ = 'import _testinternalcapi' if interpid is None else ''
postcleanup = f'''
{import_}
mod._clear_globals()
_testinternalcapi.clear_extension(name, {self.ORIGIN!r})
'''
try:
pipe = self._pipe
except AttributeError:
r, w = pipe = self._pipe = os.pipe()
self.addCleanup(os.close, r)
self.addCleanup(os.close, w)
snapshot = TestSinglePhaseSnapshot.from_subinterp(
name,
interpid,
pipe=pipe,
import_first=True,
postscript=postscript,
postcleanup=postcleanup,
)
return types.SimpleNamespace(
name=name,
module=None,
snapshot=snapshot,
)
# checks
def check_common(self, loaded):
isolated = False
mod = loaded.module
if not mod:
# It came from a subinterpreter.
isolated = True
mod = loaded.snapshot.module
# mod.__name__ might not match, but the spec will.
self.assertEqual(mod.__spec__.name, loaded.name)
self.assertEqual(mod.__file__, self.FILE)
self.assertEqual(mod.__spec__.origin, self.ORIGIN)
if not isolated:
self.assertTrue(issubclass(mod.error, Exception))
self.assertEqual(mod.int_const, 1969)
self.assertEqual(mod.str_const, 'something different')
self.assertIsInstance(mod._module_initialized, float)
self.assertGreater(mod._module_initialized, 0)
snap = loaded.snapshot
self.assertEqual(snap.summed, 3)
if snap.state_initialized is not None:
self.assertIsInstance(snap.state_initialized, float)
self.assertGreater(snap.state_initialized, 0)
if isolated:
# The "looked up" module is interpreter-specific
# (interp->imports.modules_by_index was set for the module).
self.assertEqual(snap.lookedup_id, snap.id)
self.assertEqual(snap.cached_id, snap.id)
with self.assertRaises(AttributeError):
snap.spam
else:
self.assertIs(snap.lookedup, mod)
self.assertIs(snap.cached, mod)
def check_direct(self, loaded):
# The module has its own PyModuleDef, with a matching name.
self.assertEqual(loaded.module.__name__, loaded.name)
self.assertIs(loaded.snapshot.lookedup, loaded.module)
def check_indirect(self, loaded, orig):
# The module re-uses another's PyModuleDef, with a different name.
assert orig is not loaded.module
assert orig.__name__ != loaded.name
self.assertNotEqual(loaded.module.__name__, loaded.name)
self.assertIs(loaded.snapshot.lookedup, loaded.module)
def check_basic(self, loaded, expected_init_count):
# m_size == -1
# The module loads fresh the first time and copies m_copy after.
snap = loaded.snapshot
self.assertIsNot(snap.state_initialized, None)
self.assertIsInstance(snap.init_count, int)
self.assertGreater(snap.init_count, 0)
self.assertEqual(snap.init_count, expected_init_count)
def check_with_reinit(self, loaded):
# m_size >= 0
# The module loads fresh every time.
pass
def check_fresh(self, loaded):
"""
The module had not been loaded before (at least since fully reset).
"""
snap = loaded.snapshot
# The module's init func was run.
# A copy of the module's __dict__ was stored in def->m_base.m_copy.
# The previous m_copy was deleted first.
# _PyRuntime.imports.extensions was set.
self.assertEqual(snap.init_count, 1)
# The global state was initialized.
# The module attrs were initialized from that state.
self.assertEqual(snap.module._module_initialized,
snap.state_initialized)
def check_semi_fresh(self, loaded, base, prev):
"""
The module had been loaded before and then reset
(but the module global state wasn't).
"""
snap = loaded.snapshot
# The module's init func was run again.
# A copy of the module's __dict__ was stored in def->m_base.m_copy.
# The previous m_copy was deleted first.
# The module globals did not get reset.
self.assertNotEqual(snap.id, base.snapshot.id)
self.assertNotEqual(snap.id, prev.snapshot.id)
self.assertEqual(snap.init_count, prev.snapshot.init_count + 1)
# The global state was updated.
# The module attrs were initialized from that state.
self.assertEqual(snap.module._module_initialized,
snap.state_initialized)
self.assertNotEqual(snap.state_initialized,
base.snapshot.state_initialized)
self.assertNotEqual(snap.state_initialized,
prev.snapshot.state_initialized)
def check_copied(self, loaded, base):
"""
The module had been loaded before and never reset.
"""
snap = loaded.snapshot
# The module's init func was not run again.
# The interpreter copied m_copy, as set by the other interpreter,
# with objects owned by the other interpreter.
# The module globals did not get reset.
self.assertNotEqual(snap.id, base.snapshot.id)
self.assertEqual(snap.init_count, base.snapshot.init_count)
# The global state was not updated since the init func did not run.
# The module attrs were not directly initialized from that state.
# The state and module attrs still match the previous loading.
self.assertEqual(snap.module._module_initialized,
snap.state_initialized)
self.assertEqual(snap.state_initialized,
base.snapshot.state_initialized)
#########################
# the tests
def test_cleared_globals(self):
loaded = self.load(self.NAME)
_testsinglephase = loaded.module
init_before = _testsinglephase.state_initialized()
_testsinglephase._clear_globals()
init_after = _testsinglephase.state_initialized()
init_count = _testsinglephase.initialized_count()
self.assertGreater(init_before, 0)
self.assertEqual(init_after, 0)
self.assertEqual(init_count, -1)
def test_variants(self):
# Exercise the most meaningful variants described in Python/import.c.
self.maxDiff = None
# Check the "basic" module.
name = self.NAME
expected_init_count = 1
with self.subTest(name):
loaded = self.load(name)
self.check_common(loaded)
self.check_direct(loaded)
self.check_basic(loaded, expected_init_count)
basic = loaded.module
# Check its indirect variants.
name = f'{self.NAME}_basic_wrapper'
self.add_module_cleanup(name)
expected_init_count += 1
with self.subTest(name):
loaded = self.load(name)
self.check_common(loaded)
self.check_indirect(loaded, basic)
self.check_basic(loaded, expected_init_count)
# Currently PyState_AddModule() always replaces the cached module.
self.assertIs(basic.look_up_self(), loaded.module)
self.assertEqual(basic.initialized_count(), expected_init_count)
# The cached module shouldn't change after this point.
basic_lookedup = loaded.module
# Check its direct variant.
name = f'{self.NAME}_basic_copy'
self.add_module_cleanup(name)
expected_init_count += 1
with self.subTest(name):
loaded = self.load(name)
self.check_common(loaded)
self.check_direct(loaded)
self.check_basic(loaded, expected_init_count)
# This should change the cached module for _testsinglephase.
self.assertIs(basic.look_up_self(), basic_lookedup)
self.assertEqual(basic.initialized_count(), expected_init_count)
# Check the non-basic variant that has no state.
name = f'{self.NAME}_with_reinit'
self.add_module_cleanup(name)
with self.subTest(name):
loaded = self.load(name)
self.check_common(loaded)
self.assertIs(loaded.snapshot.state_initialized, None)
self.check_direct(loaded)
self.check_with_reinit(loaded)
# This should change the cached module for _testsinglephase.
self.assertIs(basic.look_up_self(), basic_lookedup)
self.assertEqual(basic.initialized_count(), expected_init_count)
# Check the basic variant that has state.
name = f'{self.NAME}_with_state'
self.add_module_cleanup(name)
with self.subTest(name):
loaded = self.load(name)
self.addCleanup(loaded.module._clear_module_state)
self.check_common(loaded)
self.assertIsNot(loaded.snapshot.state_initialized, None)
self.check_direct(loaded)
self.check_with_reinit(loaded)
# This should change the cached module for _testsinglephase.
self.assertIs(basic.look_up_self(), basic_lookedup)
self.assertEqual(basic.initialized_count(), expected_init_count)
def test_basic_reloaded(self):
# m_copy is copied into the existing module object.
# Global state is not changed.
self.maxDiff = None
for name in [
self.NAME, # the "basic" module
f'{self.NAME}_basic_wrapper', # the indirect variant
f'{self.NAME}_basic_copy', # the direct variant
]:
self.add_module_cleanup(name)
with self.subTest(name):
loaded = self.load(name)
reloaded = self.re_load(name, loaded.module)
self.check_common(loaded)
self.check_common(reloaded)
# Make sure the original __dict__ did not get replaced.
self.assertEqual(id(loaded.module.__dict__),
loaded.snapshot.ns_id)
self.assertEqual(loaded.snapshot.ns.__dict__,
loaded.module.__dict__)
self.assertEqual(reloaded.module.__spec__.name, reloaded.name)
self.assertEqual(reloaded.module.__name__,
reloaded.snapshot.ns.__name__)
self.assertIs(reloaded.module, loaded.module)
self.assertIs(reloaded.module.__dict__, loaded.module.__dict__)
# It only happens to be the same but that's good enough here.
# We really just want to verify that the re-loaded attrs
# didn't change.
self.assertIs(reloaded.snapshot.lookedup,
loaded.snapshot.lookedup)
self.assertEqual(reloaded.snapshot.state_initialized,
loaded.snapshot.state_initialized)
self.assertEqual(reloaded.snapshot.init_count,
loaded.snapshot.init_count)
self.assertIs(reloaded.snapshot.cached, reloaded.module)
def test_with_reinit_reloaded(self):
# The module's m_init func is run again.
self.maxDiff = None
# Keep a reference around.
basic = self.load(self.NAME)
for name, has_state in [
(f'{self.NAME}_with_reinit', False), # m_size == 0
(f'{self.NAME}_with_state', True), # m_size > 0
]:
self.add_module_cleanup(name)
with self.subTest(name=name, has_state=has_state):
loaded = self.load(name)
if has_state:
self.addCleanup(loaded.module._clear_module_state)
reloaded = self.re_load(name, loaded.module)
if has_state:
self.addCleanup(reloaded.module._clear_module_state)
self.check_common(loaded)
self.check_common(reloaded)
# Make sure the original __dict__ did not get replaced.
self.assertEqual(id(loaded.module.__dict__),
loaded.snapshot.ns_id)
self.assertEqual(loaded.snapshot.ns.__dict__,
loaded.module.__dict__)
self.assertEqual(reloaded.module.__spec__.name, reloaded.name)
self.assertEqual(reloaded.module.__name__,
reloaded.snapshot.ns.__name__)
self.assertIsNot(reloaded.module, loaded.module)
self.assertNotEqual(reloaded.module.__dict__,
loaded.module.__dict__)
self.assertIs(reloaded.snapshot.lookedup, reloaded.module)
if loaded.snapshot.state_initialized is None:
self.assertIs(reloaded.snapshot.state_initialized, None)
else:
self.assertGreater(reloaded.snapshot.state_initialized,
loaded.snapshot.state_initialized)
self.assertIs(reloaded.snapshot.cached, reloaded.module)
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
def test_check_state_first(self):
for variant in ['', '_with_reinit', '_with_state']:
name = f'{self.NAME}{variant}_check_cache_first'
with self.subTest(name):
mod = self._load_dynamic(name, self.ORIGIN)
self.assertEqual(mod.__name__, name)
sys.modules.pop(name, None)
_testinternalcapi.clear_extension(name, self.ORIGIN)
# Currently, for every single-phrase init module loaded
# in multiple interpreters, those interpreters share a
# PyModuleDef for that object, which can be a problem.
# Also, we test with a single-phase module that has global state,
# which is shared by all interpreters.
@requires_subinterpreters
def test_basic_multiple_interpreters_main_no_reset(self):
# without resetting; already loaded in main interpreter
# At this point:
# * alive in 0 interpreters
# * module def may or may not be loaded already
# * module def not in _PyRuntime.imports.extensions
# * mod init func has not run yet (since reset, at least)
# * m_copy not set (hasn't been loaded yet or already cleared)
# * module's global state has not been initialized yet
# (or already cleared)
main_loaded = self.load(self.NAME)
_testsinglephase = main_loaded.module
# Attrs set after loading are not in m_copy.
_testsinglephase.spam = 'spam, spam, spam, spam, eggs, and spam'
self.check_common(main_loaded)
self.check_fresh(main_loaded)
interpid1 = self.add_subinterpreter()
interpid2 = self.add_subinterpreter()
# At this point:
# * alive in 1 interpreter (main)
# * module def in _PyRuntime.imports.extensions
# * mod init func ran for the first time (since reset, at least)
# * m_copy was copied from the main interpreter (was NULL)
# * module's global state was initialized
# Use an interpreter that gets destroyed right away.
loaded = self.import_in_subinterp()
self.check_common(loaded)
self.check_copied(loaded, main_loaded)
# At this point:
# * alive in 1 interpreter (main)
# * module def still in _PyRuntime.imports.extensions
# * mod init func ran again
# * m_copy is NULL (cleared when the interpreter was destroyed)
# (was from main interpreter)
# * module's global state was updated, not reset
# Use a subinterpreter that sticks around.
loaded = self.import_in_subinterp(interpid1)
self.check_common(loaded)
self.check_copied(loaded, main_loaded)
# At this point:
# * alive in 2 interpreters (main, interp1)
# * module def still in _PyRuntime.imports.extensions
# * mod init func ran again
# * m_copy was copied from interp1
# * module's global state was updated, not reset
# Use a subinterpreter while the previous one is still alive.
loaded = self.import_in_subinterp(interpid2)
self.check_common(loaded)
self.check_copied(loaded, main_loaded)
# At this point:
# * alive in 3 interpreters (main, interp1, interp2)
# * module def still in _PyRuntime.imports.extensions
# * mod init func ran again
# * m_copy was copied from interp2 (was from interp1)
# * module's global state was updated, not reset
@no_rerun(reason="rerun not possible; module state is never cleared (see gh-102251)")
@requires_subinterpreters
def test_basic_multiple_interpreters_deleted_no_reset(self):
# without resetting; already loaded in a deleted interpreter
if Py_TRACE_REFS:
# It's a Py_TRACE_REFS build.
# This test breaks interpreter isolation a little,
# which causes problems on Py_TRACE_REF builds.
raise unittest.SkipTest('crashes on Py_TRACE_REFS builds')
# At this point:
# * alive in 0 interpreters
# * module def may or may not be loaded already
# * module def not in _PyRuntime.imports.extensions
# * mod init func has not run yet (since reset, at least)
# * m_copy not set (hasn't been loaded yet or already cleared)
# * module's global state has not been initialized yet
# (or already cleared)
interpid1 = self.add_subinterpreter()
interpid2 = self.add_subinterpreter()
# First, load in the main interpreter but then completely clear it.
loaded_main = self.load(self.NAME)
loaded_main.module._clear_globals()
_testinternalcapi.clear_extension(self.NAME, self.ORIGIN)
# At this point:
# * alive in 0 interpreters
# * module def loaded already
# * module def was in _PyRuntime.imports.extensions, but cleared
# * mod init func ran for the first time (since reset, at least)
# * m_copy was set, but cleared (was NULL)
# * module's global state was initialized but cleared
# Start with an interpreter that gets destroyed right away.
base = self.import_in_subinterp(
postscript='''
# Attrs set after loading are not in m_copy.
mod.spam = 'spam, spam, mash, spam, eggs, and spam'
''')
self.check_common(base)
self.check_fresh(base)
# At this point:
# * alive in 0 interpreters
# * module def in _PyRuntime.imports.extensions
# * mod init func ran for the first time (since reset)
# * m_copy is still set (owned by main interpreter)
# * module's global state was initialized, not reset
# Use a subinterpreter that sticks around.
loaded_interp1 = self.import_in_subinterp(interpid1)
self.check_common(loaded_interp1)
self.check_copied(loaded_interp1, base)
# At this point:
# * alive in 1 interpreter (interp1)
# * module def still in _PyRuntime.imports.extensions
# * mod init func did not run again
# * m_copy was not changed
# * module's global state was not touched
# Use a subinterpreter while the previous one is still alive.
loaded_interp2 = self.import_in_subinterp(interpid2)
self.check_common(loaded_interp2)
self.check_copied(loaded_interp2, loaded_interp1)
# At this point:
# * alive in 2 interpreters (interp1, interp2)
# * module def still in _PyRuntime.imports.extensions
# * mod init func did not run again
# * m_copy was not changed
# * module's global state was not touched
@requires_subinterpreters
def test_basic_multiple_interpreters_reset_each(self):
# resetting between each interpreter
# At this point:
# * alive in 0 interpreters
# * module def may or may not be loaded already
# * module def not in _PyRuntime.imports.extensions
# * mod init func has not run yet (since reset, at least)
# * m_copy not set (hasn't been loaded yet or already cleared)
# * module's global state has not been initialized yet
# (or already cleared)
interpid1 = self.add_subinterpreter()
interpid2 = self.add_subinterpreter()
# Use an interpreter that gets destroyed right away.
loaded = self.import_in_subinterp(
postscript='''
# Attrs set after loading are not in m_copy.
mod.spam = 'spam, spam, mash, spam, eggs, and spam'
''',
postcleanup=True,
)
self.check_common(loaded)
self.check_fresh(loaded)
# At this point:
# * alive in 0 interpreters
# * module def in _PyRuntime.imports.extensions
# * mod init func ran for the first time (since reset, at least)
# * m_copy is NULL (cleared when the interpreter was destroyed)
# * module's global state was initialized, not reset
# Use a subinterpreter that sticks around.
loaded = self.import_in_subinterp(interpid1, postcleanup=True)
self.check_common(loaded)
self.check_fresh(loaded)
# At this point:
# * alive in 1 interpreter (interp1)
# * module def still in _PyRuntime.imports.extensions
# * mod init func ran again
# * m_copy was copied from interp1 (was NULL)
# * module's global state was initialized, not reset
# Use a subinterpreter while the previous one is still alive.
loaded = self.import_in_subinterp(interpid2, postcleanup=True)
self.check_common(loaded)
self.check_fresh(loaded)
# At this point:
# * alive in 2 interpreters (interp2, interp2)
# * module def still in _PyRuntime.imports.extensions
# * mod init func ran again
# * m_copy was copied from interp2 (was from interp1)
# * module's global state was initialized, not reset
@cpython_only
class CAPITests(unittest.TestCase):
def test_pyimport_addmodule(self):
# gh-105922: Test PyImport_AddModuleRef(), PyImport_AddModule()
# and PyImport_AddModuleObject()
_testcapi = import_module("_testcapi")
for name in (
'sys', # frozen module
'test', # package
__name__, # package.module
):
_testcapi.check_pyimport_addmodule(name)
def test_pyimport_addmodule_create(self):
# gh-105922: Test PyImport_AddModuleRef(), create a new module
_testcapi = import_module("_testcapi")
name = 'dontexist'
self.assertNotIn(name, sys.modules)
self.addCleanup(unload, name)
mod = _testcapi.check_pyimport_addmodule(name)
self.assertIs(mod, sys.modules[name])
@cpython_only
class TestMagicNumber(unittest.TestCase):
def test_magic_number_endianness(self):
magic_number_bytes = _imp.pyc_magic_number_token.to_bytes(4, 'little')
self.assertEqual(magic_number_bytes[2:], b'\r\n')
# Starting with Python 3.11, Python 3.n starts with magic number 2900+50n.
magic_number = int.from_bytes(magic_number_bytes[:2], 'little')
start = 2900 + sys.version_info.minor * 50
self.assertIn(magic_number, range(start, start + 50))
if __name__ == '__main__':
# Test needs to be a package, so we can do relative imports.
unittest.main()
|