2004-10-19 07:17:12 +04:00
|
|
|
/* adapter_binary.c - Binary objects
|
|
|
|
*
|
2010-02-13 01:34:53 +03:00
|
|
|
* Copyright (C) 2003-2010 Federico Di Gregorio <fog@debian.org>
|
2004-10-19 07:17:12 +04:00
|
|
|
*
|
|
|
|
* This file is part of psycopg.
|
|
|
|
*
|
2010-02-13 01:34:53 +03:00
|
|
|
* psycopg2 is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2004-10-19 07:17:12 +04:00
|
|
|
*
|
2010-02-13 01:34:53 +03:00
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link this program with the OpenSSL library (or with
|
|
|
|
* modified versions of OpenSSL that use the same license as OpenSSL),
|
|
|
|
* and distribute linked combinations including the two.
|
2004-10-19 07:17:12 +04:00
|
|
|
*
|
2010-02-13 01:34:53 +03:00
|
|
|
* You must obey the GNU Lesser General Public License in all respects for
|
|
|
|
* all of the code used other than OpenSSL.
|
|
|
|
*
|
|
|
|
* psycopg2 is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
* License for more details.
|
2004-10-19 07:17:12 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define PSYCOPG_MODULE
|
|
|
|
#include "psycopg/psycopg.h"
|
2010-12-12 13:55:19 +03:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
#include "psycopg/adapter_binary.h"
|
2005-03-02 17:51:24 +03:00
|
|
|
#include "psycopg/microprotocols_proto.h"
|
2010-12-12 13:55:19 +03:00
|
|
|
#include "psycopg/connection.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
/** the quoting code */
|
|
|
|
|
2006-05-24 14:02:33 +04:00
|
|
|
static unsigned char *
|
2007-04-10 10:36:18 +04:00
|
|
|
binary_escape(unsigned char *from, size_t from_length,
|
|
|
|
size_t *to_length, PGconn *conn)
|
2006-05-24 14:02:33 +04:00
|
|
|
{
|
2009-02-17 10:00:52 +03:00
|
|
|
#if PG_VERSION_HEX >= 0x080104
|
2006-06-18 09:57:01 +04:00
|
|
|
if (conn)
|
|
|
|
return PQescapeByteaConn(conn, from, from_length, to_length);
|
|
|
|
else
|
2006-05-24 14:29:35 +04:00
|
|
|
#endif
|
2006-06-18 09:57:01 +04:00
|
|
|
return PQescapeBytea(from, from_length, to_length);
|
2006-05-24 14:02:33 +04:00
|
|
|
}
|
2004-10-19 07:17:12 +04:00
|
|
|
|
2011-02-16 04:23:53 +03:00
|
|
|
#define HAS_BUFFER (PY_MAJOR_VERSION < 3)
|
|
|
|
#define HAS_MEMORYVIEW (PY_MAJOR_VERSION > 2 || PY_MINOR_VERSION >= 6)
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
/* binary_quote - do the quote process on plain and unicode strings */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
binary_quote(binaryObject *self)
|
|
|
|
{
|
2011-02-16 04:23:53 +03:00
|
|
|
char *to = NULL;
|
|
|
|
const char *buffer = NULL;
|
2006-09-23 09:14:24 +04:00
|
|
|
Py_ssize_t buffer_len;
|
2004-10-19 07:17:12 +04:00
|
|
|
size_t len = 0;
|
2011-02-16 04:23:53 +03:00
|
|
|
PyObject *rv = NULL;
|
|
|
|
#if HAS_MEMORYVIEW
|
|
|
|
Py_buffer view;
|
|
|
|
int got_view = 0;
|
|
|
|
#endif
|
2006-09-01 12:23:51 +04:00
|
|
|
|
2012-03-05 06:08:45 +04:00
|
|
|
/* Allow Binary(None) to work */
|
|
|
|
if (self->wrapped == Py_None) {
|
|
|
|
Py_INCREF(psyco_null);
|
|
|
|
rv = psyco_null;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
/* if we got a plain string or a buffer we escape it and save the buffer */
|
2011-02-16 04:23:53 +03:00
|
|
|
|
|
|
|
#if HAS_MEMORYVIEW
|
|
|
|
if (PyObject_CheckBuffer(self->wrapped)) {
|
|
|
|
if (0 > PyObject_GetBuffer(self->wrapped, &view, PyBUF_CONTIG_RO)) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
got_view = 1;
|
|
|
|
buffer = (const char *)(view.buf);
|
|
|
|
buffer_len = view.len;
|
|
|
|
}
|
2010-12-13 00:48:54 +03:00
|
|
|
#endif
|
2011-02-16 04:23:53 +03:00
|
|
|
|
|
|
|
#if HAS_BUFFER
|
|
|
|
if (!buffer && (Bytes_Check(self->wrapped) || PyBuffer_Check(self->wrapped))) {
|
2008-01-11 01:19:23 +03:00
|
|
|
if (PyObject_AsReadBuffer(self->wrapped, (const void **)&buffer,
|
2011-02-16 04:23:53 +03:00
|
|
|
&buffer_len) < 0) {
|
|
|
|
goto exit;
|
2004-10-19 07:17:12 +04:00
|
|
|
}
|
2011-02-16 04:23:53 +03:00
|
|
|
}
|
|
|
|
#endif
|
2004-10-19 07:17:12 +04:00
|
|
|
|
2011-02-16 04:23:53 +03:00
|
|
|
if (!buffer) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* escape and build quoted buffer */
|
2007-11-09 08:51:12 +03:00
|
|
|
|
2012-03-05 06:08:45 +04:00
|
|
|
to = (char *)binary_escape((unsigned char*)buffer, (size_t)buffer_len,
|
2011-02-16 04:23:53 +03:00
|
|
|
&len, self->conn ? ((connectionObject*)self->conn)->pgconn : NULL);
|
|
|
|
if (to == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
goto exit;
|
2004-10-19 07:17:12 +04:00
|
|
|
}
|
2007-04-10 10:36:18 +04:00
|
|
|
|
2011-02-16 04:23:53 +03:00
|
|
|
if (len > 0)
|
|
|
|
rv = Bytes_FromFormat(
|
|
|
|
(self->conn && ((connectionObject*)self->conn)->equote)
|
|
|
|
? "E'%s'::bytea" : "'%s'::bytea" , to);
|
|
|
|
else
|
|
|
|
rv = Bytes_FromString("''::bytea");
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if (to) { PQfreemem(to); }
|
|
|
|
#if HAS_MEMORYVIEW
|
|
|
|
if (got_view) { PyBuffer_Release(&view); }
|
|
|
|
#endif
|
|
|
|
|
2011-02-15 18:53:07 +03:00
|
|
|
/* if the wrapped object is not bytes or a buffer, this is an error */
|
2011-02-16 04:23:53 +03:00
|
|
|
if (!rv && !PyErr_Occurred()) {
|
|
|
|
PyErr_Format(PyExc_TypeError, "can't escape %s to binary",
|
|
|
|
Py_TYPE(self->wrapped)->tp_name);
|
2004-10-19 07:17:12 +04:00
|
|
|
}
|
2007-04-10 10:36:18 +04:00
|
|
|
|
2011-02-16 04:23:53 +03:00
|
|
|
return rv;
|
2004-10-19 07:17:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* binary_str, binary_getquoted - return result of quoting */
|
2010-12-25 14:03:15 +03:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
static PyObject *
|
2010-12-25 14:03:15 +03:00
|
|
|
binary_getquoted(binaryObject *self, PyObject *args)
|
2004-10-19 07:17:12 +04:00
|
|
|
{
|
|
|
|
if (self->buffer == NULL) {
|
2011-02-16 04:23:53 +03:00
|
|
|
self->buffer = binary_quote(self);
|
2004-10-19 07:17:12 +04:00
|
|
|
}
|
2011-02-16 04:23:53 +03:00
|
|
|
Py_XINCREF(self->buffer);
|
2004-10-19 07:17:12 +04:00
|
|
|
return self->buffer;
|
|
|
|
}
|
|
|
|
|
2008-01-13 19:05:59 +03:00
|
|
|
static PyObject *
|
2010-12-25 14:03:15 +03:00
|
|
|
binary_str(binaryObject *self)
|
2004-10-19 07:17:12 +04:00
|
|
|
{
|
2010-12-25 14:03:15 +03:00
|
|
|
return psycopg_ensure_text(binary_getquoted(self, NULL));
|
2004-10-19 07:17:12 +04:00
|
|
|
}
|
|
|
|
|
2008-01-13 19:05:59 +03:00
|
|
|
static PyObject *
|
2006-05-24 14:02:33 +04:00
|
|
|
binary_prepare(binaryObject *self, PyObject *args)
|
|
|
|
{
|
2012-03-04 21:59:51 +04:00
|
|
|
PyObject *conn;
|
2006-05-24 14:02:33 +04:00
|
|
|
|
2012-03-04 21:59:51 +04:00
|
|
|
if (!PyArg_ParseTuple(args, "O!", &connectionType, &conn))
|
2006-05-24 14:02:33 +04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Py_XDECREF(self->conn);
|
2012-03-04 21:59:51 +04:00
|
|
|
self->conn = conn;
|
|
|
|
Py_INCREF(self->conn);
|
2006-05-24 14:02:33 +04:00
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
2008-01-13 19:05:59 +03:00
|
|
|
static PyObject *
|
2005-03-02 17:51:24 +03:00
|
|
|
binary_conform(binaryObject *self, PyObject *args)
|
2004-10-19 07:17:12 +04:00
|
|
|
{
|
2005-03-02 17:51:24 +03:00
|
|
|
PyObject *res, *proto;
|
2007-04-10 10:36:18 +04:00
|
|
|
|
2005-03-02 17:51:24 +03:00
|
|
|
if (!PyArg_ParseTuple(args, "O", &proto)) return NULL;
|
2004-10-19 07:17:12 +04:00
|
|
|
|
2005-03-02 17:51:24 +03:00
|
|
|
if (proto == (PyObject*)&isqlquoteType)
|
|
|
|
res = (PyObject*)self;
|
|
|
|
else
|
|
|
|
res = Py_None;
|
2007-04-10 10:36:18 +04:00
|
|
|
|
2005-03-02 17:51:24 +03:00
|
|
|
Py_INCREF(res);
|
|
|
|
return res;
|
2004-10-19 07:17:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** the Binary object **/
|
|
|
|
|
|
|
|
/* object member list */
|
|
|
|
|
|
|
|
static struct PyMemberDef binaryObject_members[] = {
|
2010-12-12 16:36:30 +03:00
|
|
|
{"adapted", T_OBJECT, offsetof(binaryObject, wrapped), READONLY},
|
|
|
|
{"buffer", T_OBJECT, offsetof(binaryObject, buffer), READONLY},
|
2004-10-19 07:17:12 +04:00
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* object method table */
|
|
|
|
|
|
|
|
static PyMethodDef binaryObject_methods[] = {
|
2010-11-09 06:18:54 +03:00
|
|
|
{"getquoted", (PyCFunction)binary_getquoted, METH_NOARGS,
|
2004-10-19 07:17:12 +04:00
|
|
|
"getquoted() -> wrapped object value as SQL-quoted binary string"},
|
2006-05-24 14:02:33 +04:00
|
|
|
{"prepare", (PyCFunction)binary_prepare, METH_VARARGS,
|
|
|
|
"prepare(conn) -> prepare for binary encoding using conn"},
|
2005-03-02 17:51:24 +03:00
|
|
|
{"__conform__", (PyCFunction)binary_conform, METH_VARARGS, NULL},
|
2004-10-19 07:17:12 +04:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* initialization and finalization methods */
|
|
|
|
|
|
|
|
static int
|
|
|
|
binary_setup(binaryObject *self, PyObject *str)
|
|
|
|
{
|
2007-04-10 10:36:18 +04:00
|
|
|
Dprintf("binary_setup: init binary object at %p, refcnt = "
|
|
|
|
FORMAT_CODE_PY_SSIZE_T,
|
2010-12-13 00:31:10 +03:00
|
|
|
self, Py_REFCNT(self)
|
2007-04-10 10:36:18 +04:00
|
|
|
);
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
self->buffer = NULL;
|
2006-05-24 14:02:33 +04:00
|
|
|
self->conn = NULL;
|
2008-07-21 09:41:54 +04:00
|
|
|
Py_INCREF(str);
|
2004-10-19 07:17:12 +04:00
|
|
|
self->wrapped = str;
|
2007-04-10 10:36:18 +04:00
|
|
|
|
|
|
|
Dprintf("binary_setup: good binary object at %p, refcnt = "
|
|
|
|
FORMAT_CODE_PY_SSIZE_T,
|
2010-12-13 00:31:10 +03:00
|
|
|
self, Py_REFCNT(self));
|
2004-10-19 07:17:12 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-01 07:59:25 +04:00
|
|
|
static int
|
|
|
|
binary_traverse(PyObject *obj, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
binaryObject *self = (binaryObject *)obj;
|
|
|
|
|
|
|
|
Py_VISIT(self->wrapped);
|
|
|
|
Py_VISIT(self->buffer);
|
|
|
|
Py_VISIT(self->conn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
static void
|
|
|
|
binary_dealloc(PyObject* obj)
|
|
|
|
{
|
|
|
|
binaryObject *self = (binaryObject *)obj;
|
|
|
|
|
2008-07-21 09:41:54 +04:00
|
|
|
Py_CLEAR(self->wrapped);
|
|
|
|
Py_CLEAR(self->buffer);
|
|
|
|
Py_CLEAR(self->conn);
|
2007-04-10 10:36:18 +04:00
|
|
|
|
|
|
|
Dprintf("binary_dealloc: deleted binary object at %p, refcnt = "
|
|
|
|
FORMAT_CODE_PY_SSIZE_T,
|
2010-12-13 00:31:10 +03:00
|
|
|
obj, Py_REFCNT(obj)
|
2007-04-10 10:36:18 +04:00
|
|
|
);
|
|
|
|
|
2010-12-13 00:31:10 +03:00
|
|
|
Py_TYPE(obj)->tp_free(obj);
|
2004-10-19 07:17:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
binary_init(PyObject *obj, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
|
|
|
PyObject *str;
|
2007-04-10 10:36:18 +04:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
if (!PyArg_ParseTuple(args, "O", &str))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return binary_setup((binaryObject *)obj, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
binary_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
2007-04-10 10:36:18 +04:00
|
|
|
{
|
2004-10-19 07:17:12 +04:00
|
|
|
return type->tp_alloc(type, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
binary_repr(binaryObject *self)
|
|
|
|
{
|
2005-10-18 05:29:47 +04:00
|
|
|
return PyString_FromFormat("<psycopg2._psycopg.Binary object at %p>", self);
|
2004-10-19 07:17:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* object type */
|
|
|
|
|
|
|
|
#define binaryType_doc \
|
2005-10-18 05:29:47 +04:00
|
|
|
"Binary(buffer) -> new binary object"
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
PyTypeObject binaryType = {
|
2010-12-13 00:47:36 +03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2005-10-18 05:29:47 +04:00
|
|
|
"psycopg2._psycopg.Binary",
|
2013-03-20 05:34:50 +04:00
|
|
|
sizeof(binaryObject), 0,
|
2004-10-19 07:17:12 +04:00
|
|
|
binary_dealloc, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
0, /*tp_getattr*/
|
2007-04-10 10:36:18 +04:00
|
|
|
0, /*tp_setattr*/
|
2004-10-19 07:17:12 +04:00
|
|
|
0, /*tp_compare*/
|
|
|
|
(reprfunc)binary_repr, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
0, /*tp_hash */
|
|
|
|
0, /*tp_call*/
|
|
|
|
(reprfunc)binary_str, /*tp_str*/
|
|
|
|
0, /*tp_getattro*/
|
|
|
|
0, /*tp_setattro*/
|
|
|
|
0, /*tp_as_buffer*/
|
2008-07-01 07:59:25 +04:00
|
|
|
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/
|
2004-10-19 07:17:12 +04:00
|
|
|
binaryType_doc, /*tp_doc*/
|
2008-07-01 07:59:25 +04:00
|
|
|
binary_traverse, /*tp_traverse*/
|
2004-10-19 07:17:12 +04:00
|
|
|
0, /*tp_clear*/
|
|
|
|
0, /*tp_richcompare*/
|
|
|
|
0, /*tp_weaklistoffset*/
|
|
|
|
0, /*tp_iter*/
|
|
|
|
0, /*tp_iternext*/
|
|
|
|
binaryObject_methods, /*tp_methods*/
|
|
|
|
binaryObject_members, /*tp_members*/
|
|
|
|
0, /*tp_getset*/
|
|
|
|
0, /*tp_base*/
|
|
|
|
0, /*tp_dict*/
|
|
|
|
0, /*tp_descr_get*/
|
|
|
|
0, /*tp_descr_set*/
|
|
|
|
0, /*tp_dictoffset*/
|
|
|
|
binary_init, /*tp_init*/
|
2005-04-10 07:05:39 +04:00
|
|
|
0, /*tp_alloc will be set to PyType_GenericAlloc in module init*/
|
2004-10-19 07:17:12 +04:00
|
|
|
binary_new, /*tp_new*/
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** module-level functions **/
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
psyco_Binary(PyObject *module, PyObject *args)
|
|
|
|
{
|
|
|
|
PyObject *str;
|
2007-04-10 10:36:18 +04:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
if (!PyArg_ParseTuple(args, "O", &str))
|
|
|
|
return NULL;
|
2007-04-10 10:36:18 +04:00
|
|
|
|
2010-11-09 04:49:22 +03:00
|
|
|
return PyObject_CallFunctionObjArgs((PyObject *)&binaryType, str, NULL);
|
2004-10-19 07:17:12 +04:00
|
|
|
}
|