@@ -42,14 +42,35 @@ def positive_float(value):
4242
4343
4444def exception_result (exc , * , locations = True ):
45+ message = getattr (exc , "msg" , None )
46+ token_location = None
47+ if message is None and isinstance (exc , tokenize .TokenError ):
48+ if (
49+ len (exc .args ) == 2
50+ and isinstance (exc .args [1 ], tuple )
51+ and len (exc .args [1 ]) == 2
52+ ):
53+ message , token_location = exc .args
54+ elif exc .args :
55+ message = exc .args [0 ]
56+ if message is None :
57+ message = str (exc )
4558 result = {
4659 "type" : type (exc ).__name__ ,
47- "message" : getattr ( exc , "msg" , str ( exc )) ,
60+ "message" : message ,
4861 }
4962 if locations :
5063 result .update ({
51- "lineno" : getattr (exc , "lineno" , None ),
52- "offset" : getattr (exc , "offset" , None ),
64+ "lineno" : (
65+ token_location [0 ]
66+ if token_location is not None
67+ else getattr (exc , "lineno" , None )
68+ ),
69+ "offset" : (
70+ token_location [1 ]
71+ if token_location is not None
72+ else getattr (exc , "offset" , None )
73+ ),
5374 "end_lineno" : getattr (exc , "end_lineno" , None ),
5475 "end_offset" : getattr (exc , "end_offset" , None ),
5576 "text" : getattr (exc , "text" , None ),
@@ -70,7 +91,9 @@ def warning_results(caught):
7091 ]
7192
7293
73- def tokenizer_iter_result (data , extra_tokens , * , encoded = False , batched = False ):
94+ def tokenizer_iter_result (
95+ data , extra_tokens , * , encoded = False , batched = False , semantic = False
96+ ):
7497 tokens = []
7598 with warnings .catch_warnings (record = True ) as caught :
7699 warnings .simplefilter ("always" )
@@ -102,7 +125,7 @@ def readline():
102125 iterator = _tokenize .TokenizerIter (readline , ** kwargs )
103126 tokens .extend (iterator )
104127 except Exception as exc :
105- error = exception_result (exc )
128+ error = exception_result (exc , locations = not semantic )
106129 else :
107130 error = None
108131 return {
@@ -112,14 +135,14 @@ def readline():
112135 }
113136
114137
115- def tokenize_result (data ):
138+ def tokenize_result (data , semantic ):
116139 tokens = []
117140 with warnings .catch_warnings (record = True ) as caught :
118141 warnings .simplefilter ("always" )
119142 try :
120143 tokens .extend (tokenize .tokenize (io .BytesIO (data ).readline ))
121144 except Exception as exc :
122- error = exception_result (exc )
145+ error = exception_result (exc , locations = not semantic )
123146 else :
124147 error = None
125148 return {
@@ -180,26 +203,34 @@ def case_result(case, batched, semantic):
180203 data = Path (case ["path" ]).read_bytes ()
181204 else :
182205 data = base64 .b64decode (case ["data" ], validate = True )
183- parser_tokens = tokenizer_iter_result (data , False )
184- extra_tokens = tokenizer_iter_result (data , True )
185- encoded_tokens = tokenizer_iter_result (data , False , encoded = True )
206+ parser_tokens = tokenizer_iter_result (data , False , semantic = semantic )
207+ extra_tokens = tokenizer_iter_result (data , True , semantic = semantic )
208+ encoded_tokens = tokenizer_iter_result (
209+ data , False , encoded = True , semantic = semantic
210+ )
186211 return {
187212 "name" : case ["name" ],
188213 "parser" : compile_result (data , semantic ),
189- "tokenize" : tokenize_result (data ),
214+ "tokenize" : tokenize_result (data , semantic ),
190215 "parser_tokens" : parser_tokens ,
191216 "extra_tokens" : extra_tokens ,
192217 "encoded_tokens" : encoded_tokens ,
193218 "batched_tokens" : (
194- tokenizer_iter_result (data , False , batched = True )
219+ tokenizer_iter_result (
220+ data , False , batched = True , semantic = semantic
221+ )
195222 if batched else parser_tokens
196223 ),
197224 "batched_extra_tokens" : (
198- tokenizer_iter_result (data , True , batched = True )
225+ tokenizer_iter_result (
226+ data , True , batched = True , semantic = semantic
227+ )
199228 if batched else extra_tokens
200229 ),
201230 "batched_encoded_tokens" : (
202- tokenizer_iter_result (data , False , encoded = True , batched = True )
231+ tokenizer_iter_result (
232+ data , False , encoded = True , batched = True , semantic = semantic
233+ )
203234 if batched else encoded_tokens
204235 ),
205236 }
0 commit comments