aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_warnings/__init__.py
blob: 5c3b1250ceb0456ab5cf3a52c051dfc212c248ef (plain) (blame)
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
from contextlib import contextmanager
import linecache
import os
import importlib
import inspect
from io import StringIO
import re
import sys
import textwrap
import types
from typing import overload, get_overloads
import unittest
from test import support
from test.support import import_helper
from test.support import os_helper
from test.support import warnings_helper
from test.support import force_not_colorized
from test.support.script_helper import assert_python_ok, assert_python_failure

from test.test_warnings.data import package_helper
from test.test_warnings.data import stacklevel as warning_tests

import warnings as original_warnings
from warnings import deprecated


py_warnings = import_helper.import_fresh_module('_py_warnings')
py_warnings._set_module(py_warnings)

c_warnings = import_helper.import_fresh_module(
    "warnings", fresh=["_warnings", "_py_warnings"]
)
c_warnings._set_module(c_warnings)

@contextmanager
def warnings_state(module):
    """Use a specific warnings implementation in warning_tests."""
    global __warningregistry__
    for to_clear in (sys, warning_tests):
        try:
            to_clear.__warningregistry__.clear()
        except AttributeError:
            pass
    try:
        __warningregistry__.clear()
    except NameError:
        pass
    original_warnings = warning_tests.warnings
    if module._use_context:
        saved_context, context = module._new_context()
    else:
        original_filters = module.filters
        module.filters = original_filters[:]
    try:
        module.simplefilter("once")
        warning_tests.warnings = module
        yield
    finally:
        warning_tests.warnings = original_warnings
        if module._use_context:
            module._set_context(saved_context)
        else:
            module.filters = original_filters


class TestWarning(Warning):
    pass


class BaseTest:

    """Basic bookkeeping required for testing."""

    def setUp(self):
        self.old_unittest_module = unittest.case.warnings
        # The __warningregistry__ needs to be in a pristine state for tests
        # to work properly.
        if '__warningregistry__' in globals():
            del globals()['__warningregistry__']
        if hasattr(warning_tests, '__warningregistry__'):
            del warning_tests.__warningregistry__
        if hasattr(sys, '__warningregistry__'):
            del sys.__warningregistry__
        # The 'warnings' module must be explicitly set so that the proper
        # interaction between _warnings and 'warnings' can be controlled.
        sys.modules['warnings'] = self.module
        # Ensure that unittest.TestCase.assertWarns() uses the same warnings
        # module than warnings.catch_warnings(). Otherwise,
        # warnings.catch_warnings() will be unable to remove the added filter.
        unittest.case.warnings = self.module
        super(BaseTest, self).setUp()

    def tearDown(self):
        sys.modules['warnings'] = original_warnings
        unittest.case.warnings = self.old_unittest_module
        super(BaseTest, self).tearDown()

class PublicAPITests(BaseTest):

    """Ensures that the correct values are exposed in the
    public API.
    """

    def test_module_all_attribute(self):
        self.assertHasAttr(self.module, '__all__')
        target_api = ["warn", "warn_explicit", "showwarning",
                      "formatwarning", "filterwarnings", "simplefilter",
                      "resetwarnings", "catch_warnings", "deprecated"]
        self.assertSetEqual(set(self.module.__all__),
                            set(target_api))

class CPublicAPITests(PublicAPITests, unittest.TestCase):
    module = c_warnings

class PyPublicAPITests(PublicAPITests, unittest.TestCase):
    module = py_warnings

class FilterTests(BaseTest):

    """Testing the filtering functionality."""

    def test_error(self):
        with self.module.catch_warnings() as w:
            self.module.resetwarnings()
            self.module.filterwarnings("error", category=UserWarning)
            self.assertRaises(UserWarning, self.module.warn,
                                "FilterTests.test_error")

    def test_error_after_default(self):
        with self.module.catch_warnings() as w:
            self.module.resetwarnings()
            message = "FilterTests.test_ignore_after_default"
            def f():
                self.module.warn(message, UserWarning)

            with support.captured_stderr() as stderr:
                f()
            stderr = stderr.getvalue()
            self.assertIn("UserWarning: FilterTests.test_ignore_after_default",
                          stderr)
            self.assertIn("self.module.warn(message, UserWarning)",
                          stderr)

            self.module.filterwarnings("error", category=UserWarning)
            self.assertRaises(UserWarning, f)

    def test_ignore(self):
        with self.module.catch_warnings(record=True) as w:
            self.module.resetwarnings()
            self.module.filterwarnings("ignore", category=UserWarning)
            self.module.warn("FilterTests.test_ignore", UserWarning)
            self.assertEqual(len(w), 0)
            self.assertEqual(list(__warningregistry__), ['version'])

    def test_ignore_after_default(self):
        with self.module.catch_warnings(record=True) as w:
            self.module.resetwarnings()
            message = "FilterTests.test_ignore_after_default"
            def f():
                self.module.warn(message, UserWarning)
            f()
            self.module.filterwarnings("ignore", category=UserWarning)
            f()
            f()
            self.assertEqual(len(w), 1)

    def test_always_and_all(self):
        for mode in {"always", "all"}:
            with self.module.catch_warnings(record=True) as w:
                self.module.resetwarnings()
                self.module.filterwarnings(mode, category=UserWarning)
                message = "FilterTests.test_always_and_all"
                def f():
                    self.module.warn(message, UserWarning)
                f()
                self.assertEqual(len(w), 1)
                self.assertEqual(w[-1].message.args[0], message)
                f()
                self.assertEqual(len(w), 2)
                self.assertEqual(w[-1].message.args[0], message)

    def test_always_and_all_after_default(self):
        for mode in {"always", "all"}:
            with self.module.catch_warnings(record=True) as w:
                self.module.resetwarnings()
                message = "FilterTests.test_always_and_all_after_ignore"
                def f():
                    self.module.warn(message, UserWarning)
                f()
                self.assertEqual(len(w), 1)
                self.assertEqual(w[-1].message.args[0], message)
                f()
                self.assertEqual(len(w), 1)
                self.module.filterwarnings(mode, category=UserWarning)
                f()
                self.assertEqual(len(w), 2)
                self.assertEqual(w[-1].message.args[0], message)
                f()
                self.assertEqual(len(w), 3)
                self.assertEqual(w[-1].message.args[0], message)

    def test_default(self):
        with self.module.catch_warnings(record=True) as w:
            self.module.resetwarnings()
            self.module.filterwarnings("default", category=UserWarning)
            message = UserWarning("FilterTests.test_default")
            for x in range(2):
                self.module.warn(message, UserWarning)
                if x == 0:
                    self.assertEqual(w[-1].message, message)
                    del w[:]
                elif x == 1:
                    self.assertEqual(len(w), 0)
                else:
                    raise ValueError("loop variant unhandled")

    def test_module(self):
        with self.module.catch_warnings(record=True) as w:
            self.module.resetwarnings()
            self.module.filterwarnings("module", category=UserWarning)
            message = UserWarning("FilterTests.test_module")
            self.module.warn(message, UserWarning)
            self.assertEqual(w[-1].message, message)
            del w[:]
            self.module.warn(message, UserWarning)
            self.assertEqual(len(w), 0)

    def test_once(self):
        with self.module.catch_warnings(record=True) as w:
            self.module.resetwarnings()
            self.module.filterwarnings("once", category=UserWarning)
            message = UserWarning("FilterTests.test_once")
            self.module.warn_explicit(message, UserWarning, "__init__.py",
                                    42)
            self.assertEqual(w[-1].message, message)
            del w[:]
            self.module.warn_explicit(message, UserWarning, "__init__.py",
                                    13)
            self.assertEqual(len(w), 0)
            self.module.warn_explicit(message, UserWarning, "test_warnings2.py",
                                    42)
            self.assertEqual(len(w), 0)

    def test_module_globals(self):
        with self.module.catch_warnings(record=True) as w:
            self.module.simplefilter("always", UserWarning)

            # bpo-33509: module_globals=None must not crash
            self.module.warn_explicit('msg', UserWarning, "filename", 42,
                                      module_globals=None)
            self.assertEqual(len(w), 1)

            # Invalid module_globals type
            with self.assertRaises(TypeError):
                self.module.warn_explicit('msg', UserWarning, "filename", 42,
                                          module_globals=True)
            self.assertEqual(len(w), 1)

            # Empty module_globals
            self.module.warn_explicit('msg', UserWarning, "filename", 42,
                                      module_globals={})
            self.assertEqual(len(w), 2)

    def test_inheritance(self):
        with self.module.catch_warnings() as w:
            self.module.resetwarnings()
            self.module.filterwarnings("error", category=Warning)
            self.assertRaises(UserWarning, self.module.warn,
                                "FilterTests.test_inheritance", UserWarning)

    def test_ordering(self):
        with self.module.catch_warnings(record=True) as w:
            self.module.resetwarnings()
            self.module.filterwarnings("ignore", category=UserWarning)
            self.module.filterwarnings("error", category=UserWarning,
                                        append=True)
            del w[:]
            try:
                self.module.warn("FilterTests.test_ordering", UserWarning)
            except UserWarning:
                self.fail("order handling for actions failed")
            self.assertEqual(len(w), 0)

    def test_filterwarnings(self):
        # Test filterwarnings().
        # Implicitly also tests resetwarnings().
        with self.module.catch_warnings(record=True) as w:
            self.module.filterwarnings("error", "", Warning, "", 0)
            self.assertRaises(UserWarning, self.module.warn, 'convert to error')

            self.module.resetwarnings()
            text = 'handle normally'
            self.module.warn(text)
            self.assertEqual(str(w[-1].message), text)
            self.assertIs(w[-1].category, UserWarning)

            self.module.filterwarnings("ignore", "", Warning, "", 0)
            text = 'filtered out'
            self.module.warn(text)
            self.assertNotEqual(str(w[-1].message), text)

            self.module.resetwarnings()
            self.module.filterwarnings("error", "hex*", Warning, "", 0)
            self.assertRaises(UserWarning, self.module.warn, 'hex/oct')
            text = 'nonmatching text'
            self.module.warn(text)
            self.assertEqual(str(w[-1].message), text)
            self.assertIs(w[-1].category, UserWarning)

    def test_message_matching(self):
        with self.module.catch_warnings(record=True) as w:
            self.module.simplefilter("ignore", UserWarning)
            self.module.filterwarnings("error", "match", UserWarning)
            self.assertRaises(UserWarning, self.module.warn, "match")
            self.assertRaises(UserWarning, self.module.warn, "match prefix")
            self.module.warn("suffix match")
            self.assertEqual(w, [])
            self.module.warn("something completely different")
            self.assertEqual(w, [])

    def test_mutate_filter_list(self):
        class X:
            def match(self, a):
                L[:] = []

        L = [("default",X(),UserWarning,X(),0) for i in range(2)]
        with self.module.catch_warnings(record=True) as w:
            self.module.filters = L
            self.module.warn_explicit(UserWarning("b"), None, "f.py", 42)
            self.assertEqual(str(w[-1].message), "b")

    def test_filterwarnings_duplicate_filters(self):
        with self.module.catch_warnings():
            self.module.resetwarnings()
            self.module.filterwarnings("error", category=UserWarning)
            self.assertEqual(len(self.module._get_filters()), 1)
            self.module.filterwarnings("ignore", category=UserWarning)
            self.module.filterwarnings("error", category=UserWarning)
            self.assertEqual(
                len(self.module._get_filters()), 2,
                "filterwarnings inserted duplicate filter"
            )
            self.assertEqual(
                self.module._get_filters()[0][0], "error",
                "filterwarnings did not promote filter to "
                "the beginning of list"
            )

    def test_simplefilter_duplicate_filters(self):
        with self.module.catch_warnings():
            self.module.resetwarnings()
            self.module.simplefilter("error", category=UserWarning)
            self.assertEqual(len(self.module._get_filters()), 1)
            self.module.simplefilter("ignore", category=UserWarning)
            self.module.simplefilter("error", category=UserWarning)
            self.assertEqual(
                len(self.module._get_filters()), 2,
                "simplefilter inserted duplicate filter"
            )
            self.assertEqual(
                self.module._get_filters()[0][0], "error",
                "simplefilter did not promote filter to the beginning of list"
            )

    def test_append_duplicate(self):
        with self.module.catch_warnings(record=True) as w:
            self.module.resetwarnings()
            self.module.simplefilter("ignore")
            self.module.simplefilter("error", append=True)
            self.module.simplefilter("ignore", append=True)
            self.module.warn("test_append_duplicate", category=UserWarning)
            self.assertEqual(len(self.module._get_filters()), 2,
                "simplefilter inserted duplicate filter"
            )
            self.assertEqual(len(w), 0,
                "appended duplicate changed order of filters"
            )

    def test_argument_validation(self):
        with self.assertRaises(ValueError):
            self.module.filterwarnings(action='foo')
        with self.assertRaises(TypeError):
            self.module.filterwarnings('ignore', message=0)
        with self.assertRaises(TypeError):
            self.module.filterwarnings('ignore', category=0)
        with self.assertRaises(TypeError):
            self.module.filterwarnings('ignore', category=int)
        with self.assertRaises(TypeError):
            self.module.filterwarnings('ignore', module=0)
        with self.assertRaises(TypeError):
            self.module.filterwarnings('ignore', lineno=int)
        with self.assertRaises(ValueError):
            self.module.filterwarnings('ignore', lineno=-1)
        with self.assertRaises(ValueError):
            self.module.simplefilter(action='foo')
        with self.assertRaises(TypeError):
            self.module.simplefilter('ignore', lineno=int)
        with self.assertRaises(ValueError):
            self.module.simplefilter('ignore', lineno=-1)

    def test_catchwarnings_with_simplefilter_ignore(self):
        with self.module.catch_warnings(module=self.module):
            self.module.resetwarnings()
            self.module.simplefilter("error")
            with self.module.catch_warnings(action="ignore"):
                self.module.warn("This will be ignored")

    def test_catchwarnings_with_simplefilter_error(self):
        with self.module.catch_warnings():
            self.module.resetwarnings()
            with self.module.catch_warnings(
                action="error", category=FutureWarning
            ):
                with support.captured_stderr() as stderr:
                    error_msg = "Other types of warnings are not errors"
                    self.module.warn(error_msg)
                    self.assertRaises(FutureWarning,
                                      self.module.warn, FutureWarning("msg"))
                    stderr = stderr.getvalue()
                    self.assertIn(error_msg, stderr)

class CFilterTests(FilterTests, unittest.TestCase):
    module = c_warnings

class PyFilterTests(FilterTests, unittest.TestCase):
    module = py_warnings


class WarnTests(BaseTest):

    """Test warnings.warn() and warnings.warn_explicit()."""

    def test_message(self):
        with self.module.catch_warnings(record=True) as w:
            self.module.simplefilter("once")
            for i in range(4):
                text = 'multi %d' %i  # Different text on each call.
                self.module.warn(text)
                self.assertEqual(str(w[-1].message), text)
                self.assertIs(w[-1].category, UserWarning)

    # Issue 3639
    def test_warn_nonstandard_types(self):
        # warn() should handle non-standard types without issue.
        for ob in (Warning, None, 42):
            with self.module.catch_warnings(record=True) as w:
                self.module.simplefilter("once")
                self.module.warn(ob)
                # Don't directly compare objects since
                # ``Warning() != Warning()``.
                self.assertEqual(str(w[-1].message), str(UserWarning(ob)))

    def test_filename(self):
        with warnings_state(self.module):
            with self.module.catch_warnings(record=True) as w:
                warning_tests.inner("spam1")
                self.assertEqual(os.path.basename(w[-1].filename),
                                    "stacklevel.py")
                warning_tests.outer("spam2")
                self.assertEqual(os.path.basename(w[-1].filename),
                                    "stacklevel.py")

    def test_stacklevel(self):
        # Test stacklevel argument
        # make sure all messages are different, so the warning won't be skipped
        with warnings_state(self.module):
            with self.module.catch_warnings(record=True) as w:
                warning_tests.inner("spam3", stacklevel=1)
                self.assertEqual(os.path.basename(w[-1].filename),
                                    "stacklevel.py")
                warning_tests.outer("spam4", stacklevel=1)
                self.assertEqual(os.path.basename(w[-1].filename),
                                    "stacklevel.py")

                warning_tests.inner("spam5", stacklevel=2)
                self.assertEqual(os.path.basename(w[-1].filename),
                                    "__init__.py")
                warning_tests.outer("spam6", stacklevel=2)
                self.assertEqual(os.path.basename(w[-1].filename),
                                    "stacklevel.py")
                warning_tests.outer("spam6.5", stacklevel=3)
                self.assertEqual(os.path.basename(w[-1].filename),
                                    "__init__.py")

                warning_tests.inner("spam7", stacklevel=9999)
                self.assertEqual(os.path.basename(w[-1].filename),
                                    "<sys>")

    def test_stacklevel_import(self):
        # Issue #24305: With stacklevel=2, module-level warnings should work.
        import_helper.unload('test.test_warnings.data.import_warning')
        with warnings_state(self.module):
            with self.module.catch_warnings(record=True) as w:
                self.module.simplefilter('always')
                import test.test_warnings.data.import_warning  # noqa: F401
                self.assertEqual(len(w), 1)
                self.assertEqual(w[0].filename, __file__)

    def test_skip_file_prefixes(self):
        with warnings_state(self.module):
            with self.module.catch_warnings(record=True) as w:
                self.module.simplefilter('always')

                # Warning never attributed to the data/ package.
                package_helper.inner_api(
                        "inner_api", stacklevel=2,
                        warnings_module=warning_tests.warnings)
                self.assertEqual(w[-1].filename, __file__)
                warning_tests.package("package api", stacklevel=2)
                self.assertEqual(w[-1].filename, __file__)
                self.assertEqual(w[-2].filename, w[-1].filename)
                # Low stacklevels are overridden to 2 behavior.
                warning_tests.package("package api 1", stacklevel=1)
                self.assertEqual(w[-1].filename, __file__)
                warning_tests.package("package api 0", stacklevel=0)
                self.assertEqual(w[-1].filename, __file__)
                warning_tests.package("package api -99", stacklevel=-99)
                self.assertEqual(w[-1].filename, __file__)

                # The stacklevel still goes up out of the package.
                warning_tests.package("prefix02", stacklevel=3)
                self.assertIn("unittest", w[-1].filename)

    def test_skip_file_prefixes_file_path(self):
        # see: gh-126209
        with warnings_state(self.module):
            skipped = warning_tests.__file__
            with self.module.catch_warnings(record=True) as w:
                warning_tests.outer("msg", skip_file_prefixes=(skipped,))

            self.assertEqual(len(w), 1)
            self.assertNotEqual(w[-1].filename, skipped)

    def test_skip_file_prefixes_type_errors(self):
        with warnings_state(self.module):
            warn = warning_tests.warnings.warn
            with self.assertRaises(TypeError):
                warn("msg", skip_file_prefixes=[])
            with self.assertRaises(TypeError):
                warn("msg", skip_file_prefixes=(b"bytes",))
            with self.assertRaises(TypeError):
                warn("msg", skip_file_prefixes="a sequence of strs")

    def test_exec_filename(self):
        filename = "<warnings-test>"
        codeobj = compile(("import warnings\n"
                           "warnings.warn('hello', UserWarning)"),
                          filename, "exec")
        with self.module.catch_warnings(record=True) as w:
            self.module.simplefilter("always", category=UserWarning)
            exec(codeobj)
        self.assertEqual(w[0].filename, filename)

    def test_warn_explicit_non_ascii_filename(self):
        with self.module.catch_warnings(record=True) as w:
            self.module.resetwarnings()
            self.module.filterwarnings("always", category=UserWarning)
            filenames = ["nonascii\xe9\u20ac"]
            if not support.is_emscripten:
                # JavaScript does not like surrogates.
                # Invalid UTF-8 leading byte 0x80 encountered when
                # deserializing a UTF-8 string in wasm memory to a JS
                # string!
                filenames.append("surrogate\udc80")
            for filename in filenames:
                try:
                    os.fsencode(filename)
                except UnicodeEncodeError:
                    continue
                self.module.warn_explicit("text", UserWarning, filename, 1)
                self.assertEqual(w[-1].filename, filename)

    def test_warn_explicit_type_errors(self):
        # warn_explicit() should error out gracefully if it is given objects
        # of the wrong types.
        # lineno is expected to be an integer.
        self.assertRaises(TypeError, self.module.warn_explicit,
                            None, UserWarning, None, None)
        # Either 'message' needs to be an instance of Warning or 'category'
        # needs to be a subclass.
        self.assertRaises(TypeError, self.module.warn_explicit,
                            None, None, None, 1)
        # 'registry' must be a dict or None.
        self.assertRaises((TypeError, AttributeError),
                            self.module.warn_explicit,
                            None, Warning, None, 1, registry=42)

    def test_bad_str(self):
        # issue 6415
        # Warnings instance with a bad format string for __str__ should not
        # trigger a bus error.
        class BadStrWarning(Warning):
            """Warning with a bad format string for __str__."""
            def __str__(self):
                return ("A bad formatted string %(err)" %
                        {"err" : "there is no %(err)s"})

        with self.assertRaises(ValueError):
            self.module.warn(BadStrWarning())

    def test_warning_classes(self):
        class MyWarningClass(Warning):
            pass

        class NonWarningSubclass:
            pass

        # passing a non-subclass of Warning should raise a TypeError
        with self.assertRaises(TypeError) as cm:
            self.module.warn('bad warning category', '')
        self.assertIn('category must be a Warning subclass, not ',
                      str(cm.exception))

        with self.assertRaises(TypeError) as cm:
            self.module.warn('bad warning category', NonWarningSubclass)
        self.assertIn('category must be a Warning subclass, not ',
                      str(cm.exception))

        # check that warning instances also raise a TypeError
        with self.assertRaises(TypeError) as cm:
            self.module.warn('bad warning category', MyWarningClass())
        self.assertIn('category must be a Warning subclass, not ',
                      str(cm.exception))

        with self.module.catch_warnings():
            self.module.resetwarnings()
            self.module.filterwarnings('default')
            with self.assertWarns(MyWarningClass) as cm:
                self.module.warn('good warning category', MyWarningClass)
            self.assertEqual('good warning category', str(cm.warning))

            with self.assertWarns(UserWarning) as cm:
                self.module.warn('good warning category', None)
            self.assertEqual('good warning category', str(cm.warning))

            with self.assertWarns(MyWarningClass) as cm:
                self.module.warn('good warning category', MyWarningClass)
            self.assertIsInstance(cm.warning, Warning)

    def check_module_globals(self, module_globals):
        with self.module.catch_warnings(record=True) as w:
            self.module.filterwarnings('default')
            self.module.warn_explicit(
                'eggs', UserWarning, 'bar', 1,
                module_globals=module_globals)
        self.assertEqual(len(w), 1)
        self.assertEqual(w[0].category, UserWarning)
        self.assertEqual(str(w[0].message), 'eggs')

    def check_module_globals_error(self, module_globals, errmsg, errtype=ValueError):
        if self.module is py_warnings:
            self.check_module_globals(module_globals)
            return
        with self.module.catch_warnings(record=True) as w:
            self.module.filterwarnings('always')
            with self.assertRaisesRegex(errtype, re.escape(errmsg)):
                self.module.warn_explicit(
                    'eggs', UserWarning, 'bar', 1,
                    module_globals=module_globals)
        self.assertEqual(len(w), 0)

    def check_module_globals_deprecated(self, module_globals, msg):
        if self.module is py_warnings:
            self.check_module_globals(module_globals)
            return
        with self.module.catch_warnings(record=True) as w:
            self.module.filterwarnings('always')
            self.module.warn_explicit(
                'eggs', UserWarning, 'bar', 1,
                module_globals=module_globals)
        self.assertEqual(len(w), 2)
        self.assertEqual(w[0].category, DeprecationWarning)
        self.assertEqual(str(w[0].message), msg)
        self.assertEqual(w[1].category, UserWarning)
        self.assertEqual(str(w[1].message), 'eggs')

    def test_gh86298_no_loader_and_no_spec(self):
        self.check_module_globals({'__name__': 'bar'})

    def test_gh86298_loader_is_none_and_no_spec(self):
        self.check_module_globals({'__name__': 'bar', '__loader__': None})

    def test_gh86298_no_loader_and_spec_is_none(self):
        self.check_module_globals_error(
            {'__name__': 'bar', '__spec__': None},
            'Module globals is missing a __spec__.loader')

    def test_gh86298_loader_is_none_and_spec_is_none(self):
        self.check_module_globals_error(
            {'__name__': 'bar', '__loader__': None, '__spec__': None},
            'Module globals is missing a __spec__.loader')

    def test_gh86298_loader_is_none_and_spec_loader_is_none(self):
        self.check_module_globals_error(
            {'__name__': 'bar', '__loader__': None,
             '__spec__': types.SimpleNamespace(loader=None)},
            'Module globals is missing a __spec__.loader')

    def test_gh86298_no_spec(self):
        self.check_module_globals_deprecated(
            {'__name__': 'bar', '__loader__': object()},
            'Module globals is missing a __spec__.loader')

    def test_gh86298_spec_is_none(self):
        self.check_module_globals_deprecated(
            {'__name__': 'bar', '__loader__': object(), '__spec__': None},
            'Module globals is missing a __spec__.loader')

    def test_gh86298_no_spec_loader(self):
        self.check_module_globals_deprecated(
            {'__name__': 'bar', '__loader__': object(),
             '__spec__': types.SimpleNamespace()},
            'Module globals is missing a __spec__.loader')

    def test_gh86298_loader_and_spec_loader_disagree(self):
        self.check_module_globals_deprecated(
            {'__name__': 'bar', '__loader__': object(),
             '__spec__': types.SimpleNamespace(loader=object())},
            'Module globals; __loader__ != __spec__.loader')

    def test_gh86298_no_loader_and_no_spec_loader(self):
        self.check_module_globals_error(
            {'__name__': 'bar', '__spec__': types.SimpleNamespace()},
            'Module globals is missing a __spec__.loader', AttributeError)

    def test_gh86298_no_loader_with_spec_loader_okay(self):
        self.check_module_globals(
            {'__name__': 'bar',
             '__spec__': types.SimpleNamespace(loader=object())})

class CWarnTests(WarnTests, unittest.TestCase):
    module = c_warnings

    # As an early adopter, we sanity check the
    # test.import_helper.import_fresh_module utility function
    def test_accelerated(self):
        self.assertIsNot(original_warnings, self.module)
        self.assertNotHasAttr(self.module.warn, '__code__')

class PyWarnTests(WarnTests, unittest.TestCase):
    module = py_warnings

    # As an early adopter, we sanity check the
    # test.import_helper.import_fresh_module utility function
    def test_pure_python(self):
        self.assertIsNot(original_warnings, self.module)
        self.assertHasAttr(self.module.warn, '__code__')


class WCmdLineTests(BaseTest):

    def test_improper_input(self):
        # Uses the private _setoption() function to test the parsing
        # of command-line warning arguments
        with self.module.catch_warnings():
            self.assertRaises(self.module._OptionError,
                              self.module._setoption, '1:2:3:4:5:6')
            self.assertRaises(self.module._OptionError,
                              self.module._setoption, 'bogus::Warning')
            self.assertRaises(self.module._OptionError,
                              self.module._setoption, 'ignore:2::4:-5')
            with self.assertRaises(self.module._OptionError):
                self.module._setoption('ignore::123')
            with self.assertRaises(self.module._OptionError):
                self.module._setoption('ignore::123abc')
            with self.assertRaises(self.module._OptionError):
                self.module._setoption('ignore::===')
            with self.assertRaisesRegex(self.module._OptionError, 'Wärning'):
                self.module._setoption('ignore::Wärning')
            self.module._setoption('error::Warning::0')
            self.assertRaises(UserWarning, self.module.warn, 'convert to error')

    def test_import_from_module(self):
        with self.module.catch_warnings():
            self.module._setoption('ignore::Warning')
            with self.assertRaises(self.module._OptionError):
                self.module._setoption('ignore::TestWarning')
            with self.assertRaises(self.module._OptionError):
                self.module._setoption('ignore::test.test_warnings.bogus')
            self.module._setoption('error::test.test_warnings.TestWarning')
            with self.assertRaises(TestWarning):
                self.module.warn('test warning', TestWarning)


class CWCmdLineTests(WCmdLineTests, unittest.TestCase):
    module = c_warnings


class PyWCmdLineTests(WCmdLineTests, unittest.TestCase):
    module = py_warnings

    def test_improper_option(self):
        # Same as above, but check that the message is printed out when
        # the interpreter is executed. This also checks that options are
        # actually parsed at all.
        rc, out, err = assert_python_ok("-Wxxx", "-c", "pass")
        self.assertIn(b"Invalid -W option ignored: invalid action: 'xxx'", err)

    def test_warnings_bootstrap(self):
        # Check that the warnings module does get loaded when -W<some option>
        # is used (see issue #10372 for an example of silent bootstrap failure).
        rc, out, err = assert_python_ok("-Wi", "-c",
            "import sys; sys.modules['warnings'].warn('foo', RuntimeWarning)")
        # '-Wi' was observed
        self.assertFalse(out.strip())
        self.assertNotIn(b'RuntimeWarning', err)


class _WarningsTests(BaseTest, unittest.TestCase):

    """Tests specific to the _warnings module."""

    module = c_warnings

    def test_filter(self):
        # Everything should function even if 'filters' is not in warnings.
        with self.module.catch_warnings() as w:
            self.module.filterwarnings("error", "", Warning, "", 0)
            self.assertRaises(UserWarning, self.module.warn,
                                'convert to error')
            del self.module.filters
            self.assertRaises(UserWarning, self.module.warn,
                                'convert to error')

    def test_onceregistry(self):
        # Replacing or removing the onceregistry should be okay.
        global __warningregistry__
        message = UserWarning('onceregistry test')
        try:
            original_registry = self.module.onceregistry
            __warningregistry__ = {}
            with self.module.catch_warnings(record=True) as w:
                self.module.resetwarnings()
                self.module.filterwarnings("once", category=UserWarning)
                self.module.warn_explicit(message, UserWarning, "file", 42)
                self.assertEqual(w[-1].message, message)
                del w[:]
                self.module.warn_explicit(message, UserWarning, "file", 42)
                self.assertEqual(len(w), 0)
                # Test the resetting of onceregistry.
                self.module.onceregistry = {}
                __warningregistry__ = {}
                self.module.warn('onceregistry test')
                self.assertEqual(w[-1].message.args, message.args)
                # Removal of onceregistry is okay.
                del w[:]
                del self.module.onceregistry
                __warningregistry__ = {}
                self.module.warn_explicit(message, UserWarning, "file", 42)
                self.assertEqual(len(w), 0)
        finally:
            self.module.onceregistry = original_registry

    def test_default_action(self):
        # Replacing or removing defaultaction should be okay.
        message = UserWarning("defaultaction test")
        original = self.module.defaultaction
        try:
            with self.module.catch_warnings(record=True) as w:
                self.module.resetwarnings()
                registry = {}
                self.module.warn_explicit(message, UserWarning, "<test>", 42,
                                            registry=registry)
                self.assertEqual(w[-1].message, message)
                self.assertEqual(len(w), 1)
                # One actual registry key plus the "version" key
                self.assertEqual(len(registry), 2)
                self.assertIn("version", registry)
                del w[:]
                # Test removal.
                del self.module.defaultaction
                __warningregistry__ = {}
                registry = {}
                self.module.warn_explicit(message, UserWarning, "<test>", 43,
                                            registry=registry)
                self.assertEqual(w[-1].message, message)
                self.assertEqual(len(w), 1)
                self.assertEqual(len(registry), 2)
                del w[:]
                # Test setting.
                self.module.defaultaction = "ignore"
                __warningregistry__ = {}
                registry = {}
                self.module.warn_explicit(message, UserWarning, "<test>", 44,
                                            registry=registry)
                self.assertEqual(len(w), 0)
        finally:
            self.module.defaultaction = original

    def test_showwarning_missing(self):
        # Test that showwarning() missing is okay.
        if self.module._use_context:
            # If _use_context is true, the warnings module does not
            # override/restore showwarning()
            return
        text = 'del showwarning test'
        with self.module.catch_warnings():
            self.module.filterwarnings("always", category=UserWarning)
            del self.module.showwarning
            with support.captured_output('stderr') as stream:
                self.module.warn(text)
                result = stream.getvalue()
        self.assertIn(text, result)

    def test_showwarnmsg_missing(self):
        # Test that _showwarnmsg() missing is okay.
        text = 'del _showwarnmsg test'
        with self.module.catch_warnings():
            self.module.filterwarnings("always", category=UserWarning)

            show = self.module._showwarnmsg
            try:
                del self.module._showwarnmsg
                with support.captured_output('stderr') as stream:
                    self.module.warn(text)
                    result = stream.getvalue()
            finally:
                self.module._showwarnmsg = show
        self.assertIn(text, result)

    def test_showwarning_not_callable(self):
        orig = self.module.showwarning
        try:
            with self.module.catch_warnings():
                self.module.filterwarnings("always", category=UserWarning)
                self.module.showwarning = print
                with support.captured_output('stdout'):
                    self.module.warn('Warning!')
                self.module.showwarning = 23
                self.assertRaises(TypeError, self.module.warn, "Warning!")
        finally:
            self.module.showwarning = orig

    def test_show_warning_output(self):
        # With showwarning() missing, make sure that output is okay.
        orig = self.module.showwarning
        try:
            text = 'test show_warning'
            with self.module.catch_warnings():
                self.module.filterwarnings("always", category=UserWarning)
                del self.module.showwarning
                with support.captured_output('stderr') as stream:
                    warning_tests.inner(text)
                    result = stream.getvalue()
            self.assertEqual(result.count('\n'), 2,
                                 "Too many newlines in %r" % result)
            first_line, second_line = result.split('\n', 1)
            expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py'
            first_line_parts = first_line.rsplit(':', 3)
            path, line, warning_class, message = first_line_parts
            line = int(line)
            self.assertEqual(expected_file, path)
            self.assertEqual(warning_class, ' ' + UserWarning.__name__)
            self.assertEqual(message, ' ' + text)
            expected_line = '  ' + linecache.getline(path, line).strip() + '\n'
            assert expected_line
            self.assertEqual(second_line, expected_line)
        finally:
            self.module.showwarning = orig

    def test_filename_none(self):
        # issue #12467: race condition if a warning is emitted at shutdown
        globals_dict = globals()
        oldfile = globals_dict['__file__']
        try:
            catch = self.module.catch_warnings(record=True)
            with catch as w:
                self.module.filterwarnings("always", category=UserWarning)
                globals_dict['__file__'] = None
                self.module.warn('test', UserWarning)
                self.assertTrue(len(w))
        finally:
            globals_dict['__file__'] = oldfile

    def test_stderr_none(self):
        rc, stdout, stderr = assert_python_ok("-c",
            "import sys; sys.stderr = None; "
            "import warnings; warnings.simplefilter('always'); "
            "warnings.warn('Warning!')")
        self.assertEqual(stdout, b'')
        self.assertNotIn(b'Warning!', stderr)
        self.assertNotIn(b'Error', stderr)

    def test_issue31285(self):
        # warn_explicit() should neither raise a SystemError nor cause an
        # assertion failure, in case the return value of get_source() has a
        # bad splitlines() method.
        get_source_called = []
        def get_module_globals(*, splitlines_ret_val):
            class BadSource(str):
                def splitlines(self):
                    return splitlines_ret_val

            class BadLoader:
                def get_source(self, fullname):
                    get_source_called.append(splitlines_ret_val)
                    return BadSource('spam')

            loader = BadLoader()
            spec = importlib.machinery.ModuleSpec('foobar', loader)
            return {'__loader__': loader,
                    '__spec__': spec,
                    '__name__': 'foobar'}


        wmod = self.module
        with wmod.catch_warnings():
            wmod.filterwarnings('default', category=UserWarning)

            linecache.clearcache()
            with support.captured_stderr() as stderr:
                wmod.warn_explicit(
                    'foo', UserWarning, 'bar', 1,
                    module_globals=get_module_globals(splitlines_ret_val=42))
            self.assertIn('UserWarning: foo', stderr.getvalue())
            self.assertEqual(get_source_called, [42])

            linecache.clearcache()
            with support.swap_attr(wmod, '_showwarnmsg', None):
                del wmod._showwarnmsg
                with support.captured_stderr() as stderr:
                    wmod.warn_explicit(
                        'eggs', UserWarning, 'bar', 1,
                        module_globals=get_module_globals(splitlines_ret_val=[42]))
                self.assertIn('UserWarning: eggs', stderr.getvalue())
            self.assertEqual(get_source_called, [42, [42]])
            linecache.clearcache()

    @support.cpython_only
    def test_issue31411(self):
        # warn_explicit() shouldn't raise a SystemError in case
        # warnings.onceregistry isn't a dictionary.
        wmod = self.module
        with wmod.catch_warnings():
            wmod.filterwarnings('once')
            with support.swap_attr(wmod, 'onceregistry', None):
                with self.assertRaises(TypeError):
                    wmod.warn_explicit('foo', Warning, 'bar', 1, registry=None)

    @support.cpython_only
    def test_issue31416(self):
        # warn_explicit() shouldn't cause an assertion failure in case of a
        # bad warnings.filters or warnings.defaultaction.
        wmod = self.module
        with wmod.catch_warnings():
            wmod._get_filters()[:] = [(None, None, Warning, None, 0)]
            with self.assertRaises(TypeError):
                wmod.warn_explicit('foo', Warning, 'bar', 1)

            wmod._get_filters()[:] = []
            with support.swap_attr(wmod, 'defaultaction', None), \
                 self.assertRaises(TypeError):
                wmod.warn_explicit('foo', Warning, 'bar', 1)

    @support.cpython_only
    def test_issue31566(self):
        # warn() shouldn't cause an assertion failure in case of a bad
        # __name__ global.
        with self.module.catch_warnings():
            self.module.filterwarnings('error', category=UserWarning)
            with support.swap_item(globals(), '__name__', b'foo'), \
                 support.swap_item(globals(), '__file__', None):
                self.assertRaises(UserWarning, self.module.warn, 'bar')


class WarningsDisplayTests(BaseTest):

    """Test the displaying of warnings and the ability to overload functions
    related to displaying warnings."""

    def test_formatwarning(self):
        message = "msg"
        category = Warning
        file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
        line_num = 5
        file_line = linecache.getline(file_name, line_num).strip()
        format = "%s:%s: %s: %s\n  %s\n"
        expect = format % (file_name, line_num, category.__name__, message,
                            file_line)
        self.assertEqual(expect, self.module.formatwarning(message,
                                                category, file_name, line_num))
        # Test the 'line' argument.
        file_line += " for the win!"
        expect = format % (file_name, line_num, category.__name__, message,
                            file_line)
        self.assertEqual(expect, self.module.formatwarning(message,
                                    category, file_name, line_num, file_line))

    def test_showwarning(self):
        file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
        line_num = 3
        expected_file_line = linecache.getline(file_name, line_num).strip()
        message = 'msg'
        category = Warning
        file_object = StringIO()
        expect = self.module.formatwarning(message, category, file_name,
                                            line_num)
        self.module.showwarning(message, category, file_name, line_num,
                                file_object)
        self.assertEqual(file_object.getvalue(), expect)
        # Test 'line' argument.
        expected_file_line += "for the win!"
        expect = self.module.formatwarning(message, category, file_name,
                                            line_num, expected_file_line)
        file_object = StringIO()
        self.module.showwarning(message, category, file_name, line_num,
                                file_object, expected_file_line)
        self.assertEqual(expect, file_object.getvalue())

    def test_formatwarning_override(self):
        # bpo-35178: Test that a custom formatwarning function gets the 'line'
        # argument as a positional argument, and not only as a keyword argument
        def myformatwarning(message, category, filename, lineno, text):
            return f'm={message}:c={category}:f={filename}:l={lineno}:t={text}'

        file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
        line_num = 3
        file_line = linecache.getline(file_name, line_num).strip()
        message = 'msg'
        category = Warning
        file_object = StringIO()
        expected = f'm={message}:c={category}:f={file_name}:l={line_num}' + \
                   f':t={file_line}'
        with support.swap_attr(self.module, 'formatwarning', myformatwarning):
            self.module.showwarning(message, category, file_name, line_num,
                                    file_object, file_line)
            self.assertEqual(file_object.getvalue(), expected)


class CWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
    module = c_warnings

class PyWarningsDisplayTests(WarningsDisplayTests, unittest.TestCase):
    module = py_warnings

    def test_tracemalloc(self):
        self.addCleanup(os_helper.unlink, os_helper.TESTFN)

        with open(os_helper.TESTFN, 'w', encoding="utf-8") as fp:
            fp.write(textwrap.dedent("""
                def func():
                    f = open(__file__, "rb")
                    # Emit ResourceWarning
                    f = None

                func()
            """))

        def run(*args):
            res = assert_python_ok(*args, PYTHONIOENCODING='utf-8')
            stderr = res.err.decode('utf-8', 'replace')
            stderr = '\n'.join(stderr.splitlines())

            # normalize newlines
            stderr = re.sub('<.*>', '<...>', stderr)
            return stderr

        # tracemalloc disabled
        filename = os.path.abspath(os_helper.TESTFN)
        stderr = run('-Wd', os_helper.TESTFN)
        expected = textwrap.dedent(f'''
            {filename}:5: ResourceWarning: unclosed file <...>
              f = None
            ResourceWarning: Enable tracemalloc to get the object allocation traceback
        ''').strip()
        self.assertEqual(stderr, expected)

        # tracemalloc enabled
        stderr = run('-Wd', '-X', 'tracemalloc=2', os_helper.TESTFN)
        expected = textwrap.dedent(f'''
            {filename}:5: ResourceWarning: unclosed file <...>
              f = None
            Object allocated at (most recent call last):
              File "{filename}", lineno 7
                func()
              File "{filename}", lineno 3
                f = open(__file__, "rb")
        ''').strip()
        self.assertEqual(stderr, expected)


class CatchWarningTests(BaseTest):

    """Test catch_warnings()."""

    def test_catch_warnings_restore(self):
        if self.module._use_context:
            return  # test disabled if using context vars
        wmod = self.module
        orig_filters = wmod.filters
        orig_showwarning = wmod.showwarning
        # Ensure both showwarning and filters are restored when recording
        with wmod.catch_warnings(record=True):
            wmod.filters = wmod.showwarning = object()
        self.assertIs(wmod.filters, orig_filters)
        self.assertIs(wmod.showwarning, orig_showwarning)
        # Same test, but with recording disabled
        with wmod.catch_warnings(record=False):
            wmod.filters = wmod.showwarning = object()
        self.assertIs(wmod.filters, orig_filters)
        self.assertIs(wmod.showwarning, orig_showwarning)

    def test_catch_warnings_recording(self):
        wmod = self.module
        # Ensure warnings are recorded when requested
        with wmod.catch_warnings(record=True) as w:
            self.assertEqual(w, [])
            self.assertIs(type(w), list)
            wmod.simplefilter("always")
            wmod.warn("foo")
            self.assertEqual(str(w[-1].message), "foo")
            wmod.warn("bar")
            self.assertEqual(str(w[-1].message), "bar")
            self.assertEqual(str(w[0].message), "foo")
            self.assertEqual(str(w[1].message), "bar")
            del w[:]
            self.assertEqual(w, [])
        # Ensure warnings are not recorded when not requested
        orig_showwarning = wmod.showwarning
        with wmod.catch_warnings(record=False) as w:
            self.assertIsNone(w)
            self.assertIs(wmod.showwarning, orig_showwarning)

    def test_catch_warnings_reentry_guard(self):
        wmod = self.module
        # Ensure catch_warnings is protected against incorrect usage
        x = wmod.catch_warnings(record=True)
        self.assertRaises(RuntimeError, x.__exit__)
        with x:
            self.assertRaises(RuntimeError, x.__enter__)
        # Same test, but with recording disabled
        x = wmod.catch_warnings(record=False)
        self.assertRaises(RuntimeError, x.__exit__)
        with x:
            self.assertRaises(RuntimeError, x.__enter__)

    def test_catch_warnings_defaults(self):
        wmod = self.module
        orig_filters = wmod._get_filters()
        orig_showwarning = wmod.showwarning
        # Ensure default behaviour is not to record warnings
        with wmod.catch_warnings() as w:
            self.assertIsNone(w)
            self.assertIs(wmod.showwarning, orig_showwarning)
            self.assertIsNot(wmod._get_filters(), orig_filters)
        self.assertIs(wmod._get_filters(), orig_filters)
        if wmod is sys.modules['warnings']:
            # Ensure the default module is this one
            with wmod.catch_warnings() as w:
                self.assertIsNone(w)
                self.assertIs(wmod.showwarning, orig_showwarning)
                self.assertIsNot(wmod._get_filters(), orig_filters)
            self.assertIs(wmod._get_filters(), orig_filters)

    def test_record_override_showwarning_before(self):
        # Issue #28835: If warnings.showwarning() was overridden, make sure
        # that catch_warnings(record=True) overrides it again.
        if self.module._use_context:
            # If _use_context is true, the warnings module does not restore
            # showwarning()
            return
        text = "This is a warning"
        wmod = self.module
        my_log = []

        def my_logger(message, category, filename, lineno, file=None, line=None):
            nonlocal my_log
            my_log.append(message)

        # Override warnings.showwarning() before calling catch_warnings()
        with support.swap_attr(wmod, 'showwarning', my_logger):
            with wmod.catch_warnings(record=True) as log:
                self.assertIsNot(wmod.showwarning, my_logger)

                wmod.simplefilter("always")
                wmod.warn(text)

            self.assertIs(wmod.showwarning, my_logger)

        self.assertEqual(len(log), 1, log)
        self.assertEqual(log[0].message.args[0], text)
        self.assertEqual(my_log, [])

    def test_record_override_showwarning_inside(self):
        # Issue #28835: It is possible to override warnings.showwarning()
        # in the catch_warnings(record=True) context manager.
        if self.module._use_context:
            # If _use_context is true, the warnings module does not restore
            # showwarning()
            return
        text = "This is a warning"
        wmod = self.module
        my_log = []

        def my_logger(message, category, filename, lineno, file=None, line=None):
            nonlocal my_log
            my_log.append(message)

        with wmod.catch_warnings(record=True) as log:
            wmod.simplefilter("always")
            wmod.showwarning = my_logger
            wmod.warn(text)

        self.assertEqual(len(my_log), 1, my_log)
        self.assertEqual(my_log[0].args[0], text)
        self.assertEqual(log, [])

    def test_check_warnings(self):
        # Explicit tests for the test.support convenience wrapper
        wmod = self.module
        if wmod is not sys.modules['warnings']:
            self.skipTest('module to test is not loaded warnings module')
        with warnings_helper.check_warnings(quiet=False) as w:
            self.assertEqual(w.warnings, [])
            wmod.simplefilter("always")
            wmod.warn("foo")
            self.assertEqual(str(w.message), "foo")
            wmod.warn("bar")
            self.assertEqual(str(w.message), "bar")
            self.assertEqual(str(w.warnings[0].message), "foo")
            self.assertEqual(str(w.warnings[1].message), "bar")
            w.reset()
            self.assertEqual(w.warnings, [])

        with warnings_helper.check_warnings():
            # defaults to quiet=True without argument
            pass
        with warnings_helper.check_warnings(('foo', UserWarning)):
            wmod.warn("foo")

        with self.assertRaises(AssertionError):
            with warnings_helper.check_warnings(('', RuntimeWarning)):
                # defaults to quiet=False with argument
                pass
        with self.assertRaises(AssertionError):
            with warnings_helper.check_warnings(('foo', RuntimeWarning)):
                wmod.warn("foo")

class CCatchWarningTests(CatchWarningTests, unittest.TestCase):
    module = c_warnings

class PyCatchWarningTests(CatchWarningTests, unittest.TestCase):
    module = py_warnings


class EnvironmentVariableTests(BaseTest):

    def test_single_warning(self):
        rc, stdout, stderr = assert_python_ok("-c",
            "import sys; sys.stdout.write(str(sys.warnoptions))",
            PYTHONWARNINGS="ignore::DeprecationWarning",
            PYTHONDEVMODE="")
        self.assertEqual(stdout, b"['ignore::DeprecationWarning']")

    def test_comma_separated_warnings(self):
        rc, stdout, stderr = assert_python_ok("-c",
            "import sys; sys.stdout.write(str(sys.warnoptions))",
            PYTHONWARNINGS="ignore::DeprecationWarning,ignore::UnicodeWarning",
            PYTHONDEVMODE="")
        self.assertEqual(stdout,
            b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")

    @force_not_colorized
    def test_envvar_and_command_line(self):
        rc, stdout, stderr = assert_python_ok("-Wignore::UnicodeWarning", "-c",
            "import sys; sys.stdout.write(str(sys.warnoptions))",
            PYTHONWARNINGS="ignore::DeprecationWarning",
            PYTHONDEVMODE="")
        self.assertEqual(stdout,
            b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")

    @force_not_colorized
    def test_conflicting_envvar_and_command_line(self):
        rc, stdout, stderr = assert_python_failure("-Werror::DeprecationWarning", "-c",
            "import sys, warnings; sys.stdout.write(str(sys.warnoptions)); "
            "warnings.warn('Message', DeprecationWarning)",
            PYTHONWARNINGS="default::DeprecationWarning",
            PYTHONDEVMODE="")
        self.assertEqual(stdout,
            b"['default::DeprecationWarning', 'error::DeprecationWarning']")
        self.assertEqual(stderr.splitlines(),
            [b"Traceback (most recent call last):",
             b"  File \"<string>\", line 1, in <module>",
             b'    import sys, warnings; sys.stdout.write(str(sys.warnoptions)); warnings.w'
             b"arn('Message', DeprecationWarning)",
             b'                                                                  ~~~~~~~~~~'
             b'~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^',
             b"DeprecationWarning: Message"])

    def test_default_filter_configuration(self):
        pure_python_api = self.module is py_warnings
        if support.Py_DEBUG:
            expected_default_filters = []
        else:
            if pure_python_api:
                main_module_filter = re.compile("__main__")
            else:
                main_module_filter = "__main__"
            expected_default_filters = [
                ('default', None, DeprecationWarning, main_module_filter, 0),
                ('ignore', None, DeprecationWarning, None, 0),
                ('ignore', None, PendingDeprecationWarning, None, 0),
                ('ignore', None, ImportWarning, None, 0),
                ('ignore', None, ResourceWarning, None, 0),
            ]
        expected_output = [str(f).encode() for f in expected_default_filters]

        if pure_python_api:
            # Disable the warnings acceleration module in the subprocess
            code = "import sys; sys.modules.pop('warnings', None); sys.modules['_warnings'] = None; "
        else:
            code = ""
        code += "import warnings; [print(f) for f in warnings._get_filters()]"

        rc, stdout, stderr = assert_python_ok("-c", code, __isolated=True)
        stdout_lines = [line.strip() for line in stdout.splitlines()]
        self.maxDiff = None
        self.assertEqual(stdout_lines, expected_output)


    @unittest.skipUnless(sys.getfilesystemencoding() != 'ascii',
                         'requires non-ascii filesystemencoding')
    def test_nonascii(self):
        PYTHONWARNINGS="ignore:DeprecationWarning" + os_helper.FS_NONASCII
        rc, stdout, stderr = assert_python_ok("-c",
            "import sys; sys.stdout.write(str(sys.warnoptions))",
            PYTHONIOENCODING="utf-8",
            PYTHONWARNINGS=PYTHONWARNINGS,
            PYTHONDEVMODE="")
        self.assertEqual(stdout, str([PYTHONWARNINGS]).encode())

class CEnvironmentVariableTests(EnvironmentVariableTests, unittest.TestCase):
    module = c_warnings

class PyEnvironmentVariableTests(EnvironmentVariableTests, unittest.TestCase):
    module = py_warnings


class LocksTest(unittest.TestCase):
    @support.cpython_only
    @unittest.skipUnless(c_warnings, 'C module is required')
    def test_release_lock_no_lock(self):
        with self.assertRaisesRegex(
            RuntimeError,
            'cannot release un-acquired lock',
        ):
            c_warnings._release_lock()


class _DeprecatedTest(BaseTest, unittest.TestCase):

    """Test _deprecated()."""

    module = original_warnings

    def test_warning(self):
        version = (3, 11, 0, "final", 0)
        test = [(4, 12), (4, 11), (4, 0), (3, 12)]
        for remove in test:
            msg = rf".*test_warnings.*{remove[0]}\.{remove[1]}"
            filter = msg, DeprecationWarning
            with self.subTest(remove=remove):
                with warnings_helper.check_warnings(filter, quiet=False):
                    self.module._deprecated("test_warnings", remove=remove,
                                            _version=version)

        version = (3, 11, 0, "alpha", 0)
        msg = r".*test_warnings.*3\.11"
        with warnings_helper.check_warnings((msg, DeprecationWarning), quiet=False):
            self.module._deprecated("test_warnings", remove=(3, 11),
                                    _version=version)

    def test_RuntimeError(self):
        version = (3, 11, 0, "final", 0)
        test = [(2, 0), (2, 12), (3, 10)]
        for remove in test:
            with self.subTest(remove=remove):
                with self.assertRaises(RuntimeError):
                    self.module._deprecated("test_warnings", remove=remove,
                                            _version=version)
        for level in ["beta", "candidate", "final"]:
            version = (3, 11, 0, level, 0)
            with self.subTest(releaselevel=level):
                with self.assertRaises(RuntimeError):
                    self.module._deprecated("test_warnings", remove=(3, 11),
                                            _version=version)


class BootstrapTest(unittest.TestCase):

    def test_issue_8766(self):
        # "import encodings" emits a warning whereas the warnings is not loaded
        # or not completely loaded (warnings imports indirectly encodings by
        # importing linecache) yet
        with os_helper.temp_cwd() as cwd, os_helper.temp_cwd('encodings'):
            # encodings loaded by initfsencoding()
            assert_python_ok('-c', 'pass', PYTHONPATH=cwd)

            # Use -W to load warnings module at startup
            assert_python_ok('-c', 'pass', '-W', 'always', PYTHONPATH=cwd)


class FinalizationTest(unittest.TestCase):
    def test_finalization(self):
        # Issue #19421: warnings.warn() should not crash
        # during Python finalization
        code = """
import warnings
warn = warnings.warn

class A:
    def __del__(self):
        warn("test")

a=A()
        """
        rc, out, err = assert_python_ok("-c", code)
        self.assertEqual(err.decode().rstrip(),
                         '<string>:7: UserWarning: test')

    def test_late_resource_warning(self):
        # Issue #21925: Emitting a ResourceWarning late during the Python
        # shutdown must be logged.

        expected = b"<sys>:0: ResourceWarning: unclosed file "

        # don't import the warnings module
        # (_warnings will try to import it)
        code = "f = open(%a)" % __file__
        rc, out, err = assert_python_ok("-Wd", "-c", code)
        self.assertStartsWith(err, expected)

        # import the warnings module
        code = "import warnings; f = open(%a)" % __file__
        rc, out, err = assert_python_ok("-Wd", "-c", code)
        self.assertStartsWith(err, expected)


class AsyncTests(BaseTest):
    """Verifies that the catch_warnings() context manager behaves
    as expected when used inside async co-routines.  This requires
    that the context_aware_warnings flag is enabled, so that
    the context manager uses a context variable.
    """

    def setUp(self):
        super().setUp()
        self.module.resetwarnings()

    @unittest.skipIf(not sys.flags.context_aware_warnings,
                     "requires context aware warnings")
    def test_async_context(self):
        import asyncio

        # Events to force the execution interleaving we want.
        step_a1 = asyncio.Event()
        step_a2 = asyncio.Event()
        step_b1 = asyncio.Event()
        step_b2 = asyncio.Event()

        async def run_a():
            with self.module.catch_warnings(record=True) as w:
                await step_a1.wait()
                # The warning emitted here should be caught be the enclosing
                # context manager.
                self.module.warn('run_a warning', UserWarning)
                step_b1.set()
                await step_a2.wait()
                self.assertEqual(len(w), 1)
                self.assertEqual(w[0].message.args[0], 'run_a warning')
                step_b2.set()

        async def run_b():
            with self.module.catch_warnings(record=True) as w:
                step_a1.set()
                await step_b1.wait()
                # The warning emitted here should be caught be the enclosing
                # context manager.
                self.module.warn('run_b warning', UserWarning)
                step_a2.set()
                await step_b2.wait()
                self.assertEqual(len(w), 1)
                self.assertEqual(w[0].message.args[0], 'run_b warning')

        async def run_tasks():
            await asyncio.gather(run_a(), run_b())

        asyncio.run(run_tasks())

    @unittest.skipIf(not sys.flags.context_aware_warnings,
                     "requires context aware warnings")
    def test_async_task_inherit(self):
        """Check that a new asyncio task inherits warnings context from the
        coroutine that spawns it.
        """
        import asyncio

        step1 = asyncio.Event()
        step2 = asyncio.Event()

        async def run_child1():
            await step1.wait()
            # This should be recorded by the run_parent() catch_warnings
            # context.
            self.module.warn('child warning', UserWarning)
            step2.set()

        async def run_child2():
            # This establishes a new catch_warnings() context.  The
            # run_child1() task should still be using the context from
            # run_parent() if context-aware warnings are enabled.
            with self.module.catch_warnings(record=True) as w:
                step1.set()
                await step2.wait()

        async def run_parent():
            with self.module.catch_warnings(record=True) as w:
                await asyncio.gather(run_child1(), run_child2())
                self.assertEqual(len(w), 1)
                self.assertEqual(w[0].message.args[0], 'child warning')

        asyncio.run(run_parent())


class CAsyncTests(AsyncTests, unittest.TestCase):
    module = c_warnings


class PyAsyncTests(AsyncTests, unittest.TestCase):
    module = py_warnings


class ThreadTests(BaseTest):
    """Verifies that the catch_warnings() context manager behaves as
    expected when used within threads.  This requires that both the
    context_aware_warnings flag and thread_inherit_context flags are enabled.
    """

    ENABLE_THREAD_TESTS = (sys.flags.context_aware_warnings and
                           sys.flags.thread_inherit_context)

    def setUp(self):
        super().setUp()
        self.module.resetwarnings()

    @unittest.skipIf(not ENABLE_THREAD_TESTS,
                     "requires thread-safe warnings flags")
    def test_threaded_context(self):
        import threading

        barrier = threading.Barrier(2, timeout=2)

        def run_a():
            with self.module.catch_warnings(record=True) as w:
                barrier.wait()
                # The warning emitted here should be caught be the enclosing
                # context manager.
                self.module.warn('run_a warning', UserWarning)
                barrier.wait()
                self.assertEqual(len(w), 1)
                self.assertEqual(w[0].message.args[0], 'run_a warning')
            # Should be caught be the catch_warnings() context manager of run_threads()
            self.module.warn('main warning', UserWarning)

        def run_b():
            with self.module.catch_warnings(record=True) as w:
                barrier.wait()
                # The warning emitted here should be caught be the enclosing
                # context manager.
                barrier.wait()
                self.module.warn('run_b warning', UserWarning)
                self.assertEqual(len(w), 1)
                self.assertEqual(w[0].message.args[0], 'run_b warning')
            # Should be caught be the catch_warnings() context manager of run_threads()
            self.module.warn('main warning', UserWarning)

        def run_threads():
            threads = [
                threading.Thread(target=run_a),
                threading.Thread(target=run_b),
                ]
            with self.module.catch_warnings(record=True) as w:
                for thread in threads:
                    thread.start()
                for thread in threads:
                    thread.join()
                self.assertEqual(len(w), 2)
                self.assertEqual(w[0].message.args[0], 'main warning')
                self.assertEqual(w[1].message.args[0], 'main warning')

        run_threads()


class CThreadTests(ThreadTests, unittest.TestCase):
    module = c_warnings


class PyThreadTests(ThreadTests, unittest.TestCase):
    module = py_warnings


class DeprecatedTests(PyPublicAPITests):
    def test_dunder_deprecated(self):
        @deprecated("A will go away soon")
        class A:
            pass

        self.assertEqual(A.__deprecated__, "A will go away soon")
        self.assertIsInstance(A, type)

        @deprecated("b will go away soon")
        def b():
            pass

        self.assertEqual(b.__deprecated__, "b will go away soon")
        self.assertIsInstance(b, types.FunctionType)

        @overload
        @deprecated("no more ints")
        def h(x: int) -> int: ...
        @overload
        def h(x: str) -> str: ...
        def h(x):
            return x

        overloads = get_overloads(h)
        self.assertEqual(len(overloads), 2)
        self.assertEqual(overloads[0].__deprecated__, "no more ints")

    def test_class(self):
        @deprecated("A will go away soon")
        class A:
            pass

        with self.assertWarnsRegex(DeprecationWarning, "A will go away soon"):
            A()
        with self.assertWarnsRegex(DeprecationWarning, "A will go away soon"):
            with self.assertRaises(TypeError):
                A(42)

    def test_class_with_init(self):
        @deprecated("HasInit will go away soon")
        class HasInit:
            def __init__(self, x):
                self.x = x

        with self.assertWarnsRegex(DeprecationWarning, "HasInit will go away soon"):
            instance = HasInit(42)
        self.assertEqual(instance.x, 42)

    def test_class_with_new(self):
        has_new_called = False

        @deprecated("HasNew will go away soon")
        class HasNew:
            def __new__(cls, x):
                nonlocal has_new_called
                has_new_called = True
                return super().__new__(cls)

            def __init__(self, x) -> None:
                self.x = x

        with self.assertWarnsRegex(DeprecationWarning, "HasNew will go away soon"):
            instance = HasNew(42)
        self.assertEqual(instance.x, 42)
        self.assertTrue(has_new_called)

    def test_class_with_inherited_new(self):
        new_base_called = False

        class NewBase:
            def __new__(cls, x):
                nonlocal new_base_called
                new_base_called = True
                return super().__new__(cls)

            def __init__(self, x) -> None:
                self.x = x

        @deprecated("HasInheritedNew will go away soon")
        class HasInheritedNew(NewBase):
            pass

        with self.assertWarnsRegex(DeprecationWarning, "HasInheritedNew will go away soon"):
            instance = HasInheritedNew(42)
        self.assertEqual(instance.x, 42)
        self.assertTrue(new_base_called)

    def test_class_with_new_but_no_init(self):
        new_called = False

        @deprecated("HasNewNoInit will go away soon")
        class HasNewNoInit:
            def __new__(cls, x):
                nonlocal new_called
                new_called = True
                obj = super().__new__(cls)
                obj.x = x
                return obj

        with self.assertWarnsRegex(DeprecationWarning, "HasNewNoInit will go away soon"):
            instance = HasNewNoInit(42)
        self.assertEqual(instance.x, 42)
        self.assertTrue(new_called)

    def test_mixin_class(self):
        @deprecated("Mixin will go away soon")
        class Mixin:
            pass

        class Base:
            def __init__(self, a) -> None:
                self.a = a

        with self.assertWarnsRegex(DeprecationWarning, "Mixin will go away soon"):
            class Child(Base, Mixin):
                pass

        instance = Child(42)
        self.assertEqual(instance.a, 42)

    def test_do_not_shadow_user_arguments(self):
        new_called = False
        new_called_cls = None

        @deprecated("MyMeta will go away soon")
        class MyMeta(type):
            def __new__(mcs, name, bases, attrs, cls=None):
                nonlocal new_called, new_called_cls
                new_called = True
                new_called_cls = cls
                return super().__new__(mcs, name, bases, attrs)

        with self.assertWarnsRegex(DeprecationWarning, "MyMeta will go away soon"):
            class Foo(metaclass=MyMeta, cls='haha'):
                pass

        self.assertTrue(new_called)
        self.assertEqual(new_called_cls, 'haha')

    def test_existing_init_subclass(self):
        @deprecated("C will go away soon")
        class C:
            def __init_subclass__(cls) -> None:
                cls.inited = True

        with self.assertWarnsRegex(DeprecationWarning, "C will go away soon"):
            C()

        with self.assertWarnsRegex(DeprecationWarning, "C will go away soon"):
            class D(C):
                pass

        self.assertTrue(D.inited)
        self.assertIsInstance(D(), D)  # no deprecation

    def test_existing_init_subclass_in_base(self):
        class Base:
            def __init_subclass__(cls, x) -> None:
                cls.inited = x

        @deprecated("C will go away soon")
        class C(Base, x=42):
            pass

        self.assertEqual(C.inited, 42)

        with self.assertWarnsRegex(DeprecationWarning, "C will go away soon"):
            C()

        with self.assertWarnsRegex(DeprecationWarning, "C will go away soon"):
            class D(C, x=3):
                pass

        self.assertEqual(D.inited, 3)

    def test_init_subclass_has_correct_cls(self):
        init_subclass_saw = None

        @deprecated("Base will go away soon")
        class Base:
            def __init_subclass__(cls) -> None:
                nonlocal init_subclass_saw
                init_subclass_saw = cls

        self.assertIsNone(init_subclass_saw)

        with self.assertWarnsRegex(DeprecationWarning, "Base will go away soon"):
            class C(Base):
                pass

        self.assertIs(init_subclass_saw, C)

    def test_init_subclass_with_explicit_classmethod(self):
        init_subclass_saw = None

        @deprecated("Base will go away soon")
        class Base:
            @classmethod
            def __init_subclass__(cls) -> None:
                nonlocal init_subclass_saw
                init_subclass_saw = cls

        self.assertIsNone(init_subclass_saw)

        with self.assertWarnsRegex(DeprecationWarning, "Base will go away soon"):
            class C(Base):
                pass

        self.assertIs(init_subclass_saw, C)

    def test_function(self):
        @deprecated("b will go away soon")
        def b():
            pass

        with self.assertWarnsRegex(DeprecationWarning, "b will go away soon"):
            b()

    def test_method(self):
        class Capybara:
            @deprecated("x will go away soon")
            def x(self):
                pass

        instance = Capybara()
        with self.assertWarnsRegex(DeprecationWarning, "x will go away soon"):
            instance.x()

    def test_property(self):
        class Capybara:
            @property
            @deprecated("x will go away soon")
            def x(self):
                pass

            @property
            def no_more_setting(self):
                return 42

            @no_more_setting.setter
            @deprecated("no more setting")
            def no_more_setting(self, value):
                pass

        instance = Capybara()
        with self.assertWarnsRegex(DeprecationWarning, "x will go away soon"):
            instance.x

        with py_warnings.catch_warnings():
            py_warnings.simplefilter("error")
            self.assertEqual(instance.no_more_setting, 42)

        with self.assertWarnsRegex(DeprecationWarning, "no more setting"):
            instance.no_more_setting = 42

    def test_category(self):
        @deprecated("c will go away soon", category=RuntimeWarning)
        def c():
            pass

        with self.assertWarnsRegex(RuntimeWarning, "c will go away soon"):
            c()

    def test_turn_off_warnings(self):
        @deprecated("d will go away soon", category=None)
        def d():
            pass

        with py_warnings.catch_warnings():
            py_warnings.simplefilter("error")
            d()

    def test_only_strings_allowed(self):
        with self.assertRaisesRegex(
            TypeError,
            "Expected an object of type str for 'message', not 'type'"
        ):
            @deprecated
            class Foo: ...

        with self.assertRaisesRegex(
            TypeError,
            "Expected an object of type str for 'message', not 'function'"
        ):
            @deprecated
            def foo(): ...

    def test_no_retained_references_to_wrapper_instance(self):
        @deprecated('depr')
        def d(): pass

        self.assertFalse(any(
            isinstance(cell.cell_contents, deprecated) for cell in d.__closure__
        ))

    def test_inspect(self):
        @deprecated("depr")
        def sync():
            pass

        @deprecated("depr")
        async def coro():
            pass

        class Cls:
            @deprecated("depr")
            def sync(self):
                pass

            @deprecated("depr")
            async def coro(self):
                pass

        self.assertFalse(inspect.iscoroutinefunction(sync))
        self.assertTrue(inspect.iscoroutinefunction(coro))
        self.assertFalse(inspect.iscoroutinefunction(Cls.sync))
        self.assertTrue(inspect.iscoroutinefunction(Cls.coro))

    def test_inspect_class_signature(self):
        class Cls1:  # no __init__ or __new__
            pass

        class Cls2:  # __new__ only
            def __new__(cls, x, y):
                return super().__new__(cls)

        class Cls3:  # __init__ only
            def __init__(self, x, y):
                pass

        class Cls4:  # __new__ and __init__
            def __new__(cls, x, y):
                return super().__new__(cls)

            def __init__(self, x, y):
                pass

        class Cls5(Cls1):  # inherits no __init__ or __new__
            pass

        class Cls6(Cls2):  # inherits __new__ only
            pass

        class Cls7(Cls3):  # inherits __init__ only
            pass

        class Cls8(Cls4):  # inherits __new__ and __init__
            pass

        # The `@deprecated` decorator will update the class in-place.
        # Test the child classes first.
        for cls in reversed((Cls1, Cls2, Cls3, Cls4, Cls5, Cls6, Cls7, Cls8)):
            with self.subTest(f'class {cls.__name__} signature'):
                try:
                    original_signature = inspect.signature(cls)
                except ValueError:
                    original_signature = None
                try:
                    original_new_signature = inspect.signature(cls.__new__)
                except ValueError:
                    original_new_signature = None

                deprecated_cls = deprecated("depr")(cls)

                try:
                    deprecated_signature = inspect.signature(deprecated_cls)
                except ValueError:
                    deprecated_signature = None
                self.assertEqual(original_signature, deprecated_signature)

                try:
                    deprecated_new_signature = inspect.signature(deprecated_cls.__new__)
                except ValueError:
                    deprecated_new_signature = None
                self.assertEqual(original_new_signature, deprecated_new_signature)


def setUpModule():
    py_warnings.onceregistry.clear()
    c_warnings.onceregistry.clear()


tearDownModule = setUpModule

if __name__ == "__main__":
    unittest.main()