Skip to content

Commit 1f6ae4d

Browse files
committed
Handle exceptions in generators passed to Connection.executemany()
Fixes: #85
1 parent 71de129 commit 1f6ae4d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

asyncpg/protocol/coreproto.pyx

+8
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ cdef class CoreProtocol:
235235
buf = <WriteBuffer>next(self._execute_iter)
236236
except StopIteration:
237237
self._push_result()
238+
except BaseException as e:
239+
self.result_type = RESULT_FAILED
240+
self.result = e
241+
self._push_result()
238242
else:
239243
# Next iteration over the executemany() arg sequence
240244
self._send_bind_message(
@@ -643,6 +647,10 @@ cdef class CoreProtocol:
643647
buf = <WriteBuffer>next(bind_data)
644648
except StopIteration:
645649
self._push_result()
650+
except BaseException as e:
651+
self.result_type = RESULT_FAILED
652+
self.result = e
653+
self._push_result()
646654
else:
647655
self._send_bind_message(portal_name, stmt_name, buf, 0)
648656

tests/test_execute.py

+20
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,23 @@ async def test_execute_many_1(self):
132132
])
133133
finally:
134134
await self.con.execute('DROP TABLE exmany')
135+
136+
async def test_execute_many_2(self):
137+
await self.con.execute('CREATE TEMP TABLE exmany (b int)')
138+
139+
try:
140+
bad_data = ([1 / 0] for v in range(10))
141+
142+
with self.assertRaises(ZeroDivisionError):
143+
async with self.con.transaction():
144+
await self.con.executemany('''
145+
INSERT INTO exmany VALUES($1)
146+
''', bad_data)
147+
148+
good_data = ([v] for v in range(10))
149+
async with self.con.transaction():
150+
await self.con.executemany('''
151+
INSERT INTO exmany VALUES($1)
152+
''', good_data)
153+
finally:
154+
await self.con.execute('DROP TABLE exmany')

0 commit comments

Comments
 (0)