Skip to content

Commit d36bb4b

Browse files
committed
Introduce PNodeWithRaise
1 parent 1bfb5b8 commit d36bb4b

File tree

5 files changed

+138
-128
lines changed

5 files changed

+138
-128
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesNodes.java

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import java.util.ArrayList;
4949
import java.util.Arrays;
5050

51-
import com.oracle.graal.python.PythonLanguage;
5251
import com.oracle.graal.python.annotations.ClinicConverterFactory;
5352
import com.oracle.graal.python.annotations.ClinicConverterFactory.ArgumentIndex;
5453
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
@@ -70,7 +69,7 @@
7069
import com.oracle.graal.python.nodes.ErrorMessages;
7170
import com.oracle.graal.python.nodes.PGuards;
7271
import com.oracle.graal.python.nodes.PNodeWithContext;
73-
import com.oracle.graal.python.nodes.PRaiseNode;
72+
import com.oracle.graal.python.nodes.PNodeWithRaise;
7473
import com.oracle.graal.python.nodes.SpecialMethodNames;
7574
import com.oracle.graal.python.nodes.control.GetNextNode;
7675
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode;
@@ -109,32 +108,32 @@ public abstract static class CreateBytesNode extends Node {
109108
public abstract PBytesLike execute(PythonObjectFactory factory, PBytesLike basedOn, Object bytes);
110109

111110
@Specialization
112-
PBytesLike bytes(PythonObjectFactory factory, @SuppressWarnings("unused") PBytes basedOn, byte[] bytes) {
111+
static PBytesLike bytes(PythonObjectFactory factory, @SuppressWarnings("unused") PBytes basedOn, byte[] bytes) {
113112
return factory.createBytes(bytes);
114113
}
115114

116115
@Specialization
117-
PBytesLike bytearray(PythonObjectFactory factory, @SuppressWarnings("unused") PByteArray basedOn, byte[] bytes) {
116+
static PBytesLike bytearray(PythonObjectFactory factory, @SuppressWarnings("unused") PByteArray basedOn, byte[] bytes) {
118117
return factory.createByteArray(bytes);
119118
}
120119

121120
@Specialization
122-
PBytesLike bytes(PythonObjectFactory factory, @SuppressWarnings("unused") PBytes basedOn, SequenceStorage bytes) {
121+
static PBytesLike bytes(PythonObjectFactory factory, @SuppressWarnings("unused") PBytes basedOn, SequenceStorage bytes) {
123122
return factory.createBytes(bytes);
124123
}
125124

126125
@Specialization
127-
PBytesLike bytearray(PythonObjectFactory factory, @SuppressWarnings("unused") PByteArray basedOn, SequenceStorage bytes) {
126+
static PBytesLike bytearray(PythonObjectFactory factory, @SuppressWarnings("unused") PByteArray basedOn, SequenceStorage bytes) {
128127
return factory.createByteArray(bytes);
129128
}
130129

131130
@Specialization
132-
PBytesLike bytes(PythonObjectFactory factory, @SuppressWarnings("unused") PBytes basedOn, PBytesLike bytes) {
131+
static PBytesLike bytes(PythonObjectFactory factory, @SuppressWarnings("unused") PBytes basedOn, PBytesLike bytes) {
133132
return factory.createBytes(bytes.getSequenceStorage());
134133
}
135134

136135
@Specialization
137-
PBytesLike bytearray(PythonObjectFactory factory, @SuppressWarnings("unused") PByteArray basedOn, PBytesLike bytes) {
136+
static PBytesLike bytearray(PythonObjectFactory factory, @SuppressWarnings("unused") PByteArray basedOn, PBytesLike bytes) {
138137
return factory.createByteArray(bytes.getSequenceStorage());
139138
}
140139

@@ -195,11 +194,9 @@ public static BytesJoinNode create() {
195194
}
196195

197196
@ImportStatic({PGuards.class, SpecialMethodNames.class})
198-
public abstract static class ToBytesNode extends PNodeWithContext {
197+
public abstract static class ToBytesNode extends PNodeWithRaise {
199198
private static final String DEFAULT_FORMAT = "expected a bytes-like object, %p found";
200199

201-
@Child private PRaiseNode raise = PRaiseNode.create();
202-
203200
private final PythonBuiltinClassType errorType;
204201
private final String errorMessageFormat;
205202

@@ -234,7 +231,7 @@ static byte[] doBuffer(Object buffer,
234231

235232
@Fallback
236233
byte[] doError(Object obj) {
237-
throw raise.raise(errorType, errorMessageFormat, obj);
234+
throw raise(errorType, errorMessageFormat, obj);
238235
}
239236

240237
public static ToBytesNode create() {
@@ -246,8 +243,7 @@ public static ToBytesNode create(PythonBuiltinClassType errorType, String errorM
246243
}
247244
}
248245

249-
public abstract static class FindNode extends PNodeWithContext {
250-
@Child private PRaiseNode raise = PRaiseNode.create();
246+
public abstract static class FindNode extends PNodeWithRaise {
251247

252248
public abstract int execute(Object self, int len1, Object sub, int start, int end);
253249

@@ -369,7 +365,7 @@ protected int findElement(byte[] haystack, byte sub, int start, int end) {
369365
@SuppressWarnings("unused")
370366
@Fallback
371367
int doError(Object bytes, int len1, Object sub, int start, int end) {
372-
throw raise.raise(TypeError, ErrorMessages.EXPECTED_S_P_FOUND, "a bytes-like object", sub);
368+
throw raise(TypeError, ErrorMessages.EXPECTED_S_P_FOUND, "a bytes-like object", sub);
373369
}
374370

375371
public static FindNode create() {
@@ -412,9 +408,9 @@ public static RFindNode create() {
412408

413409
public static class FromSequenceStorageNode extends Node {
414410

415-
@Node.Child private SequenceStorageNodes.GetItemNode getItemNode;
416-
@Node.Child private CastToByteNode castToByteNode;
417-
@Node.Child private SequenceStorageNodes.LenNode lenNode;
411+
@Child private SequenceStorageNodes.GetItemNode getItemNode;
412+
@Child private CastToByteNode castToByteNode;
413+
@Child private SequenceStorageNodes.LenNode lenNode;
418414

419415
public byte[] execute(VirtualFrame frame, SequenceStorage storage) {
420416
int len = getLenNode().execute(storage);
@@ -581,70 +577,68 @@ public static ExpectStringNode create(@ArgumentIndex int argNum, String classNam
581577

582578
@TypeSystemReference(PythonArithmeticTypes.class)
583579
@ImportStatic(PGuards.class)
584-
public abstract static class BytesInitNode extends Node {
580+
public abstract static class BytesInitNode extends PNodeWithRaise {
585581

586582
public abstract byte[] execute(VirtualFrame frame, Object source, Object encoding, Object errors);
587583

588584
@Specialization
589-
byte[] none(@SuppressWarnings("unused") PNone source, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors) {
585+
static byte[] none(@SuppressWarnings("unused") PNone source, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors) {
590586
return PythonUtils.EMPTY_BYTE_ARRAY;
591587
}
592588

593589
@Specialization(guards = "isByteStorage(source)")
594-
byte[] byteslike(PBytesLike source, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors) {
590+
static byte[] byteslike(PBytesLike source, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors) {
595591
return (byte[]) ((ByteSequenceStorage) source.getSequenceStorage()).getCopyOfInternalArrayObject();
596592
}
597593

598594
@Specialization(guards = "lib.canBeIndex(source)", limit = "3")
599-
byte[] size(VirtualFrame frame, Object source, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors,
595+
static byte[] fromIndex(VirtualFrame frame, Object source, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors,
600596
@CachedLibrary("source") PythonObjectLibrary lib) {
601597
int cap = lib.asSizeWithState(source, PArguments.getThreadState(frame));
602598
return BytesUtils.fromSize(getCore(), cap);
603599
}
604600

605-
@Specialization(guards = {"!isString(source)", "!isNoValue(source)", "!lib.canBeIndex(source)"}, limit = "3")
606-
public byte[] iterable(VirtualFrame frame, Object source, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors,
601+
@Specialization(guards = {"!isString(source)", "!isNoValue(source)", "!lib.canBeIndex(source)", "!lib.isBuffer(source)"}, limit = "3")
602+
static byte[] fromIterable(VirtualFrame frame, Object source, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors,
607603
@Cached IteratorNodes.GetLength lenNode,
608604
@Cached("createCast()") IterableToByteNode toByteNode,
609605
@SuppressWarnings("unused") @CachedLibrary("source") PythonObjectLibrary lib) {
610606
return toByteNode.execute(frame, source, lenNode.execute(frame, source));
611607
}
612608

613609
@Specialization
614-
byte[] fromString(String source, String encoding, @SuppressWarnings("unused") Object errors,
615-
@Cached PRaiseNode raise) {
610+
byte[] fromString(String source, String encoding, @SuppressWarnings("unused") Object errors) {
616611
String e = errors instanceof String ? (String) errors : "strict";
617-
return BytesBuiltins.stringToByte(source, encoding, e, raise);
612+
return BytesBuiltins.stringToByte(source, encoding, e, getRaiseNode());
618613
}
619614

620615
@Specialization
621616
@SuppressWarnings("unused")
622-
byte[] fromString(String source, PNone encoding, Object errors,
623-
@Cached PRaiseNode raise) {
624-
throw raise.raise(TypeError, ErrorMessages.STRING_ARG_WO_ENCODING);
617+
byte[] fromString(String source, PNone encoding, Object errors) {
618+
throw raise(TypeError, ErrorMessages.STRING_ARG_WO_ENCODING);
625619
}
626620

627621
@Fallback
628622
@SuppressWarnings("unused")
629623
public byte[] error(Object source, Object encoding, Object errors) {
630624
if (PGuards.isNone(encoding)) {
631-
throw PythonLanguage.getCore().raise(TypeError, ErrorMessages.ENCODING_ARG_WO_STRING);
625+
throw raise(TypeError, ErrorMessages.ENCODING_ARG_WO_STRING);
632626
}
633-
throw PythonLanguage.getCore().raise(TypeError, ErrorMessages.ERRORS_WITHOUT_STR_ARG);
627+
throw raise(TypeError, ErrorMessages.ERRORS_WITHOUT_STR_ARG);
634628
}
635629

636-
protected static BytesNodes.IterableToByteNode createCast() {
637-
return BytesNodes.IterableToByteNode.create(val -> PythonLanguage.getCore().raise(TypeError, ErrorMessages.ERRORS_WITHOUT_STR_ARG));
630+
protected BytesNodes.IterableToByteNode createCast() {
631+
return BytesNodes.IterableToByteNode.create(val -> raise(TypeError, ErrorMessages.ERRORS_WITHOUT_STR_ARG));
638632
}
639633
}
640634

641635
@GenerateNodeFactory
642-
public abstract static class ByteToHexNode extends PNodeWithContext {
636+
public abstract static class ByteToHexNode extends PNodeWithRaise {
643637

644638
public abstract String execute(byte[] argbuf, int arglen, byte sep, int bytesPerSepGroup);
645639

646640
@Specialization(guards = "bytesPerSepGroup == 0")
647-
public String zero(byte[] argbuf, int arglen, @SuppressWarnings("unused") byte sep, @SuppressWarnings("unused") int bytesPerSepGroup) {
641+
public static String zero(byte[] argbuf, int arglen, @SuppressWarnings("unused") byte sep, @SuppressWarnings("unused") int bytesPerSepGroup) {
648642

649643
int resultlen = arglen * 2;
650644
byte[] retbuf = new byte[resultlen];
@@ -661,16 +655,15 @@ public String zero(byte[] argbuf, int arglen, @SuppressWarnings("unused") byte s
661655
@Specialization(guards = "bytesPerSepGroup < 0")
662656
public String negative(byte[] argbuf, int arglen, byte sep, int bytesPerSepGroup,
663657
@Cached ConditionProfile earlyExit,
664-
@Cached ConditionProfile memoryError,
665-
@Cached.Shared("error") @Cached PRaiseNode raiseNode) {
658+
@Cached ConditionProfile memoryError) {
666659
if (earlyExit.profile(arglen == 0)) {
667660
return "";
668661
}
669662
int absBytesPerSepGroup = -bytesPerSepGroup;
670663
/* How many sep characters we'll be inserting. */
671664
int resultlen = (arglen - 1) / absBytesPerSepGroup;
672665
if (memoryError.profile(arglen >= SysModuleBuiltins.MAXSIZE / 2 - resultlen)) {
673-
raiseNode.raise(MemoryError);
666+
throw raise(MemoryError);
674667
}
675668

676669
resultlen += arglen * 2;
@@ -702,16 +695,15 @@ public String negative(byte[] argbuf, int arglen, byte sep, int bytesPerSepGroup
702695
@Specialization(guards = "absBytesPerSepGroup > 0")
703696
public String positive(byte[] argbuf, int arglen, byte sep, int absBytesPerSepGroup,
704697
@Cached ConditionProfile earlyExit,
705-
@Cached ConditionProfile memoryError,
706-
@Cached.Shared("error") @Cached PRaiseNode raiseNode) {
698+
@Cached ConditionProfile memoryError) {
707699
if (earlyExit.profile(arglen == 0)) {
708700
return "";
709701
}
710702
/* How many sep characters we'll be inserting. */
711703
int resultlen = (arglen - 1) / absBytesPerSepGroup;
712704

713705
if (memoryError.profile(arglen >= SysModuleBuiltins.MAXSIZE / 2 - resultlen)) {
714-
raiseNode.raise(MemoryError);
706+
throw raise(MemoryError);
715707
}
716708

717709
resultlen += arglen * 2;
@@ -741,10 +733,9 @@ public String positive(byte[] argbuf, int arglen, byte sep, int absBytesPerSepGr
741733
}
742734
}
743735

744-
public abstract static class IterableToByteNode extends PNodeWithContext {
736+
public abstract static class IterableToByteNode extends PNodeWithRaise {
745737

746738
private final Function<Object, Object> typeErrorHandler;
747-
@Child private PRaiseNode raise = PRaiseNode.create();
748739

749740
abstract byte[] execute(VirtualFrame frame, Object iterable, int len);
750741

@@ -753,7 +744,7 @@ protected IterableToByteNode(Function<Object, Object> typeErrorHandler) {
753744
}
754745

755746
@Specialization(guards = "!lib.canBeIndex(iterable)")
756-
public byte[] bytearray(VirtualFrame frame, Object iterable, int len,
747+
public static byte[] bytearray(VirtualFrame frame, Object iterable, int len,
757748
@Cached GetNextNode getNextNode,
758749
@Cached IsBuiltinClassProfile stopIterationProfile,
759750
@Cached CastToByteNode castToByteNode,
@@ -787,9 +778,9 @@ public static IterableToByteNode create(Function<Object, Object> typeErrorHandle
787778

788779
protected CastToByteNode createCast() {
789780
return CastToByteNode.create(val -> {
790-
throw raise.raise(TypeError, ErrorMessages.OBJ_CANNOT_BE_INTERPRETED_AS_INTEGER, "bytes");
781+
throw raise(TypeError, ErrorMessages.OBJ_CANNOT_BE_INTERPRETED_AS_INTEGER, "bytes");
791782
}, val -> {
792-
throw raise.raise(ValueError, ErrorMessages.BYTE_MUST_BE_IN_RANGE);
783+
throw raise(ValueError, ErrorMessages.BYTE_MUST_BE_IN_RANGE);
793784
});
794785
}
795786

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview/MemoryViewNodes.java

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6262
import com.oracle.graal.python.nodes.ErrorMessages;
6363
import com.oracle.graal.python.nodes.PGuards;
64-
import com.oracle.graal.python.nodes.PRaiseNode;
64+
import com.oracle.graal.python.nodes.PNodeWithRaise;
6565
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
6666
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
6767
import com.oracle.graal.python.runtime.PythonContext;
@@ -128,9 +128,7 @@ static int compute(int ndim, int itemsize, int[] shape, int[] strides, int[] sub
128128
}
129129

130130
@ImportStatic(BufferFormat.class)
131-
public abstract static class UnpackValueNode extends Node {
132-
@Child private PRaiseNode raiseNode;
133-
131+
public abstract static class UnpackValueNode extends PNodeWithRaise {
134132
public abstract Object execute(BufferFormat format, String formatStr, byte[] bytes, int offset);
135133

136134
@Specialization(guards = "format != OTHER")
@@ -144,19 +142,10 @@ static Object unpack(BufferFormat format, @SuppressWarnings("unused") String for
144142
Object notImplemented(BufferFormat format, String formatStr, byte[] bytes, int offset) {
145143
throw raise(NotImplementedError, ErrorMessages.MEMORYVIEW_FORMAT_S_NOT_SUPPORTED, formatStr);
146144
}
147-
148-
private PException raise(PythonBuiltinClassType type, String message, Object... args) {
149-
if (raiseNode == null) {
150-
CompilerDirectives.transferToInterpreterAndInvalidate();
151-
raiseNode = insert(PRaiseNode.create());
152-
}
153-
throw raiseNode.raise(type, message, args);
154-
}
155145
}
156146

157147
@ImportStatic({BufferFormat.class, PGuards.class})
158-
public abstract static class PackValueNode extends Node {
159-
@Child private PRaiseNode raiseNode;
148+
public abstract static class PackValueNode extends PNodeWithRaise {
160149
@Child private IsBuiltinClassProfile isOverflowErrorProfile;
161150

162151
public abstract void execute(VirtualFrame frame, BufferFormat format, String formatStr, Object object, byte[] bytes, int offset);
@@ -186,14 +175,6 @@ private PException processException(PException e, String formatStr) {
186175
throw valueError(formatStr);
187176
}
188177

189-
private PException raise(PythonBuiltinClassType type, String message, Object... args) {
190-
if (raiseNode == null) {
191-
CompilerDirectives.transferToInterpreterAndInvalidate();
192-
raiseNode = insert(PRaiseNode.create());
193-
}
194-
throw raiseNode.raise(type, message, args);
195-
}
196-
197178
private IsBuiltinClassProfile getIsOverflowErrorProfile() {
198179
if (isOverflowErrorProfile == null) {
199180
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -421,8 +402,7 @@ public MemoryPointer(Object ptr, int offset) {
421402
}
422403

423404
@ImportStatic(PGuards.class)
424-
abstract static class PointerLookupNode extends Node {
425-
@Child private PRaiseNode raiseNode;
405+
abstract static class PointerLookupNode extends PNodeWithRaise {
426406
@Child private CExtNodes.PCallCapiFunction callCapiFunction;
427407
@Child private PythonObjectLibrary indexLib;
428408
@CompilationFinal private ConditionProfile hasSuboffsetsProfile;
@@ -549,14 +529,6 @@ private ConditionProfile getHasSuboffsetsProfile() {
549529
return hasSuboffsetsProfile;
550530
}
551531

552-
private PException raise(PythonBuiltinClassType type, String message, Object... args) {
553-
if (raiseNode == null) {
554-
CompilerDirectives.transferToInterpreterAndInvalidate();
555-
raiseNode = insert(PRaiseNode.create());
556-
}
557-
throw raiseNode.raise(type, message, args);
558-
}
559-
560532
private CExtNodes.PCallCapiFunction getCallCapiFunction() {
561533
if (callCapiFunction == null) {
562534
CompilerDirectives.transferToInterpreterAndInvalidate();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview/PMemoryView.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
4848
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
4949
import com.oracle.graal.python.nodes.ErrorMessages;
50-
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
50+
import com.oracle.graal.python.nodes.PNodeWithRaise;
5151
import com.oracle.graal.python.util.BufferFormat;
5252
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5353
import com.oracle.truffle.api.dsl.Cached;
@@ -221,7 +221,7 @@ public void setReleased() {
221221
owner = null;
222222
}
223223

224-
public void checkReleased(PythonBuiltinBaseNode node) {
224+
public void checkReleased(PNodeWithRaise node) {
225225
if (isReleased()) {
226226
throw node.raise(ValueError, ErrorMessages.MEMORYVIEW_FORBIDDEN_RELEASED);
227227
}

0 commit comments

Comments
 (0)