(
*,
addr,
loop,
params,
config,
connection_class,
record_class
)
| 1056 | |
| 1057 | |
| 1058 | async def _connect_addr( |
| 1059 | *, |
| 1060 | addr, |
| 1061 | loop, |
| 1062 | params, |
| 1063 | config, |
| 1064 | connection_class, |
| 1065 | record_class |
| 1066 | ): |
| 1067 | assert loop is not None |
| 1068 | |
| 1069 | params_input = params |
| 1070 | if callable(params.password): |
| 1071 | password = params.password() |
| 1072 | if inspect.isawaitable(password): |
| 1073 | password = await password |
| 1074 | |
| 1075 | params = params._replace(password=password) |
| 1076 | args = (addr, loop, config, connection_class, record_class, params_input) |
| 1077 | |
| 1078 | # prepare the params (which attempt has ssl) for the 2 attempts |
| 1079 | if isinstance(addr, str): |
| 1080 | return await __connect_addr(params, False, *args) |
| 1081 | elif params.sslmode == SSLMode.allow: |
| 1082 | params_retry = params |
| 1083 | params = params._replace(ssl=None) |
| 1084 | elif params.sslmode == SSLMode.prefer: |
| 1085 | params_retry = params._replace(ssl=None) |
| 1086 | else: |
| 1087 | # skip retry if we don't have to |
| 1088 | return await __connect_addr(params, False, *args) |
| 1089 | |
| 1090 | # first attempt |
| 1091 | try: |
| 1092 | return await __connect_addr(params, True, *args) |
| 1093 | except _RetryConnectSignal as retry_exc: |
| 1094 | first_error = retry_exc.__cause__ |
| 1095 | assert first_error is not None |
| 1096 | |
| 1097 | # second attempt |
| 1098 | try: |
| 1099 | return await __connect_addr(params_retry, False, *args) |
| 1100 | except _NextHostSignal as exc: |
| 1101 | # The server error will be unwrapped by _connect on host exhaustion. |
| 1102 | exc.__cause__.__cause__ = first_error |
| 1103 | raise |
| 1104 | except (Exception, asyncio.CancelledError) as second_error: |
| 1105 | # If the preferred attempt produced a useful authentication error, |
| 1106 | # do not hide it behind a generic rejection from the fallback mode. |
| 1107 | if ( |
| 1108 | isinstance(first_error, exceptions.InvalidPasswordError) |
| 1109 | and not isinstance(second_error, exceptions.InvalidPasswordError) |
| 1110 | and isinstance(second_error, ( |
| 1111 | exceptions.InvalidAuthorizationSpecificationError, |
| 1112 | exceptions.ConnectionDoesNotExistError, |
| 1113 | )) |
| 1114 | ): |
| 1115 | raise first_error from None |
no test coverage detected