If provided session is None, lend a temporary session.
(
self, session: Optional[client_session.ClientSession]
)
| 2290 | |
| 2291 | @contextlib.contextmanager |
| 2292 | def _tmp_session( |
| 2293 | self, session: Optional[client_session.ClientSession] |
| 2294 | ) -> Generator[Optional[client_session.ClientSession], None]: |
| 2295 | """If provided session is None, lend a temporary session.""" |
| 2296 | if session is not None and not isinstance(session, client_session.ClientSession): |
| 2297 | raise ValueError( |
| 2298 | f"'session' argument must be a ClientSession or None, not {type(session)}" |
| 2299 | ) |
| 2300 | |
| 2301 | # Check for a bound session. If one exists, treat it as an explicitly passed session. |
| 2302 | session = session or self._get_bound_session() |
| 2303 | if session: |
| 2304 | # Don't call end_session. |
| 2305 | yield session |
| 2306 | return |
| 2307 | |
| 2308 | s = self._ensure_session(session) |
| 2309 | if s: |
| 2310 | try: |
| 2311 | yield s |
| 2312 | except Exception as exc: |
| 2313 | if isinstance(exc, ConnectionFailure): |
| 2314 | s._server_session.mark_dirty() |
| 2315 | |
| 2316 | # Always call end_session on error. |
| 2317 | s.end_session() |
| 2318 | raise |
| 2319 | finally: |
| 2320 | # Call end_session when we exit this scope. |
| 2321 | if not s._attached_to_cursor: |
| 2322 | s.end_session() |
| 2323 | else: |
| 2324 | yield None |
| 2325 | |
| 2326 | def _process_response(self, reply: Mapping[str, Any], session: Optional[ClientSession]) -> None: |
| 2327 | self._topology.receive_cluster_time(reply.get("$clusterTime")) |
no test coverage detected