Skip to content

Commit 947befc

Browse files
committed
Implement PyCodec_Encoder/Decoder
1 parent d891115 commit 947befc

File tree

5 files changed

+160
-7
lines changed

5 files changed

+160
-7
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Copyright (c) 2023, 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+
import codecs
41+
42+
from tests.cpyext import CPyExtTestCase, CPyExtFunction, unhandled_error_compare
43+
44+
45+
class TestPyCodec(CPyExtTestCase):
46+
47+
def compile_module(self, name):
48+
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
49+
super().compile_module(name)
50+
51+
test_PyCodec_Encoder = CPyExtFunction(
52+
lambda args: codecs.lookup(args[0])[0],
53+
lambda: (
54+
("utf-8",),
55+
("ascii",),
56+
("this-doesn't-exist",),
57+
),
58+
resultspec="O",
59+
argspec='s',
60+
arguments=["char* encoding"],
61+
cmpfunc=unhandled_error_compare,
62+
)
63+
64+
test_PyCodec_Decoder = CPyExtFunction(
65+
lambda args: codecs.lookup(args[0])[1],
66+
lambda: (
67+
("utf-8",),
68+
("ascii",),
69+
("this-doesn't-exist",),
70+
),
71+
resultspec="O",
72+
argspec='s',
73+
arguments=["char* encoding"],
74+
cmpfunc=unhandled_error_compare,
75+
)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -941,8 +941,8 @@ private static boolean hasTruffleEncodingNormalized(TruffleString normalizedEnco
941941
abstract static class LookupNode extends PythonUnaryClinicBuiltinNode {
942942
@Specialization
943943
PTuple lookup(VirtualFrame frame, TruffleString encoding,
944-
@Cached InternalLookupNode internalNode) {
945-
return internalNode.execute(frame, encoding);
944+
@Cached PyCodecLookupNode lookup) {
945+
return lookup.execute(frame, encoding);
946946
}
947947

948948
@Override
@@ -952,8 +952,8 @@ protected ArgumentClinicProvider getArgumentClinic() {
952952
}
953953

954954
@GenerateUncached
955-
abstract static class InternalLookupNode extends PNodeWithContext {
956-
abstract PTuple execute(Frame frame, TruffleString encoding);
955+
public abstract static class PyCodecLookupNode extends PNodeWithContext {
956+
public abstract PTuple execute(Frame frame, TruffleString encoding);
957957

958958
@Specialization
959959
PTuple lookup(VirtualFrame frame, TruffleString encoding,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ public abstract static class LookupTextEncoding extends PNodeWithContext {
421421

422422
@Specialization
423423
Object lookup(VirtualFrame frame, TruffleString encoding, TruffleString alternateCommand,
424-
@Cached CodecsModuleBuiltins.InternalLookupNode lookupNode,
424+
@Cached CodecsModuleBuiltins.PyCodecLookupNode lookupNode,
425425
@Cached PyObjectGetAttr getAttributeNode,
426426
@Cached PRaiseNode raiseNode) {
427427
PTuple codecInfo = lookupNode.execute(frame, encoding);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2023, 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.cext;
42+
43+
import static com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiCallPath.Direct;
44+
import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.ConstCharPtrAsTruffleString;
45+
import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.PyObjectTransfer;
46+
47+
import com.oracle.graal.python.builtins.modules.CodecsModuleBuiltins;
48+
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiBuiltin;
49+
import com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiUnaryBuiltinNode;
50+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
51+
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
52+
import com.oracle.truffle.api.dsl.Cached;
53+
import com.oracle.truffle.api.dsl.Specialization;
54+
import com.oracle.truffle.api.strings.TruffleString;
55+
56+
public final class PythonCextCodecBuiltins {
57+
@CApiBuiltin(ret = PyObjectTransfer, args = {ConstCharPtrAsTruffleString}, call = Direct)
58+
abstract static class PyCodec_Encoder extends CApiUnaryBuiltinNode {
59+
@Specialization
60+
Object get(TruffleString encoding,
61+
@Cached CodecsModuleBuiltins.PyCodecLookupNode lookupNode,
62+
@Cached SequenceStorageNodes.GetItemScalarNode getItemScalarNode) {
63+
PTuple codecInfo = lookupNode.execute(null, encoding);
64+
return getItemScalarNode.execute(codecInfo.getSequenceStorage(), 0);
65+
}
66+
}
67+
68+
@CApiBuiltin(ret = PyObjectTransfer, args = {ConstCharPtrAsTruffleString}, call = Direct)
69+
abstract static class PyCodec_Decoder extends CApiUnaryBuiltinNode {
70+
@Specialization
71+
Object get(TruffleString encoding,
72+
@Cached CodecsModuleBuiltins.PyCodecLookupNode lookupNode,
73+
@Cached SequenceStorageNodes.GetItemScalarNode getItemScalarNode) {
74+
PTuple codecInfo = lookupNode.execute(null, encoding);
75+
return getItemScalarNode.execute(codecInfo.getSequenceStorage(), 1);
76+
}
77+
}
78+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
import com.oracle.graal.python.builtins.modules.cext.PythonCextCapsuleBuiltins;
200200
import com.oracle.graal.python.builtins.modules.cext.PythonCextClassBuiltins;
201201
import com.oracle.graal.python.builtins.modules.cext.PythonCextCodeBuiltins;
202+
import com.oracle.graal.python.builtins.modules.cext.PythonCextCodecBuiltins;
202203
import com.oracle.graal.python.builtins.modules.cext.PythonCextComplexBuiltins;
203204
import com.oracle.graal.python.builtins.modules.cext.PythonCextContextBuiltins;
204205
import com.oracle.graal.python.builtins.modules.cext.PythonCextDateTimeBuiltins;
@@ -1041,9 +1042,7 @@ public final class CApiFunction {
10411042
@CApiBuiltin(name = "PyCode_Optimize", ret = PyObject, args = {PyObject, PyObject, PyObject, PyObject}, call = NotImplemented)
10421043
@CApiBuiltin(name = "PyCodec_BackslashReplaceErrors", ret = PyObject, args = {PyObject}, call = NotImplemented)
10431044
@CApiBuiltin(name = "PyCodec_Decode", ret = PyObject, args = {PyObject, ConstCharPtrAsTruffleString, ConstCharPtrAsTruffleString}, call = NotImplemented)
1044-
@CApiBuiltin(name = "PyCodec_Decoder", ret = PyObject, args = {ConstCharPtrAsTruffleString}, call = NotImplemented)
10451045
@CApiBuiltin(name = "PyCodec_Encode", ret = PyObject, args = {PyObject, ConstCharPtrAsTruffleString, ConstCharPtrAsTruffleString}, call = NotImplemented)
1046-
@CApiBuiltin(name = "PyCodec_Encoder", ret = PyObject, args = {ConstCharPtrAsTruffleString}, call = NotImplemented)
10471046
@CApiBuiltin(name = "PyCodec_IgnoreErrors", ret = PyObject, args = {PyObject}, call = NotImplemented)
10481047
@CApiBuiltin(name = "PyCodec_IncrementalDecoder", ret = PyObject, args = {ConstCharPtrAsTruffleString, ConstCharPtrAsTruffleString}, call = NotImplemented)
10491048
@CApiBuiltin(name = "PyCodec_IncrementalEncoder", ret = PyObject, args = {ConstCharPtrAsTruffleString, ConstCharPtrAsTruffleString}, call = NotImplemented)
@@ -1392,6 +1391,7 @@ static List<CApiBuiltinDesc> getJavaBuiltinDefinitions() {
13921391
addCApiBuiltins(result, PythonCextCEvalBuiltins.class);
13931392
addCApiBuiltins(result, PythonCextClassBuiltins.class);
13941393
addCApiBuiltins(result, PythonCextCodeBuiltins.class);
1394+
addCApiBuiltins(result, PythonCextCodecBuiltins.class);
13951395
addCApiBuiltins(result, PythonCextComplexBuiltins.class);
13961396
addCApiBuiltins(result, PythonCextContextBuiltins.class);
13971397
addCApiBuiltins(result, PythonCextDateTimeBuiltins.class);

0 commit comments

Comments
 (0)