Skip to content

Commit 10367bd

Browse files
committed
[GR-17150] Implemented charmap_build for codecs
PullRequest: graalpython/585
2 parents 33b8830 + ecbed71 commit 10367bd

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, Oracle and/or its affiliates.
1+
# Copyright (c) 2019, Oracle and/or its affiliates.
22
# Copyright (C) 1996-2017 Python Software Foundation
33
#
44
# Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -172,3 +172,9 @@ def test_escape_decode(self):
172172
check(br"[\x410]", "[A0]")
173173
check(br"\u20ac", "\u20ac")
174174
check(br"\U0001d120", "\U0001d120")
175+
176+
177+
def test_charmap_build():
178+
import codecs
179+
assert codecs.charmap_build(u'123456') == {49: 0, 50: 1, 51: 2,
180+
52: 3, 53: 4, 54: 5}

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

+27
Original file line numberDiff line numberDiff line change
@@ -628,4 +628,31 @@ Object lookup(String encoding) {
628628
}
629629
}
630630
}
631+
632+
// _codecs.lookup(name)
633+
@Builtin(name = "charmap_build", minNumOfPositionalArgs = 1)
634+
@GenerateNodeFactory
635+
abstract static class CharmapBuildNode extends PythonBuiltinNode {
636+
// This is replaced in the core _codecs.py with the full functionality
637+
@Specialization
638+
Object lookup(String chars) {
639+
Map<Integer, Integer> charmap = createMap(chars);
640+
return factory().createDict(charmap);
641+
}
642+
643+
@TruffleBoundary
644+
private static Map<Integer, Integer> createMap(String chars) {
645+
Map<Integer, Integer> charmap = new HashMap<>();
646+
int pos = 0;
647+
int num = 0;
648+
649+
while (pos < chars.length()) {
650+
int charid = Character.codePointAt(chars, pos);
651+
charmap.put(charid, num);
652+
pos += Character.charCount(charid);
653+
num++;
654+
}
655+
return charmap;
656+
}
657+
}
631658
}

graalpython/lib-graalpython/_codecs.py

-5
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,6 @@ def charmap_decode(string, errors=None, mapping=None):
281281
return __truffle_decode(string, "cp437", errors)
282282

283283

284-
@__builtin__
285-
def charmap_build(mapping):
286-
raise NotImplementedError("charmap_build")
287-
288-
289284
@__builtin__
290285
def readbuffer_encode(data, errors=None):
291286
raise NotImplementedError("readbuffer_encode")

0 commit comments

Comments
 (0)