PostgreSQL Source Code  git master
plpy_util.c File Reference
#include "postgres.h"
#include "mb/pg_wchar.h"
#include "utils/memutils.h"
#include "plpython.h"
#include "plpy_util.h"
#include "plpy_elog.h"
Include dependency graph for plpy_util.c:

Go to the source code of this file.

Functions

PyObject * PLyUnicode_Bytes (PyObject *unicode)
 
char * PLyUnicode_AsString (PyObject *unicode)
 

Function Documentation

◆ PLyUnicode_AsString()

char* PLyUnicode_AsString ( PyObject *  unicode)

Definition at line 93 of file plpy_util.c.

References pfree(), pg_server_to_any(), PG_UTF8, PLyUnicode_Bytes(), PLyUnicode_FromStringAndSize, pstrdup(), and PyBytes_AsString.

Referenced by PLy_exec_trigger(), PLy_modify_tuple(), and PLy_spi_prepare().

94 {
95  PyObject *o = PLyUnicode_Bytes(unicode);
96  char *rv = pstrdup(PyBytes_AsString(o));
97 
98  Py_XDECREF(o);
99  return rv;
100 }
char * pstrdup(const char *in)
Definition: mcxt.c:1063
#define PyBytes_AsString
Definition: plpython.h:84
PyObject * PLyUnicode_Bytes(PyObject *unicode)
Definition: plpy_util.c:25

◆ PLyUnicode_Bytes()

PyObject* PLyUnicode_Bytes ( PyObject *  unicode)

Definition at line 25 of file plpy_util.c.

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

Referenced by PLyObject_AsString(), and PLyUnicode_AsString().

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