Changeset 802 for trunk/pgmodule.c


Ignore:
Timestamp:
Jan 31, 2016, 4:18:28 PM (4 years ago)
Author:
cito
Message:

Make the unescaping of bytea configurable

By default, bytea is returned unescaped in 5.0, but the old
behavior can now be restored with set_escaped_bytea().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pgmodule.c

    r798 r802  
    9898static char decimal_point = '.'; /* decimal point used in money values */
    9999static int use_bool = 0; /* whether or not bool objects shall be returned */
     100static int bytea_escaped = 0; /* whether bytea shall be returned escaped */
    100101
    101102static int pg_encoding_utf8 = 0;
     
    284285
    285286                case BYTEAOID:
    286                         t = PYGRES_BYTEA;
     287                        t = bytea_escaped ? PYGRES_TEXT : PYGRES_BYTEA;
    287288                        break;
    288289
     
    340341
    341342                case BYTEAARRAYOID:
    342                         t = PYGRES_BYTEA | PYGRES_ARRAY;
     343                        t = (bytea_escaped ? PYGRES_TEXT : PYGRES_BYTEA) | PYGRES_ARRAY;
    343344                        break;
    344345
     
    394395        size_t          str_len;
    395396
     397    /* this function should not be called when bytea_escaped is set */
    396398        tmp_str = (char *)PQunescapeBytea((unsigned char*)s, &str_len);
    397399        obj = PyBytes_FromStringAndSize(tmp_str, str_len);
     
    413415        {
    414416                case PYGRES_BYTEA:
     417                    /* this type should not be passed when bytea_escaped is set */
    415418                        /* we need to add a null byte */
    416419                        tmp_str = (char *) PyMem_Malloc(size + 1);
     
    51325135}
    51335136
     5137/* check whether bytea values are unescaped */
     5138static char pgGetByteaEscaped__doc__[] =
     5139"get_bytea_escaped() -- check whether bytea will be returned escaped";
     5140
     5141static PyObject *
     5142pgGetByteaEscaped(PyObject *self, PyObject * args)
     5143{
     5144        PyObject *ret = NULL;
     5145
     5146        if (PyArg_ParseTuple(args, ""))
     5147        {
     5148                ret = bytea_escaped ? Py_True : Py_False;
     5149                Py_INCREF(ret);
     5150        }
     5151        else
     5152                PyErr_SetString(PyExc_TypeError,
     5153                        "Function get_bytea_escaped() takes no arguments");
     5154
     5155        return ret;
     5156}
     5157
     5158/* set usage of bool values */
     5159static char pgSetByteaEscaped__doc__[] =
     5160"set_bytea_escaped(on) -- set whether bytea will be returned escaped";
     5161
     5162static PyObject *
     5163pgSetByteaEscaped(PyObject *self, PyObject * args)
     5164{
     5165        PyObject *ret = NULL;
     5166        int                     i;
     5167
     5168        /* gets arguments */
     5169        if (PyArg_ParseTuple(args, "i", &i))
     5170        {
     5171                bytea_escaped = i ? 1 : 0;
     5172                Py_INCREF(Py_None); ret = Py_None;
     5173        }
     5174        else
     5175                PyErr_SetString(PyExc_TypeError,
     5176                        "Function set_bytea_escaped() expects a boolean value as argument");
     5177
     5178        return ret;
     5179}
     5180
    51345181/* get named result factory */
    51355182static char pgGetNamedresult__doc__[] =
     
    56765723        {"get_bool", (PyCFunction) pgGetBool, METH_VARARGS, pgGetBool__doc__},
    56775724        {"set_bool", (PyCFunction) pgSetBool, METH_VARARGS, pgSetBool__doc__},
     5725        {"get_bytea_escaped", (PyCFunction) pgGetByteaEscaped, METH_VARARGS,
     5726                pgGetByteaEscaped__doc__},
     5727        {"set_bytea_escaped", (PyCFunction) pgSetByteaEscaped, METH_VARARGS,
     5728                pgSetByteaEscaped__doc__},
    56785729        {"get_namedresult", (PyCFunction) pgGetNamedresult, METH_VARARGS,
    56795730                        pgGetNamedresult__doc__},
Note: See TracChangeset for help on using the changeset viewer.