~james-w/ubuntu/lucid/psycopg2/precise-backport

« back to all changes in this revision

Viewing changes to psycopg/adapter_qstring.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabio Tranchitella, Jakub Wilk, Fabio Tranchitella
  • Date: 2011-06-19 18:25:53 UTC
  • mfrom: (5.1.10 sid)
  • Revision ID: james.westby@ubuntu.com-20110619182553-uye7z0g5ewab98px
Tags: 2.4.2-1
[ Jakub Wilk ]
* Add Debian Python Modules Team to Uploaders.

[ Fabio Tranchitella ]
* New upstream release.
* debian/watch: updated, use pypi.
* debian/control, debian/rules: switched to dh_python2.
* debian/control: bumped Standard-Version to 3.9.2, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 * License for more details.
24
24
 */
25
25
 
26
 
#define PY_SSIZE_T_CLEAN
27
 
#include <Python.h>
28
 
#include <structmember.h>
29
 
#include <stringobject.h>
30
 
 
31
 
#include <libpq-fe.h>
32
 
#include <string.h>
33
 
 
34
26
#define PSYCOPG_MODULE
35
 
#include "psycopg/config.h"
36
 
#include "psycopg/python.h"
37
27
#include "psycopg/psycopg.h"
 
28
 
38
29
#include "psycopg/connection.h"
39
30
#include "psycopg/adapter_qstring.h"
40
31
#include "psycopg/microprotocols_proto.h"
41
32
 
 
33
#include <string.h>
 
34
 
42
35
 
43
36
/* qstring_quote - do the quote process on plain and unicode strings */
44
37
 
56
49
    Dprintf("qstring_quote: encoding to %s", self->encoding);
57
50
 
58
51
    if (PyUnicode_Check(self->wrapped) && self->encoding) {
59
 
        PyObject *enc = PyDict_GetItemString(psycoEncodings, self->encoding);
60
 
        /* note that enc is a borrowed reference */
61
 
 
62
 
        if (enc) {
63
 
            const char *s = PyString_AsString(enc);
64
 
            Dprintf("qstring_quote: encoding unicode object to %s", s);
65
 
            str = PyUnicode_AsEncodedString(self->wrapped, s, NULL);
66
 
            Dprintf("qstring_quote: got encoded object at %p", str);
67
 
            if (str == NULL) return NULL;
68
 
        }
69
 
        else {
70
 
            /* can't find the right encoder, raise exception */
71
 
            PyErr_Format(InterfaceError,
72
 
                         "can't encode unicode string to %s", self->encoding);
73
 
            return NULL;
74
 
        }
 
52
        str = PyUnicode_AsEncodedString(self->wrapped, self->encoding, NULL);
 
53
        Dprintf("qstring_quote: got encoded object at %p", str);
 
54
        if (str == NULL) return NULL;
75
55
    }
76
56
 
 
57
#if PY_MAJOR_VERSION < 3
77
58
    /* if the wrapped object is a simple string, we don't know how to
78
59
       (re)encode it, so we pass it as-is */
79
60
    else if (PyString_Check(self->wrapped)) {
81
62
        /* INCREF to make it ref-wise identical to unicode one */
82
63
        Py_INCREF(str);
83
64
    }
 
65
#endif
84
66
 
85
67
    /* if the wrapped object is not a string, this is an error */
86
68
    else {
90
72
    }
91
73
 
92
74
    /* encode the string into buffer */
93
 
    PyString_AsStringAndSize(str, &s, &len);
 
75
    Bytes_AsStringAndSize(str, &s, &len);
94
76
 
95
77
    /* Call qstring_escape with the GIL released, then reacquire the GIL
96
78
       before verifying that the results can fit into a Python string; raise
113
95
        Py_DECREF(str);
114
96
        return NULL;
115
97
    }
116
 
    
117
 
    self->buffer = PyString_FromStringAndSize(buffer, qlen);
 
98
 
 
99
    self->buffer = Bytes_FromStringAndSize(buffer, qlen);
118
100
    PyMem_Free(buffer);
119
101
    Py_DECREF(str);
120
102
 
124
106
/* qstring_str, qstring_getquoted - return result of quoting */
125
107
 
126
108
static PyObject *
127
 
qstring_str(qstringObject *self)
 
109
qstring_getquoted(qstringObject *self, PyObject *args)
128
110
{
129
111
    if (self->buffer == NULL) {
130
112
        qstring_quote(self);
134
116
}
135
117
 
136
118
static PyObject *
137
 
qstring_getquoted(qstringObject *self, PyObject *args)
 
119
qstring_str(qstringObject *self)
138
120
{
139
 
    if (!PyArg_ParseTuple(args, "")) return NULL;
140
 
    return qstring_str(self);
 
121
    return psycopg_ensure_text(qstring_getquoted(self, NULL));
141
122
}
142
123
 
143
124
static PyObject *
152
133
       we don't need the encoding if that's not the case */
153
134
    if (PyUnicode_Check(self->wrapped)) {
154
135
        if (self->encoding) free(self->encoding);
155
 
        self->encoding = strdup(conn->encoding);
156
 
        Dprintf("qstring_prepare: set encoding to %s", conn->encoding);
 
136
        self->encoding = strdup(conn->codec);
 
137
        Dprintf("qstring_prepare: set encoding to %s", conn->codec);
157
138
    }
158
139
 
159
140
    Py_CLEAR(self->conn);
187
168
/* object member list */
188
169
 
189
170
static struct PyMemberDef qstringObject_members[] = {
190
 
    {"adapted", T_OBJECT, offsetof(qstringObject, wrapped), RO},
191
 
    {"buffer", T_OBJECT, offsetof(qstringObject, buffer), RO},
192
 
    {"encoding", T_STRING, offsetof(qstringObject, encoding), RO},
 
171
    {"adapted", T_OBJECT, offsetof(qstringObject, wrapped), READONLY},
 
172
    {"buffer", T_OBJECT, offsetof(qstringObject, buffer), READONLY},
 
173
    {"encoding", T_STRING, offsetof(qstringObject, encoding), READONLY},
193
174
    {NULL}
194
175
};
195
176
 
196
177
/* object method table */
197
178
 
198
179
static PyMethodDef qstringObject_methods[] = {
199
 
    {"getquoted", (PyCFunction)qstring_getquoted, METH_VARARGS,
 
180
    {"getquoted", (PyCFunction)qstring_getquoted, METH_NOARGS,
200
181
     "getquoted() -> wrapped object value as SQL-quoted string"},
201
182
    {"prepare", (PyCFunction)qstring_prepare, METH_VARARGS,
202
183
     "prepare(conn) -> set encoding to conn->encoding and store conn"},
211
192
{
212
193
    Dprintf("qstring_setup: init qstring object at %p, refcnt = "
213
194
        FORMAT_CODE_PY_SSIZE_T,
214
 
        self, ((PyObject *)self)->ob_refcnt
 
195
        self, Py_REFCNT(self)
215
196
      );
216
197
 
217
198
    self->buffer = NULL;
225
206
 
226
207
    Dprintf("qstring_setup: good qstring object at %p, refcnt = "
227
208
        FORMAT_CODE_PY_SSIZE_T,
228
 
        self, ((PyObject *)self)->ob_refcnt
 
209
        self, Py_REFCNT(self)
229
210
      );
230
211
    return 0;
231
212
}
254
235
 
255
236
    Dprintf("qstring_dealloc: deleted qstring object at %p, refcnt = "
256
237
        FORMAT_CODE_PY_SSIZE_T,
257
 
        obj, obj->ob_refcnt
 
238
        obj, Py_REFCNT(obj)
258
239
      );
259
240
 
260
 
    obj->ob_type->tp_free(obj);
 
241
    Py_TYPE(obj)->tp_free(obj);
261
242
}
262
243
 
263
244
static int
297
278
"QuotedString(str, enc) -> new quoted object with 'enc' encoding"
298
279
 
299
280
PyTypeObject qstringType = {
300
 
    PyObject_HEAD_INIT(NULL)
301
 
    0,
 
281
    PyVarObject_HEAD_INIT(NULL, 0)
302
282
    "psycopg2._psycopg.QuotedString",
303
283
    sizeof(qstringObject),
304
284
    0,