conn->codec rename to pyenc

This commit is contained in:
Daniele Varrazzo 2016-12-26 12:25:13 +01:00
parent 7caba160b7
commit f439ca61d6
14 changed files with 67 additions and 67 deletions

View File

@ -417,7 +417,7 @@ details.
.. data:: encodings .. data:: encodings
Mapping from `PostgreSQL encoding`__ names to `Python codec`__ names. Mapping from `PostgreSQL encoding`__ to `Python encoding`__ names.
Used by Psycopg when adapting or casting unicode strings. See Used by Psycopg when adapting or casting unicode strings. See
:ref:`unicode-handling`. :ref:`unicode-handling`.

View File

@ -355,7 +355,7 @@ Unicode handling
Psycopg can exchange Unicode data with a PostgreSQL database. Python Psycopg can exchange Unicode data with a PostgreSQL database. Python
`!unicode` objects are automatically *encoded* in the client encoding `!unicode` objects are automatically *encoded* in the client encoding
defined on the database connection (the `PostgreSQL encoding`__, available in defined on the database connection (the `PostgreSQL encoding`__, available in
`connection.encoding`, is translated into a `Python codec`__ using the `connection.encoding`, is translated into a `Python encoding`__ using the
`~psycopg2.extensions.encodings` mapping):: `~psycopg2.extensions.encodings` mapping)::
>>> print u, type(u) >>> print u, type(u)

View File

@ -43,7 +43,7 @@ _qstring_get_encoding(qstringObject *self)
conn->encoding but if the encoding is not specified we don't know what conn->encoding but if the encoding is not specified we don't know what
to do and we raise an exception */ to do and we raise an exception */
if (self->conn) { if (self->conn) {
return self->conn->codec; return self->conn->pyenc;
} }
else { else {
return self->encoding ? self->encoding : default_encoding; return self->encoding ? self->encoding : default_encoding;

View File

@ -83,7 +83,7 @@ struct connectionObject {
char *dsn; /* data source name */ char *dsn; /* data source name */
char *critical; /* critical error on this connection */ char *critical; /* critical error on this connection */
char *encoding; /* current backend encoding */ char *encoding; /* current backend encoding */
char *codec; /* python codec name for encoding */ char *pyenc; /* connection encoding python name */
long int closed; /* 1 means connection has been closed; long int closed; /* 1 means connection has been closed;
2 that something horrible happened */ 2 that something horrible happened */

View File

@ -61,8 +61,8 @@ conn_text_from_chars(connectionObject *self, const char *str)
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
return PyString_FromString(str); return PyString_FromString(str);
#else #else
const char *codec = self ? self->codec : "ascii"; const char *pyenc = self ? self->pyenc : "ascii";
return PyUnicode_Decode(str, strlen(str), codec, "replace"); return PyUnicode_Decode(str, strlen(str), pyenc, "replace");
#endif #endif
} }
@ -321,43 +321,43 @@ exit:
return rv; return rv;
} }
/* Convert a PostgreSQL encoding to a Python codec. /* Convert a PostgreSQL encoding name to a Python encoding name.
* *
* Set 'codec' to a new copy of the codec name allocated on the Python heap. * Set 'pyenc' to a new copy of the encoding name allocated on the Python heap.
* Return 0 in case of success, else -1 and set an exception. * Return 0 in case of success, else -1 and set an exception.
* *
* 'enc' should be already normalized (uppercase, no - or _). * 'pgenc' should be already normalized (uppercase, no - or _).
*/ */
RAISES_NEG static int RAISES_NEG static int
conn_encoding_to_codec(const char *enc, char **codec) conn_pgenc_to_pyenc(const char *pgenc, char **pyenc)
{ {
char *tmp; char *tmp;
Py_ssize_t size; Py_ssize_t size;
PyObject *pyenc = NULL; PyObject *opyenc = NULL;
int rv = -1; int rv = -1;
/* Find the Py codec name from the PG encoding */ /* Find the Py encoding name from the PG encoding */
if (!(pyenc = PyDict_GetItemString(psycoEncodings, enc))) { if (!(opyenc = PyDict_GetItemString(psycoEncodings, pgenc))) {
PyErr_Format(OperationalError, PyErr_Format(OperationalError,
"no Python codec for client encoding '%s'", enc); "no Python encoding for PostgreSQL encoding '%s'", pgenc);
goto exit; goto exit;
} }
/* Convert the codec in a bytes string to extract the c string. */ /* Convert the encoding in a bytes string to extract the c string. */
Py_INCREF(pyenc); Py_INCREF(opyenc);
if (!(pyenc = psycopg_ensure_bytes(pyenc))) { if (!(opyenc = psycopg_ensure_bytes(opyenc))) {
goto exit; goto exit;
} }
if (-1 == Bytes_AsStringAndSize(pyenc, &tmp, &size)) { if (-1 == Bytes_AsStringAndSize(opyenc, &tmp, &size)) {
goto exit; goto exit;
} }
/* have our own copy of the python codec name */ /* have our own copy of the python encoding name */
rv = psycopg_strdup(codec, tmp, size); rv = psycopg_strdup(pyenc, tmp, size);
exit: exit:
Py_XDECREF(pyenc); Py_XDECREF(opyenc);
return rv; return rv;
} }
@ -367,15 +367,15 @@ exit:
void void
conn_set_fast_codec(connectionObject *self) conn_set_fast_codec(connectionObject *self)
{ {
Dprintf("conn_set_fast_codec: codec=%s", self->codec); Dprintf("conn_set_fast_codec: encoding=%s", self->pyenc);
if (0 == strcmp(self->codec, "utf_8")) { if (0 == strcmp(self->pyenc, "utf_8")) {
Dprintf("conn_set_fast_codec: PyUnicode_DecodeUTF8"); Dprintf("conn_set_fast_codec: PyUnicode_DecodeUTF8");
self->cdecoder = PyUnicode_DecodeUTF8; self->cdecoder = PyUnicode_DecodeUTF8;
return; return;
} }
if (0 == strcmp(self->codec, "iso8859_1")) { if (0 == strcmp(self->pyenc, "iso8859_1")) {
Dprintf("conn_set_fast_codec: PyUnicode_DecodeLatin1"); Dprintf("conn_set_fast_codec: PyUnicode_DecodeLatin1");
self->cdecoder = PyUnicode_DecodeLatin1; self->cdecoder = PyUnicode_DecodeLatin1;
return; return;
@ -389,7 +389,7 @@ conn_set_fast_codec(connectionObject *self)
/* Read the client encoding from the connection. /* Read the client encoding from the connection.
* *
* Store the encoding in the pgconn->encoding field and the name of the * Store the encoding in the pgconn->encoding field and the name of the
* matching python codec in codec. The buffers are allocated on the Python * matching python encoding in pyenc. The buffers are allocated on the Python
* heap. * heap.
* *
* Return 0 on success, else nonzero. * Return 0 on success, else nonzero.
@ -397,7 +397,7 @@ conn_set_fast_codec(connectionObject *self)
RAISES_NEG static int RAISES_NEG static int
conn_read_encoding(connectionObject *self, PGconn *pgconn) conn_read_encoding(connectionObject *self, PGconn *pgconn)
{ {
char *enc = NULL, *codec = NULL; char *pgenc = NULL, *pyenc = NULL;
const char *tmp; const char *tmp;
int rv = -1; int rv = -1;
@ -409,31 +409,31 @@ conn_read_encoding(connectionObject *self, PGconn *pgconn)
goto exit; goto exit;
} }
if (0 > clear_encoding_name(tmp, &enc)) { if (0 > clear_encoding_name(tmp, &pgenc)) {
goto exit; goto exit;
} }
/* Look for this encoding in Python codecs. */ /* Look for this encoding in Python codecs. */
if (0 > conn_encoding_to_codec(enc, &codec)) { if (0 > conn_pgenc_to_pyenc(pgenc, &pyenc)) {
goto exit; goto exit;
} }
/* Good, success: store the encoding/codec in the connection. */ /* Good, success: store the encoding/pyenc in the connection. */
PyMem_Free(self->encoding); PyMem_Free(self->encoding);
self->encoding = enc; self->encoding = pgenc;
enc = NULL; pgenc = NULL;
PyMem_Free(self->codec); PyMem_Free(self->pyenc);
self->codec = codec; self->pyenc = pyenc;
codec = NULL; pyenc = NULL;
conn_set_fast_codec(self); conn_set_fast_codec(self);
rv = 0; rv = 0;
exit: exit:
PyMem_Free(enc); PyMem_Free(pgenc);
PyMem_Free(codec); PyMem_Free(pyenc);
return rv; return rv;
} }
@ -1252,21 +1252,21 @@ endlock:
/* conn_set_client_encoding - switch client encoding on connection */ /* conn_set_client_encoding - switch client encoding on connection */
RAISES_NEG int RAISES_NEG int
conn_set_client_encoding(connectionObject *self, const char *enc) conn_set_client_encoding(connectionObject *self, const char *pgenc)
{ {
PGresult *pgres = NULL; PGresult *pgres = NULL;
char *error = NULL; char *error = NULL;
int res = -1; int res = -1;
char *codec = NULL; char *pyenc = NULL;
char *clean_enc = NULL; char *clean_enc = NULL;
/* If the current encoding is equal to the requested one we don't /* If the current encoding is equal to the requested one we don't
issue any query to the backend */ issue any query to the backend */
if (strcmp(self->encoding, enc) == 0) return 0; if (strcmp(self->encoding, pgenc) == 0) return 0;
/* We must know what python codec this encoding is. */ /* We must know what python encoding this encoding is. */
if (0 > clear_encoding_name(enc, &clean_enc)) { goto exit; } if (0 > clear_encoding_name(pgenc, &clean_enc)) { goto exit; }
if (0 > conn_encoding_to_codec(clean_enc, &codec)) { goto exit; } if (0 > conn_pgenc_to_pyenc(clean_enc, &pyenc)) { goto exit; }
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;
pthread_mutex_lock(&self->lock); pthread_mutex_lock(&self->lock);
@ -1290,18 +1290,18 @@ conn_set_client_encoding(connectionObject *self, const char *enc)
clean_enc = NULL; clean_enc = NULL;
} }
/* Store the python codec too. */ /* Store the python encoding name too. */
{ {
char *tmp = self->codec; char *tmp = self->pyenc;
self->codec = codec; self->pyenc = pyenc;
PyMem_Free(tmp); PyMem_Free(tmp);
codec = NULL; pyenc = NULL;
} }
conn_set_fast_codec(self); conn_set_fast_codec(self);
Dprintf("conn_set_client_encoding: set encoding to %s (codec: %s)", Dprintf("conn_set_client_encoding: set encoding to %s (Python: %s)",
self->encoding, self->codec); self->encoding, self->pyenc);
endlock: endlock:
pthread_mutex_unlock(&self->lock); pthread_mutex_unlock(&self->lock);
@ -1312,7 +1312,7 @@ endlock:
exit: exit:
PyMem_Free(clean_enc); PyMem_Free(clean_enc);
PyMem_Free(codec); PyMem_Free(pyenc);
return res; return res;
} }

View File

@ -1164,7 +1164,7 @@ connection_dealloc(PyObject* obj)
PyMem_Free(self->dsn); PyMem_Free(self->dsn);
PyMem_Free(self->encoding); PyMem_Free(self->encoding);
PyMem_Free(self->codec); PyMem_Free(self->pyenc);
if (self->critical) free(self->critical); if (self->critical) free(self->critical);
if (self->cancel) PQfreeCancel(self->cancel); if (self->cancel) PQfreeCancel(self->cancel);

View File

@ -286,7 +286,7 @@ static PyObject *_psyco_curs_validate_sql_basic(
Py_INCREF(sql); Py_INCREF(sql);
} }
else if (PyUnicode_Check(sql)) { else if (PyUnicode_Check(sql)) {
char *enc = self->conn->codec; char *enc = self->conn->pyenc;
sql = PyUnicode_AsEncodedString(sql, enc, NULL); sql = PyUnicode_AsEncodedString(sql, enc, NULL);
/* if there was an error during the encoding from unicode to the /* if there was an error during the encoding from unicode to the
target encoding, we just let the exception propagate */ target encoding, we just let the exception propagate */

View File

@ -34,7 +34,7 @@ typedef struct {
PyObject *pgerror; PyObject *pgerror;
PyObject *pgcode; PyObject *pgcode;
cursorObject *cursor; cursorObject *cursor;
char *codec; char *pyenc;
PGresult *pgres; PGresult *pgres;
} errorObject; } errorObject;

View File

@ -43,7 +43,7 @@ error_text_from_chars(errorObject *self, const char *str)
return PyString_FromString(str); return PyString_FromString(str);
#else #else
return PyUnicode_Decode(str, strlen(str), return PyUnicode_Decode(str, strlen(str),
self->codec ? self->codec : "ascii", "replace"); self->pyenc ? self->pyenc : "ascii", "replace");
#endif #endif
} }
@ -113,7 +113,7 @@ error_dealloc(errorObject *self)
{ {
PyObject_GC_UnTrack((PyObject *)self); PyObject_GC_UnTrack((PyObject *)self);
error_clear(self); error_clear(self);
PyMem_Free(self->codec); PyMem_Free(self->pyenc);
CLEARPGRES(self->pgres); CLEARPGRES(self->pgres);
Py_TYPE(self)->tp_free((PyObject *)self); Py_TYPE(self)->tp_free((PyObject *)self);

View File

@ -86,7 +86,7 @@ psyco_lobj_write(lobjectObject *self, PyObject *args)
data = obj; data = obj;
} }
else if (PyUnicode_Check(obj)) { else if (PyUnicode_Check(obj)) {
if (!(data = PyUnicode_AsEncodedString(obj, self->conn->codec, NULL))) { if (!(data = PyUnicode_AsEncodedString(obj, self->conn->pyenc, NULL))) {
goto exit; goto exit;
} }
} }
@ -150,7 +150,7 @@ psyco_lobj_read(lobjectObject *self, PyObject *args)
if (self->mode & LOBJECT_BINARY) { if (self->mode & LOBJECT_BINARY) {
res = Bytes_FromStringAndSize(buffer, size); res = Bytes_FromStringAndSize(buffer, size);
} else { } else {
res = PyUnicode_Decode(buffer, size, self->conn->codec, NULL); res = PyUnicode_Decode(buffer, size, self->conn->pyenc, NULL);
} }
PyMem_Free(buffer); PyMem_Free(buffer);

View File

@ -251,9 +251,9 @@ microprotocol_getquoted(PyObject *obj, connectionObject *conn)
/* Convert to bytes. */ /* Convert to bytes. */
if (res && PyUnicode_CheckExact(res)) { if (res && PyUnicode_CheckExact(res)) {
PyObject *b; PyObject *b;
const char *codec; const char *pyenc;
codec = (conn && conn->codec) ? conn->codec : "utf8"; pyenc = (conn && conn->pyenc) ? conn->pyenc : "utf8";
b = PyUnicode_AsEncodedString(res, codec, NULL); b = PyUnicode_AsEncodedString(res, pyenc, NULL);
Py_DECREF(res); Py_DECREF(res);
res = b; res = b;
} }

View File

@ -226,8 +226,8 @@ pq_raise(connectionObject *conn, cursorObject *curs, PGresult **pgres)
if (pyerr && PyObject_TypeCheck(pyerr, &errorType)) { if (pyerr && PyObject_TypeCheck(pyerr, &errorType)) {
errorObject *perr = (errorObject *)pyerr; errorObject *perr = (errorObject *)pyerr;
PyMem_Free(perr->codec); PyMem_Free(perr->pyenc);
psycopg_strdup(&perr->codec, conn->codec, 0); psycopg_strdup(&perr->pyenc, conn->pyenc, 0);
Py_CLEAR(perr->pgerror); Py_CLEAR(perr->pgerror);
perr->pgerror = error_text_from_chars(perr, err); perr->pgerror = error_text_from_chars(perr, err);
@ -1332,8 +1332,8 @@ _pq_copy_in_v3(cursorObject *curs)
/* a file may return unicode if implements io.TextIOBase */ /* a file may return unicode if implements io.TextIOBase */
if (PyUnicode_Check(o)) { if (PyUnicode_Check(o)) {
PyObject *tmp; PyObject *tmp;
Dprintf("_pq_copy_in_v3: encoding in %s", curs->conn->codec); Dprintf("_pq_copy_in_v3: encoding in %s", curs->conn->pyenc);
if (!(tmp = PyUnicode_AsEncodedString(o, curs->conn->codec, NULL))) { if (!(tmp = PyUnicode_AsEncodedString(o, curs->conn->pyenc, NULL))) {
Dprintf("_pq_copy_in_v3: encoding() failed"); Dprintf("_pq_copy_in_v3: encoding() failed");
error = 1; error = 1;
break; break;
@ -1488,7 +1488,7 @@ _pq_copy_out_v3(cursorObject *curs)
if (len > 0 && buffer) { if (len > 0 && buffer) {
if (is_text) { if (is_text) {
obj = PyUnicode_Decode(buffer, len, curs->conn->codec, NULL); obj = PyUnicode_Decode(buffer, len, curs->conn->pyenc, NULL);
} else { } else {
obj = Bytes_FromStringAndSize(buffer, len); obj = Bytes_FromStringAndSize(buffer, len);
} }
@ -1638,7 +1638,7 @@ retry:
Dprintf("pq_read_replication_message: >>%.*s<<", data_size, buffer + hdr); Dprintf("pq_read_replication_message: >>%.*s<<", data_size, buffer + hdr);
if (repl->decode) { if (repl->decode) {
str = PyUnicode_Decode(buffer + hdr, data_size, conn->codec, NULL); str = PyUnicode_Decode(buffer + hdr, data_size, conn->pyenc, NULL);
} else { } else {
str = Bytes_FromStringAndSize(buffer + hdr, data_size); str = Bytes_FromStringAndSize(buffer + hdr, data_size);
} }

View File

@ -672,7 +672,7 @@ typecast_cast(PyObject *obj, const char *str, Py_ssize_t len, PyObject *curs)
s = PyString_FromStringAndSize(str, len); s = PyString_FromStringAndSize(str, len);
#else #else
s = PyUnicode_Decode(str, len, s = PyUnicode_Decode(str, len,
((cursorObject *)curs)->conn->codec, NULL); ((cursorObject *)curs)->conn->pyenc, NULL);
#endif #endif
} }
else { else {

View File

@ -102,7 +102,7 @@ typecast_UNICODE_cast(const char *s, Py_ssize_t len, PyObject *curs)
return conn->cdecoder(s, len, NULL); return conn->cdecoder(s, len, NULL);
} }
else { else {
return PyUnicode_Decode(s, len, conn->codec, NULL); return PyUnicode_Decode(s, len, conn->pyenc, NULL);
} }
} }