from ..pq.proto import Escaping as EscapingProto
-class _StringDumper(Dumper):
+class _StrDumper(Dumper):
_encoding = "utf-8"
self._encoding = enc
-class StringBinaryDumper(_StringDumper):
+class StrBinaryDumper(_StrDumper):
format = Format.BINARY
_oid = builtins["text"].oid
return obj.encode(self._encoding)
-class StringDumper(_StringDumper):
+class StrDumper(_StrDumper):
format = Format.TEXT
# The last one registered becomes the default for each type.
# Normally, binary is the default dumper, except for text (which plays
# the role of unknown, so it can be cast automatically to other types).
- StringBinaryDumper.register(str, ctx)
- StringDumper.register(str, ctx)
+ StrBinaryDumper.register(str, ctx)
+ StrDumper.register(str, ctx)
TextLoader.register(INVALID_OID, ctx)
TextLoader.register("bpchar", ctx)
TextLoader.register("name", ctx)
const char *PyUnicode_AsUTF8AndSize(unicode obj, Py_ssize_t *size) except NULL
-cdef class _StringDumper(CDumper):
+cdef class _StrDumper(CDumper):
cdef int is_utf8
cdef char *encoding
cdef bytes _bytes_encoding # needed to keep `encoding` alive
@cython.final
-cdef class StringBinaryDumper(_StringDumper):
+cdef class StrBinaryDumper(_StrDumper):
format = PQ_BINARY
@cython.final
-cdef class StringDumper(_StringDumper):
+cdef class StrDumper(_StrDumper):
format = PQ_TEXT
cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
- cdef Py_ssize_t size = StringBinaryDumper.cdump(self, obj, rv, offset)
+ cdef Py_ssize_t size = StrBinaryDumper.cdump(self, obj, rv, offset)
# Like the binary dump, but check for 0, or the string will be truncated
cdef const char *buf = PyByteArray_AS_STRING(rv)
def test_subclass_dumper(conn):
# This might be a C fast object: make sure that the Python code is called
- from psycopg3.types.string import StringDumper
+ from psycopg3.types.string import StrDumper
- class MyStringDumper(StringDumper):
+ class MyStrDumper(StrDumper):
def dump(self, obj):
return (obj * 2).encode("utf-8")
- MyStringDumper.register(str, conn)
+ MyStrDumper.register(str, conn)
assert conn.execute("select %t", ["hello"]).fetchone()[0] == "hellohello"
@pytest.mark.parametrize("format", [Format.TEXT, Format.BINARY])
def test_subclass_adapter(conn, format):
if format == Format.TEXT:
- from psycopg3.types.string import StringDumper as BaseDumper
+ from psycopg3.types.string import StrDumper as BaseDumper
else:
- from psycopg3.types.string import StringBinaryDumper as BaseDumper
+ from psycopg3.types.string import StrBinaryDumper as BaseDumper
- class MyStringDumper(BaseDumper):
+ class MyStrDumper(BaseDumper):
def dump(self, obj):
return super().dump(obj) * 2
- MyStringDumper.register(str, conn)
+ MyStrDumper.register(str, conn)
cur = conn.cursor()
ensure_table(cur, sample_tabledef)