Skip to content

Commit 2a6f88c

Browse files
committed
Add test for throwing native exception into generators
1 parent ba8c598 commit 2a6f88c

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_err.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,3 +769,22 @@ def test_setstate(self):
769769
e = ExceptionSubclass()
770770
e.__setstate__({'foo': 'bar'})
771771
assert e.foo == 'bar'
772+
773+
def test_throw(self):
774+
def gen():
775+
try:
776+
yield
777+
except Exception as e:
778+
yield e
779+
780+
g = gen()
781+
next(g)
782+
e = g.throw(ExceptionSubclass)
783+
assert type(e) == ExceptionSubclass
784+
785+
g = gen()
786+
next(g)
787+
e = ExceptionSubclass()
788+
e1 = g.throw(e)
789+
assert e1 is e
790+

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/BaseExceptionBuiltins.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import com.oracle.graal.python.runtime.exception.PythonErrorType;
9090
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
9191
import com.oracle.truffle.api.CompilerDirectives;
92+
import com.oracle.truffle.api.CompilerDirectives.ValueType;
9293
import com.oracle.truffle.api.dsl.Bind;
9394
import com.oracle.truffle.api.dsl.Cached;
9495
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -347,20 +348,20 @@ static Object doSetTraceback(Object self, PTraceback tb,
347348
@GenerateNodeFactory
348349
abstract static class DictNode extends PythonBinaryBuiltinNode {
349350
@Specialization
350-
static PNone dict(PBaseException self, PDict mapping,
351+
static PNone dict(Object self, PDict mapping,
351352
@Cached SetDictNode setDict) {
352353
setDict.execute(self, mapping);
353354
return PNone.NONE;
354355
}
355356

356357
@Specialization(guards = "isNoValue(mapping)")
357-
Object dict(PBaseException self, @SuppressWarnings("unused") PNone mapping,
358+
Object dict(Object self, @SuppressWarnings("unused") PNone mapping,
358359
@Cached GetOrCreateDictNode getDict) {
359360
return getDict.execute(self);
360361
}
361362

362363
@Specialization(guards = {"!isNoValue(mapping)", "!isDict(mapping)"})
363-
PNone dict(@SuppressWarnings("unused") PBaseException self, Object mapping) {
364+
PNone dict(@SuppressWarnings("unused") Object self, Object mapping) {
364365
throw raise(TypeError, ErrorMessages.DICT_MUST_BE_SET_TO_DICT, mapping);
365366
}
366367
}
@@ -449,12 +450,12 @@ Object str(VirtualFrame frame, Object self,
449450
@Builtin(name = J___SETSTATE__, minNumOfPositionalArgs = 2)
450451
@GenerateNodeFactory
451452
public abstract static class BaseExceptionSetStateNode extends PythonBinaryBuiltinNode {
452-
@CompilerDirectives.ValueType
453+
@ValueType
453454
static final class ExcState {
454455
private final HashingStorage dictStorage;
455-
private final PBaseException exception;
456+
private final Object exception;
456457

457-
ExcState(HashingStorage dictStorage, PBaseException exception) {
458+
ExcState(HashingStorage dictStorage, Object exception) {
458459
this.dictStorage = dictStorage;
459460
this.exception = exception;
460461
}
@@ -479,7 +480,7 @@ public static ExcState doIt(Frame frame, @SuppressWarnings("unused") Node inlini
479480
}
480481

481482
@Specialization
482-
Object setDict(VirtualFrame frame, PBaseException self, PDict state,
483+
Object setDict(VirtualFrame frame, Object self, PDict state,
483484
@Cached ForEachKW forEachKW,
484485
@Cached HashingStorageForEach forEachNode) {
485486
final HashingStorage dictStorage = state.getDictStorage();
@@ -488,7 +489,7 @@ Object setDict(VirtualFrame frame, PBaseException self, PDict state,
488489
}
489490

490491
@Specialization(guards = "!isDict(state)")
491-
Object generic(@SuppressWarnings("unused") PBaseException self, Object state) {
492+
Object generic(@SuppressWarnings("unused") Object self, Object state) {
492493
if (state != PNone.NONE) {
493494
throw raise(TypeError, STATE_IS_NOT_A_DICT);
494495
}

0 commit comments

Comments
 (0)