Skip to content

Commit 3693972

Browse files
committed
deal with places without source locations
1 parent 663eade commit 3693972

File tree

5 files changed

+90
-56
lines changed

5 files changed

+90
-56
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/PCode.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import com.oracle.truffle.api.CompilerDirectives;
6565
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6666
import com.oracle.truffle.api.RootCallTarget;
67+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
6768
import com.oracle.truffle.api.library.ExportMessage;
6869
import com.oracle.truffle.api.nodes.NodeUtil;
6970
import com.oracle.truffle.api.nodes.RootNode;
@@ -446,13 +447,24 @@ public RootCallTarget getRootCallTarget() {
446447
}
447448

448449
@ExportMessage
449-
public SourceSection getSourceLocation() {
450+
public SourceSection getSourceLocation() throws UnsupportedMessageException {
451+
SourceSection result = readSourceLocation();
452+
if (result != null) {
453+
return result;
454+
} else {
455+
throw UnsupportedMessageException.create();
456+
}
457+
}
458+
459+
private SourceSection readSourceLocation() {
450460
RootNode rootNode = getRootNode();
461+
SourceSection result;
451462
if (rootNode instanceof PRootNode) {
452-
return ((PRootNode) rootNode).getSourceSection();
463+
result = ((PRootNode) rootNode).getSourceSection();
453464
} else {
454-
return getForeignSourceSection(rootNode);
465+
result = getForeignSourceSection(rootNode);
455466
}
467+
return result;
456468
}
457469

458470
@TruffleBoundary
@@ -462,6 +474,6 @@ private static SourceSection getForeignSourceSection(RootNode rootNode) {
462474

463475
@ExportMessage
464476
public boolean hasSourceLocation() {
465-
return true;
477+
return readSourceLocation() != null;
466478
}
467479
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PFunction.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4444
import com.oracle.truffle.api.RootCallTarget;
4545
import com.oracle.truffle.api.Truffle;
46+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
4647
import com.oracle.truffle.api.library.ExportLibrary;
4748
import com.oracle.truffle.api.library.ExportMessage;
4849
import com.oracle.truffle.api.nodes.RootNode;
@@ -206,13 +207,25 @@ public boolean isCallable() {
206207
}
207208

208209
@ExportMessage
209-
public SourceSection getSourceLocation() {
210+
public SourceSection getSourceLocation() throws UnsupportedMessageException {
211+
SourceSection result = getSourceLocationDirect();
212+
if (result == null) {
213+
throw UnsupportedMessageException.create();
214+
} else {
215+
return result;
216+
}
217+
}
218+
219+
@TruffleBoundary
220+
private SourceSection getSourceLocationDirect() {
210221
RootNode rootNode = getCallTarget().getRootNode();
222+
SourceSection result;
211223
if (rootNode instanceof PRootNode) {
212-
return ((PRootNode) rootNode).getSourceSection();
224+
result = ((PRootNode) rootNode).getSourceSection();
213225
} else {
214-
return getForeignSourceSection(rootNode);
226+
result = getForeignSourceSection(rootNode);
215227
}
228+
return result;
216229
}
217230

218231
@TruffleBoundary
@@ -222,6 +235,6 @@ private static SourceSection getForeignSourceSection(RootNode rootNode) {
222235

223236
@ExportMessage
224237
public boolean hasSourceLocation() {
225-
return true;
238+
return getSourceLocationDirect() != null;
226239
}
227240
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/list/PList.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
3232
import com.oracle.graal.python.runtime.sequence.storage.SequenceStoreException;
3333
import com.oracle.truffle.api.CompilerDirectives;
34+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
3435
import com.oracle.truffle.api.library.ExportMessage;
3536
import com.oracle.truffle.api.library.ExportMessage.Ignore;
3637
import com.oracle.truffle.api.nodes.UnexpectedResultException;
@@ -136,9 +137,16 @@ public static PList expect(Object value) throws UnexpectedResultException {
136137
}
137138

138139
@ExportMessage
139-
public SourceSection getSourceLocation() {
140+
public SourceSection getSourceLocation() throws UnsupportedMessageException {
140141
ListLiteralNode node = getOrigin();
141-
return node != null ? node.getSourceSection() : null;
142+
SourceSection result = null;
143+
if (node != null) {
144+
result = node.getSourceSection();
145+
}
146+
if (result == null) {
147+
throw UnsupportedMessageException.create();
148+
}
149+
return result;
142150
}
143151

144152
@ExportMessage

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/PythonClass.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@
3434
import com.oracle.truffle.api.dsl.Cached.Exclusive;
3535
import com.oracle.truffle.api.dsl.Cached.Shared;
3636
import com.oracle.truffle.api.interop.InteropLibrary;
37+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
3738
import com.oracle.truffle.api.library.ExportLibrary;
3839
import com.oracle.truffle.api.library.ExportMessage;
40+
import com.oracle.truffle.api.object.DynamicObject;
41+
import com.oracle.truffle.api.source.SourceSection;
3942

4043
/**
4144
* Mutable class.
@@ -89,4 +92,48 @@ String getMetaQualifiedName(
8992
return result;
9093
}
9194
}
95+
96+
/*
97+
* N.b.: (tfel): This method is used to cache the source section of the first defined attribute
98+
* that has a source section. This isn't precisely the classes definition location, but it is
99+
* close. We can safely cache this regardless of any later shape changes or redefinitions,
100+
* because this is best-effort only anyway. If it is called early, it is very likely we're
101+
* getting some location near the actual definition. If it is called late, and potentially after
102+
* some monkey-patching, we'll get some other source location.
103+
*/
104+
protected static SourceSection findSourceSection(PythonManagedClass self) {
105+
DynamicObject storage = self.getStorage();
106+
for (Object key : storage.getShape().getKeys()) {
107+
if (key instanceof String) {
108+
Object value = ReadAttributeFromDynamicObjectNode.getUncached().execute(storage, key);
109+
InteropLibrary uncached = InteropLibrary.getFactory().getUncached();
110+
if (uncached.hasSourceLocation(value)) {
111+
try {
112+
return uncached.getSourceLocation(value);
113+
} catch (UnsupportedMessageException e) {
114+
// should not happen due to hasSourceLocation check
115+
}
116+
}
117+
}
118+
}
119+
return null;
120+
}
121+
122+
@ExportMessage
123+
@SuppressWarnings("static-method")
124+
protected SourceSection getSourceLocation(
125+
@Shared("src") @Cached(value = "findSourceSection(this)", allowUncached = true) SourceSection section) throws UnsupportedMessageException {
126+
if (section != null) {
127+
return section;
128+
} else {
129+
throw UnsupportedMessageException.create();
130+
}
131+
}
132+
133+
@ExportMessage
134+
@SuppressWarnings("static-method")
135+
protected boolean hasSourceLocation(
136+
@Shared("src") @Cached(value = "findSourceSection(this)", allowUncached = true) SourceSection section) {
137+
return section != null;
138+
}
92139
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/PythonManagedClass.java

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,12 @@
4040
import com.oracle.graal.python.builtins.objects.type.TypeNodes.ComputeMroNode;
4141
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetSubclassesNode;
4242
import com.oracle.graal.python.nodes.PGuards;
43-
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromDynamicObjectNode;
4443
import com.oracle.graal.python.runtime.sequence.storage.MroSequenceStorage;
4544
import com.oracle.truffle.api.Assumption;
4645
import com.oracle.truffle.api.CompilerAsserts;
4746
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
4847
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
49-
import com.oracle.truffle.api.dsl.Cached;
50-
import com.oracle.truffle.api.dsl.Cached.Shared;
51-
import com.oracle.truffle.api.interop.InteropLibrary;
52-
import com.oracle.truffle.api.interop.UnsupportedMessageException;
53-
import com.oracle.truffle.api.library.ExportMessage;
54-
import com.oracle.truffle.api.object.DynamicObject;
5548
import com.oracle.truffle.api.object.Shape;
56-
import com.oracle.truffle.api.source.SourceSection;
5749

5850
public abstract class PythonManagedClass extends PythonObject implements PythonAbstractClass {
5951

@@ -245,42 +237,4 @@ public final void setSulongType(Object dynamicSulongType) {
245237
public boolean needsNativeAllocation() {
246238
return needsNativeAllocation;
247239
}
248-
249-
/*
250-
* N.b.: (tfel): This method is used to cache the source section of the first defined attribute
251-
* that has a source section. This isn't precisely the classes definition location, but it is
252-
* close. We can safely cache this regardless of any later shape changes or redefinitions,
253-
* because this is best-effort only anyway. If it is called early, it is very likely we're
254-
* getting some location near the actual definition. If it is called late, and potentially after
255-
* some monkey-patching, we'll get some other source location.
256-
*/
257-
protected static SourceSection findSourceSection(PythonManagedClass self) {
258-
DynamicObject storage = self.getStorage();
259-
for (Object key : storage.getShape().getKeys()) {
260-
if (key instanceof String) {
261-
Object value = ReadAttributeFromDynamicObjectNode.getUncached().execute(storage, key);
262-
InteropLibrary uncached = InteropLibrary.getFactory().getUncached();
263-
if (uncached.hasSourceLocation(value)) {
264-
try {
265-
return uncached.getSourceLocation(value);
266-
} catch (UnsupportedMessageException e) {
267-
// should not happen due to hasSourceLocation check
268-
}
269-
}
270-
}
271-
}
272-
return null;
273-
}
274-
275-
@ExportMessage
276-
protected SourceSection getSourceLocation(
277-
@Shared("src") @Cached(value = "findSourceSection(this)", allowUncached = true) SourceSection section) {
278-
return section;
279-
}
280-
281-
@ExportMessage
282-
protected boolean hasSourceLocation(
283-
@Shared("src") @Cached(value = "findSourceSection(this)", allowUncached = true) SourceSection section) {
284-
return section != null;
285-
}
286240
}

0 commit comments

Comments
 (0)