11#include "parts.h"
22
3- #include "../../Parser/tokenizer/cursor .h"
3+ #include "../../Parser/tokenizer/source .h"
44
55static int
66check (int condition , const char * message )
@@ -34,16 +34,6 @@ check_line_view(const _PyTok_SourceText *source, Py_ssize_t lineno,
3434 "wrong source line view" );
3535}
3636
37- static int
38- same_cursor (const _PyTok_Cursor * left , const _PyTok_Cursor * right )
39- {
40- return left -> source == right -> source &&
41- left -> pos == right -> pos &&
42- left -> line_start == right -> line_start &&
43- left -> line_end == right -> line_end &&
44- left -> lineno == right -> lineno ;
45- }
46-
4737static PyObject *
4838test_tokenizer_source (PyObject * Py_UNUSED (module ),
4939 PyObject * Py_UNUSED (args ))
@@ -55,34 +45,24 @@ test_tokenizer_source(PyObject *Py_UNUSED(module),
5545 goto error ;
5646 }
5747
58- _PyTok_Loc loc ;
59- _PyTok_Line line ;
60- if (check (_PyTok_SourceLocation (
61- & source , 0 , _PYTOK_AFFINITY_RIGHT , & loc ) == 0 ,
62- "cannot locate empty source" ) < 0 ||
63- check (loc .lineno == 1 && loc .byte_col == 0 ,
64- "wrong empty source location" ) < 0 ||
65- check (_PyTok_SourceLine (& source , 1 , & line ) == 0 ,
66- "cannot find empty source line" ) < 0 ||
67- check (line .start == 0 && line .end == 0 ,
68- "wrong empty source line" ) < 0 ||
69- check_system_error (
70- _PyTok_SourceAppendLine (& source , "" , 0 , 0 ) < 0 ,
71- "accepted empty source line" ) < 0 ||
48+ if (check_system_error (
49+ _PyTok_SourceAppendLine (& source , "" , 0 , 0 ) < 0 ,
50+ "accepted empty source line" ) < 0 ||
7251 check_system_error (
7352 _PyTok_SourceAppendLine (& source , "a\nb\n" , 4 , 0 ) < 0 ,
7453 "accepted multiple source lines" ) < 0 ||
7554 check_system_error (
7655 _PyTok_SourceAppendLine (& source , "a" , 1 , 1 ) < 0 ,
77- "accepted missing implicit newline" ) < 0 ) {
78- goto error ;
79- }
80-
81- if (check (_PyTok_SourceAppendLine (& source , "alpha\n" , 6 , 0 ) == 0 ,
82- "wrong first source offset" ) < 0 ||
56+ "accepted missing implicit newline" ) < 0 ||
57+ check (_PyTok_SourceAppendLine (
58+ & source , "alpha\n" , 6 , 0 ) == 0 ,
59+ "wrong first source offset" ) < 0 ||
8360 check (_PyTok_SourceAppendLine (
8461 & source , "\xce\xb2\n" , 3 , 1 ) == 6 ,
85- "wrong second source offset" ) < 0 ) {
62+ "wrong second source offset" ) < 0 ||
63+ check (!_PyTok_SourceLineIsImplicit (& source , 1 ) &&
64+ _PyTok_SourceLineIsImplicit (& source , 2 ),
65+ "wrong implicit newline flags" ) < 0 ) {
8666 goto error ;
8767 }
8868
@@ -94,279 +74,24 @@ test_tokenizer_source(PyObject *Py_UNUSED(module),
9474 goto error ;
9575 }
9676
97- if (check (_PyTok_SourceAppendLine (& source , "nul\0x\n" , 6 , 0 ) == 9 ,
98- "wrong third source offset" ) < 0 ) {
99- goto error ;
100- }
101-
102- int marker_line = 257 ;
103- int final_line = 300 ;
104- _PyTok_Off marker_start = -1 ;
105- for (int lineno = 4 ; lineno <= final_line ; lineno ++ ) {
106- const char * text = lineno == marker_line ? "marker\n" : "x\n" ;
107- Py_ssize_t len = (Py_ssize_t )strlen (text );
108- _PyTok_Off start = _PyTok_SourceAppendLine (
109- & source , text , len , lineno == final_line );
110- if (start < 0 ) {
111- goto error ;
112- }
113- if (lineno == marker_line ) {
114- marker_start = start ;
115- }
116- }
117-
118- if (check (source .nlines == final_line , "wrong source line count" ) < 0 ||
119- check (_PyTok_SourceLine (& source , marker_line , & line ) == 0 ,
120- "cannot find late source line" ) < 0 ||
121- check (line .start == marker_start &&
122- line .end == marker_start + 7 ,
123- "wrong late source line" ) < 0 ||
124- check (!line .implicit_newline && !line .contains_nul ,
125- "wrong late source flags" ) < 0 ||
126- check (_PyTok_SourceLine (& source , 2 , & line ) == 0 ,
127- "cannot find second source line" ) < 0 ||
128- check (line .start == 6 && line .end == 9 &&
129- line .implicit_newline && !line .contains_nul ,
130- "wrong second source line" ) < 0 ||
131- check (!_PyTok_SourceLineIsImplicit (& source , 1 ) &&
132- _PyTok_SourceLineIsImplicit (& source , 2 ),
133- "wrong early implicit newline flags" ) < 0 ||
134- check (_PyTok_SourceLine (& source , 3 , & line ) == 0 ,
135- "cannot find third source line" ) < 0 ||
136- check (line .contains_nul , "missing null byte flag" ) < 0 ||
137- check (_PyTok_SourceLine (& source , final_line , & line ) == 0 ,
138- "cannot find final source line" ) < 0 ||
139- check (line .implicit_newline &&
140- _PyTok_SourceLineIsImplicit (& source , final_line ),
141- "missing late implicit newline flag" ) < 0 ) {
142- goto error ;
143- }
144-
145- Py_ssize_t view_len ;
146- const char * view = _PyTok_SourceSpanView (
147- & source , _PyTok_SpanFromBounds (6 , 8 ), & view_len );
148- if (check (view != NULL && view_len == 2 &&
149- memcmp (view , "\xce\xb2" , 2 ) == 0 ,
150- "wrong source span view" ) < 0 ||
151- check (_PyTok_SourceLocation (
152- & source , marker_start ,
153- _PYTOK_AFFINITY_LEFT , & loc ) == 0 ,
154- "cannot locate left line boundary" ) < 0 ||
155- check (loc .lineno == marker_line - 1 && loc .byte_col == 2 ,
156- "wrong left boundary location" ) < 0 ||
157- check (_PyTok_SourceLocation (
158- & source , marker_start ,
159- _PYTOK_AFFINITY_RIGHT , & loc ) == 0 ,
160- "cannot locate right line boundary" ) < 0 ||
161- check (loc .lineno == marker_line && loc .byte_col == 0 ,
162- "wrong right boundary location" ) < 0 ||
163- check (_PyTok_SourceLocation (
164- & source , marker_start + 1 ,
165- _PYTOK_AFFINITY_RIGHT , & loc ) == 0 ,
166- "cannot locate late source byte" ) < 0 ||
167- check (loc .lineno == marker_line && loc .byte_col == 1 ,
168- "wrong late source location" ) < 0 ) {
169- goto error ;
170- }
171-
172- if (check (_PyTok_SourceLocation (
173- & source , source .len , _PYTOK_AFFINITY_LEFT , & loc ) == 0 ,
174- "cannot locate left EOF" ) < 0 ||
175- check (loc .lineno == final_line && loc .byte_col == 2 ,
176- "wrong left EOF location" ) < 0 ||
177- check (_PyTok_SourceLocation (
178- & source , source .len ,
179- _PYTOK_AFFINITY_RIGHT , & loc ) == 0 ,
180- "cannot locate right EOF" ) < 0 ||
181- check (loc .lineno == final_line + 1 && loc .byte_col == 0 ,
182- "wrong right EOF location" ) < 0 ||
183- check (_PyTok_SourceLine (& source , final_line + 1 , & line ) == 0 ,
184- "cannot find virtual EOF line" ) < 0 ||
185- check (line .start == source .len && line .end == source .len ,
186- "wrong virtual EOF line" ) < 0 ||
187- check (!_PyTok_SourceLineIsImplicit (& source , 0 ) &&
188- !_PyTok_SourceLineIsImplicit (
189- & source , final_line + 1 ),
190- "virtual or invalid line is implicit" ) < 0 ) {
191- goto error ;
192- }
193-
194- view = _PyTok_SourceSpanView (
195- & source , _PyTok_SpanFromBounds (0 , source .len + 1 ), & view_len );
196- if (check_system_error (view == NULL , "accepted invalid source span" ) < 0 ||
197- check_system_error (
198- _PyTok_SourceLocation (
199- & source , source .len + 1 ,
200- _PYTOK_AFFINITY_RIGHT , & loc ) < 0 ,
201- "accepted invalid source offset" ) < 0 ||
202- check_system_error (
203- _PyTok_SourceLine (& source , final_line + 2 , & line ) < 0 ,
204- "accepted invalid source line" ) < 0 ) {
77+ if (check (source .len == 9 &&
78+ memcmp (source .bytes , "alpha\n\xce\xb2\n" , 10 ) == 0 ,
79+ "wrong source contents" ) < 0 ) {
20580 goto error ;
20681 }
20782
20883 _PyTok_SourceClear (& source );
209- _PyTok_SourceInit (& source );
21084 if (_PyTok_SourceAppendLine (& source , "tail" , 4 , 0 ) < 0 ||
21185 check_system_error (
21286 _PyTok_SourceAppendLine (& source , "x\n" , 2 , 0 ) < 0 ,
213- "appended after unterminated source line" ) < 0 ||
214- check (_PyTok_SourceLocation (
215- & source , source .len ,
216- _PYTOK_AFFINITY_RIGHT , & loc ) == 0 ,
217- "cannot locate unterminated EOF" ) < 0 ||
218- check (loc .lineno == 1 && loc .byte_col == 4 ,
219- "wrong unterminated EOF location" ) < 0 ) {
87+ "appended after unterminated source line" ) < 0 ) {
22088 goto error ;
22189 }
222-
22390 if (check_line_view (& source , 1 , "tail" ) < 0 ||
22491 check_line_view (& source , PY_SSIZE_T_MAX , "tail" ) < 0 ) {
22592 goto error ;
22693 }
22794
228- _PyTok_SourceDiscard (& source );
229- if (check (_PyTok_SourceAppendLine (& source , "a\n" , 2 , 0 ) == 4 ,
230- "wrong retained source offset" ) < 0 ||
231- _PyTok_SourceLine (& source , 1 , & line ) < 0 ||
232- check (line .start == 4 && line .end == 6 ,
233- "wrong retained source line" ) < 0 ||
234- _PyTok_SourceLocation (
235- & source , 4 , _PYTOK_AFFINITY_LEFT , & loc ) < 0 ||
236- check (loc .lineno == 1 && loc .byte_col == 0 ,
237- "wrong retained source location" ) < 0 ) {
238- goto error ;
239- }
240- view = _PyTok_SourceSpanView (
241- & source , _PyTok_SpanFromBounds (4 , 5 ), & view_len );
242- if (check (view != NULL && view_len == 1 && view [0 ] == 'a' ,
243- "wrong retained source span" ) < 0 ||
244- check_system_error (_PyTok_SourceSpanView (
245- & source , _PyTok_SpanFromBounds (0 , 1 ), & view_len ) == NULL ,
246- "accepted discarded source span" ) < 0 ) {
247- goto error ;
248- }
249-
250- _PyTok_SourceClear (& source );
251- Py_RETURN_NONE ;
252-
253- error :
254- _PyTok_SourceClear (& source );
255- return NULL ;
256- }
257-
258- static PyObject *
259- test_tokenizer_cursor (PyObject * Py_UNUSED (module ),
260- PyObject * Py_UNUSED (args ))
261- {
262- _PyTok_SourceText source ;
263- _PyTok_SourceInit (& source );
264- if (_PyTok_SourceAppendLine (& source , "ab\n" , 3 , 0 ) < 0 ||
265- _PyTok_SourceAppendLine (& source , "cd\n" , 3 , 0 ) < 0 ) {
266- goto error ;
267- }
268-
269- _PyTok_Cursor cursor ;
270- _PyTok_CursorInit (& cursor , & source );
271- if (_PyTok_CursorSetOffset (& cursor , source .len ) < 0 ||
272- check (cursor .lineno == 3 && cursor .pos == source .len ,
273- "wrong cursor at virtual EOF" ) < 0 ||
274- _PyTok_CursorSetLine (& cursor , 1 ) < 0 ) {
275- goto error ;
276- }
277-
278- char large [BUFSIZ + 1 ];
279- memset (large , 'z' , sizeof (large ));
280- large [sizeof (large ) - 1 ] = '\n' ;
281- if (_PyTok_SourceAppendLine (& source , large , sizeof (large ), 0 ) < 0 ) {
282- goto error ;
283- }
284-
285- if (check (_PyTok_CursorPeek (& cursor , 0 ) == 'a' ,
286- "wrong cursor peek after relocation" ) < 0 ||
287- check (_PyTok_CursorPeek (& cursor , 1 ) == 'b' ,
288- "wrong distant cursor peek" ) < 0 ||
289- check (_PyTok_CursorAdvance (& cursor ) == 'a' ,
290- "wrong first cursor byte" ) < 0 ||
291- check (_PyTok_CursorAdvance (& cursor ) == 'b' ,
292- "wrong second cursor byte" ) < 0 ||
293- check (_PyTok_CursorAdvance (& cursor ) == '\n' ,
294- "wrong final cursor byte" ) < 0 ||
295- check (_PyTok_CursorAdvance (& cursor ) == EOF ,
296- "cursor advanced past line" ) < 0 ||
297- check (_PyTok_CursorSetOffset (& cursor , 2 ) == 0 ,
298- "cannot seek cursor offset" ) < 0 ||
299- check (_PyTok_CursorAdvance (& cursor ) == '\n' ,
300- "wrong cursor byte after seek" ) < 0 ||
301- check (_PyTok_CursorSetOffset (& cursor , 3 ) == 0 ,
302- "cannot seek line boundary" ) < 0 ||
303- check (cursor .lineno == 2 && cursor .line_start == 3 &&
304- _PyTok_CursorAdvance (& cursor ) == 'c' ,
305- "wrong cursor at line boundary" ) < 0 ||
306- check (_PyTok_CursorSetLine (& cursor , 3 ) == 0 ,
307- "cannot advance cursor to final line" ) < 0 ||
308- check (cursor .line_start == 6 &&
309- _PyTok_CursorAdvance (& cursor ) == 'z' ,
310- "wrong cursor byte on final line" ) < 0 ) {
311- goto error ;
312- }
313-
314- _PyTok_Cursor saved = cursor ;
315- if (check_system_error (
316- _PyTok_CursorSetOffset (& cursor , source .len + 1 ) < 0 ,
317- "accepted invalid cursor offset" ) < 0 ||
318- check (same_cursor (& cursor , & saved ),
319- "invalid offset changed cursor" ) < 0 ||
320- check_system_error (
321- _PyTok_CursorSetLine (& cursor , source .nlines + 2 ) < 0 ,
322- "accepted invalid cursor line" ) < 0 ||
323- check (same_cursor (& cursor , & saved ),
324- "invalid line changed cursor" ) < 0 ||
325- check (_PyTok_CursorSetOffset (& cursor , source .len ) == 0 ,
326- "cannot set cursor to EOF" ) < 0 ||
327- check (cursor .lineno == 4 && cursor .pos == source .len ,
328- "wrong cursor at EOF" ) < 0 ) {
329- goto error ;
330- }
331-
332- #if SIZEOF_VOID_P > 4
333- char byte = 0 ;
334- _PyTok_SourceText huge_source = {
335- .bytes = & byte ,
336- .len = (_PyTok_Off )INT_MAX + 1 ,
337- };
338- _PyTok_Cursor huge_cursor = {
339- .source = & huge_source ,
340- .pos = INT_MAX ,
341- .line_end = (_PyTok_Off )INT_MAX + 1 ,
342- .lineno = 1 ,
343- };
344- if (check (_PyTok_CursorAdvance (& huge_cursor ) == EOF &&
345- huge_cursor .pos == INT_MAX ,
346- "cursor advanced past maximum column" ) < 0 ) {
347- goto error ;
348- }
349- #endif
350-
351- _PyTok_Off base = source .len ;
352- _PyTok_SourceDiscard (& source );
353- if (_PyTok_SourceAppendLine (& source , "ab\n" , 3 , 0 ) < 0 ||
354- _PyTok_SourceAppendLine (& source , "cd" , 2 , 0 ) < 0 ) {
355- goto error ;
356- }
357- _PyTok_CursorInit (& cursor , & source );
358- if (_PyTok_CursorSetLine (& cursor , 1 ) < 0 ||
359- check (cursor .pos == base && _PyTok_CursorPeek (& cursor , 1 ) == 'b' ,
360- "wrong retained cursor line" ) < 0 ||
361- _PyTok_CursorSetLine (& cursor , 2 ) < 0 ||
362- check (_PyTok_CursorAdvance (& cursor ) == 'c' ,
363- "wrong retained cursor byte" ) < 0 ||
364- _PyTok_CursorSetOffset (& cursor , base + 5 ) < 0 ||
365- check (cursor .lineno == 2 && _PyTok_CursorAdvance (& cursor ) == EOF ,
366- "wrong retained cursor EOF" ) < 0 ) {
367- goto error ;
368- }
369-
37095 _PyTok_SourceClear (& source );
37196 Py_RETURN_NONE ;
37297
@@ -440,7 +165,6 @@ test_tokenizer_source_discard(PyObject *Py_UNUSED(module),
440165
441166static PyMethodDef test_methods [] = {
442167 {"test_tokenizer_source" , test_tokenizer_source , METH_NOARGS },
443- {"test_tokenizer_cursor" , test_tokenizer_cursor , METH_NOARGS },
444168 {"test_tokenizer_source_discard" , test_tokenizer_source_discard , METH_NOARGS },
445169 {NULL },
446170};
0 commit comments