@@ -611,6 +611,97 @@ class NonWarningSubclass:
611611 self .module .warn ('good warning category' , MyWarningClass )
612612 self .assertIsInstance (cm .warning , Warning )
613613
614+ def check_module_globals (self , module_globals ):
615+ with original_warnings .catch_warnings (module = self .module , record = True ) as w :
616+ self .module .filterwarnings ('default' )
617+ self .module .warn_explicit (
618+ 'eggs' , UserWarning , 'bar' , 1 ,
619+ module_globals = module_globals )
620+ self .assertEqual (len (w ), 1 )
621+ self .assertEqual (w [0 ].category , UserWarning )
622+ self .assertEqual (str (w [0 ].message ), 'eggs' )
623+
624+ def check_module_globals_error (self , module_globals , errmsg , errtype = ValueError ):
625+ if self .module is py_warnings :
626+ self .check_module_globals (module_globals )
627+ return
628+ with original_warnings .catch_warnings (module = self .module , record = True ) as w :
629+ self .module .filterwarnings ('always' )
630+ with self .assertRaisesRegex (errtype , re .escape (errmsg )):
631+ self .module .warn_explicit (
632+ 'eggs' , UserWarning , 'bar' , 1 ,
633+ module_globals = module_globals )
634+ self .assertEqual (len (w ), 0 )
635+
636+ def check_module_globals_deprecated (self , module_globals , msg ):
637+ if self .module is py_warnings :
638+ self .check_module_globals (module_globals )
639+ return
640+ with original_warnings .catch_warnings (module = self .module , record = True ) as w :
641+ self .module .filterwarnings ('always' )
642+ self .module .warn_explicit (
643+ 'eggs' , UserWarning , 'bar' , 1 ,
644+ module_globals = module_globals )
645+ self .assertEqual (len (w ), 2 )
646+ self .assertEqual (w [0 ].category , DeprecationWarning )
647+ self .assertEqual (str (w [0 ].message ), msg )
648+ self .assertEqual (w [1 ].category , UserWarning )
649+ self .assertEqual (str (w [1 ].message ), 'eggs' )
650+
651+ def test_gh86298_no_loader_and_no_spec (self ):
652+ self .check_module_globals ({'__name__' : 'bar' })
653+
654+ def test_gh86298_loader_is_none_and_no_spec (self ):
655+ self .check_module_globals ({'__name__' : 'bar' , '__loader__' : None })
656+
657+ def test_gh86298_no_loader_and_spec_is_none (self ):
658+ self .check_module_globals_error (
659+ {'__name__' : 'bar' , '__spec__' : None },
660+ 'Module globals is missing a __spec__.loader' )
661+
662+ def test_gh86298_loader_is_none_and_spec_is_none (self ):
663+ self .check_module_globals_error (
664+ {'__name__' : 'bar' , '__loader__' : None , '__spec__' : None },
665+ 'Module globals is missing a __spec__.loader' )
666+
667+ def test_gh86298_loader_is_none_and_spec_loader_is_none (self ):
668+ self .check_module_globals_error (
669+ {'__name__' : 'bar' , '__loader__' : None ,
670+ '__spec__' : types .SimpleNamespace (loader = None )},
671+ 'Module globals is missing a __spec__.loader' )
672+
673+ def test_gh86298_no_spec (self ):
674+ self .check_module_globals_deprecated (
675+ {'__name__' : 'bar' , '__loader__' : object ()},
676+ 'Module globals is missing a __spec__.loader' )
677+
678+ def test_gh86298_spec_is_none (self ):
679+ self .check_module_globals_deprecated (
680+ {'__name__' : 'bar' , '__loader__' : object (), '__spec__' : None },
681+ 'Module globals is missing a __spec__.loader' )
682+
683+ def test_gh86298_no_spec_loader (self ):
684+ self .check_module_globals_deprecated (
685+ {'__name__' : 'bar' , '__loader__' : object (),
686+ '__spec__' : types .SimpleNamespace ()},
687+ 'Module globals is missing a __spec__.loader' )
688+
689+ def test_gh86298_loader_and_spec_loader_disagree (self ):
690+ self .check_module_globals_deprecated (
691+ {'__name__' : 'bar' , '__loader__' : object (),
692+ '__spec__' : types .SimpleNamespace (loader = object ())},
693+ 'Module globals; __loader__ != __spec__.loader' )
694+
695+ def test_gh86298_no_loader_and_no_spec_loader (self ):
696+ self .check_module_globals_error (
697+ {'__name__' : 'bar' , '__spec__' : types .SimpleNamespace ()},
698+ 'Module globals is missing a __spec__.loader' , AttributeError )
699+
700+ def test_gh86298_no_loader_with_spec_loader_okay (self ):
701+ self .check_module_globals (
702+ {'__name__' : 'bar' ,
703+ '__spec__' : types .SimpleNamespace (loader = object ())})
704+
614705class CWarnTests (WarnTests , unittest .TestCase ):
615706 module = c_warnings
616707
0 commit comments