PostgreSQL Source Code  git master
plpy_util.h File Reference
This graph shows which files directly or indirectly include this file:

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 94 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().

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

◆ PLyUnicode_Bytes()

PyObject* PLyUnicode_Bytes ( PyObject *  unicode)

Definition at line 26 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().

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