Use the proper API functions to look up codec functions

This commit is contained in:
Daniele Varrazzo 2016-12-29 21:13:19 +01:00
parent 3295beb777
commit cb5293be1f
2 changed files with 8 additions and 11 deletions

View File

@ -122,9 +122,11 @@ struct connectionObject {
PyObject *cursor_factory; /* default cursor factory from cursor() */ PyObject *cursor_factory; /* default cursor factory from cursor() */
/* Pointer to a decoding function, e.g. PyUnicode_DecodeUTF8 */ /* Optional pointer to a decoding C function, e.g. PyUnicode_DecodeUTF8 */
PyObject *(*cdecoder)(const char *, Py_ssize_t, const char *); PyObject *(*cdecoder)(const char *, Py_ssize_t, const char *);
/* Pointers to python encoding/decoding functions, e.g.
* codecs.getdecoder('utf8') */
PyObject *pyencoder; /* python codec encoding function */ PyObject *pyencoder; /* python codec encoding function */
PyObject *pydecoder; /* python codec decoding function */ PyObject *pydecoder; /* python codec decoding function */
}; };

View File

@ -451,17 +451,15 @@ conn_get_python_codec(const char *encoding,
int rv = -1; int rv = -1;
char *pgenc = NULL; char *pgenc = NULL;
PyObject *encname = NULL; PyObject *encname = NULL;
PyObject *m = NULL, *f = NULL, *codec = NULL;
PyObject *enc_tmp = NULL, *dec_tmp = NULL; PyObject *enc_tmp = NULL, *dec_tmp = NULL;
/* get the Python name of the encoding as a C string */
if (!(encname = conn_pgenc_to_pyenc(encoding, &pgenc))) { goto exit; } if (!(encname = conn_pgenc_to_pyenc(encoding, &pgenc))) { goto exit; }
if (!(encname = psycopg_ensure_bytes(encname))) { goto exit; }
/* Look up the python codec */ /* Look up the codec functions */
if (!(m = PyImport_ImportModule("codecs"))) { goto exit; } if (!(enc_tmp = PyCodec_Encoder(Bytes_AS_STRING(encname)))) { goto exit; }
if (!(f = PyObject_GetAttrString(m, "lookup"))) { goto exit; } if (!(dec_tmp = PyCodec_Decoder(Bytes_AS_STRING(encname)))) { goto exit; }
if (!(codec = PyObject_CallFunctionObjArgs(f, encname, NULL))) { goto exit; }
if (!(enc_tmp = PyObject_GetAttrString(codec, "encode"))) { goto exit; }
if (!(dec_tmp = PyObject_GetAttrString(codec, "decode"))) { goto exit; }
/* success */ /* success */
*pyenc = enc_tmp; enc_tmp = NULL; *pyenc = enc_tmp; enc_tmp = NULL;
@ -472,9 +470,6 @@ conn_get_python_codec(const char *encoding,
exit: exit:
Py_XDECREF(enc_tmp); Py_XDECREF(enc_tmp);
Py_XDECREF(dec_tmp); Py_XDECREF(dec_tmp);
Py_XDECREF(codec);
Py_XDECREF(f);
Py_XDECREF(m);
Py_XDECREF(encname); Py_XDECREF(encname);
PyMem_Free(pgenc); PyMem_Free(pgenc);