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
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
:ref:`unicode-handling`.

View File

@ -355,7 +355,7 @@ Unicode handling
Psycopg can exchange Unicode data with a PostgreSQL database. Python
`!unicode` objects are automatically *encoded* in the client encoding
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)::
>>> 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
to do and we raise an exception */
if (self->conn) {
return self->conn->codec;
return self->conn->pyenc;
}
else {
return self->encoding ? self->encoding : default_encoding;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -86,7 +86,7 @@ psyco_lobj_write(lobjectObject *self, PyObject *args)
data = 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;
}
}
@ -150,7 +150,7 @@ psyco_lobj_read(lobjectObject *self, PyObject *args)
if (self->mode & LOBJECT_BINARY) {
res = Bytes_FromStringAndSize(buffer, size);
} else {
res = PyUnicode_Decode(buffer, size, self->conn->codec, NULL);
res = PyUnicode_Decode(buffer, size, self->conn->pyenc, NULL);
}
PyMem_Free(buffer);

View File

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

View File

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