Skip to content

Commit 856b36c

Browse files
committed
Allow creating tracebacks from python code
1 parent da977c5 commit 856b36c

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
import com.oracle.graal.python.builtins.objects.set.SetNodes;
137137
import com.oracle.graal.python.builtins.objects.str.PString;
138138
import com.oracle.graal.python.builtins.objects.superobject.SuperObject;
139+
import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
139140
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
140141
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
141142
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
@@ -189,6 +190,7 @@
189190
import com.oracle.graal.python.runtime.PythonContext;
190191
import com.oracle.graal.python.runtime.PythonCore;
191192
import com.oracle.graal.python.runtime.exception.PException;
193+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
192194
import com.oracle.graal.python.runtime.sequence.PSequence;
193195
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
194196
import com.oracle.graal.python.runtime.sequence.storage.ObjectSequenceStorage;
@@ -2827,12 +2829,40 @@ Object call() {
28272829
}
28282830
}
28292831

2830-
@Builtin(name = "traceback", constructsClass = PythonBuiltinClassType.PTraceback, isPublic = false)
2832+
@Builtin(name = "TracebackType", constructsClass = PythonBuiltinClassType.PTraceback, isPublic = false, minNumOfPositionalArgs = 5, parameterNames = {"$cls", "tb_next", "tb_frame", "tb_lasti",
2833+
"tb_lineno"})
28312834
@GenerateNodeFactory
28322835
public abstract static class TracebackTypeNode extends PythonBuiltinNode {
2833-
@Specialization
2834-
Object call() {
2835-
throw raise(RuntimeError, ErrorMessages.CANNOT_CALL_CTOR_OF, "traceback type");
2836+
@Specialization(limit = "1")
2837+
Object createTraceback(@SuppressWarnings("unused") LazyPythonClass cls, PTraceback next, PFrame frame, Object lasti, Object lineno,
2838+
@CachedLibrary("lasti") PythonObjectLibrary lastiLib,
2839+
@CachedLibrary("lineno") PythonObjectLibrary linenoLib,
2840+
@Cached PythonObjectFactory factory) {
2841+
return factory.createTraceback(frame, linenoLib.asSize(lineno), lastiLib.asSize(lasti), next);
2842+
}
2843+
2844+
@Specialization(limit = "1")
2845+
Object createTraceback(@SuppressWarnings("unused") LazyPythonClass cls, @SuppressWarnings("unused") PNone next, PFrame frame, Object lasti, Object lineno,
2846+
@CachedLibrary("lasti") PythonObjectLibrary lastiLib,
2847+
@CachedLibrary("lineno") PythonObjectLibrary linenoLib,
2848+
@Cached PythonObjectFactory factory) {
2849+
return factory.createTraceback(frame, linenoLib.asSize(lineno), lastiLib.asSize(lasti), null);
2850+
}
2851+
2852+
@Specialization(guards = {"!isPTraceback(next)", "!isNone(next)"})
2853+
@SuppressWarnings("unused")
2854+
Object errorNext(LazyPythonClass cls, Object next, Object frame, Object lasti, Object lineno) {
2855+
throw raise(TypeError, "expected traceback object or None, got '%p'", next);
2856+
}
2857+
2858+
@Specialization(guards = "!isPFrame(frame)")
2859+
@SuppressWarnings("unused")
2860+
Object errorFrame(LazyPythonClass cls, Object next, Object frame, Object lasti, Object lineno) {
2861+
throw raise(TypeError, "TracebackType() argument 'tb_frame' must be frame, not %p", frame);
2862+
}
2863+
2864+
protected static boolean isPFrame(Object obj) {
2865+
return obj instanceof PFrame;
28362866
}
28372867
}
28382868

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/traceback/PTraceback.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,19 @@ public final class PTraceback extends PythonBuiltinObject {
5050
private PFrame frame;
5151
private PFrame.Reference frameInfo;
5252
private int lineno = -2;
53+
private int lasti;
5354
private PTraceback next;
5455
private LazyTraceback lazyTraceback;
5556

5657
public PTraceback(PFrame frame, int lineno, PTraceback next) {
58+
this(frame, lineno, -1, next);
59+
}
60+
61+
public PTraceback(PFrame frame, int lineno, int lasti, PTraceback next) {
5762
super(PythonBuiltinClassType.PTraceback, PythonBuiltinClassType.PTraceback.newInstance());
5863
this.frame = frame;
5964
this.lineno = lineno;
65+
this.lasti = lasti;
6066
this.next = next;
6167
}
6268

@@ -83,6 +89,10 @@ public int getLineno() {
8389
return lineno;
8490
}
8591

92+
public int getLasti() {
93+
return lasti;
94+
}
95+
8696
public LazyTraceback getLazyTraceback() {
8797
return lazyTraceback;
8898
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/traceback/TracebackBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ Object setError(@SuppressWarnings("unused") PTraceback self, Object next) {
299299
@GenerateNodeFactory
300300
public abstract static class GetTracebackLastINode extends PythonBuiltinNode {
301301
@Specialization
302-
Object get(@SuppressWarnings("unused") PTraceback self) {
303-
return -1;
302+
Object get(PTraceback self) {
303+
return self.getLasti();
304304
}
305305
}
306306

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,10 @@ public PTraceback createTraceback(PFrame frame, int lineno, PTraceback next) {
618618
return trace(new PTraceback(frame, lineno, next));
619619
}
620620

621+
public PTraceback createTraceback(PFrame frame, int lineno, int lasti, PTraceback next) {
622+
return trace(new PTraceback(frame, lineno, lasti, next));
623+
}
624+
621625
public PTraceback createTraceback(LazyTraceback tb) {
622626
return trace(new PTraceback(tb));
623627
}

0 commit comments

Comments
 (0)