from psycopg3.encodings import pg2py
cdef extern from "Python.h":
- const char *PyUnicode_AsUTF8AndSize(unicode obj, Py_ssize_t *size)
+ const char *PyUnicode_AsUTF8AndSize(unicode obj, Py_ssize_t *size) except NULL
cdef class _StringDumper(CDumper):
# Probably the fastest path, but doesn't work with subclasses
if PyUnicode_CheckExact(obj):
src = PyUnicode_AsUTF8AndSize(obj, &size)
- if src == NULL:
- # re-encode using a function raising an exception.
- # TODO: is there a better way?
- PyUnicode_AsUTF8String(obj)
else:
b = PyUnicode_AsUTF8String(obj)
PyBytes_AsStringAndSize(b, <char **>&src, &size)
cur.execute(f"select {ph}::bytea", (eur,))
+@pytest.mark.parametrize("fmt_in", [Format.TEXT, Format.BINARY])
+def test_dump_utf8_badenc(conn, fmt_in):
+ cur = conn.cursor()
+ ph = "%s" if fmt_in == Format.TEXT else "%b"
+
+ conn.client_encoding = "utf-8"
+ with pytest.raises(UnicodeEncodeError):
+ cur.execute(f"select {ph}", ("\uddf8",))
+
+
@pytest.mark.parametrize("fmt_out", [Format.TEXT, Format.BINARY])
@pytest.mark.parametrize("encoding", ["utf8", "latin9"])
@pytest.mark.parametrize("typename", ["text", "varchar", "name", "bpchar"])