1
- import unittest , test . support
1
+ from test import support
2
2
from test .support .script_helper import assert_python_ok , assert_python_failure
3
- import sys , io , os
3
+ import builtins
4
+ import codecs
5
+ import gc
6
+ import io
7
+ import locale
8
+ import operator
9
+ import os
4
10
import struct
5
11
import subprocess
12
+ import sys
13
+ import sysconfig
14
+ import test .support
6
15
import textwrap
16
+ import unittest
7
17
import warnings
8
- import operator
9
- import codecs
10
- import gc
11
- import sysconfig
12
- import locale
18
+
13
19
14
20
# count the number of test runs, used to create unique
15
21
# strings to intern in test_intern()
16
- numruns = 0
22
+ INTERN_NUMRUNS = 0
17
23
18
24
19
- class SysModuleTest (unittest .TestCase ):
25
+ class DisplayHookTest (unittest .TestCase ):
20
26
21
- def setUp (self ):
22
- self .orig_stdout = sys .stdout
23
- self .orig_stderr = sys .stderr
24
- self .orig_displayhook = sys .displayhook
27
+ def test_original_displayhook (self ):
28
+ dh = sys .__displayhook__
25
29
26
- def tearDown (self ):
27
- sys .stdout = self .orig_stdout
28
- sys .stderr = self .orig_stderr
29
- sys .displayhook = self .orig_displayhook
30
- test .support .reap_children ()
30
+ with support .captured_stdout () as out :
31
+ dh (42 )
31
32
32
- def test_original_displayhook (self ):
33
- import builtins
34
- out = io .StringIO ()
35
- sys .stdout = out
33
+ self .assertEqual (out .getvalue (), "42\n " )
34
+ self .assertEqual (builtins ._ , 42 )
36
35
37
- dh = sys . __displayhook__
36
+ del builtins . _
38
37
39
- self .assertRaises (TypeError , dh )
40
- if hasattr (builtins , "_" ):
41
- del builtins ._
38
+ with support .captured_stdout () as out :
39
+ dh (None )
42
40
43
- dh (None )
44
41
self .assertEqual (out .getvalue (), "" )
45
42
self .assertTrue (not hasattr (builtins , "_" ))
46
- dh (42 )
47
- self .assertEqual (out .getvalue (), "42\n " )
48
- self .assertEqual (builtins ._ , 42 )
49
43
50
- del sys .stdout
51
- self .assertRaises (RuntimeError , dh , 42 )
44
+ # sys.displayhook() requires arguments
45
+ self .assertRaises (TypeError , dh )
46
+
47
+ stdout = sys .stdout
48
+ try :
49
+ del sys .stdout
50
+ self .assertRaises (RuntimeError , dh , 42 )
51
+ finally :
52
+ sys .stdout = stdout
52
53
53
54
def test_lost_displayhook (self ):
54
- del sys .displayhook
55
- code = compile ("42" , "<string>" , "single" )
56
- self .assertRaises (RuntimeError , eval , code )
55
+ displayhook = sys .displayhook
56
+ try :
57
+ del sys .displayhook
58
+ code = compile ("42" , "<string>" , "single" )
59
+ self .assertRaises (RuntimeError , eval , code )
60
+ finally :
61
+ sys .displayhook = displayhook
57
62
58
63
def test_custom_displayhook (self ):
59
64
def baddisplayhook (obj ):
60
65
raise ValueError
61
- sys .displayhook = baddisplayhook
62
- code = compile ("42" , "<string>" , "single" )
63
- self .assertRaises (ValueError , eval , code )
64
66
65
- def test_original_excepthook ( self ):
66
- err = io . StringIO ( )
67
- sys . stderr = err
67
+ with support . swap_attr ( sys , 'displayhook' , baddisplayhook ):
68
+ code = compile ( "42" , "<string>" , "single" )
69
+ self . assertRaises ( ValueError , eval , code )
68
70
69
- eh = sys .__excepthook__
70
71
71
- self .assertRaises (TypeError , eh )
72
+ class ExceptHookTest (unittest .TestCase ):
73
+
74
+ def test_original_excepthook (self ):
72
75
try :
73
76
raise ValueError (42 )
74
77
except ValueError as exc :
75
- eh (* sys .exc_info ())
78
+ with support .captured_stderr () as err :
79
+ sys .__excepthook__ (* sys .exc_info ())
76
80
77
81
self .assertTrue (err .getvalue ().endswith ("ValueError: 42\n " ))
78
82
83
+ self .assertRaises (TypeError , sys .__excepthook__ )
84
+
85
+ def test_excepthook_bytes_filename (self ):
86
+ # bpo-37467: sys.excepthook() must not crash if a filename
87
+ # is a bytes string
88
+ with warnings .catch_warnings ():
89
+ warnings .simplefilter ('ignore' , BytesWarning )
90
+
91
+ try :
92
+ raise SyntaxError ("msg" , (b"bytes_filename" , 123 , 0 , "text" ))
93
+ except SyntaxError as exc :
94
+ with support .captured_stderr () as err :
95
+ sys .__excepthook__ (* sys .exc_info ())
96
+
97
+ err = err .getvalue ()
98
+ self .assertIn (""" File "b'bytes_filename'", line 123\n """ , err )
99
+ self .assertIn (""" text\n """ , err )
100
+ self .assertTrue (err .endswith ("SyntaxError: msg\n " ))
101
+
79
102
def test_excepthook (self ):
80
103
with test .support .captured_output ("stderr" ) as stderr :
81
104
sys .excepthook (1 , '1' , 1 )
@@ -85,6 +108,12 @@ def test_excepthook(self):
85
108
# FIXME: testing the code for a lost or replaced excepthook in
86
109
# Python/pythonrun.c::PyErr_PrintEx() is tricky.
87
110
111
+
112
+ class SysModuleTest (unittest .TestCase ):
113
+
114
+ def tearDown (self ):
115
+ test .support .reap_children ()
116
+
88
117
def test_exit (self ):
89
118
# call with two arguments
90
119
self .assertRaises (TypeError , sys .exit , 42 , 42 )
@@ -492,10 +521,10 @@ def test_43581(self):
492
521
self .assertEqual (sys .__stdout__ .encoding , sys .__stderr__ .encoding )
493
522
494
523
def test_intern (self ):
495
- global numruns
496
- numruns += 1
524
+ global INTERN_NUMRUNS
525
+ INTERN_NUMRUNS += 1
497
526
self .assertRaises (TypeError , sys .intern )
498
- s = "never interned before" + str (numruns )
527
+ s = "never interned before" + str (INTERN_NUMRUNS )
499
528
self .assertTrue (sys .intern (s ) is s )
500
529
s2 = s .swapcase ().swapcase ()
501
530
self .assertTrue (sys .intern (s2 ) is s )
0 commit comments