Skip to content

Commit 2cf51c4

Browse files
committed
gh-153569: remove unused tokenizer cursor and source lookup APIs
1 parent 728c4d9 commit 2cf51c4

9 files changed

Lines changed: 20 additions & 683 deletions

File tree

Lib/test/test_capi/test_tokenizer.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ def test_source(self):
1212
def test_source_discard(self):
1313
_testinternalcapi.test_tokenizer_source_discard()
1414

15-
def test_cursor(self):
16-
_testinternalcapi.test_tokenizer_cursor()
17-
1815

1916
if __name__ == "__main__":
2017
unittest.main()

Makefile.pre.in

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ TOKENIZER_OBJS= \
399399
Parser/lexer/number.o \
400400
Parser/lexer/state.o \
401401
Parser/lexer/string.o \
402-
Parser/tokenizer/cursor.o \
403402
Parser/tokenizer/decoder.o \
404403
Parser/tokenizer/reader.o \
405404
Parser/tokenizer/source.o \
@@ -414,7 +413,6 @@ TOKENIZER_HEADERS= \
414413
Parser/lexer/lexer.h \
415414
Parser/lexer/lexer_internal.h \
416415
Parser/lexer/state.h \
417-
Parser/tokenizer/cursor.h \
418416
Parser/tokenizer/reader.h \
419417
Parser/tokenizer/reader_internal.h \
420418
Parser/tokenizer/source.h \
@@ -3462,7 +3460,7 @@ MODULE__SOCKET_DEPS=$(srcdir)/Modules/socketmodule.h $(srcdir)/Modules/addrinfo.
34623460
MODULE__SSL_DEPS=$(srcdir)/Modules/_ssl.h $(srcdir)/Modules/_openssl_mem.h $(srcdir)/Modules/_ssl/cert.c $(srcdir)/Modules/_ssl/debughelpers.c $(srcdir)/Modules/_ssl/misc.c $(srcdir)/Modules/_ssl_data_111.h $(srcdir)/Modules/_ssl_data_300.h $(srcdir)/Modules/socketmodule.h
34633461
MODULE__TESTCAPI_DEPS=$(srcdir)/Modules/_testcapi/parts.h $(srcdir)/Modules/_testcapi/util.h
34643462
MODULE__TESTLIMITEDCAPI_DEPS=$(srcdir)/Modules/_testlimitedcapi/testcapi_long.h $(srcdir)/Modules/_testlimitedcapi/parts.h $(srcdir)/Modules/_testlimitedcapi/util.h
3465-
MODULE__TESTINTERNALCAPI_DEPS=$(srcdir)/Modules/_testinternalcapi/parts.h $(srcdir)/Parser/tokenizer/cursor.h $(srcdir)/Parser/tokenizer/source.h $(srcdir)/Python/ceval.h $(srcdir)/Modules/_testinternalcapi/test_targets.h $(srcdir)/Modules/_testinternalcapi/test_cases.c.h
3463+
MODULE__TESTINTERNALCAPI_DEPS=$(srcdir)/Modules/_testinternalcapi/parts.h $(srcdir)/Parser/tokenizer/source.h $(srcdir)/Python/ceval.h $(srcdir)/Modules/_testinternalcapi/test_targets.h $(srcdir)/Modules/_testinternalcapi/test_cases.c.h
34663464
MODULE__SQLITE3_DEPS=$(srcdir)/Modules/_sqlite/connection.h $(srcdir)/Modules/_sqlite/cursor.h $(srcdir)/Modules/_sqlite/microprotocols.h $(srcdir)/Modules/_sqlite/module.h $(srcdir)/Modules/_sqlite/prepare_protocol.h $(srcdir)/Modules/_sqlite/row.h $(srcdir)/Modules/_sqlite/util.h
34673465
MODULE__ZSTD_DEPS=$(srcdir)/Modules/_zstd/_zstdmodule.h $(srcdir)/Modules/_zstd/buffer.h $(srcdir)/Modules/_zstd/zstddict.h
34683466

Modules/_testinternalcapi/tokenizer.c

Lines changed: 16 additions & 292 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "parts.h"
22

3-
#include "../../Parser/tokenizer/cursor.h"
3+
#include "../../Parser/tokenizer/source.h"
44

55
static int
66
check(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-
4737
static PyObject *
4838
test_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

441166
static 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

Comments
 (0)