/* * Generic conversion function: Convert PyObject to cstring and * cstring into PostgreSQL type. */ static Datum PLyObject_ToDatum(PLyObToDatum *arg, int32 typmod, PyObject *plrv) { PyObject *volatile plrv_bo = NULL; Datum rv; Assert(plrv != Py_None); if (PyUnicode_Check(plrv)) plrv_bo = PLyUnicode_Bytes(plrv); else { #if PY_MAJOR_VERSION >= 3 PyObject *s = PyObject_Str(plrv); plrv_bo = PLyUnicode_Bytes(s); Py_XDECREF(s); #else plrv_bo = PyObject_Str(plrv); #endif } if (!plrv_bo) PLy_elog(ERROR, "could not create string representation of Python object"); PG_TRY(); { char *plrv_sc = PyBytes_AsString(plrv_bo); size_t plen = PyBytes_Size(plrv_bo); size_t slen = strlen(plrv_sc); if (slen < plen) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("could not convert Python object into cstring: Python string representation appears to contain null bytes"))); else if (slen > plen) elog(ERROR, "could not convert Python object into cstring: Python string longer than reported length"); pg_verifymbstr(plrv_sc, slen, false); rv = InputFunctionCall(&arg->typfunc, plrv_sc, arg->typioparam, typmod); } PG_CATCH(); { Py_XDECREF(plrv_bo); PG_RE_THROW(); } PG_END_TRY(); Py_XDECREF(plrv_bo); return rv; }
/* * Convert Python object to C string in server encoding. */ char * PLyObject_AsString(PyObject *plrv) { PyObject *plrv_bo; char *plrv_sc; size_t plen; size_t slen; if (PyUnicode_Check(plrv)) plrv_bo = PLyUnicode_Bytes(plrv); else if (PyFloat_Check(plrv)) { /* use repr() for floats, str() is lossy */ #if PY_MAJOR_VERSION >= 3 PyObject *s = PyObject_Repr(plrv); plrv_bo = PLyUnicode_Bytes(s); Py_XDECREF(s); #else plrv_bo = PyObject_Repr(plrv); #endif } else { #if PY_MAJOR_VERSION >= 3 PyObject *s = PyObject_Str(plrv); plrv_bo = PLyUnicode_Bytes(s); Py_XDECREF(s); #else plrv_bo = PyObject_Str(plrv); #endif } if (!plrv_bo) PLy_elog(ERROR, "could not create string representation of Python object"); plrv_sc = pstrdup(PyBytes_AsString(plrv_bo)); plen = PyBytes_Size(plrv_bo); slen = strlen(plrv_sc); Py_XDECREF(plrv_bo); if (slen < plen) ereport(ERROR, (errcode(ERRCODE_DATATYPE_MISMATCH), errmsg("could not convert Python object into cstring: Python string representation appears to contain null bytes"))); else if (slen > plen) elog(ERROR, "could not convert Python object into cstring: Python string longer than reported length"); pg_verifymbstr(plrv_sc, slen, false); return plrv_sc; }
/* * Convert a Python unicode object to a C string in PostgreSQL server * encoding. No Python object reference is passed out of this * function. The result is palloc'ed. * * Note that this function is disguised as PyString_AsString() when * using Python 3. That function retuns a pointer into the internal * memory of the argument, which isn't exactly the interface of this * function. But in either case you get a rather short-lived * reference that you ought to better leave alone. */ char * PLyUnicode_AsString(PyObject *unicode) { PyObject *o = PLyUnicode_Bytes(unicode); char *rv = pstrdup(PyBytes_AsString(o)); Py_XDECREF(o); return rv; }