From bedff239b491a433dd58b1e4684fd60b04af7618 Mon Sep 17 00:00:00 2001 From: Varun Chawla Date: Sat, 7 Feb 2026 23:07:26 -0800 Subject: [PATCH] Fix AttributeError when executing empty or comment-only scripts When executing a script that contains only comments or is empty, PostgreSQL returns no result status message. This caused an AttributeError when trying to decode None. Fixes #1259 --- asyncpg/protocol/protocol.pyx | 5 ++++- tests/test_execute.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/asyncpg/protocol/protocol.pyx b/asyncpg/protocol/protocol.pyx index acce4e9f..11097745 100644 --- a/asyncpg/protocol/protocol.pyx +++ b/asyncpg/protocol/protocol.pyx @@ -801,7 +801,10 @@ cdef class BaseProtocol(CoreProtocol): waiter.set_result(self.result) cdef _on_result__simple_query(self, object waiter): - waiter.set_result(self.result_status_msg.decode(self.encoding)) + if self.result_status_msg is not None: + waiter.set_result(self.result_status_msg.decode(self.encoding)) + else: + waiter.set_result(None) cdef _on_result__copy_out(self, object waiter): cdef bint copy_done = self.state == PROTOCOL_COPY_OUT_DONE diff --git a/tests/test_execute.py b/tests/test_execute.py index f8a0e43a..3c0a8b7b 100644 --- a/tests/test_execute.py +++ b/tests/test_execute.py @@ -63,6 +63,19 @@ async def test_execute_script_check_transactionality(self): SELECT * FROM mytab ''') + async def test_execute_empty_script(self): + # Test executing a script with only comments (issue #1259) + result = await self.con.execute('-- hello') + self.assertIsNone(result) + + # Test executing a script with only whitespace + result = await self.con.execute(' ') + self.assertIsNone(result) + + # Test executing an empty script + result = await self.con.execute('') + self.assertIsNone(result) + async def test_execute_exceptions_1(self): with self.assertRaisesRegex(asyncpg.PostgresError, 'relation "__dne__" does not exist'):