Skip to content

Commit e9cf614

Browse files
committed
gh-159465: Share incomplete-input error cleanup
1 parent 301790f commit e9cf614

3 files changed

Lines changed: 21 additions & 25 deletions

File tree

‎Include/internal/pycore_parser.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ _Py_DECLARE_STR(empty, "")
2727
}, \
2828
}
2929

30-
/* If incomplete is non-NULL, initialize *incomplete to NULL. A returned AST
30+
/* The caller must initialize *incomplete to NULL if supplied. A returned AST
3131
can also return an owned exception there for a missing line terminator.
3232
Validate the AST before reporting that exception. */
3333
extern struct _mod* _PyParser_ASTFromString(

‎Parser/tokenizer/decoder.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,25 +300,24 @@ store_prepared_source(struct tok_state *tok, const char *data, Py_ssize_t len,
300300
int add_newline = add_final_newline &&
301301
pos + raw_line_len == len && !terminated;
302302
const char *line = data + pos;
303-
Py_ssize_t line_len = raw_line_len;
304303
// raw_line_length stops at the first CR or LF, so any CR is in
305304
// the line terminator.
306305
Py_ssize_t terminator_len = 0;
307306
if (!preserve_crlf) {
308-
if (line[line_len - 1] == '\r') {
307+
if (line[raw_line_len - 1] == '\r') {
309308
terminator_len = 1;
310309
}
311-
else if (line_len > 1 && line[line_len - 2] == '\r') {
310+
else if (raw_line_len > 1 && line[raw_line_len - 2] == '\r') {
312311
terminator_len = 2;
313312
}
314313
}
315314
_PyTok_Off appended;
316315
if (terminator_len || add_newline) {
317316
appended = _PyTok_SourceAppendLineWithNewline(
318-
&tok->source, line, line_len - terminator_len);
317+
&tok->source, line, raw_line_len - terminator_len);
319318
}
320319
else {
321-
appended = _PyTok_SourceAppendLine(&tok->source, line, line_len);
320+
appended = _PyTok_SourceAppendLine(&tok->source, line, raw_line_len);
322321
}
323322
if (appended < 0) {
324323
tok->done = PyErr_ExceptionMatches(PyExc_MemoryError)

‎Python/pythonrun.c‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,46 +1569,43 @@ _Py_CompileString(const char *str, PyObject *filename, int start,
15691569
return NULL;
15701570
}
15711571

1572-
PyCodeObject *co;
1573-
mod_ty mod;
15741572
PyArena *arena = _PyArena_New();
15751573
if (arena == NULL)
15761574
return NULL;
15771575

1576+
PyObject *result = NULL;
15781577
PyObject *incomplete = NULL;
1579-
mod = _PyParser_ASTFromString(str, filename, start, flags, arena, module,
1580-
&incomplete);
1578+
mod_ty mod = _PyParser_ASTFromString(str, filename, start, flags, arena,
1579+
module, &incomplete);
15811580
if (mod == NULL) {
1582-
_PyArena_Free(arena);
1583-
return NULL;
1581+
goto done;
15841582
}
15851583
if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
15861584
int syntax_check_only = ((flags->cf_flags & PyCF_OPTIMIZED_AST) == PyCF_ONLY_AST); /* unoptiomized AST */
15871585
if (_PyCompile_AstPreprocess(mod, filename, flags, optimize, arena,
15881586
syntax_check_only, module) < 0)
15891587
{
1590-
Py_XDECREF(incomplete);
1591-
_PyArena_Free(arena);
1592-
return NULL;
1588+
goto done;
15931589
}
15941590
if (incomplete != NULL) {
1595-
_PyArena_Free(arena);
1596-
PyErr_SetRaisedException(incomplete);
1597-
return NULL;
1591+
goto done;
15981592
}
1599-
PyObject *result = PyAST_mod2obj(mod);
1593+
result = PyAST_mod2obj(mod);
16001594
_PyArena_Free(arena);
16011595
return result;
16021596
}
1603-
co = _PyAST_Compile(mod, filename, flags, optimize, arena, module);
1597+
result = (PyObject *)_PyAST_Compile(mod, filename, flags, optimize, arena, module);
1598+
1599+
done:
16041600
_PyArena_Free(arena);
1605-
if (co != NULL && incomplete != NULL) {
1606-
Py_DECREF(co);
1601+
if (incomplete != NULL && !PyErr_Occurred()) {
1602+
Py_CLEAR(result);
16071603
PyErr_SetRaisedException(incomplete);
1608-
return NULL;
16091604
}
1610-
Py_XDECREF(incomplete);
1611-
return (PyObject *)co;
1605+
else {
1606+
Py_XDECREF(incomplete);
1607+
}
1608+
return result;
16121609
}
16131610

16141611
PyObject *

0 commit comments

Comments
 (0)