Skip to content

Commit 4efc828

Browse files
committed
Report correct length in encoder output tuple
1 parent fe6f327 commit 4efc828

File tree

2 files changed

+9
-31
lines changed

2 files changed

+9
-31
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_codecs.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019, Oracle and/or its affiliates.
1+
# Copyright (c) 2019, 2020, Oracle and/or its affiliates.
22
# Copyright (C) 1996-2017 Python Software Foundation
33
#
44
# Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -151,8 +151,7 @@ def test_escape_encode(self):
151151
for b in range(127, 256):
152152
check(chr(b), ('\\x%02x' % b).encode())
153153
check('\u20ac', br'\u20ac')
154-
# TODO Truffle: not working yet
155-
# check('\U0001d120', br'\U0001d120')
154+
check('\U0001d120', br'\U0001d120')
156155

157156
def test_escape_decode(self):
158157
decode = codecs.unicode_escape_decode

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

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import com.oracle.graal.python.builtins.objects.bytes.PBytesLike;
6666
import com.oracle.graal.python.builtins.objects.common.HashingStorage;
6767
import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
68-
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
6968
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetInternalByteArrayNode;
7069
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodesFactory.GetInternalByteArrayNodeGen;
7170
import com.oracle.graal.python.builtins.objects.dict.PDict;
@@ -344,47 +343,34 @@ protected static CodingErrorAction convertCodingErrorAction(String errors) {
344343
@Builtin(name = "__truffle_encode", minNumOfPositionalArgs = 1, parameterNames = {"obj", "encoding", "errors"})
345344
@GenerateNodeFactory
346345
public abstract static class CodecsEncodeNode extends EncodeBaseNode {
347-
@Child private SequenceStorageNodes.LenNode lenNode;
348346
@Child private HandleEncodingErrorNode handleEncodingErrorNode;
349347

350348
@Specialization(guards = "isString(str)")
351349
Object encode(Object str, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors,
352350
@Shared("castStr") @Cached CastToJavaStringNode castStr) {
353-
String profiledStr = cast(castStr, str);
354-
PBytes bytes = encodeString(str, profiledStr, "utf-8", STRICT);
355-
return factory().createTuple(new Object[]{bytes, getLength(bytes)});
351+
return encodeString(str, cast(castStr, str), "utf-8", STRICT);
356352
}
357353

358354
@Specialization(guards = {"isString(str)", "isString(encoding)"})
359355
Object encode(Object str, Object encoding, @SuppressWarnings("unused") PNone errors,
360356
@Shared("castStr") @Cached CastToJavaStringNode castStr,
361357
@Shared("castEncoding") @Cached CastToJavaStringNode castEncoding) {
362-
String profiledStr = cast(castStr, str);
363-
String profiledEncoding = cast(castEncoding, encoding);
364-
PBytes bytes = encodeString(str, profiledStr, profiledEncoding, STRICT);
365-
return factory().createTuple(new Object[]{bytes, getLength(bytes)});
358+
return encodeString(str, cast(castStr, str), cast(castEncoding, encoding), STRICT);
366359
}
367360

368361
@Specialization(guards = {"isString(str)", "isString(errors)"})
369362
Object encode(Object str, @SuppressWarnings("unused") PNone encoding, Object errors,
370363
@Shared("castStr") @Cached CastToJavaStringNode castStr,
371364
@Shared("castErrors") @Cached CastToJavaStringNode castErrors) {
372-
String profiledStr = cast(castStr, str);
373-
String profiledErrors = cast(castErrors, errors);
374-
PBytes bytes = encodeString(str, profiledStr, "utf-8", profiledErrors);
375-
return factory().createTuple(new Object[]{bytes, getLength(bytes)});
365+
return encodeString(str, cast(castStr, str), "utf-8", cast(castErrors, errors));
376366
}
377367

378368
@Specialization(guards = {"isString(str)", "isString(encoding)", "isString(errors)"})
379369
Object encode(Object str, Object encoding, Object errors,
380370
@Shared("castStr") @Cached CastToJavaStringNode castStr,
381371
@Shared("castEncoding") @Cached CastToJavaStringNode castEncoding,
382372
@Shared("castErrors") @Cached CastToJavaStringNode castErrors) {
383-
String profiledStr = cast(castStr, str);
384-
String profiledEncoding = cast(castEncoding, encoding);
385-
String profiledErrors = cast(castErrors, errors);
386-
PBytes bytes = encodeString(str, profiledStr, profiledEncoding, profiledErrors);
387-
return factory().createTuple(new Object[]{bytes, getLength(bytes)});
373+
return encodeString(str, cast(castStr, str), cast(castEncoding, encoding), cast(castErrors, errors));
388374
}
389375

390376
private static String cast(CastToJavaStringNode cast, Object obj) {
@@ -401,7 +387,7 @@ Object encode(Object str, @SuppressWarnings("unused") Object encoding, @Suppress
401387
throw raise(TypeError, ErrorMessages.CANT_CONVERT_TO_STR_EXPLICITELY, str);
402388
}
403389

404-
private PBytes encodeString(Object self, String input, String encoding, String errors) {
390+
private Object encodeString(Object self, String input, String encoding, String errors) {
405391
CodingErrorAction errorAction = convertCodingErrorAction(errors);
406392
Charset charset = CharsetMapping.getCharset(encoding);
407393
if (charset == null) {
@@ -417,15 +403,8 @@ private PBytes encodeString(Object self, String input, String encoding, String e
417403
CompilerDirectives.transferToInterpreterAndInvalidate();
418404
throw raise(MemoryError);
419405
}
420-
return factory().createBytes(encoder.getBytes());
421-
}
422-
423-
private int getLength(PBytes b) {
424-
if (lenNode == null) {
425-
CompilerDirectives.transferToInterpreterAndInvalidate();
426-
lenNode = insert(SequenceStorageNodes.LenNode.create());
427-
}
428-
return lenNode.execute(b.getSequenceStorage());
406+
PBytes bytes = factory().createBytes(encoder.getBytes());
407+
return factory().createTuple(new Object[]{bytes, input.length()});
429408
}
430409

431410
private void handleEncodingError(TruffleEncoder encoder, String errorAction, Object input) {

0 commit comments

Comments
 (0)