From: Daniele Varrazzo Date: Wed, 14 Sep 2022 20:57:05 +0000 (+0100) Subject: test: add test to verify the behavior of adapters failing init X-Git-Tag: 3.1.2~5^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ec908dd73aa01fe341dcf1ef007cfa26a524cbad;p=thirdparty%2Fpsycopg.git test: add test to verify the behavior of adapters failing init An exception in loader init doesn't get raised and causes a segfault. This reproduces the issue in #372. --- diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 43424cad6..ceb6f828a 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -440,6 +440,37 @@ def test_optimised_adapters(): assert not c_adapters +def test_dumper_init_error(conn): + class BadDumper(Dumper): + def __init__(self, cls, context): + super().__init__(cls, context) + 1 / 0 + + def dump(self, obj): + return obj.encode() + + cur = conn.cursor() + cur.adapters.register_dumper(str, BadDumper) + with pytest.raises(ZeroDivisionError): + cur.execute("select %s::text", ["hi"]) + + +def test_loader_init_error(conn): + class BadLoader(Loader): + def __init__(self, oid, context): + super().__init__(oid, context) + 1 / 0 + + def load(self, data): + return data.decode() + + cur = conn.cursor() + cur.adapters.register_loader("text", BadLoader) + with pytest.raises(ZeroDivisionError): + cur.execute("select 'hi'::text") + assert cur.fetchone() == ("hi",) + + @pytest.mark.slow @pytest.mark.parametrize("fmt", PyFormat) @pytest.mark.parametrize("fmt_out", [pq.Format.TEXT, pq.Format.BINARY])