From ec908dd73aa01fe341dcf1ef007cfa26a524cbad Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 14 Sep 2022 21:57:05 +0100 Subject: [PATCH] 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. --- tests/test_adapt.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) 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]) -- 2.47.3