Skip to content

Commit 5eb4ee3

Browse files
committed
[GR-73215] Adopt new source section builder API
PullRequest: graalpython/4255
2 parents d0bf0a9 + 54ac496 commit 5eb4ee3

3 files changed

Lines changed: 46 additions & 41 deletions

File tree

graalpython/com.oracle.graal.python.test/src/tests/test_traceback.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -36,9 +36,8 @@
3636
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
39-
import os
39+
import ast
4040
import sys
41-
import unittest
4241

4342

4443
def assert_raises(err, fn, *args, **kwargs):
@@ -658,3 +657,14 @@ def test_faulthandler_many_threads():
658657
if ids:
659658
ids_per_block.append(ids)
660659
assert len(ids) == 1, f"Interleaved output detected in block {header!r} with multiple thread func ids: {ids}"
660+
661+
662+
def test_location_from_ast():
663+
m = compile("a = 1\nx", "<stdin>", "exec", flags=ast.PyCF_ONLY_AST)
664+
665+
try:
666+
exec(compile(m, "<stdin>", "exec"))
667+
except NameError as e:
668+
assert e.__traceback__.tb_next.tb_lineno == 2
669+
else:
670+
assert False

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_dictviews.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ test.test_dictviews.DictSetTest.test_abc_registry @ darwin-arm64,linux-aarch64,l
22
test.test_dictviews.DictSetTest.test_compare_error @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github
33
test.test_dictviews.DictSetTest.test_constructors_not_callable @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github
44
test.test_dictviews.DictSetTest.test_copy @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github
5-
test.test_dictviews.DictSetTest.test_deeply_nested_repr @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,win32-AMD64,win32-AMD64-github
5+
# Unreliable RecursionError
6+
!test.test_dictviews.DictSetTest.test_deeply_nested_repr
67
test.test_dictviews.DictSetTest.test_dict_items @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github
78
test.test_dictviews.DictSetTest.test_dict_keys @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github
89
test.test_dictviews.DictSetTest.test_dict_mixed_keys_items @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/RootNodeCompiler.java

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -788,20 +788,12 @@ boolean beginSourceSection(SourceRange sourceRange, Builder b) {
788788
SourceRange oldSourceRange = this.currentLocation;
789789
this.currentLocation = sourceRange;
790790

791-
if (ctx.source.hasCharacters()) {
792-
int startOffset = getStartOffset(sourceRange);
793-
int endOffset = getEndOffset(sourceRange);
794-
int length = endOffset - startOffset;
795-
if (length == 0) {
796-
startOffset = 0;
797-
}
798-
b.beginSourceSection(startOffset, length);
791+
beginSourceSectionInner(b, sourceRange);
799792

800-
if (oldSourceRange == null || oldSourceRange.startLine != sourceRange.startLine) {
801-
b.beginTag(StatementTag.class);
802-
b.beginBlock();
803-
return true;
804-
}
793+
if (oldSourceRange == null || oldSourceRange.startLine != sourceRange.startLine) {
794+
b.beginTag(StatementTag.class);
795+
b.beginBlock();
796+
return true;
805797
}
806798
return false;
807799
}
@@ -820,39 +812,41 @@ void beginRootSourceSection(SSTNode node, Builder b) {
820812
sourceRange = node.getSourceRange();
821813
}
822814

823-
if (ctx.source.hasCharacters()) {
824-
int startOffset = getStartOffset(sourceRange);
825-
int endOffset = getEndOffset(sourceRange);
826-
int length = endOffset - startOffset;
827-
if (length == 0) {
828-
startOffset = 0;
829-
}
830-
b.beginSourceSection(startOffset, length);
831-
}
815+
beginSourceSectionInner(b, sourceRange);
832816
}
833817

834-
void endSourceSection(Builder b, boolean closeTag) {
835-
if (ctx.source.hasCharacters()) {
836-
if (closeTag) {
837-
b.endBlock();
838-
b.endTag(StatementTag.class);
818+
private static void beginSourceSectionInner(Builder b, SourceRange sourceRange) {
819+
if (sourceRange.startLine >= 1) {
820+
if (sourceRange.startColumn >= 0 && sourceRange.endLine >= sourceRange.startLine && sourceRange.endColumn >= 0) {
821+
if (sourceRange.endColumn > 0) {
822+
b.beginSourceSection(sourceRange.startLine, sourceRange.startColumn + 1, sourceRange.endLine, sourceRange.endColumn);
823+
} else {
824+
/*
825+
* Truffle doesn't allow including an empty line with no characters, so we just
826+
* include the first character to have at least something. It's not correct, but
827+
* these cases are very rare, it occurs primarily in string consituents of
828+
* top-level multiline format strings.
829+
*/
830+
b.beginSourceSection(sourceRange.startLine, sourceRange.startColumn + 1, sourceRange.endLine, 1);
831+
}
832+
} else {
833+
b.beginSourceSection(sourceRange.startLine);
839834
}
840-
b.endSourceSection();
835+
} else {
836+
b.beginSourceSectionUnavailable();
841837
}
842838
}
843839

844-
void endRootSourceSection(Builder b) {
845-
if (ctx.source.hasCharacters()) {
846-
b.endSourceSection();
840+
void endSourceSection(Builder b, boolean closeTag) {
841+
if (closeTag) {
842+
b.endBlock();
843+
b.endTag(StatementTag.class);
847844
}
845+
b.endSourceSection();
848846
}
849847

850-
int getStartOffset(SourceRange sourceRange) {
851-
return ctx.source.getLineStartOffset(sourceRange.startLine) + sourceRange.startColumn;
852-
}
853-
854-
int getEndOffset(SourceRange sourceRange) {
855-
return ctx.source.getLineStartOffset(sourceRange.endLine) + sourceRange.endColumn;
848+
void endRootSourceSection(Builder b) {
849+
b.endSourceSection();
856850
}
857851

858852
void beginReturn(Builder b) {

0 commit comments

Comments
 (0)