Skip to content

Commit 47f6686

Browse files
committed
Move the truffle codec to a separate module to make the components picklable
1 parent 4abc5e5 commit 47f6686

File tree

5 files changed

+173
-40
lines changed

5 files changed

+173
-40
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.oracle.graal.python.builtins.modules.BuiltinFunctions;
5151
import com.oracle.graal.python.builtins.modules.CmathModuleBuiltins;
5252
import com.oracle.graal.python.builtins.modules.CodecsModuleBuiltins;
53+
import com.oracle.graal.python.builtins.modules.CodecsTruffleModuleBuiltins;
5354
import com.oracle.graal.python.builtins.modules.CollectionsModuleBuiltins;
5455
import com.oracle.graal.python.builtins.modules.ContextvarsModuleBuiltins;
5556
import com.oracle.graal.python.builtins.modules.CtypesModuleBuiltins;
@@ -236,6 +237,7 @@ private static final String[] initializeCoreFiles() {
236237
"memoryview",
237238
"list",
238239
"_codecs",
240+
"_codecs_truffle",
239241
"bytes",
240242
"bytearray",
241243
"time",
@@ -371,6 +373,7 @@ private static final PythonBuiltins[] initializeBuiltins() {
371373
new FunctoolsModuleBuiltins(),
372374
new ErrnoModuleBuiltins(),
373375
new CodecsModuleBuiltins(),
376+
new CodecsTruffleModuleBuiltins(),
374377
new CollectionsModuleBuiltins(),
375378
new JavaModuleBuiltins(),
376379
new SREModuleBuiltins(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.builtins.modules;
42+
43+
import java.util.ArrayList;
44+
import java.util.List;
45+
46+
import com.oracle.graal.python.builtins.CoreFunctions;
47+
import com.oracle.graal.python.builtins.PythonBuiltins;
48+
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
49+
import com.oracle.truffle.api.dsl.NodeFactory;
50+
51+
@CoreFunctions(defineModule = "_codecs_truffle")
52+
public class CodecsTruffleModuleBuiltins extends PythonBuiltins {
53+
@Override
54+
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
55+
return new ArrayList<>();
56+
}
57+
}

graalpython/lib-graalpython/_codecs.py

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def lookup(encoding):
7878

7979
# Next, try if we have a Java implementation of the encoding
8080
if __truffle_lookup(normalized_encoding):
81-
result = __codec_info_for_truffle(normalized_encoding)
81+
import _codecs_truffle
82+
result = _codecs_truffle.codec_info_for_truffle(normalized_encoding)
8283
else:
8384
# Next, scan the search functions in order of registration
8485
for func in __codec_search_path__:
@@ -96,44 +97,6 @@ def lookup(encoding):
9697
raise LookupError("unknown encoding: %s" % encoding)
9798

9899

99-
def __codec_info_for_truffle(encoding):
100-
import codecs
101-
class TruffleCodec(codecs.Codec):
102-
def encode(self, input, errors='strict'):
103-
return __truffle_encode(input, encoding, errors)
104-
105-
def decode(self, input, errors='strict'):
106-
return __truffle_decode(input, encoding, errors)
107-
108-
class IncrementalEncoder(codecs.IncrementalEncoder):
109-
def encode(self, input, final=False):
110-
return __truffle_encode(input, encoding, self.errors)[0]
111-
112-
class IncrementalDecoder(codecs.IncrementalDecoder):
113-
def decode(self, input, final=False):
114-
return __truffle_decode(input, encoding, self.errors)[0]
115-
116-
class StreamWriter(TruffleCodec, codecs.StreamWriter):
117-
pass
118-
119-
class StreamReader(TruffleCodec, codecs.StreamReader):
120-
pass
121-
122-
class TruffleCodecInfo(codecs.CodecInfo):
123-
def __reduce__(self):
124-
return __codec_info_for_truffle, (encoding,)
125-
126-
return TruffleCodecInfo(
127-
name=encoding,
128-
encode=TruffleCodec().encode,
129-
decode=TruffleCodec().decode,
130-
incrementalencoder=IncrementalEncoder,
131-
incrementaldecoder=IncrementalDecoder,
132-
streamreader=StreamReader,
133-
streamwriter=StreamWriter,
134-
)
135-
136-
137100
def _forget_codec(encoding):
138101
normalized_encoding = __normalizestring(encoding)
139102
return __codec_search_cache__.pop(normalized_encoding)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
# This module is split out from "_codecs" to avoid circular dependency with "codecs"
41+
import _codecs
42+
import codecs
43+
44+
45+
class TruffleCodec(codecs.Codec):
46+
def __init__(self, encoding):
47+
self.encoding = encoding
48+
49+
def encode(self, input, errors='strict'):
50+
return _codecs.__truffle_encode(input, self.encoding, errors)
51+
52+
def decode(self, input, errors='strict'):
53+
return _codecs.__truffle_decode(input, self.encoding, errors)
54+
55+
56+
class TruffleIncrementalEncoder(codecs.IncrementalEncoder):
57+
def __init__(self, encoding, *args, **kwargs):
58+
super().__init__(*args, **kwargs)
59+
self.encoding = encoding
60+
61+
def encode(self, input, final=False):
62+
return _codecs.__truffle_encode(input, self.encoding, self.errors)[0]
63+
64+
65+
class TruffleIncrementalDecoder(codecs.IncrementalDecoder):
66+
def __init__(self, encoding, *args, **kwargs):
67+
super().__init__(*args, **kwargs)
68+
self.encoding = encoding
69+
70+
def decode(self, input, final=False):
71+
return _codecs.__truffle_decode(input, self.encoding, self.errors)[0]
72+
73+
74+
class TruffleStreamWriter(codecs.StreamWriter):
75+
def __init__(self, encoding, *args, **kwargs):
76+
super().__init__(*args, **kwargs)
77+
self.encoding = encoding
78+
79+
def encode(self, input, errors='strict'):
80+
return _codecs.__truffle_encode(input, self.encoding, errors)
81+
82+
83+
class TruffleStreamReader(codecs.StreamReader):
84+
def __init__(self, encoding, *args, **kwargs):
85+
super().__init__(*args, **kwargs)
86+
self.encoding = encoding
87+
88+
def decode(self, input, errors='strict'):
89+
return _codecs.__truffle_decode(input, self.encoding, errors)
90+
91+
92+
class apply_encoding:
93+
def __init__(self, fn, encoding):
94+
self.fn = fn
95+
self.encoding = encoding
96+
97+
def __call__(self, *args, **kwargs):
98+
return self.fn(self.encoding, *args, **kwargs)
99+
100+
101+
def codec_info_for_truffle(encoding):
102+
return codecs.CodecInfo(
103+
name=encoding,
104+
encode=TruffleCodec(encoding).encode,
105+
decode=TruffleCodec(encoding).decode,
106+
incrementalencoder=apply_encoding(TruffleIncrementalEncoder, encoding),
107+
incrementaldecoder=apply_encoding(TruffleIncrementalDecoder, encoding),
108+
streamreader=apply_encoding(TruffleStreamReader, encoding),
109+
streamwriter=apply_encoding(TruffleStreamWriter, encoding),
110+
)

0 commit comments

Comments
 (0)