| 1231 | f.close() |
| 1232 | |
| 1233 | async def _copy_in(self, copy_stmt, source, timeout): |
| 1234 | try: |
| 1235 | path = os.fspath(source) |
| 1236 | except TypeError: |
| 1237 | # source is not a path-like object |
| 1238 | path = None |
| 1239 | |
| 1240 | f = None |
| 1241 | reader = None |
| 1242 | data = None |
| 1243 | opened_by_us = False |
| 1244 | run_in_executor = self._loop.run_in_executor |
| 1245 | |
| 1246 | if path is not None: |
| 1247 | # a path |
| 1248 | f = await run_in_executor(None, open, path, 'rb') |
| 1249 | opened_by_us = True |
| 1250 | elif hasattr(source, 'read'): |
| 1251 | # file-like |
| 1252 | f = source |
| 1253 | elif isinstance(source, collections.abc.AsyncIterable): |
| 1254 | # assuming calling output returns an awaitable. |
| 1255 | # copy_in() is designed to handle very large amounts of data, and |
| 1256 | # the source async iterable is allowed to return an arbitrary |
| 1257 | # amount of data on every iteration. |
| 1258 | reader = source |
| 1259 | else: |
| 1260 | # assuming source is an instance supporting the buffer protocol. |
| 1261 | data = source |
| 1262 | |
| 1263 | if f is not None: |
| 1264 | # Copying from a file-like object. |
| 1265 | class _Reader: |
| 1266 | def __aiter__(self): |
| 1267 | return self |
| 1268 | |
| 1269 | async def __anext__(self): |
| 1270 | data = await run_in_executor(None, f.read, 524288) |
| 1271 | if len(data) == 0: |
| 1272 | raise StopAsyncIteration |
| 1273 | else: |
| 1274 | return data |
| 1275 | |
| 1276 | reader = _Reader() |
| 1277 | |
| 1278 | try: |
| 1279 | return await self._protocol.copy_in( |
| 1280 | copy_stmt, reader, data, None, None, timeout) |
| 1281 | finally: |
| 1282 | if opened_by_us: |
| 1283 | await run_in_executor(None, f.close) |
| 1284 | |
| 1285 | async def set_type_codec(self, typename, *, |
| 1286 | schema='public', encoder, decoder, |