Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2bf7475

Browse files
authored
GH-93662: Make sure that column offsets are correct in multi-line method calls. (GH-93673)
1 parent4caf5c2 commit2bf7475

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

‎Lib/test/test_code.py‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,15 @@ def positions_from_location_table(code):
574574
for_inrange(length):
575575
yield (line,end_line,col,end_col)
576576

577+
defdedup(lst,prev=object()):
578+
foriteminlst:
579+
ifitem!=prev:
580+
yielditem
581+
prev=item
582+
583+
deflines_from_postions(positions):
584+
returndedup(lfor (l,_,_,_)inpositions)
585+
577586
defmisshappen():
578587
"""
579588
@@ -606,6 +615,13 @@ def misshappen():
606615

607616
)elsep
608617

618+
defbug93662():
619+
example_report_generation_message= (
620+
"""
621+
"""
622+
).strip()
623+
raiseValueError()
624+
609625

610626
classCodeLocationTest(unittest.TestCase):
611627

@@ -616,10 +632,23 @@ def check_positions(self, func):
616632
self.assertEqual(l1,l2)
617633
self.assertEqual(len(pos1),len(pos2))
618634

619-
620635
deftest_positions(self):
621636
self.check_positions(parse_location_table)
622637
self.check_positions(misshappen)
638+
self.check_positions(bug93662)
639+
640+
defcheck_lines(self,func):
641+
co=func.__code__
642+
lines1=list(dedup(lfor (_,_,l)inco.co_lines()))
643+
lines2=list(lines_from_postions(positions_from_location_table(co)))
644+
forl1,l2inzip(lines1,lines2):
645+
self.assertEqual(l1,l2)
646+
self.assertEqual(len(lines1),len(lines2))
647+
648+
deftest_lines(self):
649+
self.check_lines(parse_location_table)
650+
self.check_lines(misshappen)
651+
self.check_lines(bug93662)
623652

624653

625654
ifcheck_impl_detail(cpython=True)andctypesisnotNone:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make sure that the end column offsets are correct in multi-line method
2+
calls. Previously, the end column could precede the column offset.

‎Python/compile.c‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4801,6 +4801,16 @@ is_import_originated(struct compiler *c, expr_ty e)
48014801
returnflags&DEF_IMPORT;
48024802
}
48034803

4804+
staticvoid
4805+
update_location_to_match_attr(structcompiler*c,expr_tymeth)
4806+
{
4807+
if (meth->lineno!=meth->end_lineno) {
4808+
// Make start location match attribute
4809+
c->u->u_lineno=meth->end_lineno;
4810+
c->u->u_col_offset=meth->end_col_offset- (int)PyUnicode_GetLength(meth->v.Attribute.attr)-1;
4811+
}
4812+
}
4813+
48044814
// Return 1 if the method call was optimized, -1 if not, and 0 on error.
48054815
staticint
48064816
maybe_optimize_method_call(structcompiler*c,expr_tye)
@@ -4842,8 +4852,8 @@ maybe_optimize_method_call(struct compiler *c, expr_ty e)
48424852
}
48434853
/* Alright, we can optimize the code. */
48444854
VISIT(c,expr,meth->v.Attribute.value);
4845-
intold_lineno=c->u->u_lineno;
4846-
c->u->u_lineno=meth->end_lineno;
4855+
SET_LOC(c,meth);
4856+
update_location_to_match_attr(c,meth);
48474857
ADDOP_NAME(c,LOAD_METHOD,meth->v.Attribute.attr,names);
48484858
VISIT_SEQ(c,expr,e->v.Call.args);
48494859

@@ -4853,8 +4863,9 @@ maybe_optimize_method_call(struct compiler *c, expr_ty e)
48534863
return0;
48544864
};
48554865
}
4866+
SET_LOC(c,e);
4867+
update_location_to_match_attr(c,meth);
48564868
ADDOP_I(c,CALL,argsl+kwdsl);
4857-
c->u->u_lineno=old_lineno;
48584869
return1;
48594870
}
48604871

@@ -7673,6 +7684,7 @@ write_location_info_short_form(struct assembler* a, int length, int column, int
76737684
intcolumn_low_bits=column&7;
76747685
intcolumn_group=column >>3;
76757686
assert(column<80);
7687+
assert(end_column >=column);
76767688
assert(end_column-column<16);
76777689
write_location_first_byte(a,PY_CODE_LOCATION_INFO_SHORT0+column_group,length);
76787690
write_location_byte(a, (column_low_bits <<4) | (end_column-column));
@@ -7744,7 +7756,7 @@ write_location_info_entry(struct assembler* a, struct instr* i, int isize)
77447756
}
77457757
}
77467758
elseif (i->i_end_lineno==i->i_lineno) {
7747-
if (line_delta==0&&column<80&&end_column-column<16) {
7759+
if (line_delta==0&&column<80&&end_column-column<16&&end_column >=column) {
77487760
write_location_info_short_form(a,isize,column,end_column);
77497761
return1;
77507762
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp