Skip to content

Commit

Permalink
Feed memoryview to writelines() (#715)
Browse files Browse the repository at this point in the history
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: #700
  • Loading branch information
fantix committed Mar 9, 2021
1 parent d6eea8e commit 359a34c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions asyncpg/protocol/coreproto.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ cdef class CoreProtocol:
else:
# otherwise, append SYNC and send the buffers
packet.write_bytes(SYNC_MESSAGE)
buffers.append(packet)
buffers.append(memoryview(packet))
self._writelines(buffers)
return False

Expand All @@ -976,7 +976,7 @@ cdef class CoreProtocol:
)

# collected one buffer
buffers.append(packet)
buffers.append(memoryview(packet))

# write to the wire, and signal the caller for more to send
self._writelines(buffers)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,30 @@ async def worker():
await asyncio.gather(*tasks)
await pool.close()

async def test_executemany_uvloop_ssl_issue_700(self):
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ssl_context.load_verify_locations(SSL_CA_CERT_FILE)

con = await self.connect(
host='localhost',
user='ssl_user',
ssl=ssl_context)

try:
await con.execute('CREATE TABLE test_many (v int)')
await con.executemany(
'INSERT INTO test_many VALUES ($1)',
[(x + 1,) for x in range(100)]
)
self.assertEqual(
await con.fetchval('SELECT sum(v) FROM test_many'), 5050
)
finally:
try:
await con.execute('DROP TABLE test_many')
finally:
await con.close()


class TestConnectionGC(tb.ClusterTestCase):

Expand Down

0 comments on commit 359a34c

Please sign in to comment.