2
2
# Most tests are executed with environment variables ignored
3
3
# See test_cmd_line_script.py for testing of script execution
4
4
5
- import test .support , unittest
6
5
import os
7
- import sys
8
6
import subprocess
7
+ import sys
9
8
import tempfile
10
- from test .support import script_helper , is_android
9
+ import unittest
10
+ from test import support
11
11
from 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
13
14
)
14
15
15
16
# XXX (ncoghlan): Move to script_helper and make consistent with run_python
@@ -132,11 +133,11 @@ def test_run_code(self):
132
133
# All good if execution is successful
133
134
assert_python_ok ('-c' , 'pass' )
134
135
135
- @unittest .skipUnless (test . support .FS_NONASCII , 'need support.FS_NONASCII' )
136
+ @unittest .skipUnless (support .FS_NONASCII , 'need support.FS_NONASCII' )
136
137
def test_non_ascii (self ):
137
138
# Test handling of non-ascii data
138
139
command = ("assert(ord(%r) == %s)"
139
- % (test . support .FS_NONASCII , ord (test . support .FS_NONASCII )))
140
+ % (support .FS_NONASCII , ord (support .FS_NONASCII )))
140
141
assert_python_ok ('-c' , command )
141
142
142
143
# On Windows, pass bytes to subprocess doesn't test how Python decodes the
@@ -179,7 +180,7 @@ def test_undecodable_code(self):
179
180
raise AssertionError ("%a doesn't start with %a" % (stdout , pattern ))
180
181
181
182
@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' )
183
184
def test_osx_android_utf8 (self ):
184
185
def check_output (text ):
185
186
decoded = text .decode ('utf-8' , 'surrogateescape' )
@@ -385,7 +386,7 @@ def preexec():
385
386
stderr = subprocess .PIPE ,
386
387
preexec_fn = preexec )
387
388
out , err = p .communicate ()
388
- self .assertEqual (test . support .strip_python_stderr (err ), b'' )
389
+ self .assertEqual (support .strip_python_stderr (err ), b'' )
389
390
self .assertEqual (p .returncode , 42 )
390
391
391
392
def test_no_stdin (self ):
@@ -433,8 +434,8 @@ def test_del___main__(self):
433
434
# Issue #15001: PyRun_SimpleFileExFlags() did crash because it kept a
434
435
# borrowed reference to the dict of __main__ module and later modify
435
436
# 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 )
438
439
with open (filename , "w" ) as script :
439
440
print ("import sys" , file = script )
440
441
print ("del sys.modules['__main__']" , file = script )
@@ -458,7 +459,7 @@ def test_unknown_options(self):
458
459
self .assertEqual (err .splitlines ().count (b'Unknown option: -a' ), 1 )
459
460
self .assertEqual (b'' , out )
460
461
461
- @unittest .skipIf (script_helper . interpreter_requires_environment (),
462
+ @unittest .skipIf (interpreter_requires_environment (),
462
463
'Cannot run -I tests when PYTHON env vars are required.' )
463
464
def test_isolatedmode (self ):
464
465
self .verify_valid_flag ('-I' )
@@ -469,7 +470,7 @@ def test_isolatedmode(self):
469
470
# dummyvar to prevent extraneous -E
470
471
dummyvar = "" )
471
472
self .assertEqual (out .strip (), b'1 1 1' )
472
- with test . support .temp_cwd () as tmpdir :
473
+ with support .temp_cwd () as tmpdir :
473
474
fake = os .path .join (tmpdir , "uuid.py" )
474
475
main = os .path .join (tmpdir , "main.py" )
475
476
with open (fake , "w" ) as f :
@@ -506,6 +507,46 @@ def test_sys_flags_set(self):
506
507
with self .subTest (envar_value = value ):
507
508
assert_python_ok ('-c' , code , ** env_vars )
508
509
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
+
509
550
class IgnoreEnvironmentTest (unittest .TestCase ):
510
551
511
552
def run_ignoring_vars (self , predicate , ** env_vars ):
@@ -541,8 +582,8 @@ def test_sys_flags_not_set(self):
541
582
542
583
543
584
def test_main ():
544
- test . support .run_unittest (CmdLineTest , IgnoreEnvironmentTest )
545
- test . support .reap_children ()
585
+ support .run_unittest (CmdLineTest , IgnoreEnvironmentTest )
586
+ support .reap_children ()
546
587
547
588
if __name__ == "__main__" :
548
589
test_main ()
0 commit comments