@@ -640,6 +640,97 @@ class NonWarningSubclass:
640640 self .module .warn ('good warning category' , MyWarningClass )
641641 self .assertIsInstance (cm .warning , Warning )
642642
643+ def check_module_globals (self , module_globals ):
644+ with original_warnings .catch_warnings (module = self .module , record = True ) as w :
645+ self .module .filterwarnings ('default' )
646+ self .module .warn_explicit (
647+ 'eggs' , UserWarning , 'bar' , 1 ,
648+ module_globals = module_globals )
649+ self .assertEqual (len (w ), 1 )
650+ self .assertEqual (w [0 ].category , UserWarning )
651+ self .assertEqual (str (w [0 ].message ), 'eggs' )
652+
653+ def check_module_globals_error (self , module_globals , errmsg , errtype = ValueError ):
654+ if self .module is py_warnings :
655+ self .check_module_globals (module_globals )
656+ return
657+ with original_warnings .catch_warnings (module = self .module , record = True ) as w :
658+ self .module .filterwarnings ('always' )
659+ with self .assertRaisesRegex (errtype , re .escape (errmsg )):
660+ self .module .warn_explicit (
661+ 'eggs' , UserWarning , 'bar' , 1 ,
662+ module_globals = module_globals )
663+ self .assertEqual (len (w ), 0 )
664+
665+ def check_module_globals_deprecated (self , module_globals , msg ):
666+ if self .module is py_warnings :
667+ self .check_module_globals (module_globals )
668+ return
669+ with original_warnings .catch_warnings (module = self .module , record = True ) as w :
670+ self .module .filterwarnings ('always' )
671+ self .module .warn_explicit (
672+ 'eggs' , UserWarning , 'bar' , 1 ,
673+ module_globals = module_globals )
674+ self .assertEqual (len (w ), 2 )
675+ self .assertEqual (w [0 ].category , DeprecationWarning )
676+ self .assertEqual (str (w [0 ].message ), msg )
677+ self .assertEqual (w [1 ].category , UserWarning )
678+ self .assertEqual (str (w [1 ].message ), 'eggs' )
679+
680+ def test_gh86298_no_loader_and_no_spec (self ):
681+ self .check_module_globals ({'__name__' : 'bar' })
682+
683+ def test_gh86298_loader_is_none_and_no_spec (self ):
684+ self .check_module_globals ({'__name__' : 'bar' , '__loader__' : None })
685+
686+ def test_gh86298_no_loader_and_spec_is_none (self ):
687+ self .check_module_globals_error (
688+ {'__name__' : 'bar' , '__spec__' : None },
689+ 'Module globals is missing a __spec__.loader' )
690+
691+ def test_gh86298_loader_is_none_and_spec_is_none (self ):
692+ self .check_module_globals_error (
693+ {'__name__' : 'bar' , '__loader__' : None , '__spec__' : None },
694+ 'Module globals is missing a __spec__.loader' )
695+
696+ def test_gh86298_loader_is_none_and_spec_loader_is_none (self ):
697+ self .check_module_globals_error (
698+ {'__name__' : 'bar' , '__loader__' : None ,
699+ '__spec__' : types .SimpleNamespace (loader = None )},
700+ 'Module globals is missing a __spec__.loader' )
701+
702+ def test_gh86298_no_spec (self ):
703+ self .check_module_globals_deprecated (
704+ {'__name__' : 'bar' , '__loader__' : object ()},
705+ 'Module globals is missing a __spec__.loader' )
706+
707+ def test_gh86298_spec_is_none (self ):
708+ self .check_module_globals_deprecated (
709+ {'__name__' : 'bar' , '__loader__' : object (), '__spec__' : None },
710+ 'Module globals is missing a __spec__.loader' )
711+
712+ def test_gh86298_no_spec_loader (self ):
713+ self .check_module_globals_deprecated (
714+ {'__name__' : 'bar' , '__loader__' : object (),
715+ '__spec__' : types .SimpleNamespace ()},
716+ 'Module globals is missing a __spec__.loader' )
717+
718+ def test_gh86298_loader_and_spec_loader_disagree (self ):
719+ self .check_module_globals_deprecated (
720+ {'__name__' : 'bar' , '__loader__' : object (),
721+ '__spec__' : types .SimpleNamespace (loader = object ())},
722+ 'Module globals; __loader__ != __spec__.loader' )
723+
724+ def test_gh86298_no_loader_and_no_spec_loader (self ):
725+ self .check_module_globals_error (
726+ {'__name__' : 'bar' , '__spec__' : types .SimpleNamespace ()},
727+ 'Module globals is missing a __spec__.loader' , AttributeError )
728+
729+ def test_gh86298_no_loader_with_spec_loader_okay (self ):
730+ self .check_module_globals (
731+ {'__name__' : 'bar' ,
732+ '__spec__' : types .SimpleNamespace (loader = object ())})
733+
643734class CWarnTests (WarnTests , unittest .TestCase ):
644735 module = c_warnings
645736
0 commit comments