cpython/Parser/tokenizer/source.c
Pablo Galindo Salgado 0b8aa89a05
gh-153569: Simplify tokenizer state (#158124)
* Use diagnostic line for missing-brace errors

* Keep implicit prepared newline metadata in the reader

* Reuse parsed formatted string kind in lexer

* Transfer owned tokenizer encoding strings

* Remove unused tokenizer state and redundant branches

* Avoid rescanning partial decoded tokenizer lines

* Reuse prepared tokenizer line terminators for CR detection

* Reuse interactive tokenizer chunks without carriage returns

* Release tokenizer constructor resources on allocation failure

* Narrow interactive newline normalization wrapper

* Close duplicated tokenizer descriptors when fdopen fails

* Preserve cached tokenizer lines when newline allocation fails

* Collect cycles involving tokenizer readline callbacks

* Preserve buffered text when decoding source files

* Clear the transferred syntax error line before cleanup

* Propagate tokenizer column conversion failures

* Allocate parser repetition buffers on first match

* Allocate type comment storage only when needed

* Reuse decoded multiline text when counting token columns

* Skip empty decoded tokenizer chunks

* Check parser array growth before allocating

* Name tokenizer bounds and derive byte lengths from their data

* Set the tokenizer exception when file input size overflows

* gh-153569: Send exact line endings in the REPL test

* Share parser repetition buffer growth

* Fold Unicode input coverage into the existing REPL test

* Keep parser array growth policy private

* Drop obsolete bitmap stress from source discard test

* Preserve input failures while scanning string literals

* fixup! Preserve input failures while scanning string literals

* fixup! fixup! Preserve input failures while scanning string literals

* gh-153569: Remove assertion-only lexer bookkeeping
2026-10-05 14:28:51 +01:00

137 lines
3.6 KiB
C

#include "Python.h"
#include "pycore_pymem.h" // PYMEM_DEADBYTE
#include "source.h"
#define SOURCE_BUFFER_GROWTH_FACTOR 2
void
_PyTok_SourceInit(_PyTok_SourceText *source)
{
*source = (_PyTok_SourceText){0};
}
void
_PyTok_SourceClear(_PyTok_SourceText *source)
{
PyMem_Free(source->bytes);
_PyTok_SourceInit(source);
}
void
_PyTok_SourceDiscard(_PyTok_SourceText *source)
{
assert(source->base_offset <= PY_SSIZE_T_MAX - source->len);
source->base_offset += source->len;
source->len = 0;
if (source->bytes != NULL) {
source->bytes[0] = '\0';
}
source->nlines = 0;
}
static int
reserve_bytes(_PyTok_SourceText *source, Py_ssize_t needed)
{
if (needed <= source->cap) {
return 0;
}
Py_ssize_t cap = source->cap > 0 ? source->cap : BUFSIZ;
while (cap < needed) {
if (cap > PY_SSIZE_T_MAX / SOURCE_BUFFER_GROWTH_FACTOR) {
cap = needed;
break;
}
cap *= SOURCE_BUFFER_GROWTH_FACTOR;
}
char *bytes;
#ifdef Py_DEBUG
/* Moving on growth makes stale interior pointers fail in debug builds. */
bytes = PyMem_Malloc(cap);
if (bytes != NULL && source->len > 0) {
memcpy(bytes, source->bytes, source->len);
}
#else
bytes = PyMem_Realloc(source->bytes, cap);
#endif
if (bytes == NULL) {
PyErr_NoMemory();
return -1;
}
#ifdef Py_DEBUG
if (source->bytes != NULL) {
memset(source->bytes, PYMEM_DEADBYTE, source->cap);
PyMem_Free(source->bytes);
}
#endif
source->bytes = bytes;
source->cap = cap;
return 0;
}
static int
validate_line(const _PyTok_SourceText *source, const char *bytes,
Py_ssize_t len)
{
if (len <= 0 || bytes == NULL ||
(source->nlines > 0 &&
(source->len == 0 || source->bytes[source->len - 1] != '\n'))) {
PyErr_SetString(PyExc_SystemError, "invalid tokenizer source line");
return -1;
}
const char *newline = memchr(bytes, '\n', len);
if (newline != NULL && newline != bytes + len - 1) {
PyErr_SetString(PyExc_SystemError, "invalid tokenizer source line");
return -1;
}
if (source->nlines == INT_MAX ||
(source->nlines == INT_MAX - 1 && newline != NULL)) {
PyErr_SetString(PyExc_OverflowError, "too many tokenizer source lines");
return -1;
}
return 0;
}
_PyTok_Off
_PyTok_SourceAppendLine(_PyTok_SourceText *source, const char *bytes,
Py_ssize_t len)
{
if (validate_line(source, bytes, len) < 0) {
return -1;
}
if (source->len > PY_SSIZE_T_MAX - len - 1 ||
source->base_offset > PY_SSIZE_T_MAX - source->len - len) {
PyErr_NoMemory();
return -1;
}
if (reserve_bytes(source, source->len + len + 1) < 0) {
return -1;
}
_PyTok_Off start = source->len;
memcpy(source->bytes + start, bytes, len);
source->len += len;
source->bytes[source->len] = '\0';
source->nlines++;
return source->base_offset + start;
}
const char *
_PyTok_SourceLineView(const _PyTok_SourceText *source, Py_ssize_t lineno,
Py_ssize_t *len)
{
assert(len != NULL);
const char *line = _PyTok_SourceData(source);
const char *end = line + source->len;
while (lineno > 1) {
const char *newline = memchr(line, '\n', end - line);
if (newline == NULL) {
break;
}
line = newline + 1;
lineno--;
}
const char *newline = memchr(line, '\n', end - line);
*len = (newline != NULL ? newline : end) - line;
return line;
}