summaryrefslogtreecommitdiff
path: root/psycopg/adapter_qstring.c
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2016-12-26 19:47:48 +0100
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2016-12-29 22:11:58 +0100
commit3295beb7774098659a40649d65e84f7ae9a4838e (patch)
tree9c62fb63d36d4d7bd9f47936f7410032341a642f /psycopg/adapter_qstring.c
parentdfe547856ee946163dfdc695723f7ab67865228b (diff)
downloadpsycopg2-3295beb7774098659a40649d65e84f7ae9a4838e.tar.gz
Don't look up for Python encoding
Store the encode/decode functions for the right codec in the connection. The Python encoding name has been dropped of the connection to avoid the temptation to use it...
Diffstat (limited to 'psycopg/adapter_qstring.c')
-rw-r--r--psycopg/adapter_qstring.c39
1 files changed, 12 insertions, 27 deletions
diff --git a/psycopg/adapter_qstring.c b/psycopg/adapter_qstring.c
index febb49a..73579c5 100644
--- a/psycopg/adapter_qstring.c
+++ b/psycopg/adapter_qstring.c
@@ -36,20 +36,6 @@ static const char *default_encoding = "latin1";
/* qstring_quote - do the quote process on plain and unicode strings */
-const char *
-_qstring_get_encoding(qstringObject *self)
-{
- /* if the wrapped object is an unicode object we can encode it to match
- conn->encoding but if the encoding is not specified we don't know what
- to do and we raise an exception */
- if (self->conn) {
- return self->conn->pyenc;
- }
- else {
- return self->encoding ? self->encoding : default_encoding;
- }
-}
-
static PyObject *
qstring_quote(qstringObject *self)
{
@@ -59,19 +45,15 @@ qstring_quote(qstringObject *self)
const char *encoding;
PyObject *rv = NULL;
- encoding = _qstring_get_encoding(self);
- Dprintf("qstring_quote: encoding to %s", encoding);
-
if (PyUnicode_Check(self->wrapped)) {
- if (encoding) {
- str = PyUnicode_AsEncodedString(self->wrapped, encoding, NULL);
- Dprintf("qstring_quote: got encoded object at %p", str);
- if (str == NULL) goto exit;
+ if (self->conn) {
+ if (!(str = conn_encode(self->conn, self->wrapped))) { goto exit; }
}
else {
- PyErr_SetString(PyExc_TypeError,
- "missing encoding to encode unicode object");
- goto exit;
+ encoding = self->encoding ? self->encoding : default_encoding;
+ if(!(str = PyUnicode_AsEncodedString(self->wrapped, encoding, NULL))) {
+ goto exit;
+ }
}
}
@@ -162,9 +144,12 @@ qstring_conform(qstringObject *self, PyObject *args)
static PyObject *
qstring_get_encoding(qstringObject *self)
{
- const char *encoding;
- encoding = _qstring_get_encoding(self);
- return Text_FromUTF8(encoding);
+ if (self->conn) {
+ return conn_pgenc_to_pyenc(self->conn->encoding, NULL);
+ }
+ else {
+ return Text_FromUTF8(self->encoding ? self->encoding : default_encoding);
+ }
}
static int