From 3a94e747d7de9a73a464a8be379a9d2ef5131123 Mon Sep 17 00:00:00 2001 From: Federico Di Gregorio Date: Fri, 19 Jan 2007 14:47:21 +0000 Subject: [PATCH] Encodings from Karsten (part 2.) --- psycopg/connection_type.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/psycopg/connection_type.c b/psycopg/connection_type.c index 44eb1ad3..7e9477ba 100644 --- a/psycopg/connection_type.c +++ b/psycopg/connection_type.c @@ -180,21 +180,30 @@ psyco_conn_set_isolation_level(connectionObject *self, PyObject *args) static PyObject * psyco_conn_set_client_encoding(connectionObject *self, PyObject *args) { - char *pos, *enc = NULL; - + char *buffer, *enc = NULL; + int i, j; + EXC_IF_CONN_CLOSED(self); if (!PyArg_ParseTuple(args, "s", &enc)) return NULL; - /* convert to upper case */ - for (pos = enc ; *pos != '\0' ; pos++) - *pos = toupper(*pos); + /* convert to upper case and remove '-' and '_' from string */ + buffer = PyMem_Malloc(strlen(enc)); + for (i=j=0 ; i < strlen(enc) ; i++) { + if (enc[i] == '_' || enc[i] == '-') + continue; + else + buffer[j++] = toupper(enc[i]); + } + buffer[j] = '\0'; - if (conn_set_client_encoding(self, enc) == 0) { + if (conn_set_client_encoding(self, buffer) == 0) { + PyMem_Free(buffer); Py_INCREF(Py_None); return Py_None; } else { + PyMem_Free(buffer); return NULL; } }