|
24 | 24 | import warnings as original_warnings |
25 | 25 | from warnings import deprecated |
26 | 26 |
|
| 27 | +try: |
| 28 | + import _testcapi |
| 29 | +except ImportError: |
| 30 | + _testcapi = None |
| 31 | + |
27 | 32 |
|
28 | 33 | py_warnings = import_helper.import_fresh_module('_py_warnings') |
29 | 34 | py_warnings._set_module(py_warnings) |
@@ -1237,6 +1242,24 @@ def test_issue31566(self): |
1237 | 1242 | support.swap_item(globals(), '__file__', None): |
1238 | 1243 | self.assertRaises(UserWarning, self.module.warn, 'bar') |
1239 | 1244 |
|
| 1245 | + @support.cpython_only |
| 1246 | + # Python built with Py_TRACE_REFS fail with a fatal error in |
| 1247 | + # _PyRefchain_Trace() on memory allocation error. |
| 1248 | + @unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build') |
| 1249 | + @unittest.skipIf(_testcapi is None, "requires _testcapi") |
| 1250 | + def test_issue151673(self): |
| 1251 | + # warn() shouldn't crash when the "<sys>" fallback filename |
| 1252 | + # can't be allocated under memory pressure. |
| 1253 | + code = """if 1: |
| 1254 | + import _testcapi, warnings |
| 1255 | + warnings.simplefilter("always") |
| 1256 | + _testcapi.set_nomemory(0) |
| 1257 | + warnings.warn("boom") |
| 1258 | + """ |
| 1259 | + rc, out, err = assert_python_failure("-c", code) |
| 1260 | + self.assertIn(rc, (1, 120)) |
| 1261 | + self.assertIn(b'MemoryError', err) |
| 1262 | + |
1240 | 1263 |
|
1241 | 1264 | class WarningsDisplayTests(BaseTest): |
1242 | 1265 |
|
|
0 commit comments