@@ -1174,7 +1174,7 @@ def readline():
11741174
11751175 # skip the initial encoding token and the end tokens
11761176 tokens = list (_tokenize (readline (), encoding = 'utf-8' ))[:- 2 ]
1177- expected_tokens = [TokenInfo (3 , '"ЉЊЈЁЂ"' , (1 , 0 ), (1 , 7 ), '"ЉЊЈЁЂ"' )]
1177+ expected_tokens = [TokenInfo (3 , '"ЉЊЈЁЂ"' , (1 , 0 ), (1 , 7 ), '"ЉЊЈЁЂ"\n ' )]
11781178 self .assertEqual (tokens , expected_tokens ,
11791179 "bytes not decoded with encoding" )
11801180
@@ -1657,7 +1657,6 @@ def check_roundtrip(self, f):
16571657 code = f .encode ('utf-8' )
16581658 else :
16591659 code = f .read ()
1660- f .close ()
16611660 readline = iter (code .splitlines (keepends = True )).__next__
16621661 tokens5 = list (tokenize (readline ))
16631662 tokens2 = [tok [:2 ] for tok in tokens5 ]
@@ -1672,6 +1671,17 @@ def check_roundtrip(self, f):
16721671 tokens2_from5 = [tok [:2 ] for tok in tokenize (readline5 )]
16731672 self .assertEqual (tokens2_from5 , tokens2 )
16741673
1674+ def check_line_extraction (self , f ):
1675+ if isinstance (f , str ):
1676+ code = f .encode ('utf-8' )
1677+ else :
1678+ code = f .read ()
1679+ readline = iter (code .splitlines (keepends = True )).__next__
1680+ for tok in tokenize (readline ):
1681+ if tok .type in {ENCODING , ENDMARKER }:
1682+ continue
1683+ self .assertEqual (tok .string , tok .line [tok .start [1 ]: tok .end [1 ]])
1684+
16751685 def test_roundtrip (self ):
16761686 # There are some standard formatting practices that are easy to get right.
16771687
@@ -1768,6 +1778,7 @@ def test_random_files(self):
17681778 with open (testfile , 'rb' ) as f :
17691779 # with self.subTest(file=testfile):
17701780 self .check_roundtrip (f )
1781+ self .check_line_extraction (f )
17711782
17721783
17731784 def roundtrip (self , code ):
0 commit comments