]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Adapters moved to the types package
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 28 Mar 2020 14:45:09 +0000 (03:45 +1300)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 28 Mar 2020 14:45:09 +0000 (03:45 +1300)
psycopg3/__init__.py
psycopg3/adaptation.py
psycopg3/types/numeric.py [new file with mode: 0644]
psycopg3/types/text.py [new file with mode: 0644]

index 58d0bcf08b2ca195ecb3cd11705c36716117d689..79573f3c575f713578b74f8c0331e7d836022b96 100644 (file)
@@ -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"
index 9ed76b93308c75c831545286fbbf5fbc181cf7de..d717381a09be4d2b5865bddf2ea34530f3fc3000 100644 (file)
@@ -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 (file)
index 0000000..ecf179f
--- /dev/null
@@ -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 (file)
index 0000000..37515fb
--- /dev/null
@@ -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