1414 interpreter_requires_environment
1515)
1616
17+
18+ # Debug build?
19+ Py_DEBUG = hasattr (sys , "gettotalrefcount" )
20+
21+
1722# XXX (ncoghlan): Move to script_helper and make consistent with run_python
1823def _kill_python_and_exit_code (p ):
1924 data = kill_python (p )
@@ -97,7 +102,7 @@ def run_python(*args):
97102 # "-X showrefcount" shows the refcount, but only in debug builds
98103 rc , out , err = run_python ('-X' , 'showrefcount' , '-c' , code )
99104 self .assertEqual (out .rstrip (), b"{'showrefcount': True}" )
100- if hasattr ( sys , 'gettotalrefcount' ): # debug build
105+ if Py_DEBUG :
101106 self .assertRegex (err , br'^\[\d+ refs, \d+ blocks\]' )
102107 else :
103108 self .assertEqual (err , b'' )
@@ -541,31 +546,26 @@ def test_xdev(self):
541546 code = ("import sys, warnings; "
542547 "print(' '.join('%s::%s' % (f[0], f[2].__name__) "
543548 "for f in warnings.filters))" )
549+ if Py_DEBUG :
550+ expected_filters = "default::Warning"
551+ else :
552+ expected_filters = ("default::Warning "
553+ "ignore::DeprecationWarning "
554+ "ignore::PendingDeprecationWarning "
555+ "ignore::ImportWarning "
556+ "ignore::ResourceWarning" )
544557
545558 out = self .run_xdev ("-c" , code )
546- self .assertEqual (out ,
547- "ignore::BytesWarning "
548- "default::ResourceWarning "
549- "default::Warning" )
559+ self .assertEqual (out , expected_filters )
550560
551561 out = self .run_xdev ("-b" , "-c" , code )
552- self .assertEqual (out ,
553- "default::BytesWarning "
554- "default::ResourceWarning "
555- "default::Warning" )
562+ self .assertEqual (out , f"default::BytesWarning { expected_filters } " )
556563
557564 out = self .run_xdev ("-bb" , "-c" , code )
558- self .assertEqual (out ,
559- "error::BytesWarning "
560- "default::ResourceWarning "
561- "default::Warning" )
565+ self .assertEqual (out , f"error::BytesWarning { expected_filters } " )
562566
563567 out = self .run_xdev ("-Werror" , "-c" , code )
564- self .assertEqual (out ,
565- "error::Warning "
566- "ignore::BytesWarning "
567- "default::ResourceWarning "
568- "default::Warning" )
568+ self .assertEqual (out , f"error::Warning { expected_filters } " )
569569
570570 # Memory allocator debug hooks
571571 try :
@@ -592,6 +592,46 @@ def test_xdev(self):
592592 out = self .run_xdev ("-c" , code )
593593 self .assertEqual (out , "True" )
594594
595+ def check_warnings_filters (self , cmdline_option , envvar , use_pywarning = False ):
596+ if use_pywarning :
597+ code = ("import sys; from test.support import import_fresh_module; "
598+ "warnings = import_fresh_module('warnings', blocked=['_warnings']); " )
599+ else :
600+ code = "import sys, warnings; "
601+ code += ("print(' '.join('%s::%s' % (f[0], f[2].__name__) "
602+ "for f in warnings.filters))" )
603+ args = (sys .executable , '-W' , cmdline_option , '-bb' , '-c' , code )
604+ env = dict (os .environ )
605+ env .pop ('PYTHONDEVMODE' , None )
606+ env ["PYTHONWARNINGS" ] = envvar
607+ proc = subprocess .run (args ,
608+ stdout = subprocess .PIPE ,
609+ stderr = subprocess .STDOUT ,
610+ universal_newlines = True ,
611+ env = env )
612+ self .assertEqual (proc .returncode , 0 , proc )
613+ return proc .stdout .rstrip ()
614+
615+ def test_warnings_filter_precedence (self ):
616+ expected_filters = ("error::BytesWarning "
617+ "once::UserWarning "
618+ "always::UserWarning" )
619+ if not Py_DEBUG :
620+ expected_filters += (" "
621+ "ignore::DeprecationWarning "
622+ "ignore::PendingDeprecationWarning "
623+ "ignore::ImportWarning "
624+ "ignore::ResourceWarning" )
625+
626+ out = self .check_warnings_filters ("once::UserWarning" ,
627+ "always::UserWarning" )
628+ self .assertEqual (out , expected_filters )
629+
630+ out = self .check_warnings_filters ("once::UserWarning" ,
631+ "always::UserWarning" ,
632+ use_pywarning = True )
633+ self .assertEqual (out , expected_filters )
634+
595635 def check_pythonmalloc (self , env_var , name ):
596636 code = 'import _testcapi; print(_testcapi.pymem_getallocatorsname())'
597637 env = dict (os .environ )
@@ -611,13 +651,12 @@ def check_pythonmalloc(self, env_var, name):
611651
612652 def test_pythonmalloc (self ):
613653 # Test the PYTHONMALLOC environment variable
614- pydebug = hasattr (sys , "gettotalrefcount" )
615654 pymalloc = support .with_pymalloc ()
616655 if pymalloc :
617- default_name = 'pymalloc_debug' if pydebug else 'pymalloc'
656+ default_name = 'pymalloc_debug' if Py_DEBUG else 'pymalloc'
618657 default_name_debug = 'pymalloc_debug'
619658 else :
620- default_name = 'malloc_debug' if pydebug else 'malloc'
659+ default_name = 'malloc_debug' if Py_DEBUG else 'malloc'
621660 default_name_debug = 'malloc_debug'
622661
623662 tests = [
0 commit comments