Skip to content

Commit 3f61687

Browse files
committed
Implement C API function PyUnicode_AsEncodedString.
1 parent 52a01c8 commit 3f61687

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

graalpython/com.oracle.graal.python.cext/src/unicodeobject.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,8 @@ UPCALL_ID(PyUnicode_Tailmatch);
497497
Py_ssize_t PyUnicode_Tailmatch(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction) {
498498
return UPCALL_CEXT_L(_jls_PyUnicode_Tailmatch, native_to_java(str), native_to_java(substr), start, end, direction);
499499
}
500+
501+
UPCALL_ID(PyUnicode_AsEncodedString);
502+
PyObject * PyUnicode_AsEncodedString(PyObject *unicode, const char *encoding, const char *errors) {
503+
return UPCALL_CEXT_O(_jls_PyUnicode_AsEncodedString, native_to_java(unicode), polyglot_from_string(encoding, SRC_CS), polyglot_from_string(errors, SRC_CS));
504+
}

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_unicode.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,21 @@ def compile_module(self, name):
495495
arguments=["PyObject* str"],
496496
cmpfunc=unhandled_error_compare
497497
)
498+
499+
500+
test_PyUnicode_AsEncodedString = CPyExtFunction(
501+
lambda args: args[0].encode(args[1], args[2]),
502+
lambda: (
503+
("abcd", "ascii", "report"),
504+
("abcd", "utf8", "report"),
505+
("öüä", "ascii", "report"),
506+
("öüä", "utf8", "report"),
507+
("öüä", "ascii", "ignore"),
508+
("öüä", "ascii", "replace"),
509+
),
510+
resultspec="O",
511+
argspec='Oss',
512+
arguments=["PyObject* str", "const char* encoding", "const char* errors"],
513+
cmpfunc=unhandled_error_compare
514+
)
515+

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import java.nio.charset.CoderResult;
5858
import java.nio.charset.CodingErrorAction;
5959
import java.nio.charset.StandardCharsets;
60-
import java.nio.charset.UnsupportedCharsetException;
6160
import java.text.DecimalFormat;
6261
import java.text.ParseException;
6362
import java.text.ParsePosition;
@@ -2635,9 +2634,9 @@ abstract static class PyUnicode_Decode extends NativeUnicodeBuiltin {
26352634

26362635
@Specialization
26372636
Object doDecode(VirtualFrame frame, Object module, Object cByteArray, long size, String encoding, String errors,
2638-
@Cached CExtNodes.ToSulongNode toSulongNode,
2639-
@Cached GetByteArrayNode getByteArrayNode,
2640-
@Cached GetNativeNullNode getNativeNullNode) {
2637+
@Cached CExtNodes.ToSulongNode toSulongNode,
2638+
@Cached GetByteArrayNode getByteArrayNode,
2639+
@Cached GetNativeNullNode getNativeNullNode) {
26412640

26422641
try {
26432642
ByteBuffer inputBuffer = wrap(getByteArrayNode.execute(frame, cByteArray, size));

graalpython/lib-graalpython/python_cext.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,11 @@ def PyUnicode_Tailmatch(s, substr, start, end, direction):
784784
return 1 if s[start:end].startswith(substr) else 0
785785

786786

787+
@may_raise
788+
def PyUnicode_AsEncodedString(s, encoding, errors):
789+
return s.encode(encoding, errors)
790+
791+
787792
##################### CAPSULE
788793

789794

0 commit comments

Comments
 (0)