| 652 | self._connect_kwargs = connect_kwargs |
| 653 | |
| 654 | async def _get_new_connection(self): |
| 655 | con = await self._connect( |
| 656 | *self._connect_args, |
| 657 | loop=self._loop, |
| 658 | connection_class=self._connection_class, |
| 659 | record_class=self._record_class, |
| 660 | **self._connect_kwargs, |
| 661 | ) |
| 662 | if not isinstance(con, self._connection_class): |
| 663 | good = self._connection_class |
| 664 | good_n = f'{good.__module__}.{good.__name__}' |
| 665 | bad = type(con) |
| 666 | if bad.__module__ == "builtins": |
| 667 | bad_n = bad.__name__ |
| 668 | else: |
| 669 | bad_n = f'{bad.__module__}.{bad.__name__}' |
| 670 | raise exceptions.InterfaceError( |
| 671 | "expected pool connect callback to return an instance of " |
| 672 | f"'{good_n}', got " f"'{bad_n}'" |
| 673 | ) |
| 674 | |
| 675 | if self._init is not None: |
| 676 | try: |
| 677 | await self._init(con) |
| 678 | except (Exception, asyncio.CancelledError) as ex: |
| 679 | # If a user-defined `init` function fails, we don't |
| 680 | # know if the connection is safe for re-use, hence |
| 681 | # we close it. A new connection will be created |
| 682 | # when `acquire` is called again. |
| 683 | try: |
| 684 | # Use `close()` to close the connection gracefully. |
| 685 | # An exception in `init` isn't necessarily caused |
| 686 | # by an IO or a protocol error. close() will |
| 687 | # do the necessary cleanup via _release_on_close(). |
| 688 | await con.close() |
| 689 | finally: |
| 690 | raise ex |
| 691 | |
| 692 | return con |
| 693 | |
| 694 | async def execute( |
| 695 | self, |