From a5f9a65aed35b1a978bc8f61d7efb9e4220468b1 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 15 May 2020 07:51:32 +1200 Subject: [PATCH] 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. --- psycopg3/types/text.pyx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 -- 2.47.3