Header And Logo

| The world's most advanced open source database.


plpy_util.c File Reference

#include "postgres.h"
#include "mb/pg_wchar.h"
#include "utils/memutils.h"
#include "utils/palloc.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.


void * PLy_malloc (size_t bytes)
void * PLy_malloc0 (size_t bytes)
char * PLy_strdup (const char *str)
void PLy_free (void *ptr)
PyObject * PLyUnicode_Bytes (PyObject *unicode)
char * PLyUnicode_AsString (PyObject *unicode)

Function Documentation

void PLy_free ( void *  ptr  ) 
void* PLy_malloc ( size_t  bytes  ) 

Definition at line 21 of file plpy_util.c.

References MemoryContextAlloc(), and TopMemoryContext.

Referenced by PLy_malloc0(), PLy_procedure_create(), PLy_push_execution_context(), PLy_spi_prepare(), PLy_strdup(), and PLy_subtransaction_enter().

    /* We need our allocations to be long-lived, so use TopMemoryContext */
    return MemoryContextAlloc(TopMemoryContext, bytes);

void* PLy_malloc0 ( size_t  bytes  ) 

Definition at line 28 of file plpy_util.c.

References MemSet, and PLy_malloc().

Referenced by PLy_input_datum_func2(), PLy_input_tuple_funcs(), PLy_output_datum_func2(), PLy_output_tuple_funcs(), and PLy_procedure_create().

    void       *ptr = PLy_malloc(bytes);

    MemSet(ptr, 0, bytes);
    return ptr;

char* PLy_strdup ( const char *  str  ) 

Definition at line 37 of file plpy_util.c.

References PLy_malloc().

Referenced by plpython_inline_handler(), PLy_cursor_plan(), PLy_cursor_query(), PLy_procedure_compile(), and PLy_procedure_create().

    char       *result;
    size_t      len;

    len = strlen(str) + 1;
    result = PLy_malloc(len);
    memcpy(result, str, len);

    return result;

char* PLyUnicode_AsString ( PyObject *  unicode  ) 

Definition at line 129 of file plpy_util.c.

References PLyUnicode_Bytes(), pstrdup(), and PyBytes_AsString.

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

    PyObject   *o = PLyUnicode_Bytes(unicode);
    char       *rv = pstrdup(PyBytes_AsString(o));

    return rv;

PyObject* PLyUnicode_Bytes ( PyObject *  unicode  ) 

Definition at line 62 of file plpy_util.c.

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

Referenced by PLyObject_ToDatum(), and PLyUnicode_AsString().

    PyObject    *bytes, *rv;
    char        *utf8string, *encoded;

    /* First encode the Python unicode object with UTF-8. */
    bytes = PyUnicode_AsUTF8String(unicode);
    if (bytes == NULL)
        PLy_elog(ERROR, "could not convert Python Unicode object to bytes");

    utf8string = PyBytes_AsString(bytes);
    if (utf8string == NULL) {
        PLy_elog(ERROR, "could not extract bytes from encoded string");

     * Then convert to server encoding if necessary.
     * PyUnicode_AsEncodedString could be used to encode the object directly
     * in the server encoding, but Python doesn't support all the encodings
     * that PostgreSQL does (EUC_TW and MULE_INTERNAL). UTF-8 is used as an
     * intermediary in PLyUnicode_FromString as well.
    if (GetDatabaseEncoding() != PG_UTF8)
            encoded = (char *) pg_do_encoding_conversion(
                (unsigned char *) utf8string,
        encoded = utf8string;

    /* finally, build a bytes object in the server encoding */
    rv = PyBytes_FromStringAndSize(encoded, strlen(encoded));

    /* if pg_do_encoding_conversion allocated memory, free it now */
    if (utf8string != encoded)

    return rv;