Skip to content

Commit 267b7c3

Browse files
committed
Remove duplicate code and clean up unnecessary guards
1 parent c60c85b commit 267b7c3

File tree

18 files changed

+65
-172
lines changed

18 files changed

+65
-172
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,14 @@ Object encode(Object str, @SuppressWarnings("unused") Object errors) {
146146
@Builtin(name = "unicode_escape_decode", minNumOfPositionalArgs = 1, parameterNames = {"str", "errors"})
147147
@GenerateNodeFactory
148148
abstract static class UnicodeEscapeDecode extends PythonBinaryBuiltinNode {
149-
@Specialization(guards = "isBytes(bytes)")
150-
Object encode(VirtualFrame frame, Object bytes, @SuppressWarnings("unused") PNone errors,
149+
@Specialization
150+
Object encode(VirtualFrame frame, PBytesLike bytes, @SuppressWarnings("unused") PNone errors,
151151
@Shared("toBytes") @Cached("create()") BytesNodes.ToBytesNode toBytes) {
152152
return encode(frame, bytes, "", toBytes);
153153
}
154154

155-
@Specialization(guards = "isBytes(bytes)")
156-
Object encode(VirtualFrame frame, Object bytes, @SuppressWarnings("unused") String errors,
155+
@Specialization
156+
Object encode(VirtualFrame frame, PBytesLike bytes, @SuppressWarnings("unused") String errors,
157157
@Shared("toBytes") @Cached("create()") BytesNodes.ToBytesNode toBytes) {
158158
// for now we'll just parse this as a String, ignoring any error strategies
159159
PythonCore core = getCore();

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
import com.oracle.graal.python.builtins.objects.PNone;
4545
import com.oracle.graal.python.builtins.objects.array.PArray;
4646
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
47-
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
4847
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
48+
import com.oracle.graal.python.builtins.objects.bytes.PBytesLike;
4949
import com.oracle.graal.python.builtins.objects.code.CodeNodes;
5050
import com.oracle.graal.python.builtins.objects.code.CodeNodes.CreateCodeNode;
5151
import com.oracle.graal.python.builtins.objects.code.PCode;
@@ -162,21 +162,12 @@ abstract static class LoadsNode extends PythonBuiltinNode {
162162

163163
@Child private UnmarshallerNode marshaller = UnmarshallerNode.create();
164164

165-
@SuppressWarnings("unused")
166-
@Specialization
167-
Object doit(VirtualFrame frame, PBytes bytes,
168-
@Cached("create()") BytesNodes.ToBytesNode toBytesNode) {
169-
return marshaller.execute(frame, toBytesNode.execute(frame, bytes), CURRENT_VERSION);
170-
}
171-
172-
@SuppressWarnings("unused")
173165
@Specialization
174-
Object doit(VirtualFrame frame, PByteArray bytes,
166+
Object doit(VirtualFrame frame, PBytesLike bytes,
175167
@Cached("create()") BytesNodes.ToBytesNode toBytesNode) {
176168
return marshaller.execute(frame, toBytesNode.execute(frame, bytes), CURRENT_VERSION);
177169
}
178170

179-
@SuppressWarnings("unused")
180171
@Specialization
181172
Object doit(VirtualFrame frame, PMemoryView bytes,
182173
@Cached("create()") BytesNodes.ToBytesNode toBytesNode) {
@@ -370,14 +361,7 @@ void handleInternedString(InternedString v, int version, DataOutputStream buffer
370361
}
371362

372363
@Specialization
373-
void handleBytesLike(VirtualFrame frame, PBytes v, int version, DataOutputStream buffer,
374-
@Cached("create()") BytesNodes.ToBytesNode toBytesNode) {
375-
writeByte(TYPE_BYTESLIKE, version, buffer);
376-
writeBytes(toBytesNode.execute(frame, v), version, buffer);
377-
}
378-
379-
@Specialization
380-
void handleBytesLike(VirtualFrame frame, PByteArray v, int version, DataOutputStream buffer,
364+
void handleBytesLike(VirtualFrame frame, PBytesLike v, int version, DataOutputStream buffer,
381365
@Cached("create()") BytesNodes.ToBytesNode toBytesNode) {
382366
writeByte(TYPE_BYTESLIKE, version, buffer);
383367
writeBytes(toBytesNode.execute(frame, v), version, buffer);

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@
9393
import com.oracle.graal.python.builtins.modules.PosixModuleBuiltinsFactory.StatNodeFactory;
9494
import com.oracle.graal.python.builtins.objects.PNone;
9595
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
96-
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
9796
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
97+
import com.oracle.graal.python.builtins.objects.bytes.PBytesLike;
9898
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
9999
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.LenNode;
100100
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
@@ -1189,13 +1189,7 @@ private static byte[] stringToBytes(String data) {
11891189
}
11901190

11911191
@Specialization
1192-
Object write(VirtualFrame frame, int fd, PBytes data,
1193-
@Cached("createClassProfile()") ValueProfile channelClassProfile) {
1194-
return write(frame, fd, getByteArray(data.getSequenceStorage()), channelClassProfile);
1195-
}
1196-
1197-
@Specialization
1198-
Object write(VirtualFrame frame, int fd, PByteArray data,
1192+
Object write(VirtualFrame frame, int fd, PBytesLike data,
11991193
@Cached("createClassProfile()") ValueProfile channelClassProfile) {
12001194
return write(frame, fd, getByteArray(data.getSequenceStorage()), channelClassProfile);
12011195
}

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@
5353
import com.oracle.graal.python.builtins.objects.PNone;
5454
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
5555
import com.oracle.graal.python.builtins.objects.bytes.BytesUtils;
56-
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
57-
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
56+
import com.oracle.graal.python.builtins.objects.bytes.PBytesLike;
5857
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
5958
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.ToByteArrayNodeGen;
6059
import com.oracle.graal.python.builtins.objects.memoryview.PMemoryView;
@@ -145,13 +144,7 @@ Object run(String str) {
145144
}
146145

147146
@Specialization
148-
Object run(PBytes str) {
149-
byte[] bytes = doBytes(getToByteArrayNode().execute(str.getSequenceStorage()));
150-
return factory().createByteArray(bytes);
151-
}
152-
153-
@Specialization
154-
Object run(PByteArray str) {
147+
Object run(PBytesLike str) {
155148
byte[] bytes = doBytes(getToByteArrayNode().execute(str.getSequenceStorage()));
156149
return factory().createByteArray(bytes);
157150
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import com.oracle.graal.python.builtins.objects.PNotImplemented;
4343
import com.oracle.graal.python.builtins.objects.bytes.BytesBuiltins.BytesLikeNoGeneralizationNode;
4444
import com.oracle.graal.python.builtins.objects.common.IndexNodes.NormalizeIndexNode;
45-
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
4645
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
4746
import com.oracle.graal.python.builtins.objects.iterator.IteratorNodes;
4847
import com.oracle.graal.python.builtins.objects.memoryview.PMemoryView;
@@ -101,9 +100,8 @@ protected Object doGeneric(Object self, Object idx) {
101100
public abstract static class IAddNode extends PythonBinaryBuiltinNode {
102101
@Specialization
103102
public PByteArray add(PByteArray self, PBytesLike other,
104-
@Cached SequenceNodes.GetSequenceStorageNode getStorage,
105103
@Cached("create()") SequenceStorageNodes.ConcatNode concatNode) {
106-
SequenceStorage res = concatNode.execute(self.getSequenceStorage(), getStorage.execute(other));
104+
SequenceStorage res = concatNode.execute(self.getSequenceStorage(), other.getSequenceStorage());
107105
updateSequenceStorage(self, res);
108106
return self;
109107
}

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

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@
6262
import com.oracle.graal.python.builtins.objects.PNotImplemented;
6363
import com.oracle.graal.python.builtins.objects.bytes.BytesBuiltinsFactory.BytesLikeNoGeneralizationNodeGen;
6464
import com.oracle.graal.python.builtins.objects.common.IndexNodes.NormalizeIndexNode;
65-
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
6665
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetObjectArrayNode;
67-
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.GetSequenceStorageNode;
6866
import com.oracle.graal.python.builtins.objects.common.SequenceNodesFactory.GetObjectArrayNodeGen;
6967
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
7068
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GenNodeSupplier;
@@ -182,14 +180,13 @@ public abstract static class EqNode extends PythonBinaryBuiltinNode {
182180
@Child private SequenceStorageNodes.CmpNode eqNode;
183181

184182
@Specialization
185-
boolean eq(VirtualFrame frame, PBytesLike self, PBytesLike other,
186-
@Cached GetSequenceStorageNode getStorage) {
187-
return getEqNode().execute(frame, getStorage.execute(self), getStorage.execute(other));
183+
boolean eq(VirtualFrame frame, PBytesLike self, PBytesLike other) {
184+
return getEqNode().execute(frame, self.getSequenceStorage(), other.getSequenceStorage());
188185
}
189186

190187
@Fallback
191188
public Object eq(Object self, @SuppressWarnings("unused") Object other) {
192-
if (PGuards.isBytes(self)) {
189+
if (self instanceof PBytesLike) {
193190
return PNotImplemented.NOT_IMPLEMENTED;
194191
}
195192
throw raise(TypeError, ErrorMessages.DESCRIPTOR_REQUIRES_OBJ, "__eq__", "bytes-like", self);
@@ -211,14 +208,13 @@ public abstract static class NeNode extends PythonBinaryBuiltinNode {
211208
@Child SequenceStorageNodes.CmpNode eqNode;
212209

213210
@Specialization
214-
boolean ne(VirtualFrame frame, PBytesLike self, PBytesLike other,
215-
@Cached GetSequenceStorageNode getStorage) {
216-
return !getEqNode().execute(frame, getStorage.execute(self), getStorage.execute(other));
211+
boolean ne(VirtualFrame frame, PBytesLike self, PBytesLike other) {
212+
return !getEqNode().execute(frame, self.getSequenceStorage(), other.getSequenceStorage());
217213
}
218214

219215
@Fallback
220216
Object ne(Object self, @SuppressWarnings("unused") Object other) {
221-
if (PGuards.isBytes(self)) {
217+
if (self instanceof PBytesLike) {
222218
return PNotImplemented.NOT_IMPLEMENTED;
223219
}
224220
throw raise(TypeError, ErrorMessages.DESCRIPTOR_REQUIRES_OBJ, "__ne__", "bytes-like", self);
@@ -345,9 +341,8 @@ public static boolean doByte(PBytes byteArray) {
345341

346342
@Specialization
347343
static boolean doLen(PBytesLike operand,
348-
@Cached("create()") SequenceStorageNodes.LenNode lenNode,
349-
@Cached GetSequenceStorageNode getSelfStorage) {
350-
return lenNode.execute(getSelfStorage.execute(operand)) != 0;
344+
@Cached("create()") SequenceStorageNodes.LenNode lenNode) {
345+
return lenNode.execute(operand.getSequenceStorage()) != 0;
351346
}
352347

353348
@Fallback
@@ -362,17 +357,15 @@ public abstract static class AddNode extends PythonBinaryBuiltinNode {
362357

363358
@Specialization
364359
public Object add(PBytes left, PBytesLike right,
365-
@Cached("create()") SequenceStorageNodes.ConcatNode concatNode,
366-
@Cached GetSequenceStorageNode getStorage) {
367-
ByteSequenceStorage res = (ByteSequenceStorage) concatNode.execute(left.getSequenceStorage(), getStorage.execute(right));
360+
@Cached("create()") SequenceStorageNodes.ConcatNode concatNode) {
361+
ByteSequenceStorage res = (ByteSequenceStorage) concatNode.execute(left.getSequenceStorage(), right.getSequenceStorage());
368362
return factory().createBytes(res);
369363
}
370364

371365
@Specialization
372366
public Object add(PByteArray self, PBytesLike other,
373-
@Cached SequenceStorageNodes.ConcatNode concatNode,
374-
@Cached GetSequenceStorageNode getStorage) {
375-
SequenceStorage res = concatNode.execute(self.getSequenceStorage(), getStorage.execute(other));
367+
@Cached SequenceStorageNodes.ConcatNode concatNode) {
368+
SequenceStorage res = concatNode.execute(self.getSequenceStorage(), other.getSequenceStorage());
376369
return factory().createByteArray(res);
377370
}
378371

@@ -488,7 +481,7 @@ Object doByteArray(VirtualFrame frame, PByteArray self, Object right,
488481

489482
private byte[] format(VirtualFrame frame, Object self, Object right, PythonObjectLibrary selfLib, LookupAndCallBinaryNode getItemNode, TupleBuiltins.GetItemNode getTupleItemNode,
490483
PythonContext context) {
491-
assert self instanceof PBytes || self instanceof PByteArray;
484+
assert self instanceof PBytesLike;
492485
Object state = IndirectCallContext.enter(frame, context, this);
493486
try {
494487
BytesFormatProcessor formatter = new BytesFormatProcessor(context.getCore(), getItemNode, getTupleItemNode, selfLib.getBufferBytes(self));
@@ -586,9 +579,8 @@ public Object doGeneric(Object self, Object arg) {
586579
public abstract static class LenNode extends PythonUnaryBuiltinNode {
587580
@Specialization
588581
public static int len(PBytesLike self,
589-
@Cached("create()") SequenceStorageNodes.LenNode lenNode,
590-
@Cached GetSequenceStorageNode getSelfStorage) {
591-
return lenNode.execute(getSelfStorage.execute(self));
582+
@Cached("create()") SequenceStorageNodes.LenNode lenNode) {
583+
return lenNode.execute(self.getSequenceStorage());
592584
}
593585
}
594586

@@ -607,23 +599,20 @@ private int getLength(SequenceStorage s) {
607599

608600
@Specialization
609601
boolean contains(VirtualFrame frame, PBytesLike self, PBytesLike other,
610-
@Cached("create()") BytesNodes.FindNode findNode,
611-
@Shared("getSelfStorage") @Cached GetSequenceStorageNode getSelfStorage) {
612-
return findNode.execute(frame, self, other, 0, getLength(getSelfStorage.execute(self))) != -1;
602+
@Cached("create()") BytesNodes.FindNode findNode) {
603+
return findNode.execute(frame, self, other, 0, getLength(self.getSequenceStorage())) != -1;
613604
}
614605

615606
@Specialization
616607
boolean contains(VirtualFrame frame, PBytesLike self, int other,
617-
@Cached("create()") BytesNodes.FindNode findNode,
618-
@Shared("getSelfStorage") @Cached GetSequenceStorageNode getSelfStorage) {
619-
return findNode.execute(frame, self, other, 0, getLength(getSelfStorage.execute(self))) != -1;
608+
@Cached("create()") BytesNodes.FindNode findNode) {
609+
return findNode.execute(frame, self, other, 0, getLength(self.getSequenceStorage())) != -1;
620610
}
621611

622612
@Specialization
623613
boolean contains(VirtualFrame frame, PBytesLike self, long other,
624-
@Cached("create()") BytesNodes.FindNode findNode,
625-
@Shared("getSelfStorage") @Cached GetSequenceStorageNode getSelfStorage) {
626-
return findNode.execute(frame, self, other, 0, getLength(getSelfStorage.execute(self))) != -1;
614+
@Cached("create()") BytesNodes.FindNode findNode) {
615+
return findNode.execute(frame, self, other, 0, getLength(self.getSequenceStorage())) != -1;
627616
}
628617

629618
@Specialization(guards = {"!isBytes(other)"})
@@ -878,17 +867,15 @@ public abstract static class ByteArrayIndexNode extends PythonQuaternaryBuiltinN
878867
@Specialization
879868
int index(VirtualFrame frame, PBytesLike byteArray, Object arg, @SuppressWarnings("unused") PNone start, @SuppressWarnings("unused") PNone end,
880869
@Shared("len") @Cached SequenceStorageNodes.LenNode lenNode,
881-
@Shared("storage") @Cached SequenceNodes.GetSequenceStorageNode getStorageNode,
882870
@Shared("findNode") @Cached("create()") BytesNodes.FindNode findNode) {
883-
return checkResult(findNode.execute(frame, byteArray, arg, 0, lenNode.execute(getStorageNode.execute(byteArray))));
871+
return checkResult(findNode.execute(frame, byteArray, arg, 0, lenNode.execute(byteArray.getSequenceStorage())));
884872
}
885873

886874
@Specialization(guards = {"!isPNone(start)"})
887875
int indexWithStart(VirtualFrame frame, PBytesLike byteArray, Object arg, Object start, @SuppressWarnings("unused") PNone end,
888-
@Shared("storage") @Cached SequenceNodes.GetSequenceStorageNode getStorageNode,
889876
@Shared("len") @Cached SequenceStorageNodes.LenNode lenNode,
890877
@Shared("findNode") @Cached("create()") BytesNodes.FindNode findNode) {
891-
return checkResult(findNode.execute(frame, byteArray, arg, start, lenNode.execute(getStorageNode.execute(byteArray))));
878+
return checkResult(findNode.execute(frame, byteArray, arg, start, lenNode.execute(byteArray.getSequenceStorage())));
892879
}
893880

894881
@Specialization(guards = {"!isPNone(end)"})
@@ -943,25 +930,22 @@ abstract static class FindNode extends PythonBuiltinNode {
943930
@Specialization
944931
static int find(VirtualFrame frame, PBytesLike self, Object sub, @SuppressWarnings("unused") PNone start, @SuppressWarnings("unused") PNone end,
945932
@Shared("lenNode") @Cached SequenceStorageNodes.LenNode lenNode,
946-
@Shared("findNode") @Cached BytesNodes.FindNode findNode,
947-
@Cached GetSequenceStorageNode getSelfStorage) {
948-
return find(frame, self, sub, 0, lenNode.execute(getSelfStorage.execute(self)), findNode);
933+
@Shared("findNode") @Cached BytesNodes.FindNode findNode) {
934+
return find(frame, self, sub, 0, lenNode.execute(self.getSequenceStorage()), findNode);
949935
}
950936

951937
@Specialization
952938
static int find(VirtualFrame frame, PBytesLike self, Object sub, int start, @SuppressWarnings("unused") PNone end,
953939
@Shared("lenNode") @Cached SequenceStorageNodes.LenNode lenNode,
954-
@Shared("findNode") @Cached BytesNodes.FindNode findNode,
955-
@Cached GetSequenceStorageNode getSelfStorage) {
956-
return find(frame, self, sub, start, lenNode.execute(getSelfStorage.execute(self)), findNode);
940+
@Shared("findNode") @Cached BytesNodes.FindNode findNode) {
941+
return find(frame, self, sub, start, lenNode.execute(self.getSequenceStorage()), findNode);
957942
}
958943

959944
@Specialization
960945
static int find(VirtualFrame frame, PBytesLike self, Object sub, Object start, @SuppressWarnings("unused") PNone end,
961946
@Shared("lenNode") @Cached SequenceStorageNodes.LenNode lenNode,
962-
@Shared("findNode") @Cached BytesNodes.FindNode findNode,
963-
@Cached GetSequenceStorageNode getSelfStorage) {
964-
return find(frame, self, sub, start, lenNode.execute(getSelfStorage.execute(self)), findNode);
947+
@Shared("findNode") @Cached BytesNodes.FindNode findNode) {
948+
return find(frame, self, sub, start, lenNode.execute(self.getSequenceStorage()), findNode);
965949
}
966950

967951
@Specialization

0 commit comments

Comments
 (0)