Skip to content

Commit ef2a2c3

Browse files
committed
Implement codec lookup for all encodings we support
1 parent b963522 commit ef2a2c3

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

graalpython/lib-graalpython/_codecs.py

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,17 @@ def lookup(encoding):
6262
if result:
6363
return result
6464

65-
# Next, scan the search functions in order of registration
66-
for func in __codec_search_path__:
67-
result = func(normalized_encoding)
68-
if result:
69-
if not (isinstance(result, tuple) and len(result) == 4):
70-
raise TypeError("codec search functions must return 4-tuples %r")
71-
break
65+
# Next, try if we have a Java implementation of the encoding
66+
if __truffle_lookup(normalized_encoding):
67+
result = __codec_info_for_truffle(normalized_encoding)
68+
else:
69+
# Next, scan the search functions in order of registration
70+
for func in __codec_search_path__:
71+
result = func(normalized_encoding)
72+
if result:
73+
if not (isinstance(result, tuple) and len(result) == 4):
74+
raise TypeError("codec search functions must return 4-tuples %r")
75+
break
7276

7377
if result:
7478
# Cache and return the result
@@ -78,6 +82,40 @@ def lookup(encoding):
7882
raise LookupError("unknown encoding: %s" % encoding)
7983

8084

85+
def __codec_info_for_truffle(encoding):
86+
import codecs
87+
class TruffleCodec(codecs.Codec):
88+
def encode(self, input, errors='strict'):
89+
return __truffle_encode(input, encoding, errors)
90+
91+
def decode(self, input, errors='strict'):
92+
return __truffle_decode(input, encoding, errors)
93+
94+
class IncrementalEncoder(codecs.IncrementalEncoder):
95+
def encode(self, input, final=False):
96+
return __truffle_encode(input, encoding, self.errors)[0]
97+
98+
class IncrementalDecoder(codecs.IncrementalDecoder):
99+
def decode(self, input, final=False):
100+
return __truffle_decode(input, encoding, self.errors)[0]
101+
102+
class StreamWriter(TruffleCodec, codecs.StreamWriter):
103+
pass
104+
105+
class StreamReader(TruffleCodec, codecs.StreamReader):
106+
pass
107+
108+
return codecs.CodecInfo(
109+
name=encoding,
110+
encode=TruffleCodec().encode,
111+
decode=TruffleCodec().decode,
112+
incrementalencoder=IncrementalEncoder,
113+
incrementaldecoder=IncrementalDecoder,
114+
streamreader=StreamReader,
115+
streamwriter=StreamWriter,
116+
)
117+
118+
81119
def _forget_codec(encoding):
82120
normalized_encoding = __normalizestring(encoding)
83121
return __codec_search_cache__.pop(normalized_encoding)
@@ -321,10 +359,6 @@ def code_page_decode(code_page, string, errors=None, final=False):
321359
sys.path.append(__graalpython__.stdlib_home)
322360
try:
323361
import encodings
324-
# we import the below two encodings, because they are often used so it's
325-
# useful to have them available preloaded
326-
import encodings.ascii
327-
import encodings.utf_8
328362
finally:
329363
assert len(sys.path) == 1
330364
sys.path.pop()

0 commit comments

Comments
 (0)