Skip to content

Commit 28f7f9f

Browse files
committed
Fix parser tests
1 parent 4ffd6c2 commit 28f7f9f

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

‎Parser/action_helpers.c‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include "pycore_pystate.h" // _PyInterpreterState_GET()
33
#include "pycore_runtime.h" // _PyRuntime
44
#include "pycore_unicodeobject.h" // _PyUnicode_InternImmortal()
5-
#include "pycore_pyerrors.h" // _PyErr_EmitSyntaxWarning()
65

76
#include "pegen.h"
87
#include "string_parser.h" // _PyPegen_decode_string()
@@ -1996,7 +1995,7 @@ _warn_relative_import_of_lazy(Parser *p, asdl_seq *dots, expr_ty module)
19961995
}
19971996

19981997
int count = _PyPegen_seq_count_dots(dots);
1999-
char *buf = PyMem_RawMalloc(sizeof(char *) * (count + 1));
1998+
char *buf = malloc(sizeof(char *) * (count + 1));
20001999
if (buf == NULL) {
20012000
PyErr_NoMemory();
20022001
return -1;
@@ -2013,10 +2012,9 @@ _warn_relative_import_of_lazy(Parser *p, asdl_seq *dots, expr_ty module)
20132012
return -1;
20142013
}
20152014

2016-
return _PyErr_EmitSyntaxWarning(msg, p->tok->filename,
2017-
module->lineno, module->col_offset,
2018-
module->end_lineno, module->end_col_offset,
2019-
p->tok->module);
2015+
int result = _PyPegen_set_syntax_warning(p, msg, module);
2016+
Py_DECREF(msg);
2017+
return result;
20202018
}
20212019

20222020
stmt_ty

‎Parser/pegen.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ void *_PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
181181
Py_ssize_t lineno, Py_ssize_t col_offset,
182182
Py_ssize_t end_lineno, Py_ssize_t end_col_offset,
183183
const char *errmsg, va_list va);
184+
int _PyPegen_set_syntax_warning(Parser* p, PyObject *msg, expr_ty context);
184185
void _Pypegen_set_syntax_error(Parser* p, Token* last_token);
185186
void _Pypegen_stack_overflow(Parser *p);
186187

‎Parser/pegen_errors.c‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,38 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
411411
return NULL;
412412
}
413413

414+
/* Emits a SyntaxWarning and returns 0 on success.
415+
If a SyntaxWarning is raised as error, replaces it with a SyntaxError
416+
and returns -1.
417+
*/
418+
int
419+
_PyPegen_set_syntax_warning(Parser* p, PyObject *msg, expr_ty context)
420+
{
421+
PyObject *filename = p->tok->filename;
422+
PyObject *mod = p->tok->module;
423+
int lineno = context->lineno;
424+
int col_offset = context->col_offset;
425+
int end_lineno = context->end_lineno;
426+
int end_col_offset = context->end_col_offset;
427+
428+
// Copied from `_PyErr_EmitSyntaxWarning`:
429+
if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, filename, lineno,
430+
mod, NULL) < 0)
431+
{
432+
if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
433+
/* Replace the SyntaxWarning exception with a SyntaxError
434+
to get a more accurate error report */
435+
PyErr_Clear();
436+
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError,
437+
lineno, col_offset,
438+
end_lineno, end_col_offset,
439+
"%U", msg);
440+
}
441+
return -1;
442+
}
443+
return 0;
444+
}
445+
414446
void
415447
_Pypegen_set_syntax_error(Parser* p, Token* last_token) {
416448
// Existing syntax error

0 commit comments

Comments
 (0)