From: Daniele Varrazzo Date: Thu, 14 May 2020 19:51:32 +0000 (+1200) Subject: No need to store a generic c encoder X-Git-Tag: 3.0.dev0~522 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a5f9a65aed35b1a978bc8f61d7efb9e4220468b1;p=thirdparty%2Fpsycopg.git No need to store a generic c encoder The only one we care about is utf-8, the only other available is ascii, but in case of ascii we don't decode and return the bytes. --- diff --git a/psycopg3/types/text.pyx b/psycopg3/types/text.pyx index faeb2e86d..27f0ef3af 100644 --- a/psycopg3/types/text.pyx +++ b/psycopg3/types/text.pyx @@ -10,17 +10,16 @@ from cpython.object cimport PyObject from cpython.unicode cimport PyUnicode_DecodeUTF8 from psycopg3.pq cimport libpq -ctypedef unicode (*decodefunc)(char *s, Py_ssize_t size, char *errors) cdef struct TextContext: PyObject *pydecoder - decodefunc cdecoder + int is_utf8 cdef object load_text(const char *data, size_t length, void *context): cdef TextContext *tcontext = context - if tcontext.cdecoder: - return tcontext.cdecoder(data, length, NULL) + if tcontext.is_utf8: + return PyUnicode_DecodeUTF8(data, length, NULL) b = PyBytes_FromStringAndSize(data, length) decoder = (tcontext.pydecoder) @@ -34,10 +33,11 @@ cdef object load_text(const char *data, size_t length, void *context): cdef void *get_context_text(object loader): cdef TextContext *rv = PyMem_Malloc(sizeof(TextContext)) rv.pydecoder = loader.decode - rv.cdecoder = NULL if loader.connection is None or loader.connection.encoding == "UTF8": - rv.cdecoder = PyUnicode_DecodeUTF8 + rv.is_utf8 = 1 + else: + rv.is_utf8 = 0 return rv