File tree 1 file changed +14
-8
lines changed 1 file changed +14
-8
lines changed Original file line number Diff line number Diff line change @@ -477,19 +477,25 @@ def setUp(self):
477
477
self .directory = tempfile .mkdtemp ()
478
478
self .source_path = os .path .join (self .directory , '_test.py' )
479
479
with open (self .source_path , 'w' , encoding = 'utf-8' ) as file :
480
- file .write ('# -*- coding: utf-8 -*-\n ' )
481
- file .write ('print u"\u20ac "\n ' )
480
+ # Intentional syntax error: bytes can only contain
481
+ # ASCII literal characters.
482
+ file .write ('b"\u20ac "' )
482
483
483
484
def tearDown (self ):
484
485
shutil .rmtree (self .directory )
485
486
486
487
def test_error (self ):
487
- try :
488
- orig_stdout = sys .stdout
489
- sys .stdout = io .TextIOWrapper (io .BytesIO (),encoding = 'ascii' )
490
- compileall .compile_dir (self .directory )
491
- finally :
492
- sys .stdout = orig_stdout
488
+ buffer = io .TextIOWrapper (io .BytesIO (), encoding = 'ascii' )
489
+ with contextlib .redirect_stdout (buffer ):
490
+ compiled = compileall .compile_dir (self .directory )
491
+ self .assertFalse (compiled ) # should not be successful
492
+ buffer .seek (0 )
493
+ res = buffer .read ()
494
+ self .assertIn (
495
+ 'SyntaxError: bytes can only contain ASCII literal characters' ,
496
+ res ,
497
+ )
498
+ self .assertNotIn ('UnicodeEncodeError' , res )
493
499
494
500
495
501
class CommandLineTestsBase :
You can’t perform that action at this time.
0 commit comments