psycopg.git / summary / log / commit / refs
commit 052d799757aee9d2466c0feeb6691124d5bac33b
Author: Daniele Varrazzo <daniele.varrazzo@gmail.com>
Date: Sat Mar 28 14:45:09 2020 +0000
Adapters moved to the types package
psycopg3/__init__.py | 5 +++++
psycopg3/adaptation.py | 50 ++---------------------------------------------
psycopg3/types/numeric.py | 20 +++++++++++++++++++
psycopg3/types/text.py | 45 ++++++++++++++++++++++++++++++++++++++++++
4 files changed, 72 insertions(+), 48 deletions(-)
diff --git a/psycopg3/__init__.py b/psycopg3/__init__.py
index 58d0bcf0..79573f3c 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 9ed76b93..d717381a 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 00000000..ecf179ff
--- /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 00000000..37515fb2
--- /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
[parent: fe132322bc15]