55import ast
66import io
77import sys
8- import traceback
98
109import testtools
1110from testtools .compat import (
12- _b ,
13- reraise ,
1411 text_repr ,
1512 unicode_output_stream ,
1613)
@@ -45,28 +42,28 @@ def test_no_encoding_becomes_ascii(self):
4542 """A stream with no encoding attribute gets ascii/replace strings"""
4643 sout = _FakeOutputStream ()
4744 unicode_output_stream (sout ).write (self .uni )
48- self .assertEqual ([_b ( "pa???n" ) ], sout .writelog )
45+ self .assertEqual ([b "pa???n" ], sout .writelog )
4946
5047 def test_encoding_as_none_becomes_ascii (self ):
5148 """A stream with encoding value of None gets ascii/replace strings"""
5249 sout = _FakeOutputStream ()
5350 sout .encoding = None
5451 unicode_output_stream (sout ).write (self .uni )
55- self .assertEqual ([_b ( "pa???n" ) ], sout .writelog )
52+ self .assertEqual ([b "pa???n" ], sout .writelog )
5653
5754 def test_bogus_encoding_becomes_ascii (self ):
5855 """A stream with a bogus encoding gets ascii/replace strings"""
5956 sout = _FakeOutputStream ()
6057 sout .encoding = "bogus"
6158 unicode_output_stream (sout ).write (self .uni )
62- self .assertEqual ([_b ( "pa???n" ) ], sout .writelog )
59+ self .assertEqual ([b "pa???n" ], sout .writelog )
6360
6461 def test_partial_encoding_replace (self ):
6562 """A string which can be partly encoded correctly should be"""
6663 sout = _FakeOutputStream ()
6764 sout .encoding = "iso-8859-7"
6865 unicode_output_stream (sout ).write (self .uni )
69- self .assertEqual ([_b ( "pa?\xe8 ?n" ) ], sout .writelog )
66+ self .assertEqual ([b "pa?\xe8 ?n" ], sout .writelog )
7067
7168 def test_stringio (self ):
7269 """A StringIO object should maybe get an ascii native str type"""
@@ -126,11 +123,11 @@ class TestTextRepr(testtools.TestCase):
126123
127124 # Bytes with the high bit set should always be escaped
128125 bytes_examples = (
129- (_b ( "\x80 " ) , "'\\ x80'" , "'''\\ \n \\ x80'''" ),
130- (_b ( "\xa0 " ) , "'\\ xa0'" , "'''\\ \n \\ xa0'''" ),
131- (_b ( "\xc0 " ) , "'\\ xc0'" , "'''\\ \n \\ xc0'''" ),
132- (_b ( "\xff " ) , "'\\ xff'" , "'''\\ \n \\ xff'''" ),
133- (_b ( "\xc2 \xa7 " ) , "'\\ xc2\\ xa7'" , "'''\\ \n \\ xc2\\ xa7'''" ),
126+ (b "\x80 " , "'\\ x80'" , "'''\\ \n \\ x80'''" ),
127+ (b "\xa0 " , "'\\ xa0'" , "'''\\ \n \\ xa0'''" ),
128+ (b "\xc0 " , "'\\ xc0'" , "'''\\ \n \\ xc0'''" ),
129+ (b "\xff " , "'\\ xff'" , "'''\\ \n \\ xff'''" ),
130+ (b "\xc2 \xa7 " , "'\\ xc2\\ xa7'" , "'''\\ \n \\ xc2\\ xa7'''" ),
134131 )
135132
136133 # Unicode doesn't escape printable characters as per the Python 3 model
@@ -153,12 +150,12 @@ class TestTextRepr(testtools.TestCase):
153150 # Unprintable general categories not fully tested: Cc, Cf, Co, Cn, Zs
154151 )
155152
156- b_prefix = repr (_b ( "" ) )[:- 2 ]
153+ b_prefix = repr (b"" )[:- 2 ]
157154 u_prefix = repr ("" )[:- 2 ]
158155
159156 def test_ascii_examples_oneline_bytes (self ):
160157 for s , expected , _ in self .ascii_examples :
161- b = _b ( s )
158+ b = s . encode ( "utf-8" )
162159 actual = text_repr (b , multiline = False )
163160 # Add self.assertIsInstance check?
164161 self .assertEqual (actual , self .b_prefix + expected )
@@ -173,7 +170,7 @@ def test_ascii_examples_oneline_unicode(self):
173170
174171 def test_ascii_examples_multiline_bytes (self ):
175172 for s , _ , expected in self .ascii_examples :
176- b = _b ( s )
173+ b = s . encode ( "utf-8" )
177174 actual = text_repr (b , multiline = True )
178175 self .assertEqual (actual , self .b_prefix + expected )
179176 self .assertEqual (ast .literal_eval (actual ), b )
@@ -187,7 +184,7 @@ def test_ascii_examples_multiline_unicode(self):
187184 def test_ascii_examples_defaultline_bytes (self ):
188185 for s , one , multi in self .ascii_examples :
189186 expected = ("\n " in s and multi ) or one
190- self .assertEqual (text_repr (_b ( s )), self .b_prefix + expected )
187+ self .assertEqual (text_repr (s . encode ( "utf-8" )), self .b_prefix + expected )
191188
192189 def test_ascii_examples_defaultline_unicode (self ):
193190 for s , one , multi in self .ascii_examples :
@@ -219,43 +216,6 @@ def test_unicode_examples_multiline(self):
219216 self .assertEqual (ast .literal_eval (actual ), u )
220217
221218
222- class TestReraise (testtools .TestCase ):
223- """Tests for trivial reraise wrapper needed for Python 2/3 changes"""
224-
225- def test_exc_info (self ):
226- """After reraise exc_info matches plus some extra traceback"""
227- try :
228- raise ValueError ("Bad value" )
229- except ValueError :
230- _exc_info = sys .exc_info ()
231- try :
232- reraise (* _exc_info )
233- except ValueError :
234- _new_exc_info = sys .exc_info ()
235- self .assertIs (_exc_info [0 ], _new_exc_info [0 ])
236- self .assertIs (_exc_info [1 ], _new_exc_info [1 ])
237- expected_tb = traceback .extract_tb (_exc_info [2 ])
238- self .assertEqual (
239- expected_tb , traceback .extract_tb (_new_exc_info [2 ])[- len (expected_tb ) :]
240- )
241-
242- def test_custom_exception_no_args (self ):
243- """Reraising does not require args attribute to contain params"""
244-
245- class CustomException (Exception ):
246- """Exception that expects and sets attrs but not args"""
247-
248- def __init__ (self , value ):
249- Exception .__init__ (self )
250- self .value = value
251-
252- try :
253- raise CustomException ("Some value" )
254- except CustomException :
255- _exc_info = sys .exc_info ()
256- self .assertRaises (CustomException , reraise , * _exc_info )
257-
258-
259219def test_suite ():
260220 from unittest import TestLoader
261221
0 commit comments