Skip to content

Commit 6b9962d

Browse files
committed
quick fix for 'B' type specifier in arrays
1 parent 46273ae commit 6b9962d

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ protected boolean isByteArray(String typeCode) {
117117
return typeCode.charAt(0) == 'b';
118118
}
119119

120+
protected boolean isCharArray(String typeCode) {
121+
return typeCode.charAt(0) == 'B';
122+
}
123+
120124
protected boolean isDoubleArray(String typeCode) {
121125
return typeCode.charAt(0) == 'd';
122126
}
@@ -146,6 +150,31 @@ PArray arrayByteInitializer(VirtualFrame frame, LazyPythonClass cls, @SuppressWa
146150
return factory().createArray(cls, byteArray);
147151
}
148152

153+
@Specialization(guards = "isCharArray(typeCode)")
154+
PArray arrayCharInitializer(VirtualFrame frame, LazyPythonClass cls, @SuppressWarnings("unused") String typeCode, PSequence initializer,
155+
@Cached("createCast()") CastToByteNode castToByteNode,
156+
@Cached("create()") GetIteratorNode getIterator,
157+
@Cached("create()") GetNextNode next,
158+
@Cached("create()") IsBuiltinClassProfile errorProfile,
159+
@Cached("create()") SequenceNodes.LenNode lenNode) {
160+
Object iter = getIterator.executeWith(frame, initializer);
161+
int i = 0;
162+
byte[] byteArray = new byte[lenNode.execute(initializer)];
163+
164+
while (true) {
165+
Object nextValue;
166+
try {
167+
nextValue = next.execute(frame, iter);
168+
} catch (PException e) {
169+
e.expectStopIteration(errorProfile);
170+
break;
171+
}
172+
byteArray[i++] = castToByteNode.execute(nextValue);
173+
}
174+
175+
return factory().createArray(cls, byteArray);
176+
}
177+
149178
@Specialization(guards = "isIntArray(typeCode)")
150179
PArray arrayIntInitializer(VirtualFrame frame, LazyPythonClass cls, @SuppressWarnings("unused") String typeCode, PSequence initializer,
151180
@Cached("create()") GetIteratorNode getIterator,
@@ -238,9 +267,9 @@ PArray arrayDoubleInitializer(VirtualFrame frame, LazyPythonClass cls, @Suppress
238267
@Specialization
239268
@TruffleBoundary
240269
PArray arrayWithObjectInitializer(@SuppressWarnings("unused") LazyPythonClass cls, @SuppressWarnings("unused") String typeCode, Object initializer) {
241-
if (!(isIntArray(typeCode) || isByteArray(typeCode) || isDoubleArray(typeCode))) {
270+
if (!(isIntArray(typeCode) || isByteArray(typeCode) || isDoubleArray(typeCode) || isCharArray(typeCode))) {
242271
// TODO implement support for typecodes: b, B, u, h, H, i, I, l, L, q, Q, f or d
243-
throw raise(ValueError, "bad typecode (must be i, d, b, or l)");
272+
throw raise(ValueError, "bad typecode (must be i, d, b, B, or l)");
244273
}
245274
throw new RuntimeException("Unsupported initializer " + initializer);
246275
}
@@ -259,8 +288,9 @@ private PArray makeEmptyArray(LazyPythonClass cls, char type) {
259288
switch (type) {
260289
case 'c':
261290
case 'b':
291+
return factory().createArray(cls, new byte[0]);
262292
case 'B':
263-
return factory().createArray(cls, new char[0]);
293+
return factory().createArray(cls, new byte[0]);
264294
case 'i':
265295
return factory().createArray(cls, new int[0]);
266296
case 'd':

0 commit comments

Comments
 (0)