summaryrefslogtreecommitdiffstats
path: root/Parser/pegen
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2020-05-14 20:11:48 (GMT)
committerGitHub <noreply@github.com>2020-05-14 20:11:48 (GMT)
commitbcc30360951a303aa72b0502b77aad2c5f09f30d (patch)
tree2a34a277802399700512ff2fe203c20fde0b6122 /Parser/pegen
parenta482dc500b6ec4889f6a126ba08cbad6c11e37bc (diff)
downloadcpython-bcc30360951a303aa72b0502b77aad2c5f09f30d.zip
cpython-bcc30360951a303aa72b0502b77aad2c5f09f30d.tar.gz
cpython-bcc30360951a303aa72b0502b77aad2c5f09f30d.tar.bz2
bpo-40619: Correctly handle error lines in programs without file mode (GH-20090)
Diffstat (limited to 'Parser/pegen')
-rw-r--r--Parser/pegen/pegen.c30
1 files changed, 5 insertions, 25 deletions
diff --git a/Parser/pegen/pegen.c b/Parser/pegen/pegen.c
index 083088b..8b79a73 100644
--- a/Parser/pegen/pegen.c
+++ b/Parser/pegen/pegen.c
@@ -300,30 +300,6 @@ error:
Py_XDECREF(tuple);
}
-static inline PyObject *
-get_error_line(char *buffer, int is_file)
-{
- const char *newline;
- if (is_file) {
- newline = strrchr(buffer, '\n');
- } else {
- newline = strchr(buffer, '\n');
- }
-
- if (is_file) {
- while (newline > buffer && newline[-1] == '\n') {
- --newline;
- }
- }
-
- if (newline) {
- return PyUnicode_DecodeUTF8(buffer, newline - buffer, "replace");
- }
- else {
- return PyUnicode_DecodeUTF8(buffer, strlen(buffer), "replace");
- }
-}
-
static int
tokenizer_error(Parser *p)
{
@@ -422,7 +398,11 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
}
if (!error_line) {
- error_line = get_error_line(p->tok->buf, p->start_rule == Py_file_input);
+ Py_ssize_t size = p->tok->inp - p->tok->buf;
+ if (size && p->tok->buf[size-1] == '\n') {
+ size--;
+ }
+ error_line = PyUnicode_DecodeUTF8(p->tok->buf, size, "replace");
if (!error_line) {
goto error;
}