summaryrefslogtreecommitdiffstats
path: root/Parser/pegen.c
diff options
context:
space:
mode:
authorAmmar Askar <ammar@ammaraskar.com>2021-07-04 23:14:33 (GMT)
committerGitHub <noreply@github.com>2021-07-04 23:14:33 (GMT)
commit5644c7b3ffd49bed58dc095be6e6148e0bb4431e (patch)
treec53063e2f78685d1f284f6e333e05f44e49ea5ca /Parser/pegen.c
parent693cec0e2dcafa393ed5cdaa606f8dc8e3876adf (diff)
downloadcpython-5644c7b3ffd49bed58dc095be6e6148e0bb4431e.zip
cpython-5644c7b3ffd49bed58dc095be6e6148e0bb4431e.tar.gz
cpython-5644c7b3ffd49bed58dc095be6e6148e0bb4431e.tar.bz2
bpo-43950: Print columns in tracebacks (PEP 657) (GH-26958)
The traceback.c and traceback.py mechanisms now utilize the newly added code.co_positions and PyCode_Addr2Location to print carets on the specific expressions involved in a traceback. Co-authored-by: Pablo Galindo <Pablogsal@gmail.com> Co-authored-by: Ammar Askar <ammar@ammaraskar.com> Co-authored-by: Batuhan Taskaya <batuhanosmantaskaya@gmail.com>
Diffstat (limited to 'Parser/pegen.c')
-rw-r--r--Parser/pegen.c46
1 files changed, 23 insertions, 23 deletions
diff --git a/Parser/pegen.c b/Parser/pegen.c
index 3472d48..3e8ddfb 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -139,27 +139,6 @@ _create_dummy_identifier(Parser *p)
return _PyPegen_new_identifier(p, "");
}
-static inline Py_ssize_t
-byte_offset_to_character_offset(PyObject *line, Py_ssize_t col_offset)
-{
- const char *str = PyUnicode_AsUTF8(line);
- if (!str) {
- return 0;
- }
- Py_ssize_t len = strlen(str);
- if (col_offset > len + 1) {
- col_offset = len + 1;
- }
- assert(col_offset >= 0);
- PyObject *text = PyUnicode_DecodeUTF8(str, col_offset, "replace");
- if (!text) {
- return 0;
- }
- Py_ssize_t size = PyUnicode_GET_LENGTH(text);
- Py_DECREF(text);
- return size;
-}
-
const char *
_PyPegen_get_expr_name(expr_ty e)
{
@@ -418,6 +397,27 @@ get_error_line(Parser *p, Py_ssize_t lineno)
return PyUnicode_DecodeUTF8(cur_line, next_newline - cur_line, "replace");
}
+Py_ssize_t
+_PyPegen_byte_offset_to_character_offset(PyObject *line, Py_ssize_t col_offset)
+{
+ const char *str = PyUnicode_AsUTF8(line);
+ if (!str) {
+ return 0;
+ }
+ Py_ssize_t len = strlen(str);
+ if (col_offset > len + 1) {
+ col_offset = len + 1;
+ }
+ assert(col_offset >= 0);
+ PyObject *text = PyUnicode_DecodeUTF8(str, col_offset, "replace");
+ if (!text) {
+ return 0;
+ }
+ Py_ssize_t size = PyUnicode_GET_LENGTH(text);
+ Py_DECREF(text);
+ return size;
+}
+
void *
_PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
Py_ssize_t lineno, Py_ssize_t col_offset,
@@ -498,9 +498,9 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
Py_ssize_t end_col_number = end_col_offset;
if (p->tok->encoding != NULL) {
- col_number = byte_offset_to_character_offset(error_line, col_offset);
+ col_number = _PyPegen_byte_offset_to_character_offset(error_line, col_offset);
end_col_number = end_col_number > 0 ?
- byte_offset_to_character_offset(error_line, end_col_offset) :
+ _PyPegen_byte_offset_to_character_offset(error_line, end_col_offset) :
end_col_number;
}
tmp = Py_BuildValue("(OiiNii)", p->tok->filename, lineno, col_number, error_line, end_lineno, end_col_number);