From f713dc9fc1c2a915b306c944e51e800d5ab7e1ef Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 18 Jan 2019 13:17:02 +0000 Subject: [PATCH] Preliminary test for a BYTES adapter. Allow returning unparsed bytes from databases with mixed encodings. See issue #519. --- lib/extensions.py | 2 +- psycopg/typecast_basic.c | 22 +++++++++++++++------- psycopg/typecast_builtins.c | 4 ++-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/extensions.py b/lib/extensions.py index 3e23906e..bb907020 100644 --- a/lib/extensions.py +++ b/lib/extensions.py @@ -35,7 +35,7 @@ This module holds all the extensions to the DBAPI-2.0 provided by psycopg. import re as _re from psycopg2._psycopg import ( # noqa - BINARYARRAY, BOOLEAN, BOOLEANARRAY, DATE, DATEARRAY, DATETIMEARRAY, + BINARYARRAY, BOOLEAN, BOOLEANARRAY, BYTES, DATE, DATEARRAY, DATETIMEARRAY, DECIMAL, DECIMALARRAY, FLOAT, FLOATARRAY, INTEGER, INTEGERARRAY, INTERVAL, INTERVALARRAY, LONGINTEGER, LONGINTEGERARRAY, ROWIDARRAY, STRINGARRAY, TIME, TIMEARRAY, UNICODE, UNICODEARRAY, diff --git a/psycopg/typecast_basic.c b/psycopg/typecast_basic.c index db6c5a93..ff4cf7b8 100644 --- a/psycopg/typecast_basic.c +++ b/psycopg/typecast_basic.c @@ -75,18 +75,16 @@ typecast_FLOAT_cast(const char *s, Py_ssize_t len, PyObject *curs) return flo; } -/** STRING - cast strings of any type to python string **/ -#if PY_MAJOR_VERSION < 3 +/** BYTES - cast strings of any type to python bytes **/ + static PyObject * -typecast_STRING_cast(const char *s, Py_ssize_t len, PyObject *curs) +typecast_BYTES_cast(const char *s, Py_ssize_t len, PyObject *curs) { if (s == NULL) { Py_RETURN_NONE; } - return PyString_FromStringAndSize(s, len); + return Bytes_FromStringAndSize(s, len); } -#else -#define typecast_STRING_cast typecast_UNICODE_cast -#endif + /** UNICODE - cast strings of any type to a python unicode object **/ @@ -101,6 +99,16 @@ typecast_UNICODE_cast(const char *s, Py_ssize_t len, PyObject *curs) return conn_decode(conn, s, len); } + +/** STRING - cast strings of any type to python string **/ + +#if PY_MAJOR_VERSION < 3 +#define typecast_STRING_cast typecast_BYTES_cast +#else +#define typecast_STRING_cast typecast_UNICODE_cast +#endif + + /** BOOLEAN - cast boolean value into right python object **/ static PyObject * diff --git a/psycopg/typecast_builtins.c b/psycopg/typecast_builtins.c index 446dd14b..11e2605c 100644 --- a/psycopg/typecast_builtins.c +++ b/psycopg/typecast_builtins.c @@ -3,7 +3,6 @@ static long int typecast_LONGINTEGER_types[] = {20, 0}; static long int typecast_INTEGER_types[] = {23, 21, 0}; static long int typecast_FLOAT_types[] = {701, 700, 0}; static long int typecast_DECIMAL_types[] = {1700, 0}; -static long int typecast_UNICODE_types[] = {19, 18, 25, 1042, 1043, 0}; static long int typecast_STRING_types[] = {19, 18, 25, 1042, 1043, 0}; static long int typecast_BOOLEAN_types[] = {16, 0}; static long int typecast_DATETIME_types[] = {1114, 0}; @@ -39,8 +38,9 @@ static typecastObject_initlist typecast_builtins[] = { {"INTEGER", typecast_INTEGER_types, typecast_INTEGER_cast, NULL}, {"FLOAT", typecast_FLOAT_types, typecast_FLOAT_cast, NULL}, {"DECIMAL", typecast_DECIMAL_types, typecast_DECIMAL_cast, NULL}, - {"UNICODE", typecast_UNICODE_types, typecast_UNICODE_cast, NULL}, + {"UNICODE", typecast_STRING_types, typecast_UNICODE_cast, NULL}, {"STRING", typecast_STRING_types, typecast_STRING_cast, NULL}, + {"BYTES", typecast_STRING_types, typecast_BYTES_cast, NULL}, {"BOOLEAN", typecast_BOOLEAN_types, typecast_BOOLEAN_cast, NULL}, {"DATETIME", typecast_DATETIME_types, typecast_DATETIME_cast, NULL}, {"DATETIMETZ", typecast_DATETIMETZ_types, typecast_DATETIMETZ_cast, NULL},