File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -477,19 +477,25 @@ def setUp(self):
477477 self .directory = tempfile .mkdtemp ()
478478 self .source_path = os .path .join (self .directory , '_test.py' )
479479 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 "' )
482483
483484 def tearDown (self ):
484485 shutil .rmtree (self .directory )
485486
486487 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 )
493499
494500
495501class CommandLineTestsBase :
You can’t perform that action at this time.
0 commit comments