PostgreSQL Source Code  git master
plpy_util.c File Reference
#include "postgres.h"
#include "mb/pg_wchar.h"
#include "plpy_elog.h"
#include "plpy_util.h"
#include "plpython.h"
#include "utils/memutils.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 89 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().

90 {
91  PyObject *o = PLyUnicode_Bytes(unicode);
92  char *rv = pstrdup(PyBytes_AsString(o));
93 
94  Py_XDECREF(o);
95  return rv;
96 }
char * pstrdup(const char *in)
Definition: mcxt.c:1186
#define PyBytes_AsString
Definition: plpython.h:86
PyObject * PLyUnicode_Bytes(PyObject *unicode)
Definition: plpy_util.c:21

◆ PLyUnicode_Bytes()

PyObject* PLyUnicode_Bytes ( PyObject *  unicode)

Definition at line 21 of file plpy_util.c.

References generate_unaccent_rules::bytes(), 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().

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  */
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 }
def bytes(source, encoding='ascii', errors='strict')
#define PyBytes_FromStringAndSize
Definition: plpython.h:87
#define PyBytes_AsString
Definition: plpython.h:86
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
#define PLy_elog
int GetDatabaseEncoding(void)
Definition: mbutils.c:1046
#define PG_CATCH()
Definition: elog.h:332
#define PG_RE_THROW()
Definition: elog.h:363
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:581
#define PG_TRY()
Definition: elog.h:322
#define PG_END_TRY()
Definition: elog.h:347