8000 Simplify testing the warning filename (GH-91868) · python/cpython@0907217 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0907217

Browse files
Simplify testing the warning filename (GH-91868)
The context manager result has the "filename" attribute.
1 parent b4e0484 commit 0907217

File tree

8 files changed

+43
-43
lines changed

8 files changed

+43
-43
lines changed

Lib/test/test_asyncio/test_events.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,12 +2712,12 @@ def get_event_loop(self):
27122712
with self.assertWarns(DeprecationWarning) as cm:
27132713
with self.assertRaises(TestError):
27142714
asyncio.get_event_loop()
2715-
self.assertEqual(cm.warnings[0].filename, __file__)
2715+
self.assertEqual(cm.filename, __file__)
27162716
asyncio.set_event_loop(None)
27172717
with self.assertWarns(DeprecationWarning) as cm:
27182718
with self.assertRaises(TestError):
27192719
asyncio.get_event_loop()
2720-
self.assertEqual(cm.warnings[0].filename, __file__)
2720+
self.assertEqual(cm.filename, __file__)
27212721

27222722
with self.assertRaisesRegex(RuntimeError, 'no running'):
27232723
asyncio.get_running_loop()
@@ -2734,13 +2734,13 @@ async def func():
27342734
with self.assertWarns(DeprecationWarning) as cm:
27352735
with self.assertRaises(TestError):
27362736
asyncio.get_event_loop()
2737-
self.assertEqual(cm.warnings[0].filename, __file__)
2737+
self.assertEqual(cm.filename, __file__)
27382738

27392739
asyncio.set_event_loop(None)
27402740
with self.assertWarns(DeprecationWarning) as cm:
27412741
with self.assertRaises(TestError):
27422742
asyncio.get_event_loop()
2743-
self.assertEqual(cm.warnings[0].filename, __file__)
2743+
self.assertEqual(cm.filename, __file__)
27442744

27452745
finally:
27462746
asyncio.set_event_loop_policy(old_policy)
@@ -2762,12 +2762,12 @@ def test_get_event_loop_returns_running_loop2(self):
27622762
with self.assertWarns(DeprecationWarning) as cm:
27632763
loop2 = asyncio.get_event_loop()
27642764
self.addCleanup(loop2.close)
2765-
self.assertEqual(cm.warnings[0].filename, __file__)
2765+
self.assertEqual(cm.filename, __file__)
27662766
asyncio.set_event_loop(None)
27672767
with self.assertWarns(DeprecationWarning) as cm:
27682768
with self.assertRaisesRegex(RuntimeError, 'no current'):
27692769
asyncio.get_event_loop()
2770-
self.assertEqual(cm.warnings[0].filename, __file__)
2770+
self.assertEqual(cm.filename, __file__)
27712771

27722772
with self.assertRaisesRegex(RuntimeError, 'no running'):
27732773
asyncio.get_running_loop()
@@ -2783,13 +2783,13 @@ async def func():
27832783
asyncio.set_event_loop(loop)
27842784
with self.assertWarns(DeprecationWarning) as cm:
27852785
self.assertIs(asyncio.get_event_loop(), loop)
2786-
self.assertEqual(cm.warnings[0].filename, __file__)
2786+
self.assertEqual(cm.filename, __file__)
27872787

27882788
asyncio.set_event_loop(None)
27892789
with self.assertWarns(DeprecationWarning) as cm:
27902790
with self.assertRaisesRegex(RuntimeError, 'no current'):
27912791
asyncio.get_event_loop()
2792-
self.assertEqual(cm.warnings[0].filename, __file__)
2792+
self.assertEqual(cm.filename, __file__)
27932793

27942794
finally:
27952795
asyncio.set_event_loop_policy(old_policy)

Lib/test/test_asyncio/test_futures.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def test_constructor_without_loop(self):
148148
with self.assertWarns(DeprecationWarning) as cm:
149149
with self.assertRaisesRegex(RuntimeError, 'There is no current event loop'):
150150
self._new_future()
151-
self.assertEqual(cm.warnings[0].filename, __file__)
151+
self.assertEqual(cm.filename, __file__)
152152

153153
def test_constructor_use_running_loop(self):
154154
async def test():
@@ -163,7 +163,7 @@ def test_constructor_use_global_loop(self):
163163
self.addCleanup(asyncio.set_event_loop, None)
164164
with self.assertWarns(DeprecationWarning) as cm:
165165
f = self._new_future()
166-
self.assertEqual(cm.warnings[0].filename, __file__)
166+
self.assertEqual(cm.filename, __file__)
167167
self.assertIs(f._loop, self.loop)
168168
self.assertIs(f.get_loop(), self.loop)
169169

@@ -510,7 +510,7 @@ def run(arg):
510510
with self.assertWarns(DeprecationWarning) as cm:
511511
with self.assertRaises(RuntimeError):
512512
asyncio.wrap_future(f1)
513-
self.assertEqual(cm.warnings[0].filename, __file__)
513+
self.assertEqual(cm.filename, __file__)
514514
ex.shutdown(wait=True)
515515

516516
def test_wrap_future_use_running_loop(self):
@@ -534,7 +534,7 @@ def run(arg):
534534
f1 = ex.submit(run, 'oi')
535535
with self.assertWarns(DeprecationWarning) as cm:
536536
f2 = asyncio.wrap_future(f1)
537-
self.assertEqual(cm.warnings[0].filename, __file__)
537+
self.assertEqual(cm.filename, __file__)
538538
self.assertIs(self.loop, f2._loop)
539539
ex.shutdown(wait=True)
540540

Lib/test/test_asyncio/test_streams.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ def test_streamreader_constructor_without_loop(self):
813813
with self.assertWarns(DeprecationWarning) as cm:
814814
with self.assertRaisesRegex(RuntimeError, 'There is no current event loop'):
815815
asyncio.StreamReader()
816-
self.assertEqual(cm.warnings[0].filename, __file__)
816+
self.assertEqual(cm.filename, __file__)
817817

818818
def test_streamreader_constructor_use_running_loop(self):
819819
# asyncio issue #184: Ensure that StreamReaderProtocol constructor
@@ -832,7 +832,7 @@ def test_streamreader_constructor_use_global_loop(self):
832832
asyncio.set_event_loop(self.loop)
833833
with self.assertWarns(DeprecationWarning) as cm:
834834
reader = asyncio.StreamReader()
835-
self.assertEqual(cm.warnings[0].filename, __file__)
835+
self.assertEqual(cm.filename, __file__)
836836
self.assertIs(reader._loop, self.loop)
837837

838838

@@ -841,7 +841,7 @@ def test_streamreaderprotocol_constructor_without_loop(self):
841841
with self.assertWarns(DeprecationWarning) as cm:
842842
with self.assertRaisesRegex(RuntimeError, 'There is no current event loop'):
843843
asyncio.StreamReaderProtocol(reader)
844-
self.assertEqual(cm.warnings[0].filename, __file__)
844+
self.assertEqual(cm.filename, __file__)
845845

846846
def test_streamreaderprotocol_constructor_use_running_loop(self):
847847
# asyncio issue #184: Ensure that StreamReaderProtocol constructor
@@ -861,7 +861,7 @@ def test_streamreaderprotocol_constructor_use_global_loop(self):
861861
reader = mock.Mock()
862862
with self.assertWarns(DeprecationWarning) as cm:
863863
protocol = asyncio.StreamReaderProtocol(reader)
864-
self.assertEqual(cm.warnings[0].filename, __file__)
864+
self.assertEqual(cm.filename, __file__)
865865
self.assertIs(protocol._loop, self.loop)
866866

867867
def test_drain_raises(self):

Lib/test/test_asyncio/test_tasks.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ async def notmuch():
211211
with self.assertWarns(DeprecationWarning) as cm:
212212
with self.assertRaisesRegex(RuntimeError, 'There is no current event loop'):
213213
asyncio.ensure_future(a)
214-
self.assertEqual(cm.warnings[0].filename, __file__)
214+
self.assertEqual(cm.filename, __file__)
215215

216216
async def test():
217217
return asyncio.ensure_future(notmuch())
@@ -226,7 +226,7 @@ async def test():
226226
self.addCleanup(asyncio.set_event_loop, None)
227227
with self.assertWarns(DeprecationWarning) as cm:
228228
t = asyncio.ensure_future(notmuch())
229-
self.assertEqual(cm.warnings[0].filename, __file__)
229+
self.assertEqual(cm.filename, __file__)
230230
self.assertIs(t._loop, self.loop)
231231
self.loop.run_until_complete(t)
232232
self.assertTrue(t.done())
@@ -1449,7 +1449,7 @@ async def coro():
14491449
with self.assertWarns(DeprecationWarning) as cm:
14501450
with self.assertRaisesRegex(RuntimeError, 'There is no current event loop'):
14511451
list(futs)
1452-
self.assertEqual(cm.warnings[0].filename, __file__)
1452+
self.assertEqual(cm.filename, __file__)
14531453

14541454
def test_as_completed_coroutine_use_running_loop(self):
14551455
loop = self.new_test_loop()
@@ -1881,7 +1881,7 @@ async def coro():
18811881
with self.assertWarns(DeprecationWarning) as cm:
18821882
with self.assertRaisesRegex(RuntimeError, 'There is no current event loop'):
18831883
asyncio.shield(inner)
1884-
self.assertEqual(cm.warnings[0].filename, __file__)
1884+
self.assertEqual(cm.filename, __file__)
18851885

18861886
def test_shield_coroutine_use_running_loop(self):
18871887
async def coro():
@@ -1903,7 +1903,7 @@ async def coro():
19031903
self.addCleanup(asyncio.set_event_loop, None)
19041904
with self.assertWarns(DeprecationWarning) as cm:
19051905
outer = asyncio.shield(coro())
1906-
self.assertEqual(cm.warnings[0].filename, __file__)
1906+
self.assertEqual(cm.filename, __file__)
19071907
self.assertEqual(outer._loop, self.loop)
19081908
res = self.loop.run_until_complete(outer)
19091909
self.assertEqual(res, 42)
@@ -2911,7 +2911,7 @@ def test_constructor_empty_sequence_without_loop(self):
29112911
with self.assertWarns(DeprecationWarning) as cm:
29122912
with self.assertRaises(RuntimeError):
29132913
asyncio.gather()
2914-
self.assertEqual(cm.warnings[0].filename, __file__)
2914+
self.assertEqual(cm.filename, __file__)
29152915

29162916
def test_constructor_empty_sequence_use_running_loop(self):
29172917
async def gather():
@@ -2929,7 +2929,7 @@ def test_constructor_empty_sequence_use_global_loop(self):
29292929
self.addCleanup(asyncio.set_event_loop, None)
29302930
with self.assertWarns(DeprecationWarning) as cm:
29312931
fut = asyncio.gather()
2932-
self.assertEqual(cm.warnings[0].filename, __file__)
2932+
self.assertEqual(cm.filename, __file__)
29332933
self.assertIsInstance(fut, asyncio.Future)
29342934
self.assertIs(fut._loop, self.one_loop)
29352935
self._run_loop(self.one_loop)
@@ -3020,7 +3020,7 @@ async def coro():
30203020
with self.assertWarns(DeprecationWarning) as cm:
30213021
with self.assertRaises(RuntimeError):
30223022
asyncio.gather(gen1, gen2)
3023-
self.assertEqual(cm.warnings[0].filename, __file__)
3023+
self.assertEqual(cm.filename, __file__)
30243024

30253025
def test_constructor_use_running_loop(self):
30263026
async def coro():
@@ -3043,7 +3043,7 @@ async def coro():
30433043
gen2 = coro()
30443044
with self.assertWarns(DeprecationWarning) as cm:
30453045
fut = asyncio.gather(gen1, gen2)
3046-
self.assertEqual(cm.warnings[0].filename, __file__)
3046+
self.assertEqual(cm.filename, __file__)
30473047
self.assertIs(fut._loop, self.other_loop)
30483048
self.other_loop.run_until_complete(fut)
30493049

Lib/test/test_re.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,11 +2608,11 @@ def test_deprecated_modules(self):
26082608
for name in deprecated:
26092609
with self.subTest(module=name):
26102610
sys.modules.pop(name, None)
2611-
with self.assertWarns(DeprecationWarning) as cm:
2611+
with self.assertWarns(DeprecationWarning) as w:
26122612
__import__(name)
2613-
self.assertEqual(str(cm.warnings[0].message),
2613+
self.assertEqual(str(w.warning),
26142614
f"module {name!r} is deprecated")
2615-
self.assertEqual(cm.warnings[0].filename, __file__)
2615+
self.assertEqual(w.filename, __file__)
26162616
self.assertIn(name, sys.modules)
26172617
mod = sys.modules[name]
26182618
self.assertEqual(mod.__name__, name)

Lib/unittest/test/test_async_case.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,15 @@ async def test2(self):
259259

260260
with self.assertWarns(DeprecationWarning) as w:
261261
Test('test1').run()
262-
self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
263-
self.assertIn('test1', str(w.warnings[0].message))
264-
self.assertEqual(w.warnings[0].filename, __file__)
262+
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
263+
self.assertIn('test1', str(w.warning))
264+
self.assertEqual(w.filename, __file__)
265265

266266
with self.assertWarns(DeprecationWarning) as w:
267267
Test('test2').run()
268-
self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
269-
self.assertIn('test2', str(w.warnings[0].message))
270-
self.assertEqual(w.warnings[0].filename, __file__)
268+
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
269+
self.assertIn('test2', str(w.warning))
270+
self.assertEqual(w.filename, __file__)
271271

272272
def test_cleanups_interleave_order(self):
273273
events = []

Lib/unittest/test/test_case.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,15 @@ def test2(self):
316316

317317
with self.assertWarns(DeprecationWarning) as w:
318318
Foo('test1').run()
319-
self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
320-
self.assertIn('test1', str(w.warnings[0].message))
321-
self.assertEqual(w.warnings[0].filename, __file__)
319+
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
320+
self.assertIn('test1', str(w.warning))
321+
self.assertEqual(w.filename, __file__)
322322

323323
with self.assertWarns(DeprecationWarning) as w:
324324
Foo('test2').run()
325-
self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
326-
self.assertIn('test2', str(w.warnings[0].message))
327-
self.assertEqual(w.warnings[0].filename, __file__)
325+
self.assertIn('It is deprecated to return a value!=None', str(w.warning))
326+
self.assertIn('test2', str(w.warning))
327+
self.assertEqual(w.filename, __file__)
328328

329329
def _check_call_order__subtests(self, result, events, expected_events):
330330
class Foo(Test.LoggingTestCase):

Lib/unittest/test/test_loader.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,15 +1609,15 @@ def test_getTestCaseNames(self):
16091609
tests = unittest.getTestCaseNames(self.MyTestCase,
16101610
prefix='check', sortUsing=self.reverse_three_way_cmp,
16111611
testNamePatterns=None)
1612-
self.assertEqual(w.warnings[0].filename, __file__)
1612+
self.assertEqual(w.filename, __file__)
16131613
self.assertEqual(tests, ['check_2', 'check_1'])
16141614

16151615
def test_makeSuite(self):
16161616
with self.assertWarns(DeprecationWarning) as w:
16171617
suite = unittest.makeSuite(self.MyTestCase,
16181618
prefix='check', sortUsing=self.reverse_three_way_cmp,
16191619
suiteClass=self.MyTestSuite)
1620-
self.assertEqual(w.warnings[0].filename, __file__)
1620+
self.assertEqual(w.filename, __file__)
16211621
self.assertIsInstance(suite, self.MyTestSuite)
16221622
expected = self.MyTestSuite([self.MyTestCase('check_2'),
16231623
self.MyTestCase('check_1')])
@@ -1631,7 +1631,7 @@ def test_findTestCases(self):
16311631
suite = unittest.findTestCases(m,
16321632
prefix='check', sortUsing=self.reverse_three_way_cmp,
16331633
suiteClass=self.MyTestSuite)
1634-
self.assertEqual(w.warnings[0].filename, __file__)
1634+
self.assertEqual(w.filename, __file__)
16351635
self.assertIsInstance(suite, self.MyTestSuite)
16361636
expected = [self.MyTestSuite([self.MyTestCase('check_2'),
16371637
self.MyTestCase('check_1')])]

0 commit comments

Comments
 (0)
0