@@ -179,17 +179,25 @@ def ckmsg(src, msg, exception=SyntaxError):
179179 ckmsg (s , "inconsistent use of tabs and spaces in indentation" , TabError )
180180
181181 def testSyntaxErrorOffset (self ):
182- def check (src , lineno , offset ):
182+ def check (src , lineno , offset , encoding = 'utf-8' ):
183183 with self .assertRaises (SyntaxError ) as cm :
184184 compile (src , '<fragment>' , 'exec' )
185185 self .assertEqual (cm .exception .lineno , lineno )
186186 self .assertEqual (cm .exception .offset , offset )
187+ if cm .exception .text is not None :
188+ if not isinstance (src , str ):
189+ src = src .decode (encoding , 'replace' )
190+ line = src .split ('\n ' )[lineno - 1 ]
191+ self .assertEqual (cm .exception .text .rstrip ('\n ' ), line )
187192
188193 check ('def fact(x):\n \t return x!\n ' , 2 , 10 )
189194 check ('1 +\n ' , 1 , 4 )
190195 check ('def spam():\n print(1)\n print(2)' , 3 , 10 )
191196 check ('Python = "Python" +' , 1 , 20 )
192197 check ('Python = "\u1e54 \xfd \u0163 \u0125 \xf2 \xf1 " +' , 1 , 20 )
198+ check (b'# -*- coding: cp1251 -*-\n Python = "\xcf \xb3 \xf2 \xee \xed " +' ,
199+ 2 , 19 , encoding = 'cp1251' )
200+ check (b'Python = "\xcf \xb3 \xf2 \xee \xed " +' , 1 , 18 )
193201 check ('x = "a' , 1 , 7 )
194202 check ('lambda x: x = 2' , 1 , 1 )
195203
@@ -205,6 +213,10 @@ def check(src, lineno, offset):
205213 check ('0010 + 2' , 1 , 4 )
206214 check ('x = 32e-+4' , 1 , 8 )
207215 check ('x = 0o9' , 1 , 6 )
216+ check ('\u03b1 = 0xI' , 1 , 6 )
217+ check (b'\xce \xb1 = 0xI' , 1 , 6 )
218+ check (b'# -*- coding: iso8859-7 -*-\n \xe1 = 0xI' , 2 , 6 ,
219+ encoding = 'iso8859-7' )
208220
209221 # Errors thrown by symtable.c
210222 check ('x = [(yield i) for i in range(3)]' , 1 , 5 )
0 commit comments