From: Daniele Varrazzo Date: Sat, 28 Mar 2020 14:45:09 +0000 (+1300) Subject: Adapters moved to the types package X-Git-Tag: 3.0.dev0~658 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=052d799757aee9d2466c0feeb6691124d5bac33b;p=thirdparty%2Fpsycopg.git Adapters moved to the types package --- diff --git a/psycopg3/__init__.py b/psycopg3/__init__.py index 58d0bcf08..79573f3c5 100644 --- a/psycopg3/__init__.py +++ b/psycopg3/__init__.py @@ -20,6 +20,11 @@ from .exceptions import ( NotSupportedError, ) +# register default adapters +from .types import numeric # noqa +from .types import text # noqa + + # DBAPI compliancy connect = Connection.connect apilevel = "2.0" diff --git a/psycopg3/adaptation.py b/psycopg3/adaptation.py index 9ed76b933..d717381a0 100644 --- a/psycopg3/adaptation.py +++ b/psycopg3/adaptation.py @@ -14,10 +14,6 @@ from .connection import BaseConnection INVALID_OID = 0 -ascii_encode = codecs.lookup("ascii").encode -ascii_decode = codecs.lookup("ascii").decode -utf8_codec = codecs.lookup("utf-8") - global_adapters = {} global_casters = {} @@ -220,7 +216,7 @@ class Transformer: return global_adapters[key] raise exc.ProgrammingError( - f"cannot adapt type {cls.__name__} to format {fmt}" + f"cannot adapt type {cls.__name__} to format {Format(fmt).name}" ) def cast_row(self, result, n): @@ -279,48 +275,6 @@ class Typecaster: raise NotImplementedError() -@adapter(str) -@binary_adapter(str) -class StringAdapter(Adapter): - def __init__(self, cls, conn): - super().__init__(cls, conn) - self.encode = (conn.codec if conn is not None else utf8_codec).encode - - def adapt(self, obj): - return self.encode(obj)[0] - - -@caster(type_oid["text"]) -@binary_caster(type_oid["text"]) -class StringCaster(Typecaster): - def __init__(self, oid, conn): - super().__init__(oid, conn) - if conn is not None: - if conn.pgenc != b"SQL_ASCII": - self.decode = conn.codec.decode - else: - self.decode = None - else: - self.decode = utf8_codec.decode - - def cast(self, data): - if self.decode is not None: - return self.decode(data)[0] - else: - # return bytes for SQL_ASCII db - return data - - -@adapter(int) -def adapt_int(obj): - return ascii_encode(str(obj))[0], type_oid["numeric"] - - -@caster(type_oid["numeric"]) -def cast_int(data): - return int(ascii_decode(data)[0]) - - @caster(INVALID_OID) class UnknownCaster(Typecaster): """ @@ -332,7 +286,7 @@ class UnknownCaster(Typecaster): if conn is not None: self.decode = conn.codec.decode else: - self.decode = utf8_codec.decode + self.decode = codecs.lookup("utf8").decode def cast(self, data): return self.decode(data)[0] diff --git a/psycopg3/types/numeric.py b/psycopg3/types/numeric.py new file mode 100644 index 000000000..ecf179ff4 --- /dev/null +++ b/psycopg3/types/numeric.py @@ -0,0 +1,20 @@ +""" +Adapters of numeric types. +""" + +# Copyright (C) 2020 The Psycopg Team + +import codecs + +from ..adaptation import adapter, caster +from .oids import type_oid + + +@adapter(int) +def adapt_int(obj, encode=codecs.lookup("ascii").encode): + return encode(str(obj))[0], type_oid["numeric"] + + +@caster(type_oid["numeric"]) +def cast_int(data, decode=codecs.lookup("ascii").decode): + return int(decode(data)[0]) diff --git a/psycopg3/types/text.py b/psycopg3/types/text.py new file mode 100644 index 000000000..37515fb2d --- /dev/null +++ b/psycopg3/types/text.py @@ -0,0 +1,45 @@ +""" +Adapters of textual types. +""" + +# Copyright (C) 2020 The Psycopg Team + +import codecs + +from ..adaptation import Adapter, adapter, binary_adapter +from ..adaptation import Typecaster, caster, binary_caster +from .oids import type_oid + + +@adapter(str) +@binary_adapter(str) +class StringAdapter(Adapter): + def __init__(self, cls, conn): + super().__init__(cls, conn) + self.encode = ( + conn.codec if conn is not None else codecs.lookup("utf8") + ).encode + + def adapt(self, obj): + return self.encode(obj)[0] + + +@caster(type_oid["text"]) +@binary_caster(type_oid["text"]) +class StringCaster(Typecaster): + def __init__(self, oid, conn): + super().__init__(oid, conn) + if conn is not None: + if conn.pgenc != b"SQL_ASCII": + self.decode = conn.codec.decode + else: + self.decode = None + else: + self.decode = codecs.lookup("utf8").decode + + def cast(self, data): + if self.decode is not None: + return self.decode(data)[0] + else: + # return bytes for SQL_ASCII db + return data