Skip to content

Commit bedff23

Browse files
committed
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
1 parent 9b2b027 commit bedff23

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

asyncpg/protocol/protocol.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,10 @@ cdef class BaseProtocol(CoreProtocol):
801801
waiter.set_result(self.result)
802802

803803
cdef _on_result__simple_query(self, object waiter):
804-
waiter.set_result(self.result_status_msg.decode(self.encoding))
804+
if self.result_status_msg is not None:
805+
waiter.set_result(self.result_status_msg.decode(self.encoding))
806+
else:
807+
waiter.set_result(None)
805808

806809
cdef _on_result__copy_out(self, object waiter):
807810
cdef bint copy_done = self.state == PROTOCOL_COPY_OUT_DONE

tests/test_execute.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ async def test_execute_script_check_transactionality(self):
6363
SELECT * FROM mytab
6464
''')
6565

66+
async def test_execute_empty_script(self):
67+
# Test executing a script with only comments (issue #1259)
68+
result = await self.con.execute('-- hello')
69+
self.assertIsNone(result)
70+
71+
# Test executing a script with only whitespace
72+
result = await self.con.execute(' ')
73+
self.assertIsNone(result)
74+
75+
# Test executing an empty script
76+
result = await self.con.execute('')
77+
self.assertIsNone(result)
78+
6679
async def test_execute_exceptions_1(self):
6780
with self.assertRaisesRegex(asyncpg.PostgresError,
6881
'relation "__dne__" does not exist'):

0 commit comments

Comments
 (0)