@@ -62,13 +62,17 @@ def lookup(encoding):
62
62
if result :
63
63
return result
64
64
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
72
76
73
77
if result :
74
78
# Cache and return the result
@@ -78,6 +82,40 @@ def lookup(encoding):
78
82
raise LookupError ("unknown encoding: %s" % encoding )
79
83
80
84
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
+
81
119
def _forget_codec (encoding ):
82
120
normalized_encoding = __normalizestring (encoding )
83
121
return __codec_search_cache__ .pop (normalized_encoding )
@@ -321,10 +359,6 @@ def code_page_decode(code_page, string, errors=None, final=False):
321
359
sys .path .append (__graalpython__ .stdlib_home )
322
360
try :
323
361
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
328
362
finally :
329
363
assert len (sys .path ) == 1
330
364
sys .path .pop ()
0 commit comments