MCPcopy Create free account
hub / github.com/apache/cloudberry / PLyUnicode_Bytes

Function PLyUnicode_Bytes

src/pl/plpython/plpy_util.c:20–75  ·  view source on GitHub ↗

* Convert a Python unicode object to a Python string/bytes object in * PostgreSQL server encoding. Reference ownership is passed to the * caller. */

Source from the content-addressed store, hash-verified

18 * caller.
19 */
20PyObject *
21PLyUnicode_Bytes(PyObject *unicode)
22{
23 PyObject *bytes,
24 *rv;
25 char *utf8string,
26 *encoded;
27
28 /* First encode the Python unicode object with UTF-8. */
29 bytes = PyUnicode_AsUTF8String(unicode);
30 if (bytes == NULL)
31 PLy_elog(ERROR, "could not convert Python Unicode object to bytes");
32
33 utf8string = PyBytes_AsString(bytes);
34 if (utf8string == NULL)
35 {
36 Py_DECREF(bytes);
37 PLy_elog(ERROR, "could not extract bytes from encoded string");
38 }
39
40 /*
41 * Then convert to server encoding if necessary.
42 *
43 * PyUnicode_AsEncodedString could be used to encode the object directly
44 * in the server encoding, but Python doesn't support all the encodings
45 * that PostgreSQL does (EUC_TW and MULE_INTERNAL). UTF-8 is used as an
46 * intermediary in PLyUnicode_FromString as well.
47 */
48 if (GetDatabaseEncoding() != PG_UTF8)
49 {
50 PG_TRY();
51 {
52 encoded = pg_any_to_server(utf8string,
53 strlen(utf8string),
54 PG_UTF8);
55 }
56 PG_CATCH();
57 {
58 Py_DECREF(bytes);
59 PG_RE_THROW();
60 }
61 PG_END_TRY();
62 }
63 else
64 encoded = utf8string;
65
66 /* finally, build a bytes object in the server encoding */
67 rv = PyBytes_FromStringAndSize(encoded, strlen(encoded));
68
69 /* if pg_any_to_server allocated memory, free it now */
70 if (utf8string != encoded)
71 pfree(encoded);
72
73 Py_DECREF(bytes);
74 return rv;
75}
76
77/*

Callers 2

PLyUnicode_AsStringFunction · 0.85
PLyObject_AsStringFunction · 0.85

Calls 3

GetDatabaseEncodingFunction · 0.85
pg_any_to_serverFunction · 0.85
pfreeFunction · 0.50

Tested by

no test coverage detected