]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Fixed raising unicode encode errors in C
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 12 Jan 2021 01:00:59 +0000 (02:00 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 12 Jan 2021 15:15:02 +0000 (16:15 +0100)
psycopg3_c/psycopg3_c/types/text.pyx
tests/types/test_text.py

index 8f85fa536a8f12247914eba1b4aa6f85618a7246..c790f8f62d03a126c79575ded1f0b6c2d169383b 100644 (file)
@@ -22,7 +22,7 @@ from psycopg3 import errors as e
 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):
@@ -57,10 +57,6 @@ 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)
index 095d0d24ced74aa3e8eec3e512c6e6dabcc3a265..3b6e69ba08b1be839f629c2e781ff51f8fa32da9 100644 (file)
@@ -96,6 +96,16 @@ def test_dump_badenc(conn, fmt_in):
         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"])