23
23
* License for more details.
26
#define PY_SSIZE_T_CLEAN
28
#include <structmember.h>
29
#include <stringobject.h>
34
26
#define PSYCOPG_MODULE
35
#include "psycopg/config.h"
36
#include "psycopg/python.h"
37
27
#include "psycopg/psycopg.h"
38
29
#include "psycopg/connection.h"
39
30
#include "psycopg/adapter_qstring.h"
40
31
#include "psycopg/microprotocols_proto.h"
43
36
/* qstring_quote - do the quote process on plain and unicode strings */
56
49
Dprintf("qstring_quote: encoding to %s", self->encoding);
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 */
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;
70
/* can't find the right encoder, raise exception */
71
PyErr_Format(InterfaceError,
72
"can't encode unicode string to %s", self->encoding);
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;
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)) {
92
74
/* encode the string into buffer */
93
PyString_AsStringAndSize(str, &s, &len);
75
Bytes_AsStringAndSize(str, &s, &len);
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
124
106
/* qstring_str, qstring_getquoted - return result of quoting */
126
108
static PyObject *
127
qstring_str(qstringObject *self)
109
qstring_getquoted(qstringObject *self, PyObject *args)
129
111
if (self->buffer == NULL) {
130
112
qstring_quote(self);
136
118
static PyObject *
137
qstring_getquoted(qstringObject *self, PyObject *args)
119
qstring_str(qstringObject *self)
139
if (!PyArg_ParseTuple(args, "")) return NULL;
140
return qstring_str(self);
121
return psycopg_ensure_text(qstring_getquoted(self, NULL));
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);
159
140
Py_CLEAR(self->conn);
187
168
/* object member list */
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},
196
177
/* object method table */
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"},
297
278
"QuotedString(str, enc) -> new quoted object with 'enc' encoding"
299
280
PyTypeObject qstringType = {
300
PyObject_HEAD_INIT(NULL)
281
PyVarObject_HEAD_INIT(NULL, 0)
302
282
"psycopg2._psycopg.QuotedString",
303
283
sizeof(qstringObject),