22# Most tests are executed with environment variables ignored
33# See test_cmd_line_script.py for testing of script execution
44
5- import test .support , unittest
65import os
7- import sys
86import subprocess
7+ import sys
98import tempfile
10- from test .support import script_helper , is_android
9+ import unittest
10+ from test import support
1111from test .support .script_helper import (
12- spawn_python , kill_python , assert_python_ok , assert_python_failure
12+ spawn_python , kill_python , assert_python_ok , assert_python_failure ,
13+ interpreter_requires_environment
1314)
1415
1516# XXX (ncoghlan): Move to script_helper and make consistent with run_python
@@ -132,11 +133,11 @@ def test_run_code(self):
132133 # All good if execution is successful
133134 assert_python_ok ('-c' , 'pass' )
134135
135- @unittest .skipUnless (test . support .FS_NONASCII , 'need support.FS_NONASCII' )
136+ @unittest .skipUnless (support .FS_NONASCII , 'need support.FS_NONASCII' )
136137 def test_non_ascii (self ):
137138 # Test handling of non-ascii data
138139 command = ("assert(ord(%r) == %s)"
139- % (test . support .FS_NONASCII , ord (test . support .FS_NONASCII )))
140+ % (support .FS_NONASCII , ord (support .FS_NONASCII )))
140141 assert_python_ok ('-c' , command )
141142
142143 # On Windows, pass bytes to subprocess doesn't test how Python decodes the
@@ -179,7 +180,7 @@ def test_undecodable_code(self):
179180 raise AssertionError ("%a doesn't start with %a" % (stdout , pattern ))
180181
181182 @unittest .skipUnless ((sys .platform == 'darwin' or
182- is_android ), 'test specific to Mac OS X and Android' )
183+ support . is_android ), 'test specific to Mac OS X and Android' )
183184 def test_osx_android_utf8 (self ):
184185 def check_output (text ):
185186 decoded = text .decode ('utf-8' , 'surrogateescape' )
@@ -385,7 +386,7 @@ def preexec():
385386 stderr = subprocess .PIPE ,
386387 preexec_fn = preexec )
387388 out , err = p .communicate ()
388- self .assertEqual (test . support .strip_python_stderr (err ), b'' )
389+ self .assertEqual (support .strip_python_stderr (err ), b'' )
389390 self .assertEqual (p .returncode , 42 )
390391
391392 def test_no_stdin (self ):
@@ -433,8 +434,8 @@ def test_del___main__(self):
433434 # Issue #15001: PyRun_SimpleFileExFlags() did crash because it kept a
434435 # borrowed reference to the dict of __main__ module and later modify
435436 # the dict whereas the module was destroyed
436- filename = test . support .TESTFN
437- self .addCleanup (test . support .unlink , filename )
437+ filename = support .TESTFN
438+ self .addCleanup (support .unlink , filename )
438439 with open (filename , "w" ) as script :
439440 print ("import sys" , file = script )
440441 print ("del sys.modules['__main__']" , file = script )
@@ -458,7 +459,7 @@ def test_unknown_options(self):
458459 self .assertEqual (err .splitlines ().count (b'Unknown option: -a' ), 1 )
459460 self .assertEqual (b'' , out )
460461
461- @unittest .skipIf (script_helper . interpreter_requires_environment (),
462+ @unittest .skipIf (interpreter_requires_environment (),
462463 'Cannot run -I tests when PYTHON env vars are required.' )
463464 def test_isolatedmode (self ):
464465 self .verify_valid_flag ('-I' )
@@ -469,7 +470,7 @@ def test_isolatedmode(self):
469470 # dummyvar to prevent extraneous -E
470471 dummyvar = "" )
471472 self .assertEqual (out .strip (), b'1 1 1' )
472- with test . support .temp_cwd () as tmpdir :
473+ with support .temp_cwd () as tmpdir :
473474 fake = os .path .join (tmpdir , "uuid.py" )
474475 main = os .path .join (tmpdir , "main.py" )
475476 with open (fake , "w" ) as f :
@@ -506,6 +507,46 @@ def test_sys_flags_set(self):
506507 with self .subTest (envar_value = value ):
507508 assert_python_ok ('-c' , code , ** env_vars )
508509
510+ def run_xdev (self , code , check_exitcode = True ):
511+ env = dict (os .environ )
512+ env .pop ('PYTHONWARNINGS' , None )
513+ # Force malloc() to disable the debug hooks which are enabled
514+ # by default for Python compiled in debug mode
515+ env ['PYTHONMALLOC' ] = 'malloc'
516+
517+ args = (sys .executable , '-X' , 'dev' , '-c' , code )
518+ proc = subprocess .run (args ,
519+ stdout = subprocess .PIPE ,
520+ stderr = subprocess .STDOUT ,
521+ universal_newlines = True ,
522+ env = env )
523+ if check_exitcode :
524+ self .assertEqual (proc .returncode , 0 , proc )
525+ return proc .stdout .rstrip ()
526+
527+ def test_xdev (self ):
528+ out = self .run_xdev ("import sys; print(sys.warnoptions)" )
529+ self .assertEqual (out , "['default']" )
530+
531+ try :
532+ import _testcapi
533+ except ImportError :
534+ pass
535+ else :
536+ code = "import _testcapi; _testcapi.pymem_api_misuse()"
537+ with support .SuppressCrashReport ():
538+ out = self .run_xdev (code , check_exitcode = False )
539+ self .assertIn ("Debug memory block at address p=" , out )
540+
541+ try :
542+ import faulthandler
543+ except ImportError :
544+ pass
545+ else :
546+ code = "import faulthandler; print(faulthandler.is_enabled())"
547+ out = self .run_xdev (code )
548+ self .assertEqual (out , "True" )
549+
509550class IgnoreEnvironmentTest (unittest .TestCase ):
510551
511552 def run_ignoring_vars (self , predicate , ** env_vars ):
@@ -541,8 +582,8 @@ def test_sys_flags_not_set(self):
541582
542583
543584def test_main ():
544- test . support .run_unittest (CmdLineTest , IgnoreEnvironmentTest )
545- test . support .reap_children ()
585+ support .run_unittest (CmdLineTest , IgnoreEnvironmentTest )
586+ support .reap_children ()
546587
547588if __name__ == "__main__" :
548589 test_main ()
0 commit comments