File tree Expand file tree Collapse file tree 1 file changed +14
-8
lines changed
Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Original file line number Diff line number Diff line change @@ -502,19 +502,25 @@ def setUp(self):
502502 self .directory = tempfile .mkdtemp ()
503503 self .source_path = os .path .join (self .directory , '_test.py' )
504504 with open (self .source_path , 'w' , encoding = 'utf-8' ) as file :
505- file .write ('# -*- coding: utf-8 -*-\n ' )
506- file .write ('print u"\u20ac "\n ' )
505+ # Intentional syntax error: bytes can only contain
506+ # ASCII literal characters.
507+ file .write ('b"\u20ac "' )
507508
508509 def tearDown (self ):
509510 shutil .rmtree (self .directory )
510511
511512 def test_error (self ):
512- try :
513- orig_stdout = sys .stdout
514- sys .stdout = io .TextIOWrapper (io .BytesIO (),encoding = 'ascii' )
515- compileall .compile_dir (self .directory )
516- finally :
517- sys .stdout = orig_stdout
513+ buffer = io .TextIOWrapper (io .BytesIO (), encoding = 'ascii' )
514+ with contextlib .redirect_stdout (buffer ):
515+ compiled = compileall .compile_dir (self .directory )
516+ self .assertFalse (compiled ) # should not be successful
517+ buffer .seek (0 )
518+ res = buffer .read ()
519+ self .assertIn (
520+ 'SyntaxError: bytes can only contain ASCII literal characters' ,
521+ res ,
522+ )
523+ self .assertNotIn ('UnicodeEncodeError' , res )
518524
519525
520526class CommandLineTestsBase :
You can’t perform that action at this time.
0 commit comments