diff options
| author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2010-12-25 11:43:42 +0100 |
|---|---|---|
| committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2010-12-31 03:18:27 +0100 |
| commit | d3f3f1caf0177f32b70dcb7632ad0a164abcfc65 (patch) | |
| tree | 79dbc842549a2fd7230bf739d46fd74017a65ea5 /psycopg/connection_int.c | |
| parent | 60841c65676941cd26d91cc6b704bd4c67fe2cb9 (diff) | |
| download | psycopg2-d3f3f1caf0177f32b70dcb7632ad0a164abcfc65.tar.gz | |
Added utility method to return a string in the connection encoding.
In Py2 the result is plain string, in Py3 an unicode decoded in the
connection encoding.
Diffstat (limited to 'psycopg/connection_int.c')
| -rw-r--r-- | psycopg/connection_int.c | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c index 6d87622..ee4bebc 100644 --- a/psycopg/connection_int.c +++ b/psycopg/connection_int.c @@ -35,6 +35,23 @@ #include <string.h> +/* Return a new "string" from a char* from the database. + * + * On Py2 just get a string, on Py3 decode it in the connection codec. + * + * Use a fallback if the connection is NULL. + */ +PyObject * +conn_text_from_chars(connectionObject *self, const char *str) +{ +#if PY_MAJOR_VERSION < 3 + return PyString_FromString(str); +#else + const char *codec = self ? self->codec : "ascii"; + return PyUnicode_Decode(str, strlen(str), codec, "replace"); +#endif +} + /* conn_notice_callback - process notices */ static void @@ -76,9 +93,7 @@ conn_notice_process(connectionObject *self) while (notice != NULL) { PyObject *msg; - /* XXX possible other encode I think */ - msg = Text_FromUTF8(notice->message); - + msg = conn_text_from_chars(self, notice->message); Dprintf("conn_notice_process: %s", notice->message); /* Respect the order in which notices were produced, @@ -146,9 +161,8 @@ conn_notifies_process(connectionObject *self) (int) pgn->be_pid, pgn->relname); if (!(pid = PyInt_FromLong((long)pgn->be_pid))) { goto error; } - /* XXX in the connection encoding? */ - if (!(channel = Text_FromUTF8(pgn->relname))) { goto error; } - if (!(payload = Text_FromUTF8(pgn->extra))) { goto error; } + if (!(channel = conn_text_from_chars(self, pgn->relname))) { goto error; } + if (!(payload = conn_text_from_chars(self, pgn->extra))) { goto error; } if (!(notify = PyObject_CallFunctionObjArgs((PyObject *)&NotifyType, pid, channel, payload, NULL))) { |
