def _pq_fetch_copy_out(self): is_text = isinstance(self._copyfile, TextIOBase) pgconn = self._conn._pgconn while True: buf = ffi.new('char **') length = libpq.PQgetCopyData(pgconn, buf, 0) if length > 0: if buf[0] == ffi.NULL: return value = ffi.buffer(buf[0], length) if is_text: value = typecasts.parse_unicode(value[:], length, self) self._copyfile.write(value) elif length == -2: raise self._conn._create_exception(cursor=self) else: break self._clear_pgres() util.pq_clear_async(pgconn)