~pythoneers/ubuntu/lucid/psycopg2/ltsppa

« back to all changes in this revision

Viewing changes to psycopg/adapter_binary.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabio Tranchitella
  • Date: 2008-06-12 09:42:55 UTC
  • mfrom: (4.1.6 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080612094255-oq7xqnh1kihod30k
Tags: 2.0.7-4
Rebuilt, this should fixes a dependency problem. (Closes: #485868)

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
20
 */
21
21
 
 
22
#define PY_SSIZE_T_CLEAN
22
23
#include <Python.h>
23
24
#include <structmember.h>
24
25
#include <stringobject.h>
38
39
 
39
40
#ifndef PSYCOPG_OWN_QUOTING
40
41
static unsigned char *
41
 
binary_escape(unsigned char *from, unsigned int from_length,
42
 
               unsigned int *to_length, PGconn *conn)
 
42
binary_escape(unsigned char *from, size_t from_length,
 
43
               size_t *to_length, PGconn *conn)
43
44
{
44
45
#if PG_MAJOR_VERSION > 8 || \
45
46
 (PG_MAJOR_VERSION == 8 && PG_MINOR_VERSION > 1) || \
52
53
}
53
54
#else
54
55
static unsigned char *
55
 
binary_escape(unsigned char *from, unsigned int from_length,
56
 
               unsigned int *to_length, PGconn *conn)
 
56
binary_escape(unsigned char *from, size_t from_length,
 
57
               size_t *to_length, PGconn *conn)
57
58
{
58
 
    unsigneed char *quoted, *chptr, *newptr;
59
 
    int i, space, new_space;
 
59
    unsigned char *quoted, *chptr, *newptr;
 
60
    size_t i, space, new_space;
60
61
 
61
62
    space = from_length + 2;
62
63
 
67
68
 
68
69
    chptr = quoted;
69
70
 
70
 
    for (i=0; i < len; i++) {
 
71
    for (i = 0; i < from_length; i++) {
71
72
        if (chptr - quoted > space - 6) {
72
73
            new_space  =  space * ((space) / (i + 1)) + 2 + 6;
73
74
            if (new_space - space < 1024) space += 1024;
102
103
            }
103
104
            else {
104
105
                unsigned char c;
105
 
                
 
106
 
106
107
                /* escape to octal notation \nnn */
107
108
                *chptr++ = '\\';
108
109
                *chptr++ = '\\';
122
123
 
123
124
    Py_END_ALLOW_THREADS;
124
125
 
125
 
    *to_size = chptr - quoted + 1;
 
126
    *to_length = chptr - quoted + 1;
126
127
    return quoted;
127
128
}
128
129
#endif
134
135
{
135
136
    char *to;
136
137
    const char *buffer;
137
 
    int buffer_len;
 
138
    Py_ssize_t buffer_len;
138
139
    size_t len = 0;
139
140
 
140
141
    /* if we got a plain string or a buffer we escape it and save the buffer */
141
142
    if (PyString_Check(self->wrapped) || PyBuffer_Check(self->wrapped)) {
142
143
        /* escape and build quoted buffer */
143
 
        PyObject_AsCharBuffer(self->wrapped, &buffer, &buffer_len);
 
144
        if (PyObject_AsReadBuffer(self->wrapped, (const void **)&buffer,
 
145
                                  &buffer_len) < 0)
 
146
            return NULL;
144
147
 
145
 
        to = (char *)binary_escape((unsigned char*)buffer, buffer_len, &len,
146
 
            self->conn ? ((connectionObject*)self->conn)->pgconn : NULL);
 
148
        to = (char *)binary_escape((unsigned char*)buffer, (size_t) buffer_len,
 
149
            &len, self->conn ? ((connectionObject*)self->conn)->pgconn : NULL);
147
150
        if (to == NULL) {
148
151
            PyErr_NoMemory();
149
152
            return NULL;
150
153
        }
151
154
 
152
 
        if (len > 0)
153
 
            self->buffer = PyString_FromFormat("'%s'", to);
154
 
        else
 
155
        if (len > 0)
 
156
            self->buffer = PyString_FromFormat(
 
157
                (self->conn && ((connectionObject*)self->conn)->equote)
 
158
                    ? "E'%s'" : "'%s'" , to);
 
159
        else
155
160
            self->buffer = PyString_FromString("''");
 
161
 
156
162
        PQfreemem(to);
157
163
    }
158
 
    
159
 
    /* if the wrapped object is not a string or a buffer, this is an error */ 
 
164
 
 
165
    /* if the wrapped object is not a string or a buffer, this is an error */
160
166
    else {
161
167
        PyErr_SetString(PyExc_TypeError, "can't escape non-string object");
162
168
        return NULL;
163
169
    }
164
 
    
 
170
 
165
171
    return self->buffer;
166
172
}
167
173
 
177
183
    return self->buffer;
178
184
}
179
185
 
180
 
PyObject *
 
186
static PyObject *
181
187
binary_getquoted(binaryObject *self, PyObject *args)
182
188
{
183
189
    if (!PyArg_ParseTuple(args, "")) return NULL;
184
190
    return binary_str(self);
185
191
}
186
192
 
187
 
PyObject *
 
193
static PyObject *
188
194
binary_prepare(binaryObject *self, PyObject *args)
189
195
{
190
196
    connectionObject *conn;
202
208
    return Py_None;
203
209
}
204
210
 
205
 
PyObject *
 
211
static PyObject *
206
212
binary_conform(binaryObject *self, PyObject *args)
207
213
{
208
214
    PyObject *res, *proto;
209
 
    
 
215
 
210
216
    if (!PyArg_ParseTuple(args, "O", &proto)) return NULL;
211
217
 
212
218
    if (proto == (PyObject*)&isqlquoteType)
213
219
        res = (PyObject*)self;
214
220
    else
215
221
        res = Py_None;
216
 
    
 
222
 
217
223
    Py_INCREF(res);
218
224
    return res;
219
225
}
244
250
static int
245
251
binary_setup(binaryObject *self, PyObject *str)
246
252
{
247
 
    Dprintf("binary_setup: init binary object at %p, refcnt = %d",
248
 
            self, ((PyObject *)self)->ob_refcnt);
 
253
    Dprintf("binary_setup: init binary object at %p, refcnt = "
 
254
        FORMAT_CODE_PY_SSIZE_T,
 
255
        self, ((PyObject *)self)->ob_refcnt
 
256
      );
249
257
 
250
258
    self->buffer = NULL;
251
259
    self->conn = NULL;
252
260
    self->wrapped = str;
253
261
    Py_INCREF(self->wrapped);
254
 
    
255
 
    Dprintf("binary_setup: good binary object at %p, refcnt = %d",
256
 
            self, ((PyObject *)self)->ob_refcnt);
 
262
 
 
263
    Dprintf("binary_setup: good binary object at %p, refcnt = "
 
264
        FORMAT_CODE_PY_SSIZE_T,
 
265
        self, ((PyObject *)self)->ob_refcnt);
257
266
    return 0;
258
267
}
259
268
 
265
274
    Py_XDECREF(self->wrapped);
266
275
    Py_XDECREF(self->buffer);
267
276
    Py_XDECREF(self->conn);
268
 
    
269
 
    Dprintf("binary_dealloc: deleted binary object at %p, refcnt = %d",
270
 
            obj, obj->ob_refcnt);
271
 
    
 
277
 
 
278
    Dprintf("binary_dealloc: deleted binary object at %p, refcnt = "
 
279
        FORMAT_CODE_PY_SSIZE_T,
 
280
        obj, obj->ob_refcnt
 
281
      );
 
282
 
272
283
    obj->ob_type->tp_free(obj);
273
284
}
274
285
 
276
287
binary_init(PyObject *obj, PyObject *args, PyObject *kwds)
277
288
{
278
289
    PyObject *str;
279
 
    
 
290
 
280
291
    if (!PyArg_ParseTuple(args, "O", &str))
281
292
        return -1;
282
293
 
285
296
 
286
297
static PyObject *
287
298
binary_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
288
 
{    
 
299
{
289
300
    return type->tp_alloc(type, 0);
290
301
}
291
302
 
315
326
    binary_dealloc, /*tp_dealloc*/
316
327
    0,          /*tp_print*/
317
328
    0,          /*tp_getattr*/
318
 
    0,          /*tp_setattr*/   
 
329
    0,          /*tp_setattr*/
319
330
 
320
331
    0,          /*tp_compare*/
321
332
    (reprfunc)binary_repr, /*tp_repr*/
333
344
    Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
334
345
 
335
346
    binaryType_doc, /*tp_doc*/
336
 
    
 
347
 
337
348
    0,          /*tp_traverse*/
338
349
    0,          /*tp_clear*/
339
350
 
350
361
    0,          /*tp_getset*/
351
362
    0,          /*tp_base*/
352
363
    0,          /*tp_dict*/
353
 
    
 
364
 
354
365
    0,          /*tp_descr_get*/
355
366
    0,          /*tp_descr_set*/
356
367
    0,          /*tp_dictoffset*/
357
 
    
 
368
 
358
369
    binary_init, /*tp_init*/
359
370
    0, /*tp_alloc  will be set to PyType_GenericAlloc in module init*/
360
371
    binary_new, /*tp_new*/
374
385
psyco_Binary(PyObject *module, PyObject *args)
375
386
{
376
387
    PyObject *str;
377
 
    
 
388
 
378
389
    if (!PyArg_ParseTuple(args, "O", &str))
379
390
        return NULL;
380
 
  
 
391
 
381
392
    return PyObject_CallFunction((PyObject *)&binaryType, "O", str);
382
393
}