gh-122255: Synchronize warnings in C and Python implementations of the warnings module (GH-122824)

In the linecache module and in the Python implementation of the
warnings module, a DeprecationWarning is issued when
m.__loader__ differs from m.__spec__.loader (like in the C
implementation of the warnings module).
This commit is contained in:
Serhiy Storchaka 2025-11-14 16:49:28 +02:00 committed by GitHub
parent c10fa5be61
commit 8deaa9393e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 82 additions and 22 deletions

View file

@ -259,22 +259,44 @@ def raise_memoryerror(*args, **kwargs):
def test_loader(self):
filename = 'scheme://path'
for loader in (None, object(), NoSourceLoader()):
linecache.clearcache()
module_globals = {'__name__': 'a.b.c', '__loader__': None}
self.assertEqual(linecache.getlines(filename, module_globals), [])
for loader in object(), NoSourceLoader():
linecache.clearcache()
module_globals = {'__name__': 'a.b.c', '__loader__': loader}
self.assertEqual(linecache.getlines(filename, module_globals), [])
with self.assertWarns(DeprecationWarning) as w:
self.assertEqual(linecache.getlines(filename, module_globals), [])
self.assertEqual(str(w.warning),
'Module globals is missing a __spec__.loader')
linecache.clearcache()
module_globals = {'__name__': 'a.b.c', '__loader__': FakeLoader()}
self.assertEqual(linecache.getlines(filename, module_globals),
['source for a.b.c\n'])
with self.assertWarns(DeprecationWarning) as w:
self.assertEqual(linecache.getlines(filename, module_globals),
['source for a.b.c\n'])
self.assertEqual(str(w.warning),
'Module globals is missing a __spec__.loader')
for spec in (None, object(), ModuleSpec('', FakeLoader())):
for spec in None, object():
linecache.clearcache()
module_globals = {'__name__': 'a.b.c', '__loader__': FakeLoader(),
'__spec__': spec}
with self.assertWarns(DeprecationWarning) as w:
self.assertEqual(linecache.getlines(filename, module_globals),
['source for a.b.c\n'])
self.assertEqual(str(w.warning),
'Module globals is missing a __spec__.loader')
linecache.clearcache()
module_globals = {'__name__': 'a.b.c', '__loader__': FakeLoader(),
'__spec__': ModuleSpec('', FakeLoader())}
with self.assertWarns(DeprecationWarning) as w:
self.assertEqual(linecache.getlines(filename, module_globals),
['source for a.b.c\n'])
self.assertEqual(str(w.warning),
'Module globals; __loader__ != __spec__.loader')
linecache.clearcache()
spec = ModuleSpec('x.y.z', FakeLoader())