Skip to content

Commit 26430d4

Browse files
committed
pass segmen_str around
1 parent d79d365 commit 26430d4

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

Python/traceback.c

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "pycore_frame.h" // _PyFrame_GetCode()
1010
#include "pycore_pyarena.h" // _PyArena_Free()
1111
#include "pycore_ast.h" // asdl_seq_*
12-
#include "pycore_compile.h" // asdl_seq_*
12+
#include "pycore_compile.h" // _PyAST_Optimize
1313
#include "pycore_parser.h" // _PyParser_ASTFromString
1414
#include "../Parser/pegen.h" // _PyPegen_byte_offset_to_character_offset()
1515
#include "structmember.h" // PyMemberDef
@@ -536,37 +536,26 @@ _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent, i
536536
#define IS_WHITESPACE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\f'))
537537

538538
static int
539-
extract_anchors_from_expr(PyObject *segment, expr_ty expr, int *left_anchor, int *right_anchor)
539+
extract_anchors_from_expr(const char *segment_str, expr_ty expr, int *left_anchor, int *right_anchor)
540540
{
541541
switch (expr->kind) {
542542
case BinOp_kind: {
543-
PyObject *operator = PyUnicode_Substring(segment, expr->v.BinOp.left->end_col_offset,
544-
expr->v.BinOp.right->col_offset);
545-
if (!operator) {
546-
return -1;
547-
}
548-
549-
const char *operator_str = PyUnicode_AsUTF8(operator);
550-
if (!operator_str) {
551-
Py_DECREF(operator);
552-
return -1;
553-
}
554-
555-
Py_ssize_t i, len = PyUnicode_GET_LENGTH(operator);
556-
for (i = 0; i < len; i++) {
557-
if (IS_WHITESPACE(operator_str[i])) {
543+
expr_ty left = expr->v.BinOp.left;
544+
expr_ty right = expr->v.BinOp.right;
545+
for (int i = left->end_col_offset + 1; i < right->col_offset; i++) {
546+
if (IS_WHITESPACE(segment_str[i])) {
558547
continue;
559548
}
560549

561-
int index = Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
562-
*left_anchor = expr->v.BinOp.left->end_col_offset + index;
563-
*right_anchor = expr->v.BinOp.left->end_col_offset + index + 1;
564-
if (i + 1 < len && !IS_WHITESPACE(operator_str[i + 1])) {
550+
*left_anchor = i;
551+
*right_anchor = i + 1;
552+
553+
// Check whether if this a two-character operator (e.g //)
554+
if (i + 1 < right->col_offset && !IS_WHITESPACE(segment_str[i + 1])) {
565555
++*right_anchor;
566556
}
567557
break;
568558
}
569-
Py_DECREF(operator);
570559
return 1;
571560
}
572561
case Subscript_kind: {
@@ -580,11 +569,11 @@ extract_anchors_from_expr(PyObject *segment, expr_ty expr, int *left_anchor, int
580569
}
581570

582571
static int
583-
extract_anchors_from_stmt(PyObject *segment, stmt_ty statement, int *left_anchor, int *right_anchor)
572+
extract_anchors_from_stmt(const char *segment_str, stmt_ty statement, int *left_anchor, int *right_anchor)
584573
{
585574
switch (statement->kind) {
586575
case Expr_kind: {
587-
return extract_anchors_from_expr(segment, statement->v.Expr.value, left_anchor, right_anchor);
576+
return extract_anchors_from_expr(segment_str, statement->v.Expr.value, left_anchor, right_anchor);
588577
}
589578
default:
590579
return 0;
@@ -631,7 +620,7 @@ extract_anchors_from_line(PyObject *filename, PyObject *line,
631620
assert(module->kind == Module_kind);
632621
if (asdl_seq_LEN(module->v.Module.body) == 1) {
633622
stmt_ty statement = asdl_seq_GET(module->v.Module.body, 0);
634-
res = extract_anchors_from_stmt(segment, statement, left_anchor, right_anchor);
623+
res = extract_anchors_from_stmt(segment_str, statement, left_anchor, right_anchor);
635624
} else {
636625
res = 0;
637626
}
@@ -711,8 +700,8 @@ tb_displayline(PyTracebackObject* tb, PyObject *f, PyObject *filename, int linen
711700
const char *primary, *secondary;
712701
primary = secondary = "^";
713702

714-
int left_end_offset, right_start_offset;
715-
left_end_offset = right_start_offset = Py_SAFE_DOWNCAST(end_offset, Py_ssize_t, int) - Py_SAFE_DOWNCAST(start_offset, Py_ssize_t, int);
703+
int left_end_offset = Py_SAFE_DOWNCAST(end_offset, Py_ssize_t, int) - Py_SAFE_DOWNCAST(start_offset, Py_ssize_t, int);
704+
int right_start_offset = left_end_offset;
716705

717706
if (source_line) {
718707
int res = extract_anchors_from_line(filename, source_line, start_offset, end_offset,

0 commit comments

Comments
 (0)