Skip to content

Commit da038ac

Browse files
committed
Fix: use correct number of characters in result tuple.
1 parent 1e17ff7 commit da038ac

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,16 +334,16 @@ public abstract static class RawEncodeNode extends EncodeBaseNode {
334334

335335
@Specialization
336336
PTuple encode(String self, @SuppressWarnings("unused") PNone none) {
337-
return encodeString(self, "strict");
337+
return encode(self, "strict");
338338
}
339339

340340
@Specialization
341341
PTuple encode(String self, String errors) {
342-
return encodeString(self, errors);
342+
return factory().createTuple(encodeString(self, errors));
343343
}
344344

345345
@TruffleBoundary
346-
private PTuple encodeString(String self, String errors) {
346+
private Object[] encodeString(String self, String errors) {
347347
CodingErrorAction errorAction = convertCodingErrorAction(errors);
348348

349349
try {
@@ -352,6 +352,7 @@ private PTuple encodeString(String self, String errors) {
352352
int n = encoded.remaining();
353353
ByteBuffer buf = ByteBuffer.allocate(n);
354354
assert n % Integer.BYTES == 0;
355+
int codePoints = n / Integer.BYTES;
355356

356357
while (encoded.hasRemaining()) {
357358
int int1 = encoded.getInt();
@@ -371,7 +372,8 @@ private PTuple encodeString(String self, String errors) {
371372
n = buf.remaining();
372373
byte[] data = new byte[n];
373374
buf.get(data);
374-
return factory().createTuple(new Object[]{factory().createBytes(data), self.length()});
375+
// TODO(fa): bytes object creation should not be behind a TruffleBoundary
376+
return new Object[]{factory().createBytes(data), codePoints};
375377
} catch (CharacterCodingException e) {
376378
throw raise(UnicodeEncodeError, "%s", e.getMessage());
377379
}

0 commit comments

Comments
 (0)