PostgreSQL Source Code git master
Loading...
Searching...
No Matches
plpy_util.c File Reference
#include "postgres.h"
#include "mb/pg_wchar.h"
#include "plpy_elog.h"
#include "plpy_util.h"
Include dependency graph for plpy_util.c:

Go to the source code of this file.

Functions

PyObjectPLyUnicode_Bytes (PyObject *unicode)
 
charPLyUnicode_AsString (PyObject *unicode)
 
PyObjectPLyUnicode_FromStringAndSize (const char *s, Py_ssize_t size)
 
PyObjectPLyUnicode_FromString (const char *s)
 

Function Documentation

◆ PLyUnicode_AsString()

char * PLyUnicode_AsString ( PyObject unicode)

Definition at line 81 of file plpy_util.c.

82{
84 char *rv = pstrdup(PyBytes_AsString(o));
85
87 return rv;
88}
char * pstrdup(const char *in)
Definition mcxt.c:1781
PyObject * PLyUnicode_Bytes(PyObject *unicode)
Definition plpy_util.c:19
static int fb(int x)

References fb(), PLyUnicode_Bytes(), and pstrdup().

Referenced by get_string_attr(), object_to_string(), PLy_cursor_plan(), PLy_exec_trigger(), PLy_get_sqlerrcode(), PLy_modify_tuple(), PLy_output(), PLy_spi_execute_plan(), PLy_spi_prepare(), and PLy_traceback().

◆ PLyUnicode_Bytes()

PyObject * PLyUnicode_Bytes ( PyObject unicode)

Definition at line 19 of file plpy_util.c.

20{
21 PyObject *bytes,
22 *rv;
23 char *utf8string,
24 *encoded;
25
26 /* First encode the Python unicode object with UTF-8. */
28 if (bytes == NULL)
29 PLy_elog(ERROR, "could not convert Python Unicode object to bytes");
30
32 if (utf8string == NULL)
33 {
34 Py_DECREF(bytes);
35 PLy_elog(ERROR, "could not extract bytes from encoded string");
36 }
37
38 /*
39 * Then convert to server encoding if necessary.
40 *
41 * PyUnicode_AsEncodedString could be used to encode the object directly
42 * in the server encoding, but Python doesn't support all the encodings
43 * that PostgreSQL does (EUC_TW and MULE_INTERNAL). UTF-8 is used as an
44 * intermediary in PLyUnicode_FromString as well.
45 */
47 {
48 PG_TRY();
49 {
52 PG_UTF8);
53 }
54 PG_CATCH();
55 {
56 Py_DECREF(bytes);
58 }
59 PG_END_TRY();
60 }
61 else
63
64 /* finally, build a bytes object in the server encoding */
66
67 /* if pg_any_to_server allocated memory, free it now */
68 if (utf8string != encoded)
70
71 Py_DECREF(bytes);
72 return rv;
73}
#define PG_RE_THROW()
Definition elog.h:405
#define PG_TRY(...)
Definition elog.h:372
#define PG_END_TRY(...)
Definition elog.h:397
#define ERROR
Definition elog.h:39
#define PG_CATCH(...)
Definition elog.h:382
#define PLy_elog
#define PG_UTF8
Definition mbprint.c:43
int GetDatabaseEncoding(void)
Definition mbutils.c:1264
char * pg_any_to_server(const char *s, int len, int encoding)
Definition mbutils.c:679
void pfree(void *pointer)
Definition mcxt.c:1616

References ERROR, fb(), GetDatabaseEncoding(), pfree(), pg_any_to_server(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PG_UTF8, and PLy_elog.

Referenced by PLyObject_AsString(), and PLyUnicode_AsString().

◆ PLyUnicode_FromString()

◆ PLyUnicode_FromStringAndSize()

PyObject * PLyUnicode_FromStringAndSize ( const char s,
Py_ssize_t  size 
)

Definition at line 95 of file plpy_util.c.

96{
97 char *utf8string;
98 PyObject *o;
99
101
102 if (utf8string == s)
103 {
105 }
106 else
107 {
110 }
111
112 return o;
113}
char * pg_server_to_any(const char *s, int len, int encoding)
Definition mbutils.c:752

References fb(), pfree(), pg_server_to_any(), and PG_UTF8.