|
100 | 100 | # threads |
101 | 101 | "threading_setup", "threading_cleanup", "reap_threads", "start_threads", |
102 | 102 | # miscellaneous |
103 | | - "check_warnings", "EnvironmentVarGuard", "run_with_locale", "swap_item", |
| 103 | + "check_warnings", "check_no_resource_warning", "EnvironmentVarGuard", |
| 104 | + "run_with_locale", "swap_item", |
104 | 105 | "swap_attr", "Matcher", "set_memlimit", "SuppressCrashReport", "sortdict", |
105 | 106 | "run_with_tz", |
106 | 107 | ] |
@@ -1147,6 +1148,27 @@ def check_warnings(*filters, **kwargs): |
1147 | 1148 | return _filterwarnings(filters, quiet) |
1148 | 1149 |
|
1149 | 1150 |
|
| 1151 | +@contextlib.contextmanager |
| 1152 | +def check_no_resource_warning(testcase): |
| 1153 | + """Context manager to check that no ResourceWarning is emitted. |
| 1154 | +
|
| 1155 | + Usage: |
| 1156 | +
|
| 1157 | + with check_no_resource_warning(self): |
| 1158 | + f = open(...) |
| 1159 | + ... |
| 1160 | + del f |
| 1161 | +
|
| 1162 | + You must remove the object which may emit ResourceWarning before |
| 1163 | + the end of the context manager. |
| 1164 | + """ |
| 1165 | + with warnings.catch_warnings(record=True) as warns: |
| 1166 | + warnings.filterwarnings('always', category=ResourceWarning) |
| 1167 | + yield |
| 1168 | + gc_collect() |
| 1169 | + testcase.assertEqual(warns, []) |
| 1170 | + |
| 1171 | + |
1150 | 1172 | class CleanImport(object): |
1151 | 1173 | """Context manager to force import to return a new module reference. |
1152 | 1174 |
|
|
0 commit comments