Skip to content

Commit 991b1ae

Browse files
committed
Fix handling of NULLs in copy_records_to_table().
Fixes: #153.
1 parent 7ff5bff commit 991b1ae

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

asyncpg/protocol/protocol.pyx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,12 @@ cdef class BaseProtocol(CoreProtocol):
385385
wbuf.write_int16(<int16_t>num_cols)
386386
# Tuple data
387387
for i in range(num_cols):
388-
codec = <Codec>cpython.PyTuple_GET_ITEM(codecs, i)
389-
codec.encode(settings, wbuf, row[i])
388+
item = row[i]
389+
if item is None:
390+
wbuf.write_int32(-1)
391+
else:
392+
codec = <Codec>cpython.PyTuple_GET_ITEM(codecs, i)
393+
codec.encode(settings, wbuf, item)
390394

391395
if wbuf.len() >= _COPY_BUFFER_SIZE:
392396
with timer:

tests/test_copy.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,12 @@ async def test_copy_records_to_table(self):
588588
for i in range(100)
589589
]
590590

591+
records.append(('a-100', None, None))
592+
591593
res = await self.con.copy_records_to_table(
592594
'copytab', records=records)
593595

594-
self.assertEqual(res, 'COPY 100')
596+
self.assertEqual(res, 'COPY 101')
595597

596598
finally:
597599
await self.con.execute('DROP TABLE copytab')

0 commit comments

Comments
 (0)