Skip to content

Commit bf91523

Browse files
committed
Feed memoryview to writelines()
This fixes an issue in 0.22.0 where we passed WriteBuffer to writelines by mistake, which leads to an error under SSL and uvloop - the implementation that calls len() on each line of writelines(). Fixes: MagicStack#700
1 parent d6eea8e commit bf91523

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

asyncpg/protocol/coreproto.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ cdef class CoreProtocol:
952952
else:
953953
# otherwise, append SYNC and send the buffers
954954
packet.write_bytes(SYNC_MESSAGE)
955-
buffers.append(packet)
955+
buffers.append(memoryview(packet))
956956
self._writelines(buffers)
957957
return False
958958

@@ -976,7 +976,7 @@ cdef class CoreProtocol:
976976
)
977977

978978
# collected one buffer
979-
buffers.append(packet)
979+
buffers.append(memoryview(packet))
980980

981981
# write to the wire, and signal the caller for more to send
982982
self._writelines(buffers)

tests/test_connect.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,30 @@ async def worker():
12471247
await asyncio.gather(*tasks)
12481248
await pool.close()
12491249

1250+
async def test_executemany_uvloop_ssl_issue_700(self):
1251+
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
1252+
ssl_context.load_verify_locations(SSL_CA_CERT_FILE)
1253+
1254+
con = await self.connect(
1255+
host='localhost',
1256+
user='ssl_user',
1257+
ssl=ssl_context)
1258+
1259+
try:
1260+
await con.execute('CREATE TABLE test_many (v int)')
1261+
await con.executemany(
1262+
'INSERT INTO test_many VALUES ($1)',
1263+
[(x + 1,) for x in range(100)]
1264+
)
1265+
self.assertEqual(
1266+
await con.fetchval('SELECT sum(v) FROM test_many'), 5050
1267+
)
1268+
finally:
1269+
try:
1270+
await con.execute('DROP TABLE test_many')
1271+
finally:
1272+
await con.close()
1273+
12501274

12511275
class TestConnectionGC(tb.ClusterTestCase):
12521276

0 commit comments

Comments
 (0)