PostgreSQL Source Code  git master
plpy_util.h File Reference
#include "plpython.h"
Include dependency graph for plpy_util.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

PGDLLEXPORT PyObject * PLyUnicode_Bytes (PyObject *unicode)
 
PGDLLEXPORT char * PLyUnicode_AsString (PyObject *unicode)
 
PGDLLEXPORT PyObject * PLyUnicode_FromString (const char *s)
 
PGDLLEXPORT PyObject * PLyUnicode_FromStringAndSize (const char *s, Py_ssize_t size)
 

Function Documentation

◆ PLyUnicode_AsString()

PGDLLEXPORT char* PLyUnicode_AsString ( PyObject *  unicode)

Definition at line 82 of file plpy_util.c.

83 {
84  PyObject *o = PLyUnicode_Bytes(unicode);
85  char *rv = pstrdup(PyBytes_AsString(o));
86 
87  Py_XDECREF(o);
88  return rv;
89 }
char * pstrdup(const char *in)
Definition: mcxt.c:1696
PyObject * PLyUnicode_Bytes(PyObject *unicode)
Definition: plpy_util.c:20

References 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()

PGDLLEXPORT PyObject* PLyUnicode_Bytes ( PyObject *  unicode)

Definition at line 20 of file plpy_util.c.

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

References ERROR, 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()

PGDLLEXPORT PyObject* PLyUnicode_FromString ( const char *  s)

Definition at line 117 of file plpy_util.c.

118 {
119  return PLyUnicode_FromStringAndSize(s, strlen(s));
120 }
PyObject * PLyUnicode_FromStringAndSize(const char *s, Py_ssize_t size)
Definition: plpy_util.c:96

References PLyUnicode_FromStringAndSize().

Referenced by PLy_generate_spi_exceptions(), PLy_quote_ident(), PLy_quote_literal(), PLy_quote_nullable(), PLy_result_colnames(), PLy_trigger_build_args(), PLyUnicode_FromScalar(), and set_string_attr().

◆ PLyUnicode_FromStringAndSize()

PGDLLEXPORT PyObject* PLyUnicode_FromStringAndSize ( const char *  s,
Py_ssize_t  size 
)

Definition at line 96 of file plpy_util.c.

97 {
98  char *utf8string;
99  PyObject *o;
100 
101  utf8string = pg_server_to_any(s, size, PG_UTF8);
102 
103  if (utf8string == s)
104  {
105  o = PyUnicode_FromStringAndSize(s, size);
106  }
107  else
108  {
109  o = PyUnicode_FromString(utf8string);
110  pfree(utf8string);
111  }
112 
113  return o;
114 }
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:749
static pg_noinline void Size size
Definition: slab.c:607

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

Referenced by PLyUnicode_FromString().