Skip to content

Commit f2358f0

Browse files
committed
Fake f_lasti for started generators
1 parent fce221b commit f2358f0

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/frame/FrameBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ int get(PFrame self) {
112112
@GenerateNodeFactory
113113
public abstract static class GetLastiNode extends PythonBuiltinNode {
114114
@Specialization
115-
int get(@SuppressWarnings("unused") PFrame self) {
116-
return -1;
115+
int get(PFrame self) {
116+
return self.getLasti();
117117
}
118118
}
119119

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/frame/PFrame.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public final class PFrame extends PythonBuiltinObject {
6565
private Node location;
6666
private RootCallTarget callTarget;
6767
private int line = -2;
68+
private int lasti = -1;
6869

6970
private PFrame.Reference backref = null;
7071

@@ -309,4 +310,16 @@ public void setArguments(Object[] arguments2) {
309310
public void setLocation(Node location) {
310311
this.location = location;
311312
}
313+
314+
/**
315+
* Last bytecode instruction. Since we don't have bytecode this is -1 by default, but can be set
316+
* to a different value to distinguish started generators from unstarted
317+
*/
318+
public int getLasti() {
319+
return lasti;
320+
}
321+
322+
public void setLasti(int lasti) {
323+
this.lasti = lasti;
324+
}
312325
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/generator/GeneratorBuiltins.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,11 @@ static Object getFrame(PGenerator self,
539539
PArguments.setClosure(arguments, PArguments.getClosure(self.getArguments()));
540540
PArguments.setGeneratorFrame(arguments, generatorFrame);
541541
frame.setArguments(arguments);
542+
if (self.isStarted()) {
543+
// Hack: Fake bytecode to make inspect.getgeneratorstate distinguish suspended
544+
// and unstarted generators
545+
frame.setLasti(10000);
546+
}
542547
return frame;
543548
}
544549
}

0 commit comments

Comments
 (0)