diff --git a/README.md b/README.md index 3c7a6ca9..8dd2f306 100644 --- a/README.md +++ b/README.md @@ -48,12 +48,17 @@ outcome = client.wait_for_review( organization_id, requested.review_id, head_commit_sha=head_commit_sha, + on_poll=lambda review: print(f"Review status: {review.status}"), ) if outcome.status != "COMPLETED": print(outcome.status, outcome.reason) ``` -`head_commit_sha` is a full SHA of 40 or 64 hexadecimal characters; any other value raises `ValueError` before a request. A review pinned to another head ends the wait on the read that names it. Its outcome is `INCOMPLETE` whatever the review's own status, its last gap has the reason code `HEAD_COMMIT_MISMATCH`, and `reason` says that the review is pinned to another head commit. A settled review that names no head has `HEAD_COMMIT_UNAVAILABLE` instead and is not `COMPLETED` either. `outcome.review.head_commit_sha` is the commit the review is pinned to. A patch review's head is a derived revision ID, so no commit matches it. In a GitHub Actions `pull_request` job, `GITHUB_SHA` is the merge commit; pass the pull request's head. +`on_poll` is optional; when supplied to `ProoflineClient`, it must be a synchronous callback and must return `None` (returning an awaitable or any return value other than `None` raises `TypeError`). `on_poll` receives each `CustomerReviewDetail` fetched and validated before the callback is reached; deadline expiry can suppress delivery. Tuples with more than 1,000 items, immutable bytes, mutable buffers, or memoryviews with more than 65,536 bytes (the 65,536-byte limit applies to immutable bytes, mutable buffers, and memoryviews, including any view backed by a buffer larger than 65,536 bytes), sets, frozensets, or deques with more than 65,536 items in review extension data skip `on_poll` snapshot delivery even with time remaining, and snapshot copy errors on unsupported mutable values propagate before `on_poll` runs. `on_poll` exceptions and callback return-validation errors propagate even when a terminal outcome has already been determined and can prevent the waiter from returning a classified outcome. Synchronous callbacks must return before a terminal result can be delivered and the polling deadline cannot interrupt them. + +With `AsyncProoflineClient`, `wait_for_review` can be awaited; `on_poll` must be a callable that accepts the review snapshot and returns `None` or an awaitable (such as a coroutine or future). Any coroutines returned by `on_poll` must be unstarted, and any pending futures must belong to the polling event loop. Callbacks cannot return the running polling task (doing so raises `ValueError`). Deadline expiry before invocation (including during snapshot copying) can skip `on_poll` without discarding an already-classified outcome. Tuples with more than 1,000 items, immutable bytes, mutable buffers, or memoryviews with more than 65,536 bytes (the 65,536-byte limit applies to immutable bytes, mutable buffers, and memoryviews, including any view backed by a buffer larger than 65,536 bytes), sets, frozensets, or deques with more than 65,536 items in review extension data skip `on_poll` snapshot delivery even with time remaining, and snapshot copy errors on unsupported mutable values propagate before `on_poll` runs. Callbacks must return promptly even when they return an awaitable, because the polling deadline cannot interrupt the invocation. Return-validation errors and inline failures propagate and can prevent the waiter from returning a classified outcome (cancellation requested during callback invocation takes precedence over return validation). Failure propagation depends on the awaitable type and the polling deadline: already-done futures must be returned before the deadline to propagate (failures of already-done futures returned after expiry are detached and suppressed), coroutines propagate if their wrapper task completes before the deadline (timed at wrapper completion rather than when the waiter retrieves them, except for wrappers cancelled before their first step which are timed when their done callback runs), and pending future failures propagate only if their done callback executes before the deadline expires (cutting off at done-callback execution and allowing any classified outcome to be returned). When the deadline expires or when the wait is cancelled, unstarted coroutines returned by `on_poll` are closed without entering their body (a wrapper task may already have been scheduled; cancelling the waiter through an underlying pending future can leave unresumed tasks unflagged until resumption unless observed through the pending future), while already-running callback tasks and pending futures are detached without cancellation and may continue running (detached callback coroutines suppress late SystemExit and KeyboardInterrupt signals; waiter cancellation propagates without cancelling detached callback work). + +`head_commit_sha` is a full SHA of 40 or 64 hexadecimal characters; any other value raises `ValueError` before a request. A review pinned to another head ends the wait on the read that names it, and its outcome is `INCOMPLETE` whatever the review's own status (unless a callback exception, callback return-validation error, snapshot copy error, waiter cancellation during inline callback invocation, or waiter cancellation while callback work is pending propagates), its last gap has the reason code `HEAD_COMMIT_MISMATCH`, and `reason` says that the review is pinned to another head commit. A settled review that names no head has `HEAD_COMMIT_UNAVAILABLE` instead and is not `COMPLETED` either. `outcome.review.head_commit_sha` is the commit the review is pinned to. A patch review's head is a derived revision ID, so no commit matches it. In a GitHub Actions `pull_request` job, `GITHUB_SHA` is the merge commit; pass the pull request's head. ## Review exports diff --git a/proofline/client.py b/proofline/client.py index dda559a3..b2cfa790 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -2,12 +2,64 @@ from __future__ import annotations +import array +import ctypes +import datetime import math -from asyncio import sleep as async_sleep +import mmap +import weakref +from asyncio import ( + CancelledError as AsyncCancelledError, +) +from asyncio import ( + Future as AsyncFuture, +) +from asyncio import ( + Task as AsyncTask, +) +from asyncio import ( + create_task as async_create_task, +) +from asyncio import ( + current_task as async_current_task, +) +from asyncio import ( + get_running_loop as async_get_running_loop, +) +from asyncio import ( + isfuture as async_isfuture, +) +from asyncio import ( + sleep as async_sleep, +) +from asyncio import ( + wait as async_wait, +) +from collections import defaultdict, deque from collections.abc import AsyncIterator, Awaitable, Callable, Iterator +from enum import Enum +from inspect import ( + CORO_CREATED, + GEN_CREATED, + getcoroutinestate, + getgeneratorstate, + isawaitable, + iscoroutine, + isgenerator, +) +from inspect import ( + signature as inspect_signature, +) from string import hexdigits from time import monotonic, sleep -from types import TracebackType +from types import ( + BuiltinFunctionType, + FunctionType, + MemberDescriptorType, + MethodType, + ModuleType, + TracebackType, +) from typing import Any, Self, TypedDict, TypeVar, Unpack, cast from urllib.parse import quote from uuid import UUID @@ -78,6 +130,12 @@ from .outcomes import ReviewOutcome, outcome T = TypeVar("T") + + +async def _await_res(res: Awaitable[Any]) -> None: + await res + + AgentSessionPrompt = AgentSessionPromptType0 | AgentSessionPromptType1 _AgentSessionPromptModels = tuple[type[AgentSessionPromptType0], type[AgentSessionPromptType1]] _AGENT_SESSION_PROMPT_MODELS: _AgentSessionPromptModels = ( @@ -193,6 +251,2468 @@ def _export_page_limit(max_pages: int) -> None: raise ValueError("Export pagination requires max_pages 1..1000.") +_IMMUTABLE_TYPES: set[type] = { + int, + float, + str, + bool, + bytes, + datetime.date, +} + + +def _has_mutable_captures(val: Any, seen: set[int] | None = None) -> bool: + if seen is None: + seen = set() + if id(val) in seen: + return False + seen.add(id(val)) + + closure = getattr(val, "__closure__", None) + if closure: + return True + + defaults = getattr(val, "__defaults__", None) + if defaults: + for d in defaults: + if not _is_immutable(d, seen): + return True + + kwdefaults = getattr(val, "__kwdefaults__", None) + if kwdefaults and isinstance(kwdefaults, dict): + for d in kwdefaults.values(): + if not _is_immutable(d, seen): + return True + + return False + + +_SAFE_BUILTIN_CLASSES: set[type] = { + int, + float, + str, + bool, + bytes, + list, + dict, + set, + tuple, + bytearray, + deque, + defaultdict, + datetime.date, + datetime.datetime, + datetime.time, + UUID, +} + + +def _is_identity_hashed(obj: Any) -> bool: + try: + return type(obj).__hash__ is object.__hash__ + except Exception: + return False + + +def _deadline_expired() -> bool: + budget = _transport.remaining() + return budget is not None and budget <= 0 + + +def _is_immutable_enum(val: Enum, seen: set[int] | None = None) -> bool: + if not _is_immutable(val.value, seen): + return False + inst_dict = getattr(val, "__dict__", None) + if inst_dict: + for v in inst_dict.values(): + if not _is_immutable(v, seen): + return False + seen_slots: set[str] = set() + for cls in type(val).__mro__: + slots = getattr(cls, "__slots__", None) + if slots is not None: + if isinstance(slots, str): + slots = (slots,) + for slot_name in slots: + if slot_name in seen_slots or slot_name in ("__dict__", "__weakref__"): + continue + seen_slots.add(slot_name) + try: + slot_val = getattr(val, slot_name) + except AttributeError: + continue + if not _is_immutable(slot_val, seen): + return False + return True + + +def _is_immutable(val: Any, seen: set[int] | None = None) -> bool: + if val is None or val is UNSET: + return True + if isinstance(val, type): + return val in _SAFE_BUILTIN_CLASSES + t = type(val) + if t in _IMMUTABLE_TYPES: + return True + if isinstance(val, Enum): + return _is_immutable_enum(val, seen) + if t is datetime.datetime: + tz = val.tzinfo + return tz is None or type(tz) is datetime.timezone + if isinstance(val, (FunctionType, MethodType)): + return False + if isinstance(val, BuiltinFunctionType): + if bool(getattr(val, "__dict__", None)): + return False + curr = getattr(val, "__self__", None) + if curr is None: + return True + if isinstance(curr, ModuleType): + return False + if isinstance(curr, type): + return curr in _SAFE_BUILTIN_CLASSES or curr is object + local_seen = seen if seen is not None else set() + val_id = id(val) + if val_id in local_seen: + return False + local_seen.add(val_id) + while curr is not None and isinstance(curr, (BuiltinFunctionType, MethodType)): + curr_id = id(curr) + if curr_id in local_seen: + return False + local_seen.add(curr_id) + if _deadline_expired(): + return False + curr = getattr(curr, "__self__", None) + if curr is None: + return True + if isinstance(curr, ModuleType): + return False + if isinstance(curr, type): + return curr in _SAFE_BUILTIN_CLASSES or curr is object + return _is_immutable(curr, local_seen) + return False + + +_SAFE_SPECIAL_ATTRS: set[str] = { + "__abstractmethods__", + "__annotations__", + "__attrs_attrs__", + "__attrs_own_setattr__", + "__attrs_props__", + "__class__", + "__classcell__", + "__dataclass_fields__", + "__dataclass_params__", + "__dict__", + "__doc__", + "__firstlineno__", + "__hash__", + "__match_args__", + "__module__", + "__mro_entries__", + "__name__", + "__orig_bases__", + "__parameters__", + "__pydantic_decorators__", + "__pydantic_fields__", + "__pydantic_generic_metadata__", + "__qualname__", + "__slots__", + "__static_attributes__", + "__subclasshook__", + "__weakref__", + "_fields", + "_field_defaults", + "_field_types", +} + + +def _is_pointer_bearing_ctypes_type(t: Any, seen: set[type] | None = None) -> bool: + if not isinstance(t, type): + return True + if seen is None: + seen = set() + if t in seen: + return False + seen.add(t) + cfuncptr = getattr(ctypes, "_CFuncPtr", None) + if issubclass( + t, + ( + ctypes.py_object, + ctypes._Pointer, + ctypes.c_void_p, + ctypes.c_char_p, + ctypes.c_wchar_p, + ), + ) or (cfuncptr is not None and issubclass(t, cfuncptr)): + return True + code = getattr(t, "_type_", None) + if code in ("O", "P", "z", "Z", "u"): + return True + if issubclass(t, ctypes.Array): + elem_t = getattr(t, "_type_", None) + return _is_pointer_bearing_ctypes_type(elem_t, seen) + if issubclass(t, (ctypes.Structure, ctypes.Union)): + for base in t.__mro__: + fields = getattr(base, "_fields_", None) + if fields: + for field_spec in fields: + if len(field_spec) >= 2: + field_type = field_spec[1] + if _is_pointer_bearing_ctypes_type(field_type, seen): + return True + return False + + +def _has_mutable_class_attrs(cls: type) -> bool: + for base in cls.__mro__: + if base in _SAFE_BUILTIN_CLASSES or base is object: + continue + is_arr_cls = issubclass(base, ctypes.Array) + for name, attr in base.__dict__.items(): + if is_arr_cls and name in ("_type_", "_length_"): + if name == "_length_": + if not isinstance(attr, int) or attr < 0: + return True + continue + if name == "_type_": + if not (isinstance(attr, type) or hasattr(attr, "_type_")): + return True + if _is_pointer_bearing_ctypes_type(attr): + return True + continue + if name in _SAFE_SPECIAL_ATTRS: + continue + if isinstance(attr, MemberDescriptorType) or type(attr).__name__ in ( + "member_descriptor", + "getset_descriptor", + "wrapper_descriptor", + "method_descriptor", + "slotwrapper", + "classmethod_descriptor", + "_tuplegetter", + ): + continue + if isinstance( + attr, + ( + FunctionType, + MethodType, + classmethod, + staticmethod, + property, + BuiltinFunctionType, + ), + ): + continue + if not _is_immutable(attr): + return True + return False + + +def _assign_target(target: Any, key: Any, is_attr: bool, val: Any) -> None: + if target is None: + return + if is_attr: + slot_or_name, is_desc = key + if is_desc: + try: + slot_or_name.__set__(target, val) + except (AttributeError, TypeError): + pass + else: + try: + setattr(target, slot_or_name, val) + except (AttributeError, TypeError): + try: + object.__setattr__(target, slot_or_name, val) + except Exception: + pass + else: + if isinstance(target, deque): + deque.__setitem__(target, key, val) + elif isinstance(target, list): + list.__setitem__(target, key, val) + else: + target[key] = val + + +def _get_obj_attrs(obj: Any) -> list[tuple[Any, bool, Any]]: + items: list[tuple[Any, bool, Any]] = [] + seen_slots: set[int] = set() + + for cls in type(obj).__mro__: + for name, desc in cls.__dict__.items(): + if isinstance(desc, MemberDescriptorType) or ( + type(desc).__name__ == "member_descriptor" + and hasattr(desc, "__get__") + and hasattr(desc, "__set__") + ): + if name in ("__dict__", "__weakref__"): + continue + if id(desc) in seen_slots: + continue + seen_slots.add(id(desc)) + try: + val = desc.__get__(obj, cls) + items.append((desc, True, val)) + except AttributeError: + pass + + return items + + +def _install_cloned_dict( + val: Any, + new_obj: Any, + memo: dict[int, Any], + stack: list[Any], +) -> None: + if hasattr(val, "__dict__"): + try: + inst_dict = val.__dict__ + if isinstance(inst_dict, dict): + dict_id = id(inst_dict) + if dict_id in memo: + cloned_dict = memo[dict_id] + else: + try: + cloned_dict = dict.__new__(type(inst_dict)) + dict.clear(cloned_dict) + except Exception: + cloned_dict = {} + memo[dict_id] = cloned_dict + stack.append((_DICT_EXPAND, cloned_dict, iter(inst_dict.items()))) + if new_obj is not None: + try: + new_obj.__dict__ = cloned_dict + except (AttributeError, TypeError): + try: + object.__setattr__(new_obj, "__dict__", cloned_dict) + except Exception: + pass + except Exception: + pass + + +_ITEM_TASK = 1 +_DICT_EXPAND = 2 +_LIST_EXPAND = 3 +_OBJ_EXPAND = 4 +_TUPLE_EXPAND = 5 +_TUPLE_FINALIZE = 6 +_SET_FINALIZE = 7 +_SET_EXPAND = 8 +_EXPAND_BATCH_SIZE = 64 +_MAX_SAFE_TUPLE_SIZE = 1000 +_MAX_SAFE_BUFFER_SIZE = 65_536 +_UNRESOLVED_SLOT = object() +_MISSING = object() + + +class _Py_buffer(ctypes.Structure): + _fields_ = [ + ("buf", ctypes.c_void_p), + ("obj", ctypes.c_void_p), + ("len", ctypes.c_ssize_t), + ("itemsize", ctypes.c_ssize_t), + ("readonly", ctypes.c_int), + ("ndim", ctypes.c_int), + ("format", ctypes.c_char_p), + ("shape", ctypes.POINTER(ctypes.c_ssize_t)), + ("strides", ctypes.POINTER(ctypes.c_ssize_t)), + ("suboffsets", ctypes.POINTER(ctypes.c_ssize_t)), + ("internal", ctypes.c_void_p), + ] + + +_py_get_buffer = ctypes.pythonapi.PyObject_GetBuffer +_py_get_buffer.restype = ctypes.c_int +_py_get_buffer.argtypes = [ctypes.py_object, ctypes.POINTER(_Py_buffer), ctypes.c_int] + +_py_release_buffer = ctypes.pythonapi.PyBuffer_Release +_py_release_buffer.restype = None +_py_release_buffer.argtypes = [ctypes.POINTER(_Py_buffer)] + + +class _DeadlineAborted(Exception): + pass + + +def _snapshot_review(detail: CustomerReviewDetail) -> CustomerReviewDetail | None: + try: + return _snapshot_review_impl(detail) + except _DeadlineAborted: + return None + + +def _snapshot_review_impl(detail: CustomerReviewDetail) -> CustomerReviewDetail | None: + if _deadline_expired(): + return None + + memo: dict[int, Any] = {id(UNSET): UNSET} + root = object.__new__(type(detail)) + memo[id(detail)] = root + + deferred_dict_insertions: list[tuple[Any, Any, Any]] = [] + deferred_frozenset_insertions: list[tuple[Any, Any, bool, int, Any, list[Any]]] = [] + deferred_tuple_finalizations: list[tuple[Any, Any, bool, int, Any, list[Any]]] = [] + deferred_set_insertions: list[tuple[Any, Any, list[Any]]] = [] + pending_frozenset_ids: set[int] = set() + pending_assignments: dict[int, list[tuple[Any, Any, bool]]] = {} + fallback_built_frozensets: list[tuple[Any, list[Any]]] = [] + deferred_attr_assignments: list[tuple[Any, tuple[Any, bool], Any]] = [] + populated_mutable_sets: list[tuple[Any, list[Any]]] = [] + backing_buffer_memo: dict[int, Any] = {} + + def _collect_set_members(val: Any) -> list[Any] | None: + if isinstance(val, set): + if set.__len__(val) > _MAX_SAFE_BUFFER_SIZE: + return None + elif isinstance(val, frozenset): + if frozenset.__len__(val) > _MAX_SAFE_BUFFER_SIZE: + return None + elif hasattr(val, "__len__") and len(val) > _MAX_SAFE_BUFFER_SIZE: + return None + members: list[Any] = [] + it = iter(val) + batch_count = 0 + while True: + try: + m = next(it) + except StopIteration: + break + members.append(m) + if len(members) > _MAX_SAFE_BUFFER_SIZE: + return None + batch_count += 1 + if (batch_count & 63) == 0 and _deadline_expired(): + return None + return members + + stack: list[Any] = [(_OBJ_EXPAND, root, iter(_get_obj_attrs(detail)))] + _install_cloned_dict(detail, root, memo, stack) + + while stack: + if _deadline_expired(): + return None + + task_type, target, it_or_key, *extra = stack.pop() + + if task_type == _ITEM_TASK: + key, is_attr, val = it_or_key, extra[0], extra[1] + if ( + isinstance( + val, + ( + FunctionType, + MethodType, + weakref.ref, + weakref.ProxyType, + weakref.CallableProxyType, + ), + ) + or ( + isinstance(val, BuiltinFunctionType) + and isinstance(getattr(val, "__self__", None), ModuleType) + ) + or isinstance(val, mmap.mmap) + or type(val).__name__ == "mmap" + ): + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + if isinstance(val, Enum): + if not _is_immutable(val): + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + _assign_target(target, key, is_attr, val) + continue + if _is_immutable(val): + if type(val) is bytes and len(val) > _MAX_SAFE_BUFFER_SIZE: + return None + _assign_target(target, key, is_attr, val) + continue + val_id = id(val) + if val_id in memo: + _assign_target(target, key, is_attr, memo[val_id]) + continue + if val_id in pending_frozenset_ids: + if target is not None: + pending_assignments.setdefault(val_id, []).append((target, key, is_attr)) + continue + if isinstance(val, UUID): + uuid_cls: Any = type(val) + if uuid_cls is UUID: + cloned_uuid = UUID(int=val.int, is_safe=val.is_safe) + else: + if _has_mutable_class_attrs(uuid_cls): + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{uuid_cls.__name__}" + ) + try: + cloned_uuid = uuid_cls(int=val.int, is_safe=val.is_safe) + except Exception: + try: + cloned_uuid = UUID.__new__(uuid_cls) + object.__setattr__(cloned_uuid, "int", val.int) + object.__setattr__(cloned_uuid, "is_safe", val.is_safe) + except Exception: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{uuid_cls.__name__}" + ) + memo[val_id] = cloned_uuid + _assign_target(target, key, is_attr, cloned_uuid) + if uuid_cls is not UUID: + if hasattr(val, "__dict__"): + _install_cloned_dict(val, cloned_uuid, memo, stack) + attrs = list(_get_obj_attrs(val)) + custom_attrs = [ + (desc, is_desc, v) + for (desc, is_desc, v) in attrs + if getattr(desc, "__name__", "") not in ("int", "is_safe") + ] + if custom_attrs: + stack.append((_OBJ_EXPAND, cloned_uuid, iter(custom_attrs))) + continue + if type(val) is dict: + new_dict: dict[Any, Any] = {} + memo[val_id] = new_dict + _assign_target(target, key, is_attr, new_dict) + stack.append((_DICT_EXPAND, new_dict, iter(val.items()))) + elif type(val) is list: + new_list: list[Any] = [] + memo[val_id] = new_list + _assign_target(target, key, is_attr, new_list) + stack.append((_LIST_EXPAND, new_list, iter(val))) + elif isinstance(val, tuple): + if type(val) is not tuple and _has_mutable_class_attrs(type(val)): + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + if tuple.__len__(val) > _MAX_SAFE_TUPLE_SIZE: + return None + if tuple.__len__(val) == 0: + if type(val) is tuple: + res_empty: Any = () + else: + try: + val_cls: Any = type(val) + res_empty = tuple.__new__(val_cls, ()) + except Exception: + return None + memo[val_id] = res_empty + if hasattr(val, "__dict__") or hasattr(val, "__slots__"): + _install_cloned_dict(val, res_empty, memo, stack) + stack.append((_OBJ_EXPAND, res_empty, iter(_get_obj_attrs(val)))) + _assign_target(target, key, is_attr, res_empty) + else: + temp_list: list[Any] = [] + stack.append( + ( + _TUPLE_FINALIZE, + target, + key, + is_attr, + val_id, + val, + temp_list, + ) + ) + stack.append((_TUPLE_EXPAND, temp_list, tuple.__iter__(val))) + elif isinstance(val, dict): + if type(val) not in (dict, defaultdict) and _has_mutable_class_attrs(type(val)): + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + if "__copy__" in type(val).__dict__: + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + try: + if isinstance(val, defaultdict): + new_dict = type(val)(None) + else: + new_dict = dict.__new__(type(val)) + dict.clear(new_dict) + except Exception as exc: + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) from exc + memo[val_id] = new_dict + _assign_target(target, key, is_attr, new_dict) + if isinstance(val, defaultdict) and val.default_factory is not None: + stack.append( + ( + _ITEM_TASK, + new_dict, + ("default_factory", False), + True, + val.default_factory, + ) + ) + if hasattr(val, "__dict__") or hasattr(val, "__slots__"): + _install_cloned_dict(val, new_dict, memo, stack) + stack.append((_OBJ_EXPAND, new_dict, iter(_get_obj_attrs(val)))) + stack.append((_DICT_EXPAND, new_dict, iter(val.items()))) + elif isinstance(val, list): + if type(val) is not list and _has_mutable_class_attrs(type(val)): + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + if "__copy__" in type(val).__dict__: + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + try: + new_list = list.__new__(type(val)) + list.clear(new_list) + except Exception as exc: + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) from exc + memo[val_id] = new_list + _assign_target(target, key, is_attr, new_list) + stack.append((_LIST_EXPAND, new_list, iter(val))) + if hasattr(val, "__dict__") or hasattr(val, "__slots__"): + _install_cloned_dict(val, new_list, memo, stack) + stack.append((_OBJ_EXPAND, new_list, iter(_get_obj_attrs(val)))) + elif isinstance(val, set): + set_len = set.__len__(val) + if set_len > _MAX_SAFE_BUFFER_SIZE: + return None + if type(val) is not set and _has_mutable_class_attrs(type(val)): + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + if "__copy__" in type(val).__dict__: + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + if type(val) is not set: + try: + if set_len == 0: + inspect_signature(type(val).__init__).bind(type(val)) + else: + inspect_signature(type(val).__init__).bind(type(val), ()) + except TypeError: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) + if type(val) is set: + new_set: set[Any] = set() + else: + try: + new_set = set.__new__(type(val)) + set.clear(new_set) + except Exception as exc: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) from exc + if set(new_set): + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) + memo[val_id] = new_set + _assign_target(target, key, is_attr, new_set) + if hasattr(val, "__dict__") or hasattr(val, "__slots__"): + _install_cloned_dict(val, new_set, memo, stack) + stack.append((_OBJ_EXPAND, new_set, iter(_get_obj_attrs(val)))) + if set_len > 0: + orig_set_members = _collect_set_members(val) + if orig_set_members is None: + return None + deferred_set_insertions.append((new_set, val, orig_set_members)) + stack.append((_SET_EXPAND, None, iter(val))) + elif isinstance(val, frozenset): + fs_len = frozenset.__len__(val) + if fs_len > _MAX_SAFE_BUFFER_SIZE: + return None + if type(val) is not frozenset and _has_mutable_class_attrs(type(val)): + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + if "__copy__" in type(val).__dict__: + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + if type(val) is not frozenset: + try: + if fs_len == 0: + inspect_signature(type(val).__init__).bind(type(val)) + else: + inspect_signature(type(val).__init__).bind(type(val), ()) + except TypeError: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) + if fs_len == 0: + if type(val) is frozenset: + res_empty_fs: Any = frozenset() + else: + try: + res_empty_fs = frozenset.__new__(type(val), ()) + except Exception as exc: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) from exc + if frozenset(res_empty_fs): + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) + memo[val_id] = res_empty_fs + if hasattr(val, "__dict__") or hasattr(val, "__slots__"): + _install_cloned_dict(val, res_empty_fs, memo, stack) + stack.append((_OBJ_EXPAND, res_empty_fs, iter(_get_obj_attrs(val)))) + _assign_target(target, key, is_attr, res_empty_fs) + else: + orig_fs_members = _collect_set_members(val) + if orig_fs_members is None: + return None + pending_frozenset_ids.add(val_id) + deferred_frozenset_insertions.append( + (target, key, is_attr, val_id, val, orig_fs_members) + ) + if hasattr(val, "__dict__"): + _install_cloned_dict(val, None, memo, stack) + if hasattr(val, "__dict__") or hasattr(val, "__slots__"): + for slot_or_name, is_desc, attr_val in _get_obj_attrs(val): + if type(attr_val) is bytes and len(attr_val) > _MAX_SAFE_BUFFER_SIZE: + return None + if ( + not _is_immutable(attr_val) + and id(attr_val) not in memo + and id(attr_val) not in pending_frozenset_ids + ): + stack.append((_ITEM_TASK, None, None, False, attr_val)) + stack.append((_SET_EXPAND, None, iter(val))) + elif isinstance(val, deque): + if deque.__len__(val) > _MAX_SAFE_BUFFER_SIZE: + return None + if type(val) is deque: + new_deque: deque[Any] = deque(maxlen=val.maxlen) + else: + if type(val) is not deque and _has_mutable_class_attrs(type(val)): + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) + try: + new_deque = deque.__new__(type(val)) + deque.__init__(new_deque, maxlen=val.maxlen) + except Exception as exc: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) from exc + memo[val_id] = new_deque + _assign_target(target, key, is_attr, new_deque) + stack.append((_LIST_EXPAND, new_deque, deque.__iter__(val))) + if hasattr(val, "__dict__") or hasattr(val, "__slots__"): + _install_cloned_dict(val, new_deque, memo, stack) + stack.append((_OBJ_EXPAND, new_deque, iter(_get_obj_attrs(val)))) + elif isinstance(val, bytearray): + if type(val) is not bytearray and _has_mutable_class_attrs(type(val)): + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + if bytearray.__len__(val) > _MAX_SAFE_BUFFER_SIZE: + return None + try: + resolved = bytearray.__new__(type(val)) + bytearray.__init__(resolved, val) + except Exception as exc: + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) from exc + memo[val_id] = resolved + _assign_target(target, key, is_attr, resolved) + if hasattr(val, "__dict__") or hasattr(val, "__slots__"): + _install_cloned_dict(val, resolved, memo, stack) + stack.append((_OBJ_EXPAND, resolved, iter(_get_obj_attrs(val)))) + elif isinstance(val, array.array): + if type(val) is not array.array and _has_mutable_class_attrs(type(val)): + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + native_len: int | None = None + try: + b_arr = _Py_buffer() + if _py_get_buffer(val, ctypes.byref(b_arr), 0x0118) == 0: + try: + native_len = b_arr.len + finally: + _py_release_buffer(ctypes.byref(b_arr)) + except BaseException: + pass + if native_len is not None: + if native_len > _MAX_SAFE_BUFFER_SIZE: + return None + else: + try: + arr_byte_count = array.array.buffer_info(val)[ + 1 + ] * array.array.itemsize.__get__(val) + except BaseException: + arr_byte_count = val.buffer_info()[1] * val.itemsize + if arr_byte_count > _MAX_SAFE_BUFFER_SIZE: + return None + try: + try: + native_tc = array.array.typecode.__get__(val) + except BaseException: + native_tc = val.typecode + resolved_arr = cast(Any, array.array).__new__(type(val), native_tc, val) + except Exception as exc: + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) from exc + clone_len: int | None = None + try: + b_chk = _Py_buffer() + if _py_get_buffer(resolved_arr, ctypes.byref(b_chk), 0x0118) == 0: + try: + clone_len = b_chk.len + finally: + _py_release_buffer(ctypes.byref(b_chk)) + except BaseException: + pass + if clone_len is not None: + if clone_len > _MAX_SAFE_BUFFER_SIZE: + return None + else: + try: + arr_clone_bytes = array.array.buffer_info(resolved_arr)[ + 1 + ] * array.array.itemsize.__get__(resolved_arr) + except BaseException: + arr_clone_bytes = resolved_arr.buffer_info()[1] * resolved_arr.itemsize + if arr_clone_bytes > _MAX_SAFE_BUFFER_SIZE: + return None + memo[val_id] = resolved_arr + _assign_target(target, key, is_attr, resolved_arr) + if hasattr(val, "__dict__") or hasattr(val, "__slots__"): + _install_cloned_dict(val, resolved_arr, memo, stack) + stack.append((_OBJ_EXPAND, resolved_arr, iter(_get_obj_attrs(val)))) + elif isinstance(val, memoryview): + if val.nbytes > _MAX_SAFE_BUFFER_SIZE: + return None + exporter = getattr(val, "obj", None) + if exporter is not None: + try: + b_exp_check = _Py_buffer() + if _py_get_buffer(exporter, ctypes.byref(b_exp_check), 0x0118) == 0: + try: + if b_exp_check.len > _MAX_SAFE_BUFFER_SIZE: + return None + finally: + _py_release_buffer(ctypes.byref(b_exp_check)) + except BaseException: + pass + resolved_mv: Any + if isinstance(exporter, (ctypes.Array, ctypes.Structure, ctypes.Union)): + exp_cls = type(exporter) + if _is_pointer_bearing_ctypes_type(exp_cls): + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{exp_cls.__name__}" + ) + if isinstance(exporter, ctypes.Array): + exp_elem_type = getattr(exp_cls, "_type_", None) + exp_len = getattr(exp_cls, "_length_", None) + if not ( + isinstance(exp_elem_type, type) or hasattr(exp_elem_type, "_type_") + ): + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{exp_cls.__name__}" + ) + if not isinstance(exp_len, int) or exp_len < 0: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{exp_cls.__name__}" + ) + if _has_mutable_class_attrs(exp_cls): + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{exp_cls.__name__}" + ) + exp_id = id(exporter) + if exp_id in memo: + cloned_exporter = memo[exp_id] + else: + if ctypes.sizeof(exporter) > _MAX_SAFE_BUFFER_SIZE: + return None + cloned_exporter = type(exporter).from_buffer_copy(exporter) + memo[exp_id] = cloned_exporter + if hasattr(exporter, "__dict__") or hasattr(exporter, "__slots__"): + _install_cloned_dict(exporter, cloned_exporter, memo, stack) + stack.append( + (_OBJ_EXPAND, cloned_exporter, iter(_get_obj_attrs(exporter))) + ) + b_val = _Py_buffer() + b_exp = _Py_buffer() + offset = 0 + if _py_get_buffer(val, ctypes.byref(b_val), 0x0118) != 0: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) + try: + if _py_get_buffer(exporter, ctypes.byref(b_exp), 0x0118) != 0: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(exporter).__name__}" + ) + try: + offset = b_val.buf - b_exp.buf + finally: + _py_release_buffer(ctypes.byref(b_exp)) + finally: + _py_release_buffer(ctypes.byref(b_val)) + elem_type = getattr(type(exporter), "_type_", ctypes.c_byte) + val_strides = val.strides + val_shape = val.shape + full_mv = memoryview(cloned_exporter) + if val.format == full_mv.format: + itemsize = val.itemsize + item_offset = offset // itemsize if itemsize > 0 else 0 + if val.ndim == 0: + scalar_obj = elem_type.from_buffer(cloned_exporter, offset) + resolved_mv = memoryview(scalar_obj) + elif ( + full_mv.ndim >= 1 + and full_mv.strides is not None + and val_shape is not None + and val_strides is not None + ): + exp_stride = full_mv.strides[0] + start = offset // exp_stride if exp_stride != 0 else 0 + step = val_strides[0] // exp_stride if exp_stride != 0 else 1 + length = val_shape[0] + if length == 0: + resolved_mv = full_mv[0:0] + elif step > 0: + resolved_mv = full_mv[start : start + length * step : step] + else: + last = start + (length - 1) * step + stop = None if (last + step < 0) else (last + step) + resolved_mv = full_mv[start:stop:step] + elif val.c_contiguous: + count = val.nbytes // val.itemsize if val.itemsize > 0 else 0 + resolved_mv = full_mv[item_offset : item_offset + count] + else: + resolved_mv = full_mv + elif val.format == "B": + full_bytes = full_mv.cast("B") + if val.ndim == 0: + resolved_mv = full_bytes[offset : offset + 1].cast("B", shape=()) + elif val.ndim == 1 and val_shape is not None and val_strides is not None: + shape = val_shape[0] + stride = val_strides[0] + if shape == 0: + resolved_mv = full_bytes[0:0] + elif stride > 0: + resolved_mv = full_bytes[offset : offset + shape * stride : stride] + else: + last = offset + (shape - 1) * stride + stop = None if (last + stride < 0) else (last + stride) + resolved_mv = full_bytes[offset:stop:stride] + elif val_shape is not None and val.ndim > 1: + if val_shape[0] == 0: + plane_bytes = val.itemsize + for d in val_shape[1:]: + plane_bytes *= d + s_0 = val_strides[0] if val_strides is not None else 1 + if len(full_bytes) >= plane_bytes and plane_bytes > 0: + nonempty_shape = (1,) + val_shape[1:] + base_shaped = full_bytes[:plane_bytes].cast( + "B", shape=nonempty_shape + ) + resolved_mv = ( + base_shaped[::-1][0:0] if s_0 < 0 else base_shaped[0:0] + ) + else: + resolved_mv = full_bytes[0:0] + elif val_strides is not None: + d_0 = val_shape[0] + s_0 = val_strides[0] + plane_bytes = val.itemsize + for d in val_shape[1:]: + plane_bytes *= d + min_p = 0 if s_0 >= 0 else (d_0 - 1) + max_p = (d_0 - 1) if s_0 >= 0 else 0 + min_byte = offset + min_p * s_0 + max_byte = offset + max_p * s_0 + plane_bytes + span_bytes_nd: Any = full_bytes[min_byte:max_byte] + physical_planes = ( + len(span_bytes_nd) // plane_bytes if plane_bytes > 0 else d_0 + ) + physical_shape = (physical_planes,) + val_shape[1:] + span_nd: Any = span_bytes_nd.cast("B", shape=physical_shape) + abs_step = abs(s_0) // plane_bytes if plane_bytes > 0 else 1 + resolved_mv = ( + span_nd[::-abs_step] if s_0 < 0 else span_nd[::abs_step] + ) + else: + full_bytes_slice: Any = full_bytes[offset : offset + val.nbytes] + resolved_mv = full_bytes_slice.cast("B", shape=val_shape) + elif val.c_contiguous: + resolved_mv = full_bytes[offset : offset + val.nbytes] + elif val_shape is not None: + full_bytes_slice = full_bytes[offset : offset + val.nbytes] + resolved_mv = full_bytes_slice.cast("B", shape=val_shape) + else: + resolved_mv = full_bytes[offset : offset + val.nbytes] + else: + full_bytes = full_mv.cast("B") + try: + if ( + val.ndim == 1 + and val_shape is not None + and val_strides is not None + and val.itemsize > 0 + ): + shape = val_shape[0] + stride = val_strides[0] + itemsize = val.itemsize + if shape == 0: + empty_bytes: Any = full_bytes[0:0] + resolved_mv = empty_bytes.cast(val.format) + else: + elem_stride = stride // itemsize + if stride >= 0: + min_byte = offset + max_byte = offset + (shape - 1) * stride + itemsize + else: + min_byte = offset + (shape - 1) * stride + max_byte = offset + itemsize + span_bytes: Any = full_bytes[min_byte:max_byte] + span_cast: Any = span_bytes.cast(val.format) + local_offset = (offset - min_byte) // itemsize + if elem_stride > 0: + resolved_mv = span_cast[ + local_offset : local_offset + + shape * elem_stride : elem_stride + ] + else: + last = local_offset + (shape - 1) * elem_stride + stop = ( + None + if (last + elem_stride < 0) + else (last + elem_stride) + ) + resolved_mv = span_cast[local_offset:stop:elem_stride] + elif ( + val.ndim > 1 + and val_shape is not None + and val_strides is not None + and val.itemsize > 0 + ): + d_0 = val_shape[0] + s_0 = val_strides[0] + plane_bytes = val.itemsize + for d in val_shape[1:]: + plane_bytes *= d + min_p = 0 if s_0 >= 0 else (d_0 - 1) + max_p = (d_0 - 1) if s_0 >= 0 else 0 + min_byte = offset + min_p * s_0 + max_byte = offset + max_p * s_0 + plane_bytes + span_bytes_fmt: Any = full_bytes[min_byte:max_byte] + physical_planes = ( + len(span_bytes_fmt) // plane_bytes if plane_bytes > 0 else d_0 + ) + physical_shape = (physical_planes,) + val_shape[1:] + span_fmt: Any = span_bytes_fmt.cast( + val.format, shape=physical_shape + ) + abs_step = abs(s_0) // plane_bytes if plane_bytes > 0 else 1 + resolved_mv = ( + span_fmt[::-abs_step] if s_0 < 0 else span_fmt[::abs_step] + ) + else: + full_bytes_slice = full_bytes[offset : offset + val.nbytes] + if val_shape is not None: + resolved_mv = full_bytes_slice.cast(val.format, shape=val_shape) + else: + resolved_mv = full_bytes_slice.cast(val.format) + except Exception: + resolved_mv = full_mv + if val.readonly: + resolved_mv = resolved_mv.toreadonly() + memo[val_id] = resolved_mv + _assign_target(target, key, is_attr, resolved_mv) + continue + elif isinstance(exporter, (bytearray, array.array, bytes)): + if type(exporter) not in ( + bytearray, + array.array, + bytes, + ) and _has_mutable_class_attrs(type(exporter)): + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(exporter).__name__}" + ) + exp_id = id(exporter) + if exp_id in memo: + cloned_exporter = memo[exp_id] + else: + exp_native_len: int | None = None + try: + b_chk2 = _Py_buffer() + if _py_get_buffer(exporter, ctypes.byref(b_chk2), 0x0118) == 0: + try: + exp_native_len = b_chk2.len + finally: + _py_release_buffer(ctypes.byref(b_chk2)) + except BaseException: + pass + if exp_native_len is not None: + if exp_native_len > _MAX_SAFE_BUFFER_SIZE: + return None + else: + if isinstance(exporter, bytearray): + exp_bytes_len = bytearray.__len__(exporter) + elif isinstance(exporter, bytes): + exp_bytes_len = bytes.__len__(exporter) + else: + exp_bytes_len = array.array.buffer_info(exporter)[ + 1 + ] * array.array.itemsize.__get__(exporter) + if exp_bytes_len > _MAX_SAFE_BUFFER_SIZE: + return None + try: + if isinstance(exporter, bytes): + if type(exporter) is bytes: + cloned_exporter = bytes(exporter) + else: + mv_exp = memoryview(exporter) + if mv_exp.nbytes > _MAX_SAFE_BUFFER_SIZE: + return None + raw_exp = bytes(mv_exp) + cloned_exporter = bytes.__new__(type(exporter), raw_exp) + elif isinstance(exporter, bytearray): + cloned_exporter = bytearray.__new__(type(exporter)) + bytearray.__init__(cloned_exporter, exporter) + else: + try: + native_exp_tc = array.array.typecode.__get__(exporter) + except BaseException: + native_exp_tc = exporter.typecode + cloned_exporter = cast(Any, array.array).__new__( + type(exporter), native_exp_tc, exporter + ) + clone_exp_len: int | None = None + try: + b_chk = _Py_buffer() + if ( + _py_get_buffer(cloned_exporter, ctypes.byref(b_chk), 0x0118) + == 0 + ): + try: + clone_exp_len = b_chk.len + finally: + _py_release_buffer(ctypes.byref(b_chk)) + except BaseException: + pass + if clone_exp_len is not None: + if clone_exp_len > _MAX_SAFE_BUFFER_SIZE: + return None + else: + try: + arr_exp_bytes = array.array.buffer_info(cloned_exporter)[ + 1 + ] * array.array.itemsize.__get__(cloned_exporter) + except BaseException: + arr_exp_bytes = ( + cloned_exporter.buffer_info()[1] + * cloned_exporter.itemsize + ) + if arr_exp_bytes > _MAX_SAFE_BUFFER_SIZE: + return None + except Exception as exc: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(exporter).__name__}" + ) from exc + memo[exp_id] = cloned_exporter + if hasattr(exporter, "__dict__") or hasattr(exporter, "__slots__"): + _install_cloned_dict(exporter, cloned_exporter, memo, stack) + stack.append( + (_OBJ_EXPAND, cloned_exporter, iter(_get_obj_attrs(exporter))) + ) + b_val = _Py_buffer() + b_exp = _Py_buffer() + if _py_get_buffer(val, ctypes.byref(b_val), 0x0118) != 0: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) + try: + if _py_get_buffer(exporter, ctypes.byref(b_exp), 0x0118) != 0: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(exporter).__name__}" + ) + try: + offset = b_val.buf - b_exp.buf + finally: + _py_release_buffer(ctypes.byref(b_exp)) + finally: + _py_release_buffer(ctypes.byref(b_val)) + base_bytes = cast(Any, memoryview(cloned_exporter)).cast("B") + val_strides = val.strides + val_shape = val.shape + if val.ndim == 1 and val_shape is not None and val_strides is not None: + shape = val_shape[0] + itemsize = val.itemsize + stride = val_strides[0] + elem_stride = stride // itemsize + if shape == 0: + casted = cast(Any, base_bytes[0:0]).cast(val.format) + resolved_mv = casted[::elem_stride] + elif stride >= 0: + byte_len = (shape - 1) * stride + itemsize + byte_slice = base_bytes[offset : offset + byte_len] + casted = cast(Any, byte_slice).cast(val.format) + resolved_mv = casted[::elem_stride] + else: + lowest_byte = offset + (shape - 1) * stride + byte_len = (shape - 1) * abs(stride) + itemsize + byte_slice = base_bytes[lowest_byte : lowest_byte + byte_len] + casted = cast(Any, byte_slice).cast(val.format) + start_elem = (shape - 1) * (-elem_stride) + resolved_mv = casted[start_elem:None:elem_stride] + elif val.ndim == 0: + resolved_mv = cast(Any, base_bytes[offset : offset + val.itemsize]).cast( + val.format, shape=() + ) + elif val.c_contiguous: + resolved_mv = cast(Any, base_bytes[offset : offset + val.nbytes]).cast( + val.format, shape=val.shape + ) + else: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) + else: + if not val.c_contiguous: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) + if exporter is not None: + if isinstance(exporter, mmap.mmap) or type(exporter).__name__ == "mmap": + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(exporter).__name__}" + ) + exp_id = id(exporter) + if exp_id in backing_buffer_memo: + cloned_buf = backing_buffer_memo[exp_id] + else: + exp_readonly = False + try: + b_exp_chk = _Py_buffer() + if _py_get_buffer(exporter, ctypes.byref(b_exp_chk), 0x0118) == 0: + try: + exp_readonly = bool(b_exp_chk.readonly) + finally: + _py_release_buffer(ctypes.byref(b_exp_chk)) + else: + exp_readonly = getattr( + exporter, "readonly", False + ) or isinstance(exporter, (bytes, tuple)) + except BaseException: + exp_readonly = getattr(exporter, "readonly", False) or isinstance( + exporter, (bytes, tuple) + ) + if exp_readonly: + cloned_buf = bytes(exporter) + else: + cloned_buf = bytearray(exporter) + backing_buffer_memo[exp_id] = cloned_buf + offset = 0 + try: + b_val = _Py_buffer() + b_exp = _Py_buffer() + if _py_get_buffer(val, ctypes.byref(b_val), 0x0118) == 0: + try: + if _py_get_buffer(exporter, ctypes.byref(b_exp), 0x0118) == 0: + try: + offset = b_val.buf - b_exp.buf + finally: + _py_release_buffer(ctypes.byref(b_exp)) + finally: + _py_release_buffer(ctypes.byref(b_val)) + except BaseException: + pass + resolved_mv = memoryview(cloned_buf)[offset : offset + val.nbytes] + else: + if val.readonly: + resolved_mv = memoryview(bytes(val)) + else: + resolved_mv = memoryview(bytearray(val)) + val_strides = val.strides + val_shape = val.shape + try: + if val.ndim == 1 and val_shape is not None and val_strides is not None: + shape = val_shape[0] + itemsize = val.itemsize + stride = val_strides[0] + elem_stride = stride // itemsize if itemsize > 0 else 1 + if shape == 0: + casted = cast(Any, resolved_mv).cast(val.format) + resolved_mv = casted[::elem_stride] + elif val.format != "B": + resolved_mv = cast(Any, resolved_mv).cast(val.format) + elif val.ndim == 0: + resolved_mv = cast(Any, resolved_mv).cast(val.format, shape=()) + elif val.format != "B" or (val.ndim > 1 and val.shape != resolved_mv.shape): + resolved_mv = cast(Any, resolved_mv).cast(val.format, shape=val.shape) + except Exception as exc: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) from exc + if val.readonly: + resolved_mv = resolved_mv.toreadonly() + memo[val_id] = resolved_mv + _assign_target(target, key, is_attr, resolved_mv) + elif isinstance(val, datetime.datetime): + if type(val) is not datetime.datetime and _has_mutable_class_attrs(type(val)): + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + if type(val) is not datetime.datetime and "__copy__" in type(val).__dict__: + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + tz = val.tzinfo + copied_tz = tz + if tz is not None and type(tz) is not datetime.timezone: + if _has_mutable_class_attrs(type(tz)): + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(tz).__name__}" + ) + if id(tz) in memo: + copied_tz = memo[id(tz)] + else: + if "__copy__" in type(tz).__dict__: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(tz).__name__}" + ) + try: + copied_tz = datetime.tzinfo.__new__(type(tz)) + except Exception as exc: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(tz).__name__}" + ) from exc + memo[id(tz)] = copied_tz + if hasattr(tz, "__dict__") or hasattr(tz, "__slots__"): + _install_cloned_dict(tz, copied_tz, memo, stack) + stack.append((_OBJ_EXPAND, copied_tz, iter(_get_obj_attrs(tz)))) + + if type(val) is datetime.datetime: + if copied_tz is tz and (tz is None or type(tz) is datetime.timezone): + _assign_target(target, key, is_attr, val) + else: + new_dt = val.replace(tzinfo=copied_tz) + memo[val_id] = new_dt + _assign_target(target, key, is_attr, new_dt) + else: + try: + new_dt = datetime.datetime.__new__( + type(val), + val.year, + val.month, + val.day, + val.hour, + val.minute, + val.second, + val.microsecond, + tzinfo=copied_tz, + fold=val.fold, + ) + except Exception as exc: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) from exc + if new_dt is val or new_dt.tzinfo is not copied_tz: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(val).__name__}" + ) + memo[val_id] = new_dt + _assign_target(target, key, is_attr, new_dt) + if hasattr(val, "__dict__") or hasattr(val, "__slots__"): + _install_cloned_dict(val, new_dt, memo, stack) + stack.append((_OBJ_EXPAND, new_dt, iter(_get_obj_attrs(val)))) + elif isinstance(val, ctypes.Array): + arr_nbytes = ctypes.sizeof(val) + if arr_nbytes > _MAX_SAFE_BUFFER_SIZE: + return None + val_cls = type(val) + val_elem_type = getattr(val_cls, "_type_", None) + val_len = getattr(val_cls, "_length_", None) + if not (isinstance(val_elem_type, type) or hasattr(val_elem_type, "_type_")): + raise TypeError( + f"Unsupported mutable value in review extension data: {val_cls.__name__}" + ) + if _is_pointer_bearing_ctypes_type(val_cls): + raise TypeError( + f"Unsupported mutable value in review extension data: {val_cls.__name__}" + ) + if not isinstance(val_len, int) or val_len < 0: + raise TypeError( + f"Unsupported mutable value in review extension data: {val_cls.__name__}" + ) + if _has_mutable_class_attrs(val_cls): + raise TypeError( + f"Unsupported mutable value in review extension data: {val_cls.__name__}" + ) + new_arr: Any + try: + new_arr = type(val).from_buffer_copy(val) + except Exception as exc: + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) from exc + memo[val_id] = new_arr + _assign_target(target, key, is_attr, new_arr) + if hasattr(val, "__dict__") or hasattr(val, "__slots__"): + _install_cloned_dict(val, new_arr, memo, stack) + stack.append((_OBJ_EXPAND, new_arr, iter(_get_obj_attrs(val)))) + elif hasattr(val, "__dict__") or hasattr(val, "__slots__"): + if _has_mutable_class_attrs(type(val)): + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + new_obj: Any + try: + if isinstance(val, str): + new_obj = str.__new__(type(val), val) + elif isinstance(val, bytes): + mv_bytes = memoryview(val) + if mv_bytes.nbytes > _MAX_SAFE_BUFFER_SIZE: + return None + raw_bytes = bytes(mv_bytes) + new_obj = bytes.__new__(type(val), raw_bytes) + elif isinstance(val, int): + new_obj = int.__new__(type(val), val) + elif isinstance(val, float): + new_obj = float.__new__(type(val), val) + else: + new_obj = object.__new__(type(val)) + except Exception as exc: + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) from exc + memo[val_id] = new_obj + _assign_target(target, key, is_attr, new_obj) + _install_cloned_dict(val, new_obj, memo, stack) + stack.append((_OBJ_EXPAND, new_obj, iter(_get_obj_attrs(val)))) + else: + raise TypeError( + f"Unsupported mutable value in review extension data: {type(val).__name__}" + ) + + elif task_type == _DICT_EXPAND: + target_dict, it = target, it_or_key + for _ in range(_EXPAND_BATCH_SIZE): + try: + k, v = next(it) + except StopIteration: + break + if (type(k) is bytes and len(k) > _MAX_SAFE_BUFFER_SIZE) or ( + type(v) is bytes and len(v) > _MAX_SAFE_BUFFER_SIZE + ): + return None + deferred_dict_insertions.append((target_dict, k, v)) + k_needed = not _is_immutable(k) and id(k) not in memo + v_needed = not _is_immutable(v) and id(v) not in memo + if k_needed or v_needed: + stack.append((_DICT_EXPAND, target_dict, it)) + if v_needed: + stack.append((_ITEM_TASK, None, 0, False, v)) + if k_needed: + stack.append((_ITEM_TASK, None, 0, False, k)) + break + else: + stack.append((_DICT_EXPAND, target_dict, it)) + + elif task_type == _SET_EXPAND: + it = it_or_key + for _ in range(_EXPAND_BATCH_SIZE): + try: + m = next(it) + except StopIteration: + break + if type(m) is bytes and len(m) > _MAX_SAFE_BUFFER_SIZE: + return None + if not _is_immutable(m) and id(m) not in memo: + stack.append((_SET_EXPAND, None, it)) + stack.append((_ITEM_TASK, None, 0, False, m)) + break + else: + stack.append((_SET_EXPAND, None, it)) + + elif task_type == _LIST_EXPAND: + target_list, it = target, it_or_key + is_dq = isinstance(target_list, deque) + app_fn = deque.append if is_dq else list.append + len_fn = deque.__len__ if is_dq else list.__len__ + for _ in range(_EXPAND_BATCH_SIZE): + try: + v = next(it) + except StopIteration: + break + if type(v) is bytes and len(v) > _MAX_SAFE_BUFFER_SIZE: + return None + if _is_immutable(v): + app_fn(target_list, v) + elif id(v) in memo: + app_fn(target_list, memo[id(v)]) + else: + idx = len_fn(target_list) + app_fn(target_list, None) + stack.append((_LIST_EXPAND, target_list, it)) + stack.append((_ITEM_TASK, target_list, idx, False, v)) + break + else: + stack.append((_LIST_EXPAND, target_list, it)) + + elif task_type == _OBJ_EXPAND: + new_obj, it = target, it_or_key + for _ in range(_EXPAND_BATCH_SIZE): + try: + slot_or_name, is_desc, attr_val = next(it) + except StopIteration: + break + if type(attr_val) is bytes and len(attr_val) > _MAX_SAFE_BUFFER_SIZE: + return None + if _is_immutable(attr_val): + _assign_target(new_obj, (slot_or_name, is_desc), True, attr_val) + elif id(attr_val) in memo: + _assign_target(new_obj, (slot_or_name, is_desc), True, memo[id(attr_val)]) + else: + stack.append((_OBJ_EXPAND, new_obj, it)) + stack.append((_ITEM_TASK, new_obj, (slot_or_name, is_desc), True, attr_val)) + break + else: + stack.append((_OBJ_EXPAND, new_obj, it)) + + elif task_type == _TUPLE_EXPAND: + temp_list, it = target, it_or_key + for _ in range(_EXPAND_BATCH_SIZE): + try: + v = next(it) + except StopIteration: + break + if type(v) is bytes and len(v) > _MAX_SAFE_BUFFER_SIZE: + return None + if _is_immutable(v): + temp_list.append(v) + elif id(v) in memo: + temp_list.append(memo[id(v)]) + else: + idx = len(temp_list) + temp_list.append(_UNRESOLVED_SLOT) + stack.append((_TUPLE_EXPAND, temp_list, it)) + stack.append((_ITEM_TASK, temp_list, idx, False, v)) + break + else: + stack.append((_TUPLE_EXPAND, temp_list, it)) + + elif task_type == _TUPLE_FINALIZE: + key, is_attr, val_id, orig_val, temp_list = ( + it_or_key, + extra[0], + extra[1], + extra[2], + extra[3], + ) + if any(x is _UNRESOLVED_SLOT for x in temp_list): + deferred_tuple_finalizations.append( + (target, key, is_attr, val_id, orig_val, temp_list) + ) + if hasattr(orig_val, "__dict__"): + _install_cloned_dict(orig_val, None, memo, stack) + if hasattr(orig_val, "__dict__") or hasattr(orig_val, "__slots__"): + for slot_or_name, is_desc, attr_val in _get_obj_attrs(orig_val): + if not _is_immutable(attr_val) and id(attr_val) not in memo: + stack.append((_ITEM_TASK, None, None, False, attr_val)) + continue + if val_id in memo: + res_tuple = memo[val_id] + elif type(orig_val) is tuple and all(a is b for a, b in zip(orig_val, temp_list)): + res_tuple = orig_val + memo[val_id] = res_tuple + else: + if type(orig_val) is tuple: + res_tuple = tuple(temp_list) + else: + if _has_mutable_class_attrs(type(orig_val)): + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(orig_val).__name__}" + ) + try: + orig_cls: Any = type(orig_val) + res_tuple = tuple.__new__(orig_cls, temp_list) + except Exception: + return None + memo[val_id] = res_tuple + if hasattr(orig_val, "__dict__") or hasattr(orig_val, "__slots__"): + _install_cloned_dict(orig_val, res_tuple, memo, stack) + stack.append((_OBJ_EXPAND, res_tuple, iter(_get_obj_attrs(orig_val)))) + _assign_target(target, key, is_attr, res_tuple) + + if _deadline_expired(): + return None + + dict_ordered_entries: dict[int, list[tuple[Any, Any]]] = {} + dict_targets: dict[int, Any] = {} + dict_next_index: dict[int, int] = {} + + for count, (target_dict, orig_k, orig_v) in enumerate(deferred_dict_insertions): + if (count & 63) == 0 and _deadline_expired(): + return None + td_id = id(target_dict) + if td_id not in dict_ordered_entries: + dict_ordered_entries[td_id] = [] + dict_targets[td_id] = target_dict + dict_next_index[td_id] = 0 + dict_ordered_entries[td_id].append((orig_k, orig_v)) + + pending_frozensets: list[Any] = deferred_frozenset_insertions + + def _is_member_state_populated(m_clone: Any, current_target_id: int | None = None) -> bool: + if _deadline_expired(): + raise _DeadlineAborted() + if _is_immutable(m_clone): + return True + if _is_identity_hashed(m_clone): + return True + visited: set[int] = set() + root_in_custom = not _is_identity_hashed(m_clone) and type(m_clone) not in ( + tuple, + frozenset, + ) + stack_nodes: list[tuple[Any, bool]] = [(m_clone, root_in_custom)] + container_ids: set[int] = set() + while stack_nodes: + if _deadline_expired(): + raise _DeadlineAborted() + cur, in_custom = stack_nodes.pop() + if _is_immutable(cur): + continue + cid = id(cur) + if cid in visited: + continue + visited.add(cid) + container_ids.add(cid) + if ( + not in_custom + and not _is_identity_hashed(cur) + and type(cur) + not in ( + tuple, + frozenset, + ) + ): + in_custom = True + if in_custom: + if hasattr(cur, "__dict__") and isinstance(cur.__dict__, dict): + stack_nodes.append((cur.__dict__, True)) + if hasattr(cur, "__slots__") or hasattr(cur, "__dict__"): + for _, _, slot_val in _get_obj_attrs(cur): + if _deadline_expired(): + raise _DeadlineAborted() + if not _is_immutable(slot_val): + stack_nodes.append((slot_val, True)) + if isinstance(cur, dict): + for k, v in cur.items(): + if _deadline_expired(): + raise _DeadlineAborted() + if not _is_immutable(k): + stack_nodes.append((k, True)) + if not _is_immutable(v): + stack_nodes.append((v, True)) + elif isinstance(cur, tuple): + for item in cur: + if _deadline_expired(): + raise _DeadlineAborted() + if not _is_immutable(item): + stack_nodes.append((item, True)) + elif isinstance(cur, (list, set, frozenset, deque)): + for item in cur: + if _deadline_expired(): + raise _DeadlineAborted() + if not _is_immutable(item): + stack_nodes.append((item, True)) + else: + if isinstance(cur, (tuple, frozenset)): + for item in cur: + if _deadline_expired(): + raise _DeadlineAborted() + if _is_immutable(item): + continue + if _is_identity_hashed(item): + continue + item_is_custom = type(item) not in (tuple, frozenset) + stack_nodes.append((item, item_is_custom)) + + for target, _, _, _, _, _ in deferred_tuple_finalizations: + if _deadline_expired(): + raise _DeadlineAborted() + if id(target) in container_ids: + if id(target) == current_target_id: + try: + hash(m_clone) + _ = m_clone == m_clone + continue + except Exception: + return False + return False + for target, _, _, _, _, _ in pending_frozensets: + if _deadline_expired(): + raise _DeadlineAborted() + if id(target) in container_ids: + if id(target) == current_target_id: + try: + hash(m_clone) + _ = m_clone == m_clone + continue + except Exception: + return False + return False + for s, _, _ in deferred_set_insertions: + if _deadline_expired(): + raise _DeadlineAborted() + if id(s) in container_ids: + if id(s) == current_target_id: + try: + hash(m_clone) + _ = m_clone == m_clone + continue + except Exception: + return False + return False + for td_id, entries in dict_ordered_entries.items(): + if _deadline_expired(): + raise _DeadlineAborted() + if td_id in container_ids: + if td_id == current_target_id: + try: + hash(m_clone) + _ = m_clone == m_clone + continue + except Exception: + return False + if dict_next_index[td_id] < len(entries): + return False + return True + + def _flush_pending_structures() -> None: + nonlocal deferred_tuple_finalizations, deferred_set_insertions, deferred_attr_assignments + changed = True + while changed: + if _deadline_expired(): + return + changed = False + if deferred_tuple_finalizations: + still_tuples = [] + for ( + target, + key, + is_attr, + val_id, + orig_val, + temp_list, + ) in deferred_tuple_finalizations: + elements_ready = not any(x is _UNRESOLVED_SLOT for x in temp_list) + attrs_ready = True + if hasattr(orig_val, "__dict__") or hasattr(orig_val, "__slots__"): + for _, _, attr_val in _get_obj_attrs(orig_val): + if not _is_immutable(attr_val) and id(attr_val) not in memo: + attrs_ready = False + break + if elements_ready and attrs_ready: + if val_id in memo: + res_tuple = memo[val_id] + else: + if type(orig_val) is tuple: + res_tuple = tuple(temp_list) + else: + if _has_mutable_class_attrs(type(orig_val)): + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(orig_val).__name__}" + ) + try: + orig_tuple_cls: Any = type(orig_val) + res_tuple = tuple.__new__(orig_tuple_cls, temp_list) + except Exception: + continue + memo[val_id] = res_tuple + if hasattr(orig_val, "__dict__"): + _install_cloned_dict(orig_val, res_tuple, memo, stack) + if hasattr(orig_val, "__dict__") or hasattr(orig_val, "__slots__"): + for slot_or_name, is_desc, attr_val in _get_obj_attrs(orig_val): + val_to_set = ( + attr_val if _is_immutable(attr_val) else memo[id(attr_val)] + ) + _assign_target(res_tuple, (slot_or_name, is_desc), True, val_to_set) + _assign_target(target, key, is_attr, res_tuple) + changed = True + else: + still_tuples.append((target, key, is_attr, val_id, orig_val, temp_list)) + deferred_tuple_finalizations = still_tuples + + if deferred_set_insertions: + still_sets = [] + for s, orig_val, orig_members in deferred_set_insertions: + if _deadline_expired(): + return + members_ready = True + for m_idx, m in enumerate(orig_members): + if (m_idx & 63) == 0 and _deadline_expired(): + return + if not (_is_immutable(m) or id(m) in memo): + members_ready = False + break + if not members_ready: + still_sets.append((s, orig_val, orig_members)) + continue + resolved_members = [] + for m_idx, m in enumerate(orig_members): + if (m_idx & 63) == 0 and _deadline_expired(): + return + rm = m if _is_immutable(m) else memo[id(m)] + resolved_members.append(rm) + if not _is_member_state_populated(rm, current_target_id=id(s)): + members_ready = False + break + if not members_ready: + still_sets.append((s, orig_val, orig_members)) + continue + for ins_idx in range(0, len(resolved_members), 64): + if ins_idx > 0 and _deadline_expired(): + return + chunk = resolved_members[ins_idx : ins_idx + 64] + set.update(s, chunk) + if _deadline_expired(): + return + if set.__len__(s) != len(orig_members): + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(orig_val).__name__}" + ) + populated_mutable_sets.append((s, resolved_members)) + changed = True + deferred_set_insertions = still_sets + + op_count = 0 + for td_id, entries in dict_ordered_entries.items(): + target_dict = dict_targets[td_id] + idx = dict_next_index[td_id] + while idx < len(entries): + op_count += 1 + if (op_count & 63) == 0 and _deadline_expired(): + return + orig_k, orig_v = entries[idx] + k_ready = _is_immutable(orig_k) or id(orig_k) in memo + v_ready = _is_immutable(orig_v) or id(orig_v) in memo + if k_ready and v_ready: + k = orig_k if _is_immutable(orig_k) else memo[id(orig_k)] + v = orig_v if _is_immutable(orig_v) else memo[id(orig_v)] + if not _is_member_state_populated(k, current_target_id=td_id): + break + target_dict[k] = v + idx += 1 + changed = True + else: + break + dict_next_index[td_id] = idx + + if deferred_attr_assignments: + still_attrs = [] + for res_obj, key_desc, orig_attr in deferred_attr_assignments: + if _is_immutable(orig_attr) or id(orig_attr) in memo: + val_to_set = orig_attr if _is_immutable(orig_attr) else memo[id(orig_attr)] + _assign_target(res_obj, key_desc, True, val_to_set) + changed = True + else: + still_attrs.append((res_obj, key_desc, orig_attr)) + deferred_attr_assignments = still_attrs + + _flush_pending_structures() + if _deadline_expired(): + return None + + pending_frozensets = deferred_frozenset_insertions + while pending_frozensets: + if _deadline_expired(): + return None + progress = False + remaining_frozensets = [] + for target, key, is_attr, val_id, orig_val, orig_members in pending_frozensets: + if val_id in memo: + _assign_target(target, key, is_attr, memo[val_id]) + progress = True + continue + members_ready = True + for m_idx, m in enumerate(orig_members): + if (m_idx & 63) == 0 and _deadline_expired(): + return None + if not (_is_immutable(m) or id(m) in memo): + members_ready = False + break + attrs_ready = True + if hasattr(orig_val, "__dict__") or hasattr(orig_val, "__slots__"): + for _, _, attr_val in _get_obj_attrs(orig_val): + if ( + not _is_immutable(attr_val) + and id(attr_val) not in memo + and id(attr_val) != val_id + ): + attrs_ready = False + break + if members_ready and attrs_ready: + resolved_members = [] + for m_idx, m in enumerate(orig_members): + if (m_idx & 63) == 0 and _deadline_expired(): + return None + rm = m if _is_immutable(m) else memo[id(m)] + resolved_members.append(rm) + if not _is_member_state_populated(rm, current_target_id=id(target)): + members_ready = False + break + if not members_ready: + remaining_frozensets.append( + (target, key, is_attr, val_id, orig_val, orig_members) + ) + continue + if type(orig_val) is frozenset: + res_set = frozenset(resolved_members) + else: + try: + res_set = frozenset.__new__(type(orig_val), resolved_members) + except Exception as exc: + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(orig_val).__name__}" + ) from exc + if frozenset.__len__(res_set) != len(orig_members): + raise TypeError( + "Unsupported mutable value in review extension data: " + f"{type(orig_val).__name__}" + ) + memo[val_id] = res_set + pending_frozenset_ids.discard(val_id) + if hasattr(orig_val, "__dict__"): + _install_cloned_dict(orig_val, res_set, memo, stack) + if hasattr(orig_val, "__dict__") or hasattr(orig_val, "__slots__"): + for slot_or_name, is_desc, attr_val in _get_obj_attrs(orig_val): + if _is_immutable(attr_val): + if type(attr_val) is bytes and len(attr_val) > _MAX_SAFE_BUFFER_SIZE: + return None + _assign_target(res_set, (slot_or_name, is_desc), True, attr_val) + elif id(attr_val) in memo: + _assign_target( + res_set, (slot_or_name, is_desc), True, memo[id(attr_val)] + ) + else: + deferred_attr_assignments.append( + (res_set, (slot_or_name, is_desc), attr_val) + ) + _assign_target(target, key, is_attr, res_set) + for p_target, p_key, p_is_attr in pending_assignments.pop(val_id, []): + _assign_target(p_target, p_key, p_is_attr, res_set) + progress = True + _flush_pending_structures() + else: + remaining_frozensets.append((target, key, is_attr, val_id, orig_val, orig_members)) + + pending_frozensets = remaining_frozensets + + if not progress and pending_frozensets: + resolved_any = False + still_pending = [] + for target, key, is_attr, val_id, orig_val, orig_members in pending_frozensets: + if _deadline_expired(): + raise _DeadlineAborted() + can_resolve = True + resolved_members = [] + for m in orig_members: + if _deadline_expired(): + raise _DeadlineAborted() + if _is_immutable(m): + rm = m + else: + rm = memo.get(id(m), _MISSING) + if rm is _MISSING: + can_resolve = False + break + try: + if _deadline_expired(): + raise _DeadlineAborted() + hash(rm) + _ = rm == rm + except _DeadlineAborted: + raise + except Exception: + can_resolve = False + break + resolved_members.append(rm) + if can_resolve: + if _deadline_expired(): + raise _DeadlineAborted() + if type(orig_val) is frozenset: + res_set = frozenset(resolved_members) + else: + res_set = frozenset.__new__(type(orig_val), resolved_members) + memo[val_id] = res_set + pending_frozenset_ids.discard(val_id) + fallback_built_frozensets.append((res_set, resolved_members)) + if hasattr(orig_val, "__dict__"): + _install_cloned_dict(orig_val, res_set, memo, stack) + if hasattr(orig_val, "__dict__") or hasattr(orig_val, "__slots__"): + for slot_or_name, is_desc, attr_val in _get_obj_attrs(orig_val): + if _is_immutable(attr_val): + if ( + type(attr_val) is bytes + and len(attr_val) > _MAX_SAFE_BUFFER_SIZE + ): + return None + _assign_target(res_set, (slot_or_name, is_desc), True, attr_val) + elif id(attr_val) in memo: + _assign_target( + res_set, (slot_or_name, is_desc), True, memo[id(attr_val)] + ) + else: + deferred_attr_assignments.append( + (res_set, (slot_or_name, is_desc), attr_val) + ) + _assign_target(target, key, is_attr, res_set) + for p_target, p_key, p_is_attr in pending_assignments.pop(val_id, []): + _assign_target(p_target, p_key, p_is_attr, res_set) + resolved_any = True + else: + still_pending.append((target, key, is_attr, val_id, orig_val, orig_members)) + if resolved_any: + pending_frozensets = still_pending + _flush_pending_structures() + continue + if _deadline_expired(): + raise _DeadlineAborted() + raise TypeError("Unsupported cyclic reference in frozenset") + + _flush_pending_structures() + if _deadline_expired(): + return None + for fb_set, fb_members in fallback_built_frozensets: + if _deadline_expired(): + raise _DeadlineAborted() + try: + for rm in fb_members: + if _deadline_expired(): + raise _DeadlineAborted() + if rm not in fb_set: + raise TypeError("Member lookup failed in fallback frozenset") + except TypeError: + raise + except Exception as exc: + raise TypeError("Member lookup failed in fallback frozenset") from exc + if deferred_set_insertions: + raise TypeError("Unsupported cyclic reference in set") + for td_id, entries in dict_ordered_entries.items(): + if dict_next_index[td_id] < len(entries): + raise TypeError("Unsupported cyclic reference in dict") + target_dict = dict_targets[td_id] + for orig_k, _ in entries: + if _deadline_expired(): + return None + k = orig_k if _is_immutable(orig_k) else memo.get(id(orig_k), orig_k) + try: + if k not in target_dict: + return None + except Exception: + return None + for s, members in populated_mutable_sets: + for rm in members: + if _deadline_expired(): + return None + try: + if rm not in s: + return None + except Exception: + return None + if deferred_tuple_finalizations: + raise TypeError("Unsupported cyclic reference in tuple") + if deferred_attr_assignments: + raise TypeError("Unsupported cyclic reference in attributes") + return root + + +_DETACHED_TASKS: weakref.WeakValueDictionary[int, Any] = weakref.WeakValueDictionary() +_DETACHED_FUTURES: dict[int, Any] = {} + + +def _safe_cancel_target(target: Any) -> None: + try: + target.cancel() + except BaseException: + pass + + +def _ensure_loop_close_hooked(loop: Any) -> bool: + hook_installed = getattr(loop, "_proofline_close_hooked", False) + if hook_installed: + return True + try: + orig_loop_close = loop.close + + def _drain_exception(target: Any) -> None: + try: + is_done = target.done() if hasattr(target, "done") else AsyncFuture.done(target) + is_canc = ( + target.cancelled() + if hasattr(target, "cancelled") + else AsyncFuture.cancelled(target) + ) + if is_done and not is_canc: + if hasattr(target, "exception"): + target.exception() + else: + AsyncFuture.exception(target) + except BaseException: + pass + + def _hooked_loop_close(*args: Any, **kwargs: Any) -> Any: + close_res = orig_loop_close(*args, **kwargs) + l_futs = getattr(loop, "_proofline_detached_futures", None) + if l_futs is not None: + for fid, f in list(l_futs.items()): + _drain_exception(f) + _DETACHED_FUTURES.pop(fid, None) + l_futs.clear() + l_tasks = getattr(loop, "_proofline_detached_tasks", None) + if l_tasks is not None: + for tid, t in list(l_tasks.items()): + _drain_exception(t) + _DETACHED_TASKS.pop(tid, None) + l_tasks.clear() + for fid, f in list(_DETACHED_FUTURES.items()): + f_loop = None + try: + f_loop = f.get_loop() if hasattr(f, "get_loop") else AsyncFuture.get_loop(f) + except BaseException: + f_loop = getattr(f, "_loop", None) + if f_loop is loop: + _drain_exception(f) + _DETACHED_FUTURES.pop(fid, None) + for tid, t in list(_DETACHED_TASKS.items()): + t_loop = None + try: + t_loop = t.get_loop() if hasattr(t, "get_loop") else AsyncTask.get_loop(t) + except BaseException: + t_loop = getattr(t, "_loop", None) + if t_loop is loop: + _drain_exception(t) + _DETACHED_TASKS.pop(tid, None) + return close_res + + loop.close = _hooked_loop_close + setattr(loop, "_proofline_close_hooked", True) + return True + except BaseException: + return False + + +def _detach_overdue_future(fut: Any) -> None: + if hasattr(fut, "get_coro"): + _detach_overdue_task(fut) + return + try: + is_done = fut.done() if hasattr(fut, "done") else AsyncFuture.done(fut) + if is_done: + is_canc = fut.cancelled() if hasattr(fut, "cancelled") else AsyncFuture.cancelled(fut) + if not is_canc: + try: + if hasattr(fut, "exception"): + fut.exception() + else: + AsyncFuture.exception(fut) + except Exception: + pass + return + try: + loop = fut.get_loop() if hasattr(fut, "get_loop") else AsyncFuture.get_loop(fut) + except Exception: + return + try: + running_loop = async_get_running_loop() + except Exception: + return + if loop is not running_loop or loop.is_closed(): + return + fut_id = id(fut) + loop_futures = getattr(loop, "_proofline_detached_futures", None) + if loop_futures is None: + loop_futures = {} + try: + setattr(loop, "_proofline_detached_futures", loop_futures) + except Exception: + pass + if loop_futures is not None: + loop_futures[fut_id] = fut + + hook_installed = _ensure_loop_close_hooked(loop) + + if hook_installed: + try: + _DETACHED_FUTURES[fut_id] = fut + except (TypeError, BaseException): + pass + + def _on_detached_fut_done(t: Any) -> None: + _DETACHED_FUTURES.pop(fut_id, None) + if loop is not None: + l_futs = getattr(loop, "_proofline_detached_futures", None) + if l_futs is not None: + l_futs.pop(fut_id, None) + l_futs.pop(id(t), None) + try: + is_canc = t.cancelled() if hasattr(t, "cancelled") else False + if not is_canc: + if hasattr(t, "exception"): + t.exception() + except BaseException: + pass + + registered = False + if hasattr(fut, "add_done_callback"): + try: + fut.add_done_callback(_on_detached_fut_done) + registered = True + except BaseException: + pass + if not registered: + try: + AsyncFuture.add_done_callback(fut, _on_detached_fut_done) + registered = True + except BaseException: + pass + if not registered: + try: + loop = async_get_running_loop() + + async def _observe_fut() -> None: + try: + await fut + except BaseException: + pass + finally: + _DETACHED_FUTURES.pop(fut_id, None) + + obs_fut = loop.create_task(_observe_fut()) + obs_fut.add_done_callback(lambda _: None) + except BaseException: + _DETACHED_FUTURES.pop(fut_id, None) + except BaseException: + pass + + +def _safe_close(obj: Any) -> None: + try: + closer = getattr(obj, "close", None) + if callable(closer): + closer() + except BaseException: + pass + + +def _cleanup_discarded_awaitable(res: Any, waiter_task: Any = None) -> None: + if res is None: + return + if async_isfuture(res): + if waiter_task is None or res is not waiter_task: + _detach_overdue_future(res) + return + if iscoroutine(res): + try: + if getcoroutinestate(res) == CORO_CREATED: + res.close() + except BaseException: + pass + return + if isgenerator(res): + try: + if getgeneratorstate(res) == GEN_CREATED: + res.close() + except BaseException: + pass + return + try: + if getattr(res, "_proofline_cleaned_up", False): + return + setattr(res, "_proofline_cleaned_up", True) + except BaseException: + pass + _safe_close(res) + + +def _detach_overdue_task( + task: Any, + res: Any = None, + wrapper_started: bool = True, +) -> None: + try: + is_done = task.done() if hasattr(task, "done") else False + except BaseException: + is_done = False + + if is_done: + try: + is_canc = task.cancelled() if hasattr(task, "cancelled") else False + if not is_canc and hasattr(task, "exception"): + task.exception() + except BaseException: + pass + if not wrapper_started: + try: + task.cancel() + except BaseException: + pass + _cleanup_discarded_awaitable(res) + return + + try: + running_loop = async_get_running_loop() + except Exception: + running_loop = None + + try: + loop = task.get_loop() if hasattr(task, "get_loop") else running_loop + except Exception: + loop = running_loop + + if running_loop is not None and loop is not running_loop: + return + if loop is None or loop.is_closed(): + return + + try: + loop = task.get_loop() if hasattr(task, "get_loop") else async_get_running_loop() + except Exception: + try: + loop = async_get_running_loop() + except Exception: + loop = None + + task_id = id(task) + hook_installed = False + if loop is not None and not loop.is_closed(): + loop_tasks = getattr(loop, "_proofline_detached_tasks", None) + if loop_tasks is None: + loop_tasks = {} + try: + setattr(loop, "_proofline_detached_tasks", loop_tasks) + except Exception: + pass + if loop_tasks is not None: + loop_tasks[task_id] = task + + hook_installed = _ensure_loop_close_hooked(loop) + + if hook_installed: + try: + _DETACHED_TASKS[task_id] = task + except (TypeError, BaseException): + pass + + def _on_detached_task_done(t: Any) -> None: + _DETACHED_TASKS.pop(task_id, None) + if loop is not None: + loop_tasks = getattr(loop, "_proofline_detached_tasks", None) + if loop_tasks is not None: + loop_tasks.pop(task_id, None) + loop_tasks.pop(id(t), None) + try: + is_canc = t.cancelled() if hasattr(t, "cancelled") else False + if not is_canc: + if hasattr(t, "exception"): + t.exception() + except BaseException: + pass + + registered = False + if hasattr(task, "add_done_callback"): + try: + task.add_done_callback(_on_detached_task_done) + registered = True + except BaseException: + pass + if not registered: + try: + if isinstance(task, AsyncTask): + AsyncTask.add_done_callback(task, _on_detached_task_done) + registered = True + elif isinstance(task, AsyncFuture): + AsyncFuture.add_done_callback(task, _on_detached_task_done) + registered = True + else: + AsyncFuture.add_done_callback(task, _on_detached_task_done) + registered = True + except BaseException: + pass + if not registered: + try: + loop = async_get_running_loop() + + async def _observe_task() -> None: + try: + await task + except BaseException: + pass + finally: + _DETACHED_TASKS.pop(task_id, None) + if loop is not None: + loop_tasks = getattr(loop, "_proofline_detached_tasks", None) + if loop_tasks is not None: + loop_tasks.pop(task_id, None) + + obs = loop.create_task(_observe_task()) + obs.add_done_callback(lambda _: None) + except BaseException: + _DETACHED_TASKS.pop(task_id, None) + + if not wrapper_started: + try: + task.cancel() + except BaseException: + pass + _cleanup_discarded_awaitable(res) + + def _export_next_cursor(page: ExportPage, seen: set[str]) -> str | None: cursor = page.next_cursor if cursor is None: @@ -545,6 +3065,7 @@ def wait_for_review( *, timeout: float = 300, head_commit_sha: str | None = None, + on_poll: Callable[[CustomerReviewDetail], None] | None = None, ) -> ReviewOutcome: """Wait for recorded review completion or return TIMED_OUT. @@ -553,6 +3074,21 @@ def wait_for_review( head_commit_sha holds the outcome to that commit. A review pinned to another head ends the wait on that read and is never COMPLETED. + + When on_poll is provided, it is called on each review poll with the + latest CustomerReviewDetail. Only reviews fetched and validated before + the callback is reached are delivered; deadline expiry can suppress + delivery. Tuples with more than 1,000 items, immutable bytes, mutable buffers + or memoryviews with more than 65,536 bytes (or backed by a buffer larger than 65,536 + bytes), sets, frozensets, or deques with more than 65,536 items in review extension + data skip on_poll snapshot delivery even with time remaining, and snapshot copy errors on + unsupported mutable values propagate before on_poll runs. + on_poll exceptions and return-validation errors propagate even when a + terminal outcome has already been determined and can prevent the waiter + from returning a classified outcome. Synchronous callbacks must return + None (returning an awaitable or any return value other than None raises + TypeError) and must return before a terminal result can be delivered; + the polling deadline cannot interrupt them. """ if not math.isfinite(timeout) or timeout < 0: @@ -560,6 +3096,7 @@ def wait_for_review( _head_commit(head_commit_sha) url = _review_url(self._base_url, organization_id, review_id) last: CustomerReviewDetail | None = None + result: ReviewOutcome | None = None deadline = _transport.DEADLINE.set(monotonic() + timeout) interval = 5.0 try: @@ -567,16 +3104,53 @@ def wait_for_review( try: last = self.get_review(organization_id, review_id) except ProoflineError as error: - if error.status == 0 and (_transport.remaining() or 0) <= 0: + if (_transport.remaining() or 0) <= 0 and ( + error.type == "urn:proofline:sdk:timeout" or error.status == 0 + ): break raise if (_transport.remaining() or 0) <= 0: break result = outcome(last, url, review_id=review_id, head_commit_sha=head_commit_sha) + if on_poll is not None and (_transport.remaining() or 0) > 0: + snapshot = _snapshot_review(last) + if snapshot is not None and (_transport.remaining() or 0) > 0: + deadline_token = _transport.DEADLINE.set(None) + try: + res = on_poll(snapshot) + finally: + _transport.DEADLINE.reset(deadline_token) + try: + res_awaitable = isawaitable(res) + except Exception: + raise TypeError("Callbacks must return None.") from None + if res_awaitable: + if iscoroutine(res): + try: + if getcoroutinestate(res) == CORO_CREATED: + res.close() + except Exception: + pass + elif isgenerator(res): + try: + if getgeneratorstate(res) == GEN_CREATED: + res.close() + except Exception: + pass + raise TypeError( + "on_poll returned an awaitable; use AsyncProoflineClient " + "for async callbacks." + ) + if res is not None: + raise TypeError("Callbacks must return None.") if result is not None: return result + if on_poll is not None and (_transport.remaining() or 0) <= 0: + break sleep(min(interval, _transport.remaining() or 0)) interval = min(60, interval * 2) + if result is not None: + return result timed_out = outcome(last, url, "TIMED_OUT", head_commit_sha=head_commit_sha) return timed_out or ReviewOutcome("TIMED_OUT", url) finally: @@ -940,12 +3514,59 @@ async def wait_for_review( *, timeout: float = 300, head_commit_sha: str | None = None, + on_poll: Callable[[CustomerReviewDetail], Awaitable[object] | None] | None = None, ) -> ReviewOutcome: + """Wait for recorded review completion or return TIMED_OUT. + + A name lookup still running at the deadline returns TIMED_OUT; it never + reports completion. + + head_commit_sha holds the outcome to that commit. A review pinned to + another head ends the wait on that read and is never COMPLETED. + + When on_poll is provided, it must be a callable that accepts the review + snapshot and returns None or an awaitable (supporting both sync and + async callbacks). Any coroutines returned by on_poll must be unstarted, and + any pending futures returned by on_poll must belong to the running event + loop. Callbacks cannot return the running polling task (doing so raises + ValueError). Callbacks must return promptly even when they return + an awaitable, because the polling deadline cannot interrupt the + invocation. Only reviews fetched and validated before the callback + is reached are delivered; deadline expiry can suppress delivery. Tuples + with more than 1,000 items, immutable bytes, mutable buffers or memoryviews + with more than 65,536 bytes (or backed by a buffer larger than 65,536 bytes), + sets, frozensets, or deques with more than 65,536 items in review extension data + skip on_poll snapshot delivery even with time remaining, and snapshot copy errors + on unsupported mutable values propagate before on_poll runs. Callback + return-validation errors and inline exceptions propagate unconditionally, even when a + terminal outcome has already been determined (cancellation requested during callback + invocation takes precedence over return validation), preventing the waiter from + returning a classified outcome. + Failure propagation depends on the awaitable type and the polling deadline: + already-done futures must be returned before the deadline to propagate (failures of + already-done futures returned after expiry are detached and suppressed), coroutine + failures are timed at wrapper completion rather than when the waiter retrieves them + (except for wrappers cancelled before their first step, which are timed when their done + callback runs), and pending future failures propagate only if their done callback + executes before the deadline expires (cutting off at done-callback execution and allowing + any classified outcome to be returned). + This allows any classified outcome to be returned when a callback finishes or returns + after the deadline. When the deadline expires or when the wait is cancelled, unstarted + coroutines returned by on_poll are closed without entering their body (a wrapper task + may already have been scheduled; cancelling the waiter through an underlying pending + future can leave unresumed tasks unflagged until resumption unless observed through the + pending future), while already-running callback tasks and futures are detached without + cancellation and may continue running (detached callback coroutines suppress late + SystemExit and KeyboardInterrupt signals, which do not propagate once detached). + Synchronous callbacks must return before a terminal result can be delivered and the + polling deadline cannot interrupt them. + """ if not math.isfinite(timeout) or timeout < 0: raise ValueError("The review timeout must be a finite nonnegative number of seconds.") _head_commit(head_commit_sha) url = _review_url(self._base_url, organization_id, review_id) last: CustomerReviewDetail | None = None + result: ReviewOutcome | None = None deadline = _transport.DEADLINE.set(monotonic() + timeout) interval = 5.0 try: @@ -953,16 +3574,424 @@ async def wait_for_review( try: last = await self.get_review(organization_id, review_id) except ProoflineError as error: - if error.status == 0 and (_transport.remaining() or 0) <= 0: + if (_transport.remaining() or 0) <= 0 and ( + error.type == "urn:proofline:sdk:timeout" or error.status == 0 + ): break raise if (_transport.remaining() or 0) <= 0: break result = outcome(last, url, review_id=review_id, head_commit_sha=head_commit_sha) + if on_poll is not None and (_transport.remaining() or 0) > 0: + snapshot = _snapshot_review(last) + if snapshot is not None and (_transport.remaining() or 0) > 0: + waiter_task = async_current_task() + initial_cancelling = 0 + if waiter_task is not None: + try: + if hasattr(waiter_task, "cancelling"): + initial_cancelling = waiter_task.cancelling() + except BaseException: + pass + deadline_token = _transport.DEADLINE.set(None) + try: + res = on_poll(snapshot) + cb_return_time = monotonic() + finally: + _transport.DEADLINE.reset(deadline_token) + if waiter_task is not None: + is_task_cancelled = False + try: + if ( + hasattr(waiter_task, "cancelling") + and waiter_task.cancelling() > initial_cancelling + ): + is_task_cancelled = True + if hasattr(waiter_task, "cancelled") and waiter_task.cancelled(): + is_task_cancelled = True + if getattr(waiter_task, "_must_cancel", False): + is_task_cancelled = True + except BaseException: + pass + if is_task_cancelled: + try: + if isawaitable(res): + _cleanup_discarded_awaitable(res, waiter_task=waiter_task) + except Exception: + pass + await async_sleep(0) + raise AsyncCancelledError() + try: + res_awaitable = isawaitable(res) + except Exception: + raise TypeError("Callbacks must return None or an awaitable.") from None + if res is not None and not res_awaitable: + raise TypeError("Callbacks must return None or an awaitable.") + if res_awaitable: + if res is async_current_task(): + raise ValueError( + "Callbacks cannot return the running polling task." + ) + + if iscoroutine(res) and getcoroutinestate(res) != CORO_CREATED: + raise ValueError("Callbacks must return unstarted coroutines.") + if isgenerator(res) and getgeneratorstate(res) != GEN_CREATED: + raise ValueError("Callbacks must return unstarted coroutines.") + + if async_isfuture(res): + try: + is_done = res.done() + except BaseException: + is_done = False + if is_done: + deadline_val = _transport.DEADLINE.get() + cb_returned_in_budget = ( + deadline_val is None or cb_return_time < deadline_val + ) + if cb_returned_in_budget: + if hasattr(res, "result"): + res.result() + else: + AsyncFuture.result(res) + else: + _detach_overdue_future(res) + if result is not None: + return result + break + else: + try: + res_loop = ( + res.get_loop() + if hasattr(res, "get_loop") + else AsyncFuture.get_loop(res) + ) + except Exception: + res_loop = None + running_loop = async_get_running_loop() + if res_loop is not running_loop or ( + res_loop is not None and res_loop.is_closed() + ): + raise ValueError( + "Pending callback futures must belong " + "to the running event loop." + ) + + remaining_budget = _transport.remaining() + if remaining_budget is not None and remaining_budget <= 0: + _detach_overdue_future(res) + if result is not None: + return result + break + + try: + completion_fut = async_get_running_loop().create_future() + except BaseException: + _detach_overdue_future(res) + raise + completed_at: list[float] = [] + + def _on_future_done(_: Any) -> None: + completed_at.append(monotonic()) + if not completion_fut.done(): + completion_fut.set_result(None) + + fut_registered = False + if hasattr(res, "add_done_callback"): + try: + res.add_done_callback(_on_future_done) + fut_registered = True + except BaseException: + pass + if not fut_registered: + try: + AsyncFuture.add_done_callback(res, _on_future_done) + fut_registered = True + except BaseException: + _detach_overdue_future(res) + raise + try: + while not completed_at: + remaining_budget = _transport.remaining() + if ( + remaining_budget is not None + and remaining_budget <= 0 + ): + break + try: + await async_wait( + {completion_fut}, timeout=remaining_budget + ) + except BaseException: + _detach_overdue_future(res) + raise + if completed_at: + break + rem_after = _transport.remaining() + if rem_after is not None and rem_after <= 0: + break + finally: + try: + if hasattr(res, "remove_done_callback"): + res.remove_done_callback(_on_future_done) + else: + AsyncFuture.remove_done_callback( + res, _on_future_done + ) + except BaseException: + pass + deadline_val = _transport.DEADLINE.get() + cb_finished_in_budget = bool(completed_at) and ( + deadline_val is None or completed_at[0] < deadline_val + ) + if cb_finished_in_budget: + if hasattr(res, "result"): + res.result() + else: + AsyncFuture.result(res) + else: + _detach_overdue_future(res) + if result is not None: + return result + break + else: + remaining_budget = _transport.remaining() + if remaining_budget is not None and remaining_budget <= 0: + _cleanup_discarded_awaitable(res) + if result is not None: + return result + break + coro = res + completed_at = [] + wrapper_started = False + captured_deadline = _transport.DEADLINE.get() + wrapper_task: AsyncTask[Any] | None = None + try: + task_completion_fut = async_get_running_loop().create_future() + except BaseException: + _cleanup_discarded_awaitable(res) + raise + + async def _runner() -> Any: + nonlocal wrapper_started + waiter_cancelling = False + if waiter_task is not None: + try: + if hasattr(waiter_task, "cancelling"): + waiter_cancelling = ( + waiter_task.cancelling() > initial_cancelling + ) + elif hasattr(waiter_task, "cancelled"): + waiter_cancelling = waiter_task.cancelled() + if getattr(waiter_task, "_must_cancel", False): + waiter_cancelling = True + fw = getattr(waiter_task, "_fut_waiter", None) + while fw is not None: + try: + if ( + hasattr(fw, "cancelling") + and fw.cancelling() > 0 + ): + waiter_cancelling = True + break + if hasattr(fw, "cancelled") and fw.cancelled(): + waiter_cancelling = True + break + if getattr(fw, "_must_cancel", False): + waiter_cancelling = True + break + fw = getattr(fw, "_fut_waiter", None) + except BaseException: + break + except BaseException: + pass + curr_task = wrapper_task or async_current_task() + try: + if curr_task is not None: + if getattr(curr_task, "_must_cancel", False): + waiter_cancelling = True + elif ( + hasattr(curr_task, "cancelling") + and curr_task.cancelling() > 0 + ): + waiter_cancelling = True + elif ( + hasattr(curr_task, "cancelled") + and curr_task.cancelled() + ): + waiter_cancelling = True + except BaseException: + pass + try: + if task_completion_fut.cancelled(): + waiter_cancelling = True + except BaseException: + pass + + deadline_expired = ( + captured_deadline is not None + and monotonic() >= captured_deadline + ) + if waiter_cancelling or deadline_expired: + _cleanup_discarded_awaitable(coro) + return None + wrapper_started = True + _transport.DEADLINE.set(None) + handback_exc: BaseException | None = None + try: + assert coro is not None + await coro + except (SystemExit, KeyboardInterrupt) as exc: + handback_exc = exc + finally: + completed_at.append(monotonic()) + return handback_exc + + runner_coro = _runner() + deadline_token_task = _transport.DEADLINE.set(None) + try: + task = async_create_task(runner_coro) + wrapper_task = task + except BaseException: + if ( + iscoroutine(runner_coro) + and getcoroutinestate(runner_coro) == CORO_CREATED + ): + try: + runner_coro.close() + except BaseException: + pass + _cleanup_discarded_awaitable(res) + raise + finally: + _transport.DEADLINE.reset(deadline_token_task) + + def _on_task_done(t: Any) -> None: + if not completed_at: + try: + is_canc = ( + t.cancelled() if hasattr(t, "cancelled") else False + ) + if is_canc: + completed_at.append(monotonic()) + except BaseException: + pass + if not task_completion_fut.done(): + task_completion_fut.set_result(None) + + cb_registered = False + if hasattr(task, "add_done_callback"): + try: + task.add_done_callback(_on_task_done) + cb_registered = True + except BaseException: + pass + if not cb_registered: + try: + if isinstance(task, AsyncTask): + AsyncTask.add_done_callback(task, _on_task_done) + cb_registered = True + elif isinstance(task, AsyncFuture): + AsyncFuture.add_done_callback(task, _on_task_done) + cb_registered = True + else: + AsyncFuture.add_done_callback(task, _on_task_done) + cb_registered = True + except BaseException: + _detach_overdue_task(task, res, wrapper_started) + raise + + done_wait: set[Any] = set() + try: + deadline_val = _transport.DEADLINE.get() + cb_finished_in_budget = bool(completed_at) and ( + deadline_val is None or completed_at[0] < deadline_val + ) + try: + task_is_done = ( + task.done() if hasattr(task, "done") else False + ) + except BaseException: + _detach_overdue_task(task, res, wrapper_started) + raise + + if task_is_done and cb_finished_in_budget: + if not wrapper_started: + try: + task.cancel() + except BaseException: + pass + _cleanup_discarded_awaitable(res) + if hasattr(task, "result"): + res_val = task.result() + else: + res_val = AsyncTask.result(task) + if isinstance(res_val, (SystemExit, KeyboardInterrupt)): + raise res_val + + while True: + remaining_budget = _transport.remaining() + if remaining_budget is not None and remaining_budget <= 0: + break + try: + done_wait, _ = await async_wait( + {task_completion_fut}, timeout=remaining_budget + ) + except BaseException: + _detach_overdue_task(task, res, wrapper_started) + raise + if task_completion_fut in done_wait: + break + rem_after = _transport.remaining() + if rem_after is not None and rem_after <= 0: + break + finally: + try: + if hasattr(task, "remove_done_callback"): + task.remove_done_callback(_on_task_done) + elif isinstance(task, AsyncTask): + AsyncTask.remove_done_callback(task, _on_task_done) + else: + AsyncFuture.remove_done_callback(task, _on_task_done) + except BaseException: + pass + deadline_val = _transport.DEADLINE.get() + cb_finished_in_budget = bool(completed_at) and ( + deadline_val is None or completed_at[0] < deadline_val + ) + try: + task_is_done = task.done() if hasattr(task, "done") else False + except BaseException: + _detach_overdue_task(task, res, wrapper_started) + raise + + if cb_finished_in_budget and ( + task_completion_fut in done_wait or task_is_done + ): + if not wrapper_started: + try: + task.cancel() + except BaseException: + pass + _cleanup_discarded_awaitable(res) + if hasattr(task, "result"): + res_val = task.result() + else: + res_val = AsyncTask.result(task) + if isinstance(res_val, (SystemExit, KeyboardInterrupt)): + raise res_val + else: + _detach_overdue_task(task, res, wrapper_started) + if result is not None: + return result + break if result is not None: return result + if on_poll is not None and (_transport.remaining() or 0) <= 0: + break await async_sleep(min(interval, _transport.remaining() or 0)) interval = min(60, interval * 2) + if result is not None: + return result timed_out = outcome(last, url, "TIMED_OUT", head_commit_sha=head_commit_sha) return timed_out or ReviewOutcome("TIMED_OUT", url) finally: diff --git a/tests/test_http.py b/tests/test_http.py index 0af9d60f..382fa582 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1,5 +1,6 @@ from __future__ import annotations +import array import asyncio import csv import gzip @@ -14,6 +15,7 @@ import time import tomllib import traceback +import weakref from collections.abc import AsyncIterator from contextlib import asynccontextmanager from datetime import UTC, datetime, timedelta @@ -1294,19 +1296,201 @@ async def test_wait_ends_on_the_read_that_names_another_head( assert (result.status, result.gaps, result.reason) == ("COMPLETED", (), None) assert len(server.requests) == 3 assert clock.delays == [5, 10] + # Mismatch without on_poll ends on the read that names another head (2 requests) with Server(lambda request, index: Reply(body=responses[index])) as server: async with session(mode, server.url) as client: result = await call( - client, "wait_for_review", ORG, REVIEW, timeout=100, head_commit_sha=OTHER_HEAD + client, + "wait_for_review", + ORG, + REVIEW, + timeout=100, + head_commit_sha=OTHER_HEAD, ) assert result.status == "INCOMPLETE" assert result.review.status == "RUNNING" + assert result.review.head_commit_sha == HEAD assert gap_facts(result) == [PINNED_ELSEWHERE] assert result.reason == "The review is pinned to another head commit." - # The second read names the head, so the review that would complete is never read. assert len(server.requests) == 2 assert clock.delays == [5, 10, 5] + # Mismatch with on_poll also ends on the read that names another head (2 requests) + with Server(lambda request, index: Reply(body=responses[index])) as server: + polled_reviews: list[tuple[Any, int]] = [] + captured_mismatch_fields: list[tuple[str, Any, int]] = [] + if mode == "async": + first_cb_event = asyncio.Event() + second_cb_reached = asyncio.Event() + cb_completed = False + waiter_task: asyncio.Task[Any] | None = None + + class CallbackSignalFuture(asyncio.Future[None]): + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.signal_registered = asyncio.Event() + + def add_done_callback(self, fn: Any, *args: Any, **kwargs: Any) -> None: + super().add_done_callback(fn, *args, **kwargs) + self.signal_registered.set() + + loop = asyncio.get_running_loop() + cb_signal_fut = CallbackSignalFuture(loop=loop) + + def async_mismatch_cb(r: Any) -> Any: + captured_mismatch_fields.append((r.status, r.head_commit_sha, len(server.requests))) + polled_reviews.append((r, len(server.requests))) + r.status = "MUTATED" + if len(polled_reviews) == 2: + r.head_commit_sha = OTHER_HEAD + assert waiter_task is not None + assert not waiter_task.done() + second_cb_reached.set() + return cb_signal_fut + else: + r.head_commit_sha = "mutated_sha" + first_cb_event.set() + return None + + cb: Any = async_mismatch_cb + else: + + def sync_mismatch_cb(r: Any) -> None: + captured_mismatch_fields.append((r.status, r.head_commit_sha, len(server.requests))) + polled_reviews.append((r, len(server.requests))) + r.status = "MUTATED" + if len(polled_reviews) == 2: + r.head_commit_sha = OTHER_HEAD + else: + r.head_commit_sha = "mutated_sha" + + cb = sync_mismatch_cb + async with session(mode, server.url) as client: + if mode == "async": + waiter_task = asyncio.create_task( + client.wait_for_review( + ORG, + REVIEW, + timeout=100, + head_commit_sha=OTHER_HEAD, + on_poll=cb, + ) + ) + try: + first_waiter = asyncio.create_task(first_cb_event.wait()) + try: + done_first, _ = await asyncio.wait( + {first_waiter, waiter_task}, + timeout=5.0, + return_when=asyncio.FIRST_COMPLETED, + ) + if waiter_task in done_first: + await waiter_task + assert first_cb_event.is_set() + finally: + if not first_waiter.done(): + first_waiter.cancel() + + signal_waiter = asyncio.create_task(cb_signal_fut.signal_registered.wait()) + try: + done_sig, _ = await asyncio.wait( + {signal_waiter, waiter_task}, + timeout=5.0, + return_when=asyncio.FIRST_COMPLETED, + ) + if waiter_task in done_sig: + await waiter_task + assert cb_signal_fut.signal_registered.is_set(), ( + "waiter_task did not wait on callback completion signal" + ) + finally: + if not signal_waiter.done(): + signal_waiter.cancel() + + # Assert that the waiter's pending wait is tied to the callback + # completion future before resolving it: + registered_cbs = [ + cb_item[0] if isinstance(cb_item, tuple) else cb_item + for cb_item in (getattr(cb_signal_fut, "_callbacks", None) or []) + ] + done_cbs = [ + cb_item + for cb_item in registered_cbs + if getattr(cb_item, "__name__", "") == "_on_future_done" + ] + assert done_cbs, "Callback completion signal _on_future_done not found" + target_cb = done_cbs[0] + closure_vars = { + name: cell.cell_contents + for name, cell in zip( + target_cb.__code__.co_freevars, target_cb.__closure__ or () + ) + } + comp_fut = closure_vars.get("completion_fut") + assert comp_fut is not None, "completion_fut missing from callback closure" + assert not comp_fut.done(), "completion_fut already settled" + on_comp_cbs = [ + c[0] if isinstance(c, tuple) else c + for c in (getattr(comp_fut, "_callbacks", None) or []) + ] + assert on_comp_cbs, "No completion waiter registered on completion_fut" + on_comp_closure = { + name: cell.cell_contents + for name, cell in zip( + on_comp_cbs[0].__code__.co_freevars, + on_comp_cbs[0].__closure__ or (), + ) + } + comp_waiter = on_comp_closure.get("waiter") + assert getattr(waiter_task, "_fut_waiter", None) is comp_waiter, ( + "waiter_task is not awaiting callback completion_fut" + ) + assert comp_waiter is not None and not comp_waiter.done() + assert not cb_completed + assert not waiter_task.done() + + cb_completed = True + cb_signal_fut.set_result(None) + done_w, _ = await asyncio.wait({waiter_task}, timeout=5.0) + if waiter_task not in done_w: + pytest.fail("waiter_task did not complete in 5.0s") + result = waiter_task.result() + assert cb_completed + finally: + if not cb_signal_fut.done(): + cb_signal_fut.set_result(None) + if waiter_task is not None and not waiter_task.done(): + waiter_task.cancel() + if waiter_task is not None: + await asyncio.wait({waiter_task}, timeout=1.0) + else: + result = await call( + client, + "wait_for_review", + ORG, + REVIEW, + timeout=100, + head_commit_sha=OTHER_HEAD, + on_poll=cb, + ) + assert captured_mismatch_fields == [ + ("PENDING", UNSET, 1), + ("RUNNING", HEAD, 2), + ] + assert [p[0].status for p in polled_reviews] == ["MUTATED", "MUTATED"] + assert [p[1] for p in polled_reviews] == [1, 2] + assert polled_reviews[0][0].head_commit_sha == "mutated_sha" + assert polled_reviews[1][0].head_commit_sha == OTHER_HEAD + assert result.status == "INCOMPLETE" + assert result.review.status == "RUNNING" + assert result.review.head_commit_sha == HEAD + assert result.review is not polled_reviews[-1][0] + assert gap_facts(result) == [PINNED_ELSEWHERE] + assert result.reason == "The review is pinned to another head commit." + # The second read names the head, so the review that would complete is never read. + assert len(server.requests) == 2 + assert clock.delays == [5, 10, 5, 5] + @MODES async def test_a_review_of_another_head_is_incomplete_whatever_its_own_status( @@ -2586,3 +2770,3445 @@ def respond(request: Request, index: int) -> Reply: assert len(exported.rows) == 1 assert exported.rows[0]["claim"] == row["claim"] assert exported.next_cursor == "next-page" + + +@MODES +async def test_wait_for_review_invokes_on_poll_callback( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + clock = Clock(monkeypatch) + calls: list[tuple[str, int]] = [] + responses = [review("RUNNING"), review()] + + with Server(lambda request, index: Reply(body=responses[index])) as server: + + def sync_poll_callback(review_detail: Any) -> None: + calls.append((f"sync:{review_detail.status}", len(server.requests))) + if review_detail.status == "RUNNING": + assert review_detail.completed_at is UNSET + + async def async_poll_callback(review_detail: Any) -> None: + calls.append((f"async:{review_detail.status}", len(server.requests))) + if review_detail.status == "RUNNING": + assert review_detail.completed_at is UNSET + review_detail.status = "MUTATED" + + async with session(mode, server.url) as client: + cb = async_poll_callback if mode == "async" else sync_poll_callback + result = await call( + client, + "wait_for_review", + ORG, + REVIEW, + timeout=100, + on_poll=cb, + ) + assert (result.status, result.gaps, result.reason) == ("COMPLETED", (), None) + prefix = "async" if mode == "async" else "sync" + assert calls == [(f"{prefix}:RUNNING", 1), (f"{prefix}:COMPLETED", 2)] + assert result.review.status == "COMPLETED" + assert len(server.requests) == 2 + assert clock.delays == [5] + + # Assert async client also works with regular synchronous callbacks returning None + if mode == "async": + async_nested_responses = [dict(responses[0]), dict(responses[1])] + for resp in async_nested_responses: + resp["nested_meta"] = {"key": "original", "tags": ["a", "b"]} + with Server(lambda request, index: Reply(body=async_nested_responses[index])) as server: + sync_calls: list[tuple[str, int]] = [] + + def inline_sync_cb(r: Any) -> None: + sync_calls.append((r.status, len(server.requests))) + r.status = "MUTATED" + r.actions.publish = True + r.additional_properties["mutated"] = "mutated_value" + assert len(r.assessments) > 0 + assert r.assessments[0].checks.receipt_recorded is True + r.assessments[0].checks.receipt_recorded = False + r.additional_properties["nested_meta"]["key"] = "mutated" + r.additional_properties["nested_meta"]["tags"].append("c") + + async with session("async", server.url) as client: + result = await call( + client, + "wait_for_review", + ORG, + REVIEW, + timeout=100, + on_poll=inline_sync_cb, + ) + assert result.status == "COMPLETED" + assert sync_calls == [("RUNNING", 1), ("COMPLETED", 2)] + assert result.review.status == "COMPLETED" + assert result.review.actions.publish is False + assert "mutated" not in result.review.additional_properties + assert result.review.assessments[0].checks.receipt_recorded is True + assert result.review.additional_properties["nested_meta"]["key"] == "original" + assert result.review.additional_properties["nested_meta"]["tags"] == ["a", "b"] + + # Assert callback TimeoutError before deadline propagates + async def failing_callback(r: Any) -> None: + raise TimeoutError("callback failure") + + with Server(lambda request, index: Reply(body=review("RUNNING"))) as server: + async with session("async", server.url) as client: + with pytest.raises(TimeoutError, match="callback failure"): + await client.wait_for_review( + ORG, + REVIEW, + timeout=10, + on_poll=failing_callback, + ) + + # Assert already-done failed future propagates inline + failed_inline_futs: list[asyncio.Future[None]] = [] + call_start_inline = clock.now + + class ClockAdvancingDoneFuture(asyncio.Future[None]): + def done(self) -> bool: + clock.now = call_start_inline + 10.0 + return super().done() + + def failed_future_callback(r: Any) -> asyncio.Future[None]: + loop = asyncio.get_running_loop() + fut = ClockAdvancingDoneFuture(loop=loop) + fut.set_exception(ValueError("inline future error")) + failed_inline_futs.append(fut) + return fut + + try: + with Server(lambda request, index: Reply(body=review("RUNNING"))) as server: + async with session("async", server.url) as client: + with pytest.raises(ValueError, match="inline future error"): + await client.wait_for_review( + ORG, + REVIEW, + timeout=10, + on_poll=failed_future_callback, + ) + finally: + for ff in failed_inline_futs: + try: + if not ff.cancelled(): + ff.exception() + except BaseException: + pass + + # Assert already-failed future returned at exact fake-clock deadline preserves COMPLETED + failed_at_deadline_futs: list[asyncio.Future[None]] = [] + call_start = clock.now + + def failed_future_at_deadline_callback(r: Any) -> asyncio.Future[None]: + clock.now = call_start + 10.0 + loop = asyncio.get_running_loop() + fut: asyncio.Future[None] = loop.create_future() + fut.set_exception(ValueError("already-failed future exactly at deadline")) + failed_at_deadline_futs.append(fut) + return fut + + try: + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("async", server.url) as client: + outcome_at_dl = await client.wait_for_review( + ORG, + REVIEW, + timeout=10, + on_poll=failed_future_at_deadline_callback, + ) + assert outcome_at_dl.status == "COMPLETED" + finally: + for ff in failed_at_deadline_futs: + try: + if not ff.cancelled(): + ff.exception() + except BaseException: + pass + + # Run the watchdog cases in a subprocess with a hard timeout so cancellation-resistant + # tasks cannot stall teardown. + watchdog_code = """ +import sys +import asyncio +import pytest +import time +import weakref +from typing import Any +from proofline import AsyncProoflineClient +import proofline.client as client_module +import proofline._transport as transport_module +from proofline._generated.models.customer_review_detail import CustomerReviewDetail +from tests.test_http import review, ORG, REVIEW, Server, Reply + +class ControlledClock: + def __init__(self) -> None: + self.now = 1000.0 + + def time(self) -> float: + return self.now + + def advance(self, dt: float) -> None: + self.now += dt + +async def main() -> None: + clock = ControlledClock() + client_module.monotonic = clock.time + transport_module.monotonic = clock.time + + sleep_forbidden = False + orig_async_sleep = client_module.async_sleep + + async def guarded_async_sleep(delay: float) -> None: + if sleep_forbidden: + raise AssertionError( + "Ordinary polling sleep occurred before watchdog advanced callback deadline" + ) + await orig_async_sleep(delay) + + client_module.async_sleep = guarded_async_sleep + + running_detail = CustomerReviewDetail.from_dict(review("RUNNING")) + completed_detail = CustomerReviewDetail.from_dict(review()) + + async def fake_running(org: Any, rev: Any) -> Any: + return running_detail + + async def fake_completed(org: Any, rev: Any) -> Any: + return completed_detail + + async with AsyncProoflineClient( + token="test_token", base_url="https://api.proofline.sh" + ) as client: + async_wait_calls_1 = 0 + orig_async_wait_1 = client_module.async_wait + + async def counting_async_wait_1(*args: Any, **kwargs: Any) -> Any: + nonlocal async_wait_calls_1 + async_wait_calls_1 += 1 + return await orig_async_wait_1(*args, **kwargs) + + client_module.async_wait = counting_async_wait_1 + + # Case 1: Pending same-loop Future that times out is detached without cancelling + # Assert it is still pending before advancing the controlled deadline + class SignallingFuture(asyncio.Future[None]): + def __init__(self, loop: asyncio.AbstractEventLoop) -> None: + super().__init__(loop=loop) + self.wait_entered = asyncio.Event() + self.active_callbacks: list[Any] = [] + self.removed_callbacks: list[Any] = [] + + def add_done_callback(self, fn: Any, *, context: Any = None) -> None: + super().add_done_callback(fn, context=context) + self.active_callbacks.append(fn) + if "_on_future_done" in getattr(fn, "__name__", ""): + clock.advance(1.0) + self.wait_entered.set() + + def remove_done_callback(self, fn: Any) -> int: + count = super().remove_done_callback(fn) + if count > 0: + self.removed_callbacks.append(fn) + self.active_callbacks = [cb for cb in self.active_callbacks if cb != fn] + return count + + pending_fut: SignallingFuture | None = None + cb_1_started = asyncio.Event() + + def pending_future_callback(r: Any) -> SignallingFuture: + nonlocal pending_fut + loop = asyncio.get_running_loop() + pending_fut = SignallingFuture(loop=loop) + cb_1_started.set() + return pending_fut + + client.get_review = fake_running # type: ignore + + waiter_task_1 = asyncio.create_task( + client.wait_for_review( + ORG, + REVIEW, + timeout=0.2, + on_poll=pending_future_callback, + ) + ) + startup_waiter_1 = asyncio.create_task(cb_1_started.wait()) + try: + done_1, _ = await asyncio.wait( + {startup_waiter_1, waiter_task_1}, + return_when=asyncio.FIRST_COMPLETED, + ) + if waiter_task_1 in done_1: + await waiter_task_1 + assert pending_fut is not None + done_waiter_1, _ = await asyncio.wait({waiter_task_1}, timeout=1.0) + assert waiter_task_1 in done_waiter_1 + res_1 = waiter_task_1.result() + assert res_1.status == "TIMED_OUT" + assert not pending_fut.cancelled() + assert not pending_fut.done() + assert async_wait_calls_1 == 0 + assert id(pending_fut) in client_module._DETACHED_FUTURES + assert len(pending_fut.removed_callbacks) > 0 + assert any( + "_on_future_done" in getattr(cb, "__name__", "") + for cb in pending_fut.removed_callbacks + ) + assert not any( + "_on_future_done" in getattr(cb, "__name__", "") + for cb in pending_fut.active_callbacks + ) + pending_fut.set_result(None) + await asyncio.sleep(0) + assert id(pending_fut) not in client_module._DETACHED_FUTURES + finally: + client_module.async_wait = orig_async_wait_1 + if pending_fut is not None and not pending_fut.done(): + pending_fut.set_result(None) + if not startup_waiter_1.done(): + startup_waiter_1.cancel() + try: + await startup_waiter_1 + except (asyncio.CancelledError, BaseException): + pass + if not waiter_task_1.done(): + waiter_task_1.cancel() + try: + done_clean_1, _ = await asyncio.wait({waiter_task_1}, timeout=1.0) + except BaseException: + pass + + # Case 2: Pending same-loop Future that completes within budget without cancellation + completed_fut = asyncio.get_running_loop().create_future() + complete_fut_task: asyncio.Task[None] | None = None + + async def complete_fut_soon() -> None: + await asyncio.sleep(0.01) + completed_fut.set_result(None) + + def completing_future_callback(r: Any) -> asyncio.Future[None]: + nonlocal complete_fut_task + complete_fut_task = asyncio.create_task(complete_fut_soon()) + return completed_fut + + client.get_review = fake_completed # type: ignore + + try: + res = await client.wait_for_review( + ORG, + REVIEW, + timeout=10.0, + on_poll=completing_future_callback, + ) + assert res.status == "COMPLETED" + assert completed_fut.done() + assert not completed_fut.cancelled() + finally: + if complete_fut_task is not None: + if not complete_fut_task.done(): + complete_fut_task.cancel() + try: + await complete_fut_task + except BaseException: + pass + try: + if not completed_fut.done(): + completed_fut.cancel() + elif not completed_fut.cancelled(): + completed_fut.exception() + except BaseException: + pass + + # Case 3: Overdue callback continues running without cancellation on non-terminal review + callback_running = asyncio.Event() + callback_finished = asyncio.Event() + release_callback = asyncio.Event() + cb_task_3: asyncio.Task[Any] | None = None + orig_create_task_3 = client_module.async_create_task + + def tracking_create_task_3(coro_arg: Any, **kwargs: Any) -> asyncio.Task[Any]: + nonlocal cb_task_3 + t = orig_create_task_3(coro_arg, **kwargs) + cb_task_3 = t + return t + + client_module.async_create_task = tracking_create_task_3 + + async def continuing_coro(r: Any) -> None: + callback_running.set() + await release_callback.wait() + callback_finished.set() + + client.get_review = fake_running # type: ignore + + sleep_forbidden = True + try: + waiter_task_3 = asyncio.create_task( + client.wait_for_review( + ORG, + REVIEW, + timeout=0.2, + on_poll=continuing_coro, + ) + ) + startup_waiter_3 = asyncio.create_task(callback_running.wait()) + try: + done_3, _ = await asyncio.wait( + {startup_waiter_3, waiter_task_3}, + return_when=asyncio.FIRST_COMPLETED, + ) + if waiter_task_3 in done_3: + await waiter_task_3 + assert not waiter_task_3.done() + clock.advance(1.0) + done_w3, _ = await asyncio.wait({waiter_task_3}, timeout=1.0) + if waiter_task_3 not in done_w3: + print("Pending task: waiter_task_3", file=sys.stderr) + assert waiter_task_3 in done_w3 + res_3 = waiter_task_3.result() + assert res_3.status == "TIMED_OUT" + assert callback_running.is_set() + assert not callback_finished.is_set() + release_callback.set() + assert cb_task_3 is not None + finish_waiter_3 = asyncio.create_task(callback_finished.wait()) + try: + done_fin_3, _ = await asyncio.wait( + {finish_waiter_3, cb_task_3}, + return_when=asyncio.FIRST_COMPLETED, + ) + if cb_task_3.cancelled(): + pytest.fail("Callback wrapper task was cancelled unexpectedly") + if not callback_finished.is_set(): + if cb_task_3.done() and cb_task_3.exception(): + raise cb_task_3.exception() + pytest.fail("Callback wrapper exited without setting completion event") + finally: + if not finish_waiter_3.done(): + finish_waiter_3.cancel() + try: + await finish_waiter_3 + except (asyncio.CancelledError, BaseException): + pass + done_cb3_fin, _ = await asyncio.wait({cb_task_3}, timeout=1.0) + assert cb_task_3 in done_cb3_fin + assert cb_task_3.result() is None + finally: + if not startup_waiter_3.done(): + startup_waiter_3.cancel() + try: + await startup_waiter_3 + except (asyncio.CancelledError, BaseException): + pass + finally: + client_module.async_create_task = orig_create_task_3 + sleep_forbidden = False + release_callback.set() + if not waiter_task_3.done(): + waiter_task_3.cancel() + try: + done_w3_clean, _ = await asyncio.wait({waiter_task_3}, timeout=1.0) + if waiter_task_3 not in done_w3_clean: + print("Pending task: waiter_task_3", file=sys.stderr) + except (asyncio.CancelledError, BaseException): + pass + if cb_task_3 is not None: + if not cb_task_3.done(): + cb_task_3.cancel() + try: + done_cb3_clean, _ = await asyncio.wait({cb_task_3}, timeout=1.0) + if cb_task_3 not in done_cb3_clean: + print("Pending task: cb_task_3", file=sys.stderr) + except (asyncio.CancelledError, BaseException): + pass + + # Case 3b: Return a Task from a synchronous callback, release it after the polling deadline + # and waiter exit, and assert its client request succeeds against the local test server. + task_worker_started = asyncio.Event() + task_release_3b = asyncio.Event() + task_success_3b = asyncio.Event() + cb_task_3b: asyncio.Task[None] | None = None + + with Server(lambda request, index: Reply(body=review("RUNNING"))) as test_server: + async with AsyncProoflineClient( + token="test_token", base_url=test_server.url + ) as real_client: + async def task_worker_3b() -> None: + task_worker_started.set() + await task_release_3b.wait() + # Real SDK request against the local test server checking deadline: + rev_res = await real_client.get_review(ORG, REVIEW) + assert rev_res.status == "RUNNING" + task_success_3b.set() + + def sync_cb_returning_task(r: Any) -> Any: + nonlocal cb_task_3b + cb_task_3b = asyncio.create_task(task_worker_3b()) + return cb_task_3b + + waiter_task_3b = asyncio.create_task( + real_client.wait_for_review( + ORG, + REVIEW, + timeout=0.2, + on_poll=sync_cb_returning_task, + ) + ) + startup_waiter = asyncio.create_task(task_worker_started.wait()) + try: + done, _ = await asyncio.wait( + {startup_waiter, waiter_task_3b}, + return_when=asyncio.FIRST_COMPLETED, + ) + if waiter_task_3b in done: + await waiter_task_3b + assert cb_task_3b is not None + assert not cb_task_3b.done() + clock.advance(1.0) + done_w3b, _ = await asyncio.wait({waiter_task_3b}, timeout=1.0) + if waiter_task_3b not in done_w3b: + print("Pending task: waiter_task_3b", file=sys.stderr) + assert waiter_task_3b in done_w3b + res_3b = waiter_task_3b.result() + assert res_3b.status == "TIMED_OUT" + assert not cb_task_3b.done() + task_release_3b.set() + done_c3b, _ = await asyncio.wait({cb_task_3b}, timeout=1.0) + if cb_task_3b not in done_c3b: + print("Pending task: cb_task_3b", file=sys.stderr) + assert cb_task_3b in done_c3b + cb_task_3b.result() + assert task_success_3b.is_set() + finally: + task_release_3b.set() + if not startup_waiter.done(): + startup_waiter.cancel() + try: + done_sw, _ = await asyncio.wait({startup_waiter}, timeout=0.5) + if startup_waiter not in done_sw: + print("Pending task: startup_waiter", file=sys.stderr) + except (asyncio.CancelledError, BaseException): + pass + if not waiter_task_3b.done(): + waiter_task_3b.cancel() + try: + done_w3b_clean, _ = await asyncio.wait({waiter_task_3b}, timeout=0.5) + if waiter_task_3b not in done_w3b_clean: + print("Pending task: waiter_task_3b", file=sys.stderr) + except (asyncio.CancelledError, BaseException): + pass + if cb_task_3b is not None: + if not cb_task_3b.done(): + cb_task_3b.cancel() + try: + done_cb3b_clean, _ = await asyncio.wait({cb_task_3b}, timeout=0.5) + if cb_task_3b not in done_cb3b_clean: + print("Pending task: cb_task_3b", file=sys.stderr) + except (asyncio.CancelledError, BaseException): + pass + + # Case 4: Overdue callback continues running without cancellation on terminal review + orig_create_task_4 = client_module.async_create_task + captured_case4_wrappers: list[Any] = [] + + def tracked_create_task_4(coro: Any, *args: Any, **kwargs: Any) -> Any: + t = orig_create_task_4(coro, *args, **kwargs) + t.exception_calls = 0 + orig_exc = t.exception + + def tracked_exc() -> Any: + t.exception_calls += 1 + return orig_exc() + + t.exception = tracked_exc + captured_case4_wrappers.append(t) + return t + + client_module.async_create_task = tracked_create_task_4 + + callback_running_term = asyncio.Event() + callback_finished_term = asyncio.Event() + release_callback_term = asyncio.Event() + + async def continuing_coro_term(r: Any) -> None: + callback_running_term.set() + await release_callback_term.wait() + callback_finished_term.set() + + client.get_review = fake_completed # type: ignore + + try: + waiter_task_4 = asyncio.create_task( + client.wait_for_review( + ORG, + REVIEW, + timeout=0.2, + on_poll=continuing_coro_term, + ) + ) + startup_waiter_4 = asyncio.create_task(callback_running_term.wait()) + try: + done_4, _ = await asyncio.wait( + {startup_waiter_4, waiter_task_4}, + return_when=asyncio.FIRST_COMPLETED, + ) + if waiter_task_4 in done_4: + await waiter_task_4 + finally: + if not startup_waiter_4.done(): + startup_waiter_4.cancel() + try: + await startup_waiter_4 + except (asyncio.CancelledError, BaseException): + pass + assert not waiter_task_4.done() + clock.advance(1.0) + done_w4, _ = await asyncio.wait({waiter_task_4}, timeout=1.0) + assert waiter_task_4 in done_w4 + res_4 = waiter_task_4.result() + assert res_4.status == "COMPLETED" + assert not callback_finished_term.is_set() + + assert len(captured_case4_wrappers) == 1 + wrapper_4 = captured_case4_wrappers[0] + wrapper_4_cbs = [ + cb[0] if isinstance(cb, tuple) else cb + for cb in (getattr(wrapper_4, "_callbacks", None) or []) + ] + assert not any(getattr(cb, "__name__", "") == "_on_task_done" for cb in wrapper_4_cbs) + + release_callback_term.set() + finish_waiter_4 = asyncio.create_task(callback_finished_term.wait()) + try: + done_fin_4, _ = await asyncio.wait( + {finish_waiter_4, wrapper_4}, + return_when=asyncio.FIRST_COMPLETED, + ) + if wrapper_4.cancelled(): + pytest.fail("Callback wrapper task was cancelled unexpectedly") + if not callback_finished_term.is_set(): + if wrapper_4.done() and wrapper_4.exception(): + raise wrapper_4.exception() + pytest.fail("Callback wrapper exited without setting completion event") + finally: + if not finish_waiter_4.done(): + finish_waiter_4.cancel() + try: + await finish_waiter_4 + except (asyncio.CancelledError, BaseException): + pass + assert callback_running_term.is_set() + assert callback_finished_term.is_set() + done_w4_wrap, _ = await asyncio.wait({wrapper_4}, timeout=1.0) + assert wrapper_4 in done_w4_wrap + assert wrapper_4.result() is None + finally: + release_callback_term.set() + if not waiter_task_4.done(): + waiter_task_4.cancel() + try: + await asyncio.wait({waiter_task_4}, timeout=1.0) + except (asyncio.CancelledError, BaseException): + pass + client_module.async_create_task = orig_create_task_4 + for w in captured_case4_wrappers: + if not w.done(): + w.cancel() + try: + await asyncio.wait({w}, timeout=1.0) + except (asyncio.CancelledError, BaseException): + pass + + # Case 5: Suspending callback that succeeds within budget + resumed_within_budget = False + + async def suspending_callback(r: Any) -> None: + nonlocal resumed_within_budget + await asyncio.sleep(0.01) + resumed_within_budget = True + + client.get_review = fake_completed # type: ignore + + res = await client.wait_for_review( + ORG, + REVIEW, + timeout=10.0, + on_poll=suspending_callback, + ) + assert res.status == "COMPLETED" + assert resumed_within_budget + + # Case 6: Overdue callback failure is observed without leaking to loop handler + unhandled_exceptions: list[dict[str, Any]] = [] + loop = asyncio.get_running_loop() + old_handler = loop.get_exception_handler() + loop.set_exception_handler(lambda _loop, ctx: unhandled_exceptions.append(ctx)) + coro_trigger: asyncio.Event | None = None + waiter_coro_task: asyncio.Task[Any] | None = None + failing_fut: asyncio.Future[None] | None = None + orig_create_task_6: Any = None + captured_case6_wrappers: list[asyncio.Task[Any]] = [] + try: + waiter_fut_task: asyncio.Task[Any] | None = None + case6a_cancelled = False + try: + # 6a: Future callback cancelled after startup + failing_fut = loop.create_future() + future_started = asyncio.Event() + + def failing_future_cb(r: Any) -> asyncio.Future[None]: + future_started.set() + assert failing_fut is not None + return failing_fut + + client.get_review = fake_running # type: ignore + waiter_fut_task = asyncio.create_task( + client.wait_for_review( + ORG, + REVIEW, + timeout=10.0, + on_poll=failing_future_cb, + ) + ) + startup_waiter_6a = asyncio.create_task(future_started.wait()) + try: + done_6a, _ = await asyncio.wait( + {startup_waiter_6a, waiter_fut_task}, + return_when=asyncio.FIRST_COMPLETED, + ) + if waiter_fut_task in done_6a: + await waiter_fut_task + finally: + if not startup_waiter_6a.done(): + startup_waiter_6a.cancel() + try: + await startup_waiter_6a + except (asyncio.CancelledError, BaseException): + pass + waiter_fut_task.cancel() + done_6a_c, _ = await asyncio.wait({waiter_fut_task}, timeout=1.0) + assert waiter_fut_task in done_6a_c + assert waiter_fut_task.cancelled() + try: + await waiter_fut_task + except (asyncio.CancelledError, BaseException): + pass + case6a_cancelled = True + finally: + if not case6a_cancelled: + if failing_fut is not None and not failing_fut.done(): + failing_fut.cancel() + if waiter_fut_task is not None and not waiter_fut_task.done(): + waiter_fut_task.cancel() + if waiter_fut_task is not None: + try: + await asyncio.wait({waiter_fut_task}, timeout=1.0) + except BaseException: + pass + + assert id(failing_fut) in client_module._DETACHED_FUTURES + failing_fut_cbs = [ + cb[0] if isinstance(cb, tuple) else cb + for cb in (getattr(failing_fut, "_callbacks", None) or []) + ] + assert not any( + getattr(cb, "__name__", "") == "_on_future_done" for cb in failing_fut_cbs + ) + failing_fut.set_exception(RuntimeError("error in cancelled future callback")) + await asyncio.sleep(0) + assert id(failing_fut) not in client_module._DETACHED_FUTURES + failing_fut = None + import gc + + gc.collect() + assert unhandled_exceptions == [] + + # 6b: Pre-start wrapper cancellation: hold wrapper before entry, + # cancel waiter with budget remaining, run queued runner and + # assert callback body never starts + import inspect + + hold_wrapper = asyncio.Event() + wrapper_tasks: list[asyncio.Task[Any]] = [] + orig_create_task = client_module.async_create_task + + runner_executed = False + captured_runners: list[Any] = [] + + def holding_create_task(coro_arg: Any, **kwargs: Any) -> asyncio.Task[Any]: + captured_runners.append(coro_arg) + + async def held_wrapper() -> Any: + nonlocal runner_executed + try: + await hold_wrapper.wait() + runner_executed = True + return await coro_arg + except asyncio.CancelledError: + if hasattr(coro_arg, "close"): + coro_arg.close() + raise + + t = orig_create_task(held_wrapper(), **kwargs) + wrapper_tasks.append(t) + return t + + client_module.async_create_task = holding_create_task + + user_coro_created: list[Any] = [] + cb_body_started = False + + async def dummy_pre_start_coro(r: Any) -> None: + nonlocal cb_body_started + cb_body_started = True + + def pre_start_cb(r: Any) -> Any: + c = dummy_pre_start_coro(r) + user_coro_created.append(c) + return c + + pre_waiter_task: asyncio.Task[Any] | None = None + try: + pre_waiter_task = asyncio.create_task( + client.wait_for_review( + ORG, + REVIEW, + timeout=10.0, + on_poll=pre_start_cb, + ) + ) + await asyncio.sleep(0) + if pre_waiter_task.done(): + await pre_waiter_task + assert len(wrapper_tasks) == 1 + assert not wrapper_tasks[0].done() + + # Queue the wrapper before cancelling the waiter in the same loop turn: + hold_wrapper.set() + pre_waiter_task.cancel() + await asyncio.sleep(0) + + assert runner_executed is True + + assert not cb_body_started + assert len(user_coro_created) == 1 + assert inspect.getcoroutinestate(user_coro_created[0]) == inspect.CORO_CLOSED + assert wrapper_tasks[0].done() + + with pytest.raises(asyncio.CancelledError): + await pre_waiter_task + finally: + client_module.async_create_task = orig_create_task + if pre_waiter_task is not None: + if not pre_waiter_task.done(): + pre_waiter_task.cancel() + try: + await pre_waiter_task + except (asyncio.CancelledError, BaseException): + pass + for wt in wrapper_tasks: + if not wt.done(): + wt.cancel() + try: + await wt + except (asyncio.CancelledError, BaseException): + pass + for c in user_coro_created: + try: + if ( + inspect.iscoroutine(c) + and inspect.getcoroutinestate(c) == inspect.CORO_CREATED + ): + c.close() + except BaseException: + pass + for cr in captured_runners: + try: + if ( + inspect.iscoroutine(cr) + and inspect.getcoroutinestate(cr) == inspect.CORO_CREATED + ): + cr.close() + elif hasattr(cr, "close"): + cr.close() + except BaseException: + pass + + waiter_coro_task: asyncio.Task[Any] | None = None + case6c_cancelled = False + try: + # Coroutine callback cancelled after startup + coro_started = asyncio.Event() + coro_trigger = asyncio.Event() + about_to_raise = asyncio.Event() + captured_case6_wrappers.clear() + orig_create_task_6 = client_module.async_create_task + + def tracked_create_task_6(coro: Any, *args: Any, **kwargs: Any) -> Any: + t = orig_create_task_6(coro, *args, **kwargs) + t.exception_calls = 0 + orig_exc = t.exception + + def tracked_exc() -> Any: + t.exception_calls += 1 + return orig_exc() + + t.exception = tracked_exc + captured_case6_wrappers.append(t) + return t + + client_module.async_create_task = tracked_create_task_6 + + async def failing_overdue_coro(r: Any) -> None: + coro_started.set() + await coro_trigger.wait() + about_to_raise.set() + raise RuntimeError("error in overdue callback") + + waiter_coro_task = asyncio.create_task( + client.wait_for_review( + ORG, + REVIEW, + timeout=10.0, + on_poll=failing_overdue_coro, + ) + ) + startup_waiter_6c = asyncio.create_task(coro_started.wait()) + try: + done_6c, _ = await asyncio.wait( + {startup_waiter_6c, waiter_coro_task}, + return_when=asyncio.FIRST_COMPLETED, + ) + if waiter_coro_task in done_6c: + await waiter_coro_task + finally: + if not startup_waiter_6c.done(): + startup_waiter_6c.cancel() + try: + await startup_waiter_6c + except (asyncio.CancelledError, BaseException): + pass + waiter_coro_task.cancel() + done_c, _ = await asyncio.wait({waiter_coro_task}, timeout=1.0) + assert waiter_coro_task in done_c + assert waiter_coro_task.cancelled() + try: + await waiter_coro_task + except (asyncio.CancelledError, BaseException): + pass + case6c_cancelled = True + finally: + if not case6c_cancelled: + if coro_trigger is not None: + coro_trigger.set() + if waiter_coro_task is not None and not waiter_coro_task.done(): + waiter_coro_task.cancel() + if waiter_coro_task is not None: + try: + await asyncio.wait({waiter_coro_task}, timeout=1.0) + except BaseException: + pass + + assert len(client_module._DETACHED_TASKS) == 1 + detached_task_id = next(iter(client_module._DETACHED_TASKS.keys())) + detached_task = client_module._DETACHED_TASKS[detached_task_id] + detached_cbs = [ + cb[0] if isinstance(cb, tuple) else cb + for cb in (getattr(detached_task, "_callbacks", None) or []) + ] + assert not any(getattr(cb, "__name__", "") == "_on_task_done" for cb in detached_cbs) + + coro_trigger.set() + await asyncio.wait_for(about_to_raise.wait(), timeout=1.0) + await asyncio.sleep(0.01) + + assert detached_task_id not in client_module._DETACHED_TASKS + assert detached_task.done() + assert not detached_task.cancelled() + assert getattr(detached_task, "exception_calls", 0) > 0 + # import weakref (already imported at module level) + + dt_ref = weakref.ref(detached_task) + del detached_task + captured_case6_wrappers.clear() + gc.collect() + assert dt_ref() is None + assert unhandled_exceptions == [] + finally: + if orig_create_task_6 is not None: + client_module.async_create_task = orig_create_task_6 + if failing_fut is not None: + if not failing_fut.done(): + failing_fut.cancel() + elif not failing_fut.cancelled(): + try: + failing_fut.exception() + except BaseException: + pass + if waiter_fut_task is not None: + if not waiter_fut_task.done(): + waiter_fut_task.cancel() + try: + await asyncio.wait({waiter_fut_task}, timeout=1.0) + except (asyncio.CancelledError, BaseException): + pass + if coro_trigger is not None: + coro_trigger.set() + if waiter_coro_task is not None: + if not waiter_coro_task.done(): + waiter_coro_task.cancel() + try: + await asyncio.wait({waiter_coro_task}, timeout=1.0) + except (asyncio.CancelledError, BaseException): + pass + for t in captured_case6_wrappers: + if hasattr(t, "done") and not t.done(): + t.cancel() + try: + await t + except (asyncio.CancelledError, BaseException): + pass + for t in list(client_module._DETACHED_TASKS.values()): + if hasattr(t, "done") and not t.done(): + t.cancel() + try: + await t + except (asyncio.CancelledError, BaseException): + pass + await asyncio.sleep(0.01) + loop.set_exception_handler(old_handler) + + # Case 7: Overdue callback awaiting the polling task is not cancelled + polling_waiter_task: asyncio.Task[Any] | None = None + callback_completed_7 = asyncio.Event() + cb_7_started = asyncio.Event() + + async def awaiting_runner_callback(r: Any) -> None: + cb_7_started.set() + assert polling_waiter_task is not None + outcome_inner = await polling_waiter_task + assert outcome_inner.status == "TIMED_OUT" + callback_completed_7.set() + + client.get_review = fake_running # type: ignore + sleep_forbidden = True + try: + polling_waiter_task = asyncio.create_task( + client.wait_for_review( + ORG, + REVIEW, + timeout=0.2, + on_poll=awaiting_runner_callback, + ) + ) + startup_waiter_7 = asyncio.create_task(cb_7_started.wait()) + try: + done_7, _ = await asyncio.wait( + {startup_waiter_7, polling_waiter_task}, + return_when=asyncio.FIRST_COMPLETED, + ) + if polling_waiter_task in done_7: + await polling_waiter_task + finally: + if not startup_waiter_7.done(): + startup_waiter_7.cancel() + try: + await startup_waiter_7 + except (asyncio.CancelledError, BaseException): + pass + assert not polling_waiter_task.done() + clock.advance(1.0) + done_7_res, _ = await asyncio.wait({polling_waiter_task}, timeout=1.0) + assert polling_waiter_task in done_7_res, "polling_waiter_task deadlocked on callback" + res_7 = polling_waiter_task.result() + assert res_7.status == "TIMED_OUT" + done_cb7, _ = await asyncio.wait( + {asyncio.create_task(callback_completed_7.wait())}, timeout=1.0 + ) + assert callback_completed_7.is_set() + finally: + sleep_forbidden = False + +asyncio.run(main()) +""" + checkout_root = Path(__file__).resolve().parent.parent + subprocess.run( + [sys.executable, "-"], + input=watchdog_code, + text=True, + cwd=str(checkout_root), + check=True, + timeout=10, + ) + + # Assert callback modifying review does not mutate returned review in both sync and async + mutated_count = 0 + resp_with_nested = dict(responses[1]) + resp_with_nested["nested_meta"] = {"key": "original", "tags": ["a", "b"]} + + def sync_mutating_callback(r: Any) -> None: + nonlocal mutated_count + mutated_count += 1 + r.status = "MUTATED" + r.actions.publish = True + r.additional_properties["mutated"] = "mutated_value" + assert len(r.assessments) > 0 + assert r.assessments[0].checks.receipt_recorded is True + r.assessments[0].checks.receipt_recorded = False + r.additional_properties["nested_meta"]["key"] = "mutated" + r.additional_properties["nested_meta"]["tags"].append("c") + + async def async_mutating_callback(r: Any) -> None: + nonlocal mutated_count + mutated_count += 1 + await asyncio.sleep(0.01) + r.status = "MUTATED" + r.actions.publish = True + r.additional_properties["mutated"] = "mutated_value" + assert len(r.assessments) > 0 + assert r.assessments[0].checks.receipt_recorded is True + r.assessments[0].checks.receipt_recorded = False + r.additional_properties["nested_meta"]["key"] = "mutated" + r.additional_properties["nested_meta"]["tags"].append("c") + + with Server(lambda request, index: Reply(body=resp_with_nested)) as server: + async with session(mode, server.url) as client: + mut_cb = async_mutating_callback if mode == "async" else sync_mutating_callback + res = await call( + client, + "wait_for_review", + ORG, + REVIEW, + timeout=10, + on_poll=mut_cb, + ) + assert mutated_count == 1 + assert res.review.status != "MUTATED" + assert res.review.actions.publish is False + assert "mutated" not in res.review.additional_properties + assert res.review.assessments[0].checks.receipt_recorded is True + assert res.review.additional_properties["nested_meta"]["key"] == "original" + assert res.review.additional_properties["nested_meta"]["tags"] == ["a", "b"] + + # Test snapshot directly with an in-memory deep extension and custom tuple to bypass HTTP JSON + deep_container: dict[str, Any] = {} + curr = deep_container + for _ in range(3000): + curr["child"] = {} + curr = curr["child"] + curr["val"] = "deepest" + + detail = CustomerReviewDetail.from_dict(responses[1]) + detail.additional_properties["deep"] = deep_container + detail.additional_properties["custom_tuple"] = ({"nested": "val"}, None, 42) + detail.additional_properties["none_frozenset_tuple"] = (None, frozenset({1})) + + from proofline.client import _snapshot_review + + snap = _snapshot_review(detail) + assert snap is not None + assert snap is not detail + assert snap.additional_properties is not detail.additional_properties + + # Walk all 3,000 levels, assert the leaf value, and verify mutation leaves original unchanged + walk_snap = snap.additional_properties["deep"] + walk_orig = detail.additional_properties["deep"] + for _ in range(3000): + assert walk_snap is not walk_orig + walk_snap = walk_snap["child"] + walk_orig = walk_orig["child"] + assert walk_snap["val"] == "deepest" + assert walk_orig["val"] == "deepest" + + walk_snap["val"] = "mutated_leaf" + assert walk_orig["val"] == "deepest" + + custom_t = snap.additional_properties["custom_tuple"] + orig_t = detail.additional_properties["custom_tuple"] + assert isinstance(custom_t, tuple) + assert custom_t[0] is not orig_t[0] + assert custom_t[0]["nested"] == "val" + assert custom_t[1] is None + assert custom_t[2] == 42 + custom_t[0]["nested"] = "mutated_nested" + assert orig_t[0]["nested"] == "val" + + assert "none_frozenset_tuple" in snap.additional_properties + nft = snap.additional_properties["none_frozenset_tuple"] + assert isinstance(nft, tuple) + assert len(nft) == 2 + assert nft[0] is None + assert nft[1] == frozenset({1}) + + # Tuple with deeply nested list must snapshot iteratively without RecursionError + deep_nested_list: Any = "leaf" + for _ in range(3000): + deep_nested_list = [deep_nested_list] + detail.additional_properties["tuple_with_deep_list"] = (deep_nested_list, 999) + snap_nested = _snapshot_review(detail) + assert snap_nested is not None + assert isinstance(snap_nested.additional_properties["tuple_with_deep_list"], tuple) + orig_l = detail.additional_properties["tuple_with_deep_list"][0] + curr_l = snap_nested.additional_properties["tuple_with_deep_list"][0] + for _ in range(3000): + assert curr_l is not orig_l + curr_l = curr_l[0] + orig_l = orig_l[0] + assert curr_l == "leaf" + assert orig_l == "leaf" + + # Assert pending future on closed loop or foreign loop raises ValueError + if mode == "async": + other_loop = asyncio.new_event_loop() + closed_fut = other_loop.create_future() + other_loop.close() + + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("async", server.url) as client: + with pytest.raises(ValueError, match="running event loop"): + await client.wait_for_review( + ORG, + REVIEW, + timeout=10, + on_poll=lambda r: closed_fut, + ) + assert not closed_fut.done() + + foreign_loop = asyncio.new_event_loop() + foreign_fut = foreign_loop.create_future() + try: + + def foreign_cb_expired(r: Any) -> Any: + clock.now += 200.0 + return foreign_fut + + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("async", server.url) as client: + with pytest.raises(ValueError, match="running event loop"): + await client.wait_for_review( + ORG, + REVIEW, + timeout=10, + on_poll=foreign_cb_expired, + ) + assert not foreign_fut.done() + assert not foreign_fut.cancelled() + assert not getattr(foreign_fut, "_callbacks", None) + finally: + foreign_loop.close() + + # Assert already-suspended native coroutine is rejected at callback boundary + async def dummy_coro() -> None: + await asyncio.sleep(0) + + suspended_coro = dummy_coro() + try: + try: + suspended_coro.send(None) + except StopIteration: + pass + + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("async", server.url) as client: + with pytest.raises(ValueError, match="unstarted coroutines"): + await client.wait_for_review( + ORG, + REVIEW, + timeout=10, + on_poll=lambda r: suspended_coro, + ) + finally: + suspended_coro.close() + + # Started coroutine raises ValueError even after deadline expiry without closing + async def dummy_started() -> None: + await asyncio.sleep(10) + + started_c = dummy_started() + task_started = asyncio.create_task(started_c) + try: + await asyncio.sleep(0) + + def started_coro_cb(r: Any) -> Any: + clock.now += 100.0 + return started_c + + with Server(lambda request, index: Reply(body=responses[0])) as server: + async with session("async", server.url) as client: + with pytest.raises(ValueError, match="unstarted coroutines"): + await client.wait_for_review( + ORG, + REVIEW, + timeout=5.0, + on_poll=started_coro_cb, + ) + assert inspect.getcoroutinestate(started_c) == inspect.CORO_SUSPENDED + finally: + task_started.cancel() + try: + await task_started + except asyncio.CancelledError: + pass + + # Non-None, non-awaitable callback return raises TypeError + def sync_int_cb(r: Any) -> Any: + return 42 + + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("async", server.url) as client: + with pytest.raises(TypeError, match="Callbacks must return None or an awaitable"): + await client.wait_for_review( + ORG, + REVIEW, + timeout=10, + on_poll=sync_int_cb, + ) + + # Unrestricted awaitables returning values (e.g. int) accepted and result discarded + int_cb_completed = False + + async def int_returning_callback(r: Any) -> int: + nonlocal int_cb_completed + await asyncio.sleep(0) + int_cb_completed = True + return 42 + + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("async", server.url) as client: + res_int = await client.wait_for_review( + ORG, + REVIEW, + timeout=10, + on_poll=int_returning_callback, + ) + assert res_int.status == "COMPLETED" + assert int_cb_completed is True + + # Unhashable same-loop Future subclass completed after wait starts + class UnhashableFuture(asyncio.Future[None]): + __hash__ = None # type: ignore[assignment] + + def __init__(self, loop: asyncio.AbstractEventLoop) -> None: + super().__init__(loop=loop) + self.exception_calls = 0 + + def exception(self) -> BaseException | None: + self.exception_calls += 1 + return super().exception() + + complete_task: asyncio.Task[None] | None = None + + async def complete_unhashable_soon(fut: Any) -> None: + await asyncio.sleep(0.01) + fut.set_result(None) + + def complete_unhashable_cb(r: Any) -> Any: + nonlocal complete_task + complete_task = asyncio.create_task(complete_unhashable_soon(unhashable_fut)) + return unhashable_fut + + unhashable_fut: Any = UnhashableFuture(loop=asyncio.get_running_loop()) + try: + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("async", server.url) as client: + res_unhash = await client.wait_for_review( + ORG, + REVIEW, + timeout=10, + on_poll=complete_unhashable_cb, + ) + assert res_unhash.status == "COMPLETED" + assert unhashable_fut.done() + finally: + if complete_task is not None: + if not complete_task.done(): + complete_task.cancel() + try: + await complete_task + except (asyncio.CancelledError, BaseException): + pass + try: + if not asyncio.Future.done(unhashable_fut): + unhashable_fut.cancel() + elif not asyncio.Future.cancelled(unhashable_fut): + unhashable_fut.exception() + except BaseException: + pass + + # Cancel the waiter while an UnhashableFuture is pending, then fail and release + # the future and assert that the loop reports no unhandled exception. + # Run in a subprocess with a hard timeout, settling the callback future + # before joining in cleanup and bounding waiter cancellation join with + # asyncio.wait. + unhashable_code = f""" +import asyncio +import sys +import weakref +from pathlib import Path +from uuid import UUID + +from tests.test_http import Server, Reply, session, review +from proofline.client import _DETACHED_FUTURES + +ORG = UUID("{ORG}") +REVIEW = UUID("{REVIEW}") + +class UnhashableFuture(asyncio.Future): + __hash__ = None # type: ignore + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.exception_calls = 0 + + def exception(self): + self.exception_calls += 1 + return super().exception() + +async def main(): + unhash_cancel_reports = [] + loop = asyncio.get_running_loop() + loop.set_exception_handler(lambda _loop, ctx: unhash_cancel_reports.append(ctx)) + with Server(lambda request, index: Reply(body=review("RUNNING"))) as server: + async with session("async", server.url) as client: + unhash_box = [UnhashableFuture(loop=loop)] + cb_started = asyncio.Event() + + def unhash_failing_cb(r): + cb_started.set() + return unhash_box[0] + + waiter_task = asyncio.create_task( + client.wait_for_review( + ORG, + REVIEW, + timeout=10, + on_poll=unhash_failing_cb, + ) + ) + cb_wait_task = asyncio.create_task(cb_started.wait()) + try: + done_set, _ = await asyncio.wait( + {{cb_wait_task, waiter_task}}, + timeout=5.0, + return_when=asyncio.FIRST_COMPLETED, + ) + if waiter_task in done_set and not cb_started.is_set(): + cb_wait_task.cancel() + await asyncio.wait({{waiter_task}}, timeout=1.0) + assert cb_wait_task in done_set, "Callback startup timed out" + waiter_task.cancel() + done_cancel, _ = await asyncio.wait({{waiter_task}}, timeout=1.0) + assert waiter_task in done_cancel + assert waiter_task.cancelled() + try: + await waiter_task + except asyncio.CancelledError: + pass + target_fut = unhash_box[0] + fut_weakref = weakref.ref(target_fut) + assert id(target_fut) in _DETACHED_FUTURES + target_fut_cbs = [ + cb[0] if isinstance(cb, tuple) else cb + for cb in (getattr(target_fut, "_callbacks", None) or []) + ] + assert not any( + getattr(cb, "__name__", "") == "_on_future_done" for cb in target_fut_cbs + ) + target_fut.set_exception(RuntimeError("unhashable late failure")) + await asyncio.sleep(0) + assert id(target_fut) not in _DETACHED_FUTURES + assert target_fut.exception_calls > 0 + del target_fut_cbs + del target_fut + unhash_box.clear() + import gc + gc.collect() + assert fut_weakref() is None + assert unhash_cancel_reports == [] + finally: + if unhash_box: + f = unhash_box[0] + if not f.done(): + f.set_result(None) + elif not f.cancelled(): + try: + f.exception() + except BaseException: + pass + for t in (cb_wait_task, waiter_task): + if not t.done(): + t.cancel() + try: + await asyncio.wait({{t}}, timeout=1.0) + except (asyncio.CancelledError, BaseException): + pass + await asyncio.sleep(0) + +asyncio.run(main()) +""" + checkout_root = Path(__file__).resolve().parent.parent + subprocess.run( + [sys.executable, "-"], + input=unhashable_code, + text=True, + cwd=str(checkout_root), + check=True, + timeout=15, + ) + + # Case detaching an overdue callback Future without calling its cancel(): + # the waiter returns TIMED_OUT without calling cancel() or reporting an unhandled exception + class SyncCancelRaisingFuture(asyncio.Future[None]): + def __init__(self, loop: asyncio.AbstractEventLoop) -> None: + super().__init__(loop=loop) + self.cancel_called = False + self.exception_calls = 0 + + def cancel(self, msg: Any = None) -> bool: + self.cancel_called = True + raise RuntimeError("synchronous cancel error") + + def exception(self) -> BaseException | None: + self.exception_calls += 1 + return super().exception() + + cancel_reports: list[dict[str, Any]] = [] + loop = asyncio.get_running_loop() + old_h = loop.get_exception_handler() + loop.set_exception_handler(lambda _loop, ctx: cancel_reports.append(ctx)) + fut_sync_box: list[Any] = [] + try: + try: + fut_sync_cancel: Any = SyncCancelRaisingFuture(loop=asyncio.get_running_loop()) + fut_sync_box.append(fut_sync_cancel) + clock_now = [1000.0] + orig_client_mono = client_module.monotonic + orig_trans_mono = _transport.monotonic + client_module.monotonic = lambda: clock_now[0] + _transport.monotonic = lambda: clock_now[0] + + def sync_cancel_on_poll(r: Any) -> Any: + clock_now[0] += 0.2 + return fut_sync_cancel + + running_detail = CustomerReviewDetail.from_dict(review("RUNNING")) + + async def fake_sync_cancel_running(org: Any, rev: Any) -> Any: + return running_detail + + try: + async with AsyncProoflineClient( + token="test_token", base_url="https://api.proofline.sh" + ) as client: + client.get_review = fake_sync_cancel_running # type: ignore + poll_task = asyncio.create_task( + client.wait_for_review( + ORG, + REVIEW, + timeout=0.1, + on_poll=sync_cancel_on_poll, + ) + ) + done_poll, _ = await asyncio.wait({poll_task}, timeout=5.0) + if poll_task not in done_poll: + if not fut_sync_cancel.done(): + fut_sync_cancel.set_result(None) + poll_task.cancel() + await asyncio.wait({poll_task}, timeout=1.0) + pytest.fail( + "wait_for_review hung on overdue future instead of detaching it" + ) + res_sync_cancel = poll_task.result() + assert res_sync_cancel.status == "TIMED_OUT" + await asyncio.sleep(0) + assert fut_sync_cancel.cancel_called is False + assert id(fut_sync_cancel) in client_module._DETACHED_FUTURES + assert cancel_reports == [] + + # Late-exception and release checks in test body after detachment verified + fut_sync_cancel.set_exception(RuntimeError("sync cancel late error")) + await asyncio.sleep(0) + assert id(fut_sync_cancel) not in client_module._DETACHED_FUTURES + assert fut_sync_cancel.exception_calls > 0 + # import weakref (already imported at module level) + + fut_sync_ref = weakref.ref(fut_sync_cancel) + fut_sync_box.clear() + del fut_sync_cancel + import gc + + gc.collect() + assert fut_sync_ref() is None + finally: + client_module.monotonic = orig_client_mono + _transport.monotonic = orig_trans_mono + finally: + if fut_sync_box: + f = fut_sync_box[0] + try: + if not asyncio.Future.done(f): + f.set_result(None) + await asyncio.sleep(0) + elif not asyncio.Future.cancelled(f): + f.exception() + except BaseException: + pass + fut_sync_box.clear() + finally: + loop.set_exception_handler(old_h) + + # Current task cannot be returned by on_poll + with Server(lambda request, index: Reply(body=responses[0])) as server: + async with session("async", server.url) as client: + with pytest.raises(ValueError, match="running polling task"): + await client.wait_for_review( + ORG, + REVIEW, + timeout=10, + on_poll=lambda r: asyncio.current_task(), + ) + + # Cyclic tuple whose list points back to it + cyclic_list: list[Any] = [] + cyclic_tuple = (cyclic_list, "tuple_leaf") + cyclic_list.append(cyclic_tuple) + + detail_cyclic = CustomerReviewDetail.from_dict(responses[1]) + detail_cyclic.additional_properties["cyclic_tuple"] = cyclic_tuple + + snap_cyclic = _snapshot_review(detail_cyclic) + assert snap_cyclic is not None + snap_tup = snap_cyclic.additional_properties["cyclic_tuple"] + assert snap_tup is not cyclic_tuple + snap_lst = snap_tup[0] + assert snap_lst is not cyclic_list + assert snap_lst[0] is snap_tup + + # Writable memoryview copied to independent storage + orig_bytearray = bytearray(b"hello") + orig_view = memoryview(orig_bytearray) + detail_view = CustomerReviewDetail.from_dict(responses[1]) + detail_view.additional_properties["memview"] = orig_view + + snap_view = _snapshot_review(detail_view) + assert snap_view is not None + copied_view = snap_view.additional_properties["memview"] + assert isinstance(copied_view, memoryview) + assert not copied_view.readonly + copied_view[0] = ord(b"X") + assert orig_bytearray == bytearray(b"hello") + assert bytes(copied_view) == b"Xello" + + # Unsupported mutable value rejected + import threading + + detail_unsupported = CustomerReviewDetail.from_dict(responses[1]) + detail_unsupported.additional_properties["bad"] = threading.Lock() + with pytest.raises(TypeError, match="Unsupported mutable value"): + _snapshot_review(detail_unsupported) + + # Safe finalization size for tuples skips snapshot delivery + huge_tuple = tuple(range(1001)) + detail_huge = CustomerReviewDetail.from_dict(responses[1]) + detail_huge.additional_properties["huge"] = huge_tuple + assert _snapshot_review(detail_huge) is None + + # Oversized buffer skips snapshot delivery + oversized_buf = bytearray(65_537) + detail_buf = CustomerReviewDetail.from_dict(responses[1]) + detail_buf.additional_properties["buf"] = oversized_buf + assert _snapshot_review(detail_buf) is None + + # Multidimensional memoryview checks nbytes against cap + multi_dim_raw = b"x" * 100_000 + multi_dim_view = memoryview(multi_dim_raw).cast("B", shape=[10, 10_000]) + assert len(multi_dim_view) == 10 + assert multi_dim_view.nbytes == 100_000 + detail_multi_dim = CustomerReviewDetail.from_dict(responses[1]) + detail_multi_dim.additional_properties["multi_dim"] = multi_dim_view + assert _snapshot_review(detail_multi_dim) is None + + # Scalar subclass instance and attributes copied + class CustomStr(str): + pass + + custom_scalar = CustomStr("scalar_val") + custom_scalar.attached_list = ["mut"] # type: ignore[attr-defined] + detail_scalar = CustomerReviewDetail.from_dict(responses[1]) + detail_scalar.additional_properties["scalar"] = custom_scalar + + snap_scalar = _snapshot_review(detail_scalar) + assert snap_scalar is not None + copied_scalar = snap_scalar.additional_properties["scalar"] + assert type(copied_scalar) is CustomStr + assert copied_scalar is not custom_scalar + assert copied_scalar.attached_list is not custom_scalar.attached_list + assert copied_scalar.attached_list == ["mut"] + + # Container subclass (defaultdict) preserves type, default_factory, and attributes + from collections import defaultdict + + class CustomDict(defaultdict[str, list[str]]): + pass + + dd = CustomDict(list) + dd["k"].append("val") + dd.extra_attr = ["extra"] # type: ignore[attr-defined] + detail_dd = CustomerReviewDetail.from_dict(responses[1]) + detail_dd.additional_properties["dd"] = dd + + snap_dd = _snapshot_review(detail_dd) + assert snap_dd is not None + copied_dd = snap_dd.additional_properties["dd"] + assert isinstance(copied_dd, CustomDict) + assert copied_dd is not dd + assert copied_dd["k"] is not dd["k"] + assert copied_dd["k"] == ["val"] + assert copied_dd["missing"] == [] + assert copied_dd.extra_attr is not dd.extra_attr + assert copied_dd.extra_attr == ["extra"] + + # Traverse dictionary entries and slot descriptors including private slots + class SlotBase: + __slots__ = ("__private_slot", "public_slot") + + def __init__(self) -> None: + self.__private_slot = ["private_secret"] + self.public_slot = ["public_secret"] + + class SlotDerived(SlotBase): + pass + + slot_obj = SlotDerived() + slot_obj.extra_dict_attr = ["extra"] + + detail_slot = CustomerReviewDetail.from_dict(responses[1]) + detail_slot.additional_properties["slot_obj"] = slot_obj + + snap_slot = _snapshot_review(detail_slot) + assert snap_slot is not None + copied_slot_obj = snap_slot.additional_properties["slot_obj"] + assert copied_slot_obj is not slot_obj + assert ( + copied_slot_obj._SlotBase__private_slot # type: ignore[attr-defined] + is not slot_obj._SlotBase__private_slot + ) + assert ( + copied_slot_obj._SlotBase__private_slot # type: ignore[attr-defined] + == ["private_secret"] + ) + assert copied_slot_obj.public_slot is not slot_obj.public_slot + assert copied_slot_obj.public_slot == ["public_secret"] + assert copied_slot_obj.extra_dict_attr is not slot_obj.extra_dict_attr + assert copied_slot_obj.extra_dict_attr == ["extra"] + + # Tuple subclass preserves type, isolates elements, copies attributes, and respects size limit + class CustomTuple(tuple[Any, ...]): + pass + + ct = CustomTuple(([1, 2], {"a": "b"})) + ct.extra = ["tuple_attr"] # type: ignore[attr-defined] + detail_ct = CustomerReviewDetail.from_dict(responses[1]) + detail_ct.additional_properties["ct"] = ct + + snap_ct = _snapshot_review(detail_ct) + assert snap_ct is not None + copied_ct = snap_ct.additional_properties["ct"] + assert type(copied_ct) is CustomTuple + assert copied_ct is not ct + assert copied_ct[0] is not ct[0] + assert copied_ct[0] == [1, 2] + assert copied_ct[1] is not ct[1] + assert copied_ct[1] == {"a": "b"} + assert copied_ct.extra is not ct.extra + assert copied_ct.extra == ["tuple_attr"] + + huge_ct = CustomTuple(range(1001)) + detail_huge_ct = CustomerReviewDetail.from_dict(responses[1]) + detail_huge_ct.additional_properties["huge_ct"] = huge_ct + assert _snapshot_review(detail_huge_ct) is None + + # Nonempty tuple subclass with identical elements constructs distinct subclass instance + class IdenticalTuple(tuple[Any, ...]): + pass + + itup = IdenticalTuple((1, "str", False)) + itup.attr = ["orig_attr"] # type: ignore[attr-defined] + detail_itup = CustomerReviewDetail.from_dict(responses[1]) + detail_itup.additional_properties["itup"] = itup + + snap_itup = _snapshot_review(detail_itup) + assert snap_itup is not None + copied_itup = snap_itup.additional_properties["itup"] + assert type(copied_itup) is IdenticalTuple + assert copied_itup is not itup + assert copied_itup.attr is not itup.attr + assert copied_itup.attr == ["orig_attr"] + copied_itup.attr.append("mutated") + assert itup.attr == ["orig_attr"] + + # Oversized terminal reviews passed to both sync and async clients: + # on_poll skipped and outcome returned + detail_oversized = CustomerReviewDetail.from_dict(responses[1]) + detail_oversized.additional_properties["huge"] = tuple(range(1001)) + + for test_mode in ("sync", "async"): + oversized_poll_called = False + + def oversized_poll_cb(r: Any) -> None: + nonlocal oversized_poll_called + oversized_poll_called = True + + async with session(test_mode, "https://api.proofline.sh") as client: + if test_mode == "async": + + async def fake_get_ov(*args: Any, **kwargs: Any) -> Any: + return detail_oversized + + monkeypatch.setattr(client, "get_review", fake_get_ov) + else: + + def fake_get_ov_sync(*args: Any, **kwargs: Any) -> Any: + return detail_oversized + + monkeypatch.setattr(client, "get_review", fake_get_ov_sync) + + res_ov = await call( + client, + "wait_for_review", + ORG, + REVIEW, + timeout=10, + on_poll=oversized_poll_cb, + ) + assert res_ov.status == "COMPLETED" + assert not oversized_poll_called + + # Lock-bearing reviews passed to both sync and async clients: + # TypeError propagates and on_poll is not called + import threading + + detail_lock = CustomerReviewDetail.from_dict(responses[1]) + detail_lock.additional_properties["lock"] = threading.Lock() + + for test_mode in ("sync", "async"): + lock_poll_called = False + + def lock_poll_cb(r: Any) -> None: + nonlocal lock_poll_called + lock_poll_called = True + + async with session(test_mode, "https://api.proofline.sh") as client: + if test_mode == "async": + + async def fake_get_lock(*args: Any, **kwargs: Any) -> Any: + return detail_lock + + monkeypatch.setattr(client, "get_review", fake_get_lock) + else: + + def fake_get_lock_sync(*args: Any, **kwargs: Any) -> Any: + return detail_lock + + monkeypatch.setattr(client, "get_review", fake_get_lock_sync) + + with pytest.raises(TypeError, match="Unsupported mutable value"): + await call( + client, + "wait_for_review", + ORG, + REVIEW, + timeout=10, + on_poll=lock_poll_cb, + ) + assert not lock_poll_called + + # Multidimensional memoryview shape and format preserved + raw_mv = bytearray(b"0123456789abcdef") + mv_orig = memoryview(raw_mv).cast("B", shape=[4, 4]) + detail_mv = CustomerReviewDetail.from_dict(responses[1]) + detail_mv.additional_properties["mv"] = mv_orig + + snap_mv = _snapshot_review(detail_mv) + assert snap_mv is not None + copied_mv = snap_mv.additional_properties["mv"] + assert isinstance(copied_mv, memoryview) + assert copied_mv.shape == (4, 4) + assert copied_mv.format == "B" + assert copied_mv[1, 2] == mv_orig[1, 2] + copied_mv[1, 2] = ord("Z") + assert raw_mv != bytearray(copied_mv) + + # Named tuple preserves named fields + from typing import NamedTuple + + class Point(NamedTuple): + x: int + y: int + + pt = Point(10, 20) + detail_pt = CustomerReviewDetail.from_dict(responses[1]) + detail_pt.additional_properties["pt"] = pt + + snap_pt = _snapshot_review(detail_pt) + assert snap_pt is not None + copied_pt = snap_pt.additional_properties["pt"] + assert type(copied_pt) is Point + assert copied_pt.x == 10 + assert copied_pt.y == 20 + + # Derived slot does not hide base slot + class BaseSlot: + __slots__ = ("shared_slot",) + + class DerivedSlot(BaseSlot): + __slots__ = ("shared_slot",) + + slot_inst = DerivedSlot() + BaseSlot.shared_slot.__set__(slot_inst, ["base_data"]) + DerivedSlot.shared_slot.__set__(slot_inst, ["derived_data"]) + detail_slots = CustomerReviewDetail.from_dict(responses[1]) + detail_slots.additional_properties["slots"] = slot_inst + + snap_slots = _snapshot_review(detail_slots) + assert snap_slots is not None + copied_slots = snap_slots.additional_properties["slots"] + assert type(copied_slots) is DerivedSlot + assert BaseSlot.shared_slot.__get__(copied_slots) == ["base_data"] + assert BaseSlot.shared_slot.__get__(copied_slots) is not BaseSlot.shared_slot.__get__(slot_inst) + assert DerivedSlot.shared_slot.__get__(copied_slots) == ["derived_data"] + assert DerivedSlot.shared_slot.__get__(copied_slots) is not DerivedSlot.shared_slot.__get__( + slot_inst + ) + + # Empty tuple subclass with required __init__ argument + class EmptyArgTuple(tuple[Any, ...]): + def __init__(self, required_arg: Any) -> None: + self.attr = "initialized" + + empty_tup_inst = tuple.__new__(EmptyArgTuple, ()) + empty_tup_inst.attr = "custom_attr" # type: ignore[attr-defined] + detail_empty_arg = CustomerReviewDetail.from_dict(responses[1]) + detail_empty_arg.additional_properties["empty_arg"] = empty_tup_inst + + snap_empty_arg = _snapshot_review(detail_empty_arg) + assert snap_empty_arg is not None + copied_empty_arg = snap_empty_arg.additional_properties["empty_arg"] + assert type(copied_empty_arg) is EmptyArgTuple + assert copied_empty_arg == () + assert copied_empty_arg.attr == "custom_attr" + + # Dict keys resolved through memo preserving alias with value + class KeyValObj: + def __init__(self, name: str) -> None: + self.name = name + + shared_obj = KeyValObj("shared_alias") + key_tup = (shared_obj, 1) + dict_with_alias = {key_tup: shared_obj} + detail_alias = CustomerReviewDetail.from_dict(responses[1]) + detail_alias.additional_properties["alias_dict"] = dict_with_alias + + snap_alias = _snapshot_review(detail_alias) + assert snap_alias is not None + copied_dict = snap_alias.additional_properties["alias_dict"] + assert len(copied_dict) == 1 + for k, v in copied_dict.items(): + assert k is not key_tup + assert k[0] is v + assert v is not shared_obj + assert v.name == "shared_alias" + + # Set member containing oversized tuple skips snapshot delivery + set_with_huge_tuple = {tuple(range(1001))} + detail_set_huge = CustomerReviewDetail.from_dict(responses[1]) + detail_set_huge.additional_properties["set_huge"] = set_with_huge_tuple + assert _snapshot_review(detail_set_huge) is None + + # Mutable object whose __copy__ returns self is rejected with TypeError + class SelfCopyList(list[Any]): + def __copy__(self) -> Any: + return self + + self_copy_inst = SelfCopyList([1, 2]) + detail_self_copy = CustomerReviewDetail.from_dict(responses[1]) + detail_self_copy.additional_properties["self_copy"] = self_copy_inst + with pytest.raises(TypeError, match="Unsupported mutable value"): + _snapshot_review(detail_self_copy) + + # Ctypes array and memoryview snapshots are preserved + import ctypes + + c_arr = (ctypes.c_int32 * 4)(1, 2, 3, 4) + mv_ctypes = memoryview(c_arr) + mv_ctypes_bytes = memoryview(c_arr).cast("B") + mv_ctypes_slice = memoryview(c_arr)[3::-2] + detail_ctypes_mv = CustomerReviewDetail.from_dict(responses[1]) + detail_ctypes_mv.additional_properties["ctypes_arr"] = c_arr + detail_ctypes_mv.additional_properties["ctypes_mv"] = mv_ctypes + detail_ctypes_mv.additional_properties["ctypes_bytes"] = mv_ctypes_bytes + detail_ctypes_mv.additional_properties["ctypes_slice"] = mv_ctypes_slice + snap_ctypes = _snapshot_review(detail_ctypes_mv) + assert snap_ctypes is not None + copied_arr = snap_ctypes.additional_properties["ctypes_arr"] + copied_mv = snap_ctypes.additional_properties["ctypes_mv"] + copied_bytes = snap_ctypes.additional_properties["ctypes_bytes"] + copied_slice = snap_ctypes.additional_properties["ctypes_slice"] + assert copied_slice.shape == mv_ctypes_slice.shape == (2,) + assert copied_slice.strides == mv_ctypes_slice.strides == (-8,) + assert bytes(copied_mv) == bytes(mv_ctypes) + assert bytes(copied_bytes) == bytes(mv_ctypes_bytes) + assert bytes(copied_slice) == bytes(mv_ctypes_slice) + assert copied_arr is not c_arr + assert copied_mv.obj is not c_arr + assert copied_bytes.obj is not c_arr + # Write through cloned byte view at the second element and assert that the reversed slice + # reflects the write while the source stays unchanged: + copied_bytes[4] = 99 + assert copied_arr[1] == 99 + assert bytes(copied_mv)[4] == 99 + assert bytes(copied_slice) == b"\x04\x00\x00\x00\x63\x00\x00\x00" + assert c_arr[1] == 2 + assert bytes(mv_ctypes)[4] == 2 + assert bytes(mv_ctypes_slice) == b"\x04\x00\x00\x00\x02\x00\x00\x00" + assert mv_ctypes_bytes[4] == 2 + + # UUID instances are cloned and memoized preserving is_safe and subclass instance state + from uuid import SafeUUID + + class CustomReviewUUID(UUID): + pass + + detail_uuid = CustomerReviewDetail.from_dict(responses[1]) + orig_uuid = UUID(int=detail_uuid.id.int, is_safe=SafeUUID.safe) + detail_uuid.id = orig_uuid + subclass_uuid = CustomReviewUUID(int=orig_uuid.int, is_safe=SafeUUID.safe) + object.__setattr__(subclass_uuid, "custom_tag", "isolated_tag") + detail_uuid.additional_properties["subclass_uuid"] = subclass_uuid + + snap_uuid = _snapshot_review(detail_uuid) + assert snap_uuid is not None + assert snap_uuid.id is not orig_uuid + assert snap_uuid.id == orig_uuid + assert snap_uuid.id.is_safe == orig_uuid.is_safe == SafeUUID.safe + object.__setattr__(snap_uuid.id, "int", 999999) + assert snap_uuid.id.int == 999999 + assert orig_uuid.int != 999999 + + cloned_subclass = snap_uuid.additional_properties["subclass_uuid"] + assert type(cloned_subclass) is CustomReviewUUID + assert cloned_subclass is not subclass_uuid + assert cloned_subclass.is_safe == SafeUUID.safe + assert cloned_subclass.custom_tag == "isolated_tag" + object.__setattr__(cloned_subclass, "custom_tag", "mutated_tag") + assert subclass_uuid.custom_tag == "isolated_tag" + + # 2D memoryview bounding span cast to physical rows and row sliced + c_arr_2d = (ctypes.c_uint8 * 8)(10, 11, 20, 21, 30, 31, 40, 41) + mv_2d_slice = memoryview(c_arr_2d).cast("B", shape=(4, 2))[::2] + detail_2d = CustomerReviewDetail.from_dict(responses[1]) + detail_2d.additional_properties["ctypes_2d"] = mv_2d_slice + snap_2d = _snapshot_review(detail_2d) + assert snap_2d is not None + copied_2d = snap_2d.additional_properties["ctypes_2d"] + assert copied_2d.shape == mv_2d_slice.shape == (2, 2) + assert copied_2d.strides == mv_2d_slice.strides == (4, 1) + assert copied_2d.tobytes() == mv_2d_slice.tobytes() + copied_2d[0, 0] = 99 + assert copied_2d[0, 0] == 99 + assert mv_2d_slice[0, 0] == 10 + assert c_arr_2d[0] == 10 + + # 3D memoryview reversed first axis reconstructed from physical span + c_arr_3d = (ctypes.c_uint8 * 24)(*range(24)) + mv_3d_rev = memoryview(c_arr_3d).cast("B", shape=(2, 3, 4))[::-1] + detail_3d = CustomerReviewDetail.from_dict(responses[1]) + detail_3d.additional_properties["ctypes_3d"] = mv_3d_rev + snap_3d = _snapshot_review(detail_3d) + assert snap_3d is not None + copied_3d = snap_3d.additional_properties["ctypes_3d"] + assert copied_3d.shape == mv_3d_rev.shape == (2, 3, 4) + assert copied_3d.strides == mv_3d_rev.strides == (-12, 4, 1) + assert copied_3d.tobytes() == mv_3d_rev.tobytes() + copied_3d[0, 0, 0] = 99 + assert copied_3d[0, 0, 0] == 99 + assert mv_3d_rev[0, 0, 0] == 12 + assert c_arr_3d[12] == 12 + + class MutableArray(ctypes.c_int32 * 4): + pass + + MutableArray.mutable_attr = [1, 2, 3] # application-defined mutable attribute + mut_arr = MutableArray(1, 2, 3, 4) + detail_mut_arr = CustomerReviewDetail.from_dict(responses[1]) + detail_mut_arr.additional_properties["mut_arr"] = memoryview(mut_arr) + with pytest.raises(TypeError, match="Unsupported mutable value"): + _snapshot_review(detail_mut_arr) + + # Empty set subclass copies attributes + class CustomSet(set[Any]): + pass + + empty_custom_set = CustomSet() + empty_custom_set.custom_attr = ["set_attr"] # type: ignore[attr-defined] + detail_empty_set = CustomerReviewDetail.from_dict(responses[1]) + detail_empty_set.additional_properties["empty_set"] = empty_custom_set + + snap_empty_set = _snapshot_review(detail_empty_set) + assert snap_empty_set is not None + copied_empty_set = snap_empty_set.additional_properties["empty_set"] + assert type(copied_empty_set) is CustomSet + assert len(copied_empty_set) == 0 + assert copied_empty_set.custom_attr is not empty_custom_set.custom_attr + assert copied_empty_set.custom_attr == ["set_attr"] + + # Set subclass requiring extra constructor argument propagates TypeError + class RequiredArgSet(set[Any]): + def __init__(self, required_arg: Any) -> None: + super().__init__() + + req_set = set.__new__(RequiredArgSet) + detail_req_set = CustomerReviewDetail.from_dict(responses[1]) + detail_req_set.additional_properties["req_set"] = req_set + with pytest.raises(TypeError, match="Unsupported mutable value"): + _snapshot_review(detail_req_set) + + # Bytearray subclass preserves subclass type and copies instance attributes + class CustomByteArray(bytearray): + pass + + custom_ba = CustomByteArray(b"custom_bytes") + custom_ba.extra_attr = ["extra"] # type: ignore[attr-defined] + detail_custom_ba = CustomerReviewDetail.from_dict(responses[1]) + detail_custom_ba.additional_properties["custom_ba"] = custom_ba + snap_ba = _snapshot_review(detail_custom_ba) + assert snap_ba is not None + copied_ba = snap_ba.additional_properties["custom_ba"] + assert type(copied_ba) is CustomByteArray + assert copied_ba == b"custom_bytes" + assert copied_ba is not custom_ba + assert copied_ba.extra_attr is not custom_ba.extra_attr + assert copied_ba.extra_attr == ["extra"] + + # Class with exhausted __slots__ iterator has installed descriptors enumerated + class ExhaustedSlotsObj: + __slots__ = iter(["slot_attr"]) + + def __init__(self, val: Any) -> None: + self.slot_attr = val + + def __copy__(self) -> Any: + res = ExhaustedSlotsObj.__new__(ExhaustedSlotsObj) + res.slot_attr = self.slot_attr + return res + + orig_slots_inst = ExhaustedSlotsObj(["slot_val"]) + detail_slots = CustomerReviewDetail.from_dict(responses[1]) + detail_slots.additional_properties["exhausted_slots"] = orig_slots_inst + snap_slots = _snapshot_review(detail_slots) + assert snap_slots is not None + copied_slots_inst = snap_slots.additional_properties["exhausted_slots"] + assert copied_slots_inst is not orig_slots_inst + assert copied_slots_inst.slot_attr is not orig_slots_inst.slot_attr + assert copied_slots_inst.slot_attr == ["slot_val"] + + # Datetime with custom mutable tzinfo copies tzinfo and avoids leaking mutation + import datetime as dt_mod + + class CustomMutableTz(dt_mod.tzinfo): + def __init__(self, offset_minutes: int = 0) -> None: + self.offset_minutes = offset_minutes + self.tz_attr = ["tz_state"] + + def utcoffset(self, dt_val: Any) -> timedelta: + return timedelta(minutes=self.offset_minutes) + + def tzname(self, dt_val: Any) -> str: + return "CUSTOM" + + def dst(self, dt_val: Any) -> timedelta: + return timedelta(0) + + custom_tz_inst = CustomMutableTz(60) + dt_with_custom_tz = datetime(2026, 11, 22, 10, 0, 0, tzinfo=custom_tz_inst) + detail_dt = CustomerReviewDetail.from_dict(responses[1]) + detail_dt.additional_properties["custom_tz_dt"] = dt_with_custom_tz + snap_dt = _snapshot_review(detail_dt) + assert snap_dt is not None + copied_dt = snap_dt.additional_properties["custom_tz_dt"] + assert copied_dt == dt_with_custom_tz + assert copied_dt.tzinfo is not custom_tz_inst + assert copied_dt.tzinfo.tz_attr is not custom_tz_inst.tz_attr + assert copied_dt.tzinfo.tz_attr == ["tz_state"] + + # Deceptive tuple subclass reporting len 1 with > 1000 items is skipped + class DeceptiveTuple(tuple[Any, ...]): + def __len__(self) -> int: + return 1 + + huge_tup_items = tuple(range(1001)) + deceptive_tup = tuple.__new__(DeceptiveTuple, huge_tup_items) + detail_deceptive = CustomerReviewDetail.from_dict(responses[1]) + detail_deceptive.additional_properties["deceptive"] = deceptive_tup + assert _snapshot_review(detail_deceptive) is None + + # Bytearray subclass rejecting single-buffer constructor is allocated via base methods + class SubByteArray(bytearray): + def __init__(self, required_arg: int) -> None: + if not isinstance(required_arg, int) or isinstance(required_arg, bool): + raise TypeError("required_arg must be an int") + super().__init__() + self.required_arg = required_arg + + sub_ba = bytearray.__new__(SubByteArray) + bytearray.__init__(sub_ba, b"sub_bytes") + sub_ba.required_arg = 100 + sub_ba.custom_attr = ["sub_attr"] + + detail_sub_ba = CustomerReviewDetail.from_dict(responses[1]) + detail_sub_ba.additional_properties["sub_ba"] = sub_ba + snap_sub_ba = _snapshot_review(detail_sub_ba) + assert snap_sub_ba is not None + copied_sub_ba = snap_sub_ba.additional_properties["sub_ba"] + assert type(copied_sub_ba) is SubByteArray + assert copied_sub_ba == b"sub_bytes" + assert copied_sub_ba.custom_attr is not sub_ba.custom_attr + assert copied_sub_ba.custom_attr == ["sub_attr"] + + # Datetime subclass cloned and instance attributes snapshotted + class SubDatetime(datetime): + pass + + sub_dt = SubDatetime(2026, 11, 22, 12, 0, 0, tzinfo=UTC) + sub_dt.sub_attr = ["dt_attr"] + detail_sub_dt = CustomerReviewDetail.from_dict(responses[1]) + detail_sub_dt.additional_properties["sub_dt"] = sub_dt + snap_sub_dt = _snapshot_review(detail_sub_dt) + assert snap_sub_dt is not None + copied_sub_dt = snap_sub_dt.additional_properties["sub_dt"] + assert type(copied_sub_dt) is SubDatetime + assert copied_sub_dt == sub_dt + assert copied_sub_dt is not sub_dt + assert copied_sub_dt.sub_attr is not sub_dt.sub_attr + assert copied_sub_dt.sub_attr == ["dt_attr"] + + # Mutable tzinfo returning self from __copy__ is rejected with TypeError + class SelfCopyTz(dt_mod.tzinfo): + def __copy__(self) -> Any: + return self + + self_copy_tz = SelfCopyTz() + dt_self_copy_tz = datetime(2026, 11, 22, 10, 0, 0, tzinfo=self_copy_tz) + detail_self_copy_tz = CustomerReviewDetail.from_dict(responses[1]) + detail_self_copy_tz.additional_properties["self_copy_tz"] = dt_self_copy_tz + with pytest.raises(TypeError, match="Unsupported mutable value"): + _snapshot_review(detail_self_copy_tz) + + # SubDatetime with custom mutable tzinfo rebuilds subclass with copied tzinfo + sub_dt_custom_tz = SubDatetime(2026, 11, 22, 12, 0, 0, tzinfo=custom_tz_inst) + sub_dt_custom_tz.sub_attr = ["dt_tz_attr"] + detail_sub_tz = CustomerReviewDetail.from_dict(responses[1]) + detail_sub_tz.additional_properties["sub_dt_tz"] = sub_dt_custom_tz + snap_sub_tz = _snapshot_review(detail_sub_tz) + assert snap_sub_tz is not None + copied_sub_tz = snap_sub_tz.additional_properties["sub_dt_tz"] + assert type(copied_sub_tz) is SubDatetime + assert copied_sub_tz == sub_dt_custom_tz + assert copied_sub_tz.tzinfo is not custom_tz_inst + assert copied_sub_tz.tzinfo.tz_attr is not custom_tz_inst.tz_attr + assert copied_sub_tz.sub_attr is not sub_dt_custom_tz.sub_attr + + # Deceptive set subclass returning len 0 with members copies all members + class DeceptiveSet(set[Any]): + def __len__(self) -> int: + return 0 + + deceptive_s = DeceptiveSet(["member1", "member2"]) + detail_deceptive_s = CustomerReviewDetail.from_dict(responses[1]) + detail_deceptive_s.additional_properties["deceptive_s"] = deceptive_s + snap_deceptive_s = _snapshot_review(detail_deceptive_s) + assert snap_deceptive_s is not None + copied_deceptive_s = snap_deceptive_s.additional_properties["deceptive_s"] + assert type(copied_deceptive_s) is DeceptiveSet + assert "member1" in copied_deceptive_s and "member2" in copied_deceptive_s + + # Set subclass whose constructor inserts extra member does not reintroduce it + class InjectedSet(set[Any]): + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.add("injected_shared_member") + + injected_s = InjectedSet(["orig_member"]) + injected_s.remove("injected_shared_member") + detail_injected_s = CustomerReviewDetail.from_dict(responses[1]) + detail_injected_s.additional_properties["injected_s"] = injected_s + snap_injected_s = _snapshot_review(detail_injected_s) + assert snap_injected_s is not None + copied_injected_s = snap_injected_s.additional_properties["injected_s"] + assert type(copied_injected_s) is InjectedSet + assert "injected_shared_member" not in copied_injected_s + assert "orig_member" in copied_injected_s + + # Standard built-in set and frozenset properly snapshotted with hashable mutable payload + class HashablePayload: + def __init__(self, key: str, payload: list[str]) -> None: + self.key = key + self.payload = payload + + def __hash__(self) -> int: + return hash(self.key) + + def __eq__(self, other: Any) -> bool: + return isinstance(other, HashablePayload) and self.key == other.key + + leaf_payload = HashablePayload("leaf_key", ["leaf_original"]) + inner_fs = frozenset([leaf_payload]) + outer_fs = frozenset([inner_fs]) + + class NonemptySubFrozenset1(frozenset[Any]): + pass + + member_payload = HashablePayload("test_key", ["original_val"]) + normal_s = {member_payload} + normal_fs = NonemptySubFrozenset1([member_payload]) + normal_fs.custom_attr = "subclass_attr_1" + normal_fs.shared_list = ["shared_val_1"] + alias_dict_1 = normal_fs.__dict__ + detail_sets = CustomerReviewDetail.from_dict(responses[1]) + detail_sets.additional_properties["normal_s"] = normal_s + detail_sets.additional_properties["normal_fs"] = normal_fs + detail_sets.additional_properties["sub_fs_alias"] = alias_dict_1 + detail_sets.additional_properties["nested_fs"] = outer_fs + detail_sets.additional_properties["fs_list"] = [normal_fs, normal_fs] + snap_sets = _snapshot_review(detail_sets) + assert snap_sets is not None + assert snap_sets.additional_properties["normal_s"] == normal_s + assert snap_sets.additional_properties["normal_s"] is not normal_s + copied_fs = snap_sets.additional_properties["normal_fs"] + assert isinstance(copied_fs, NonemptySubFrozenset1) + assert copied_fs.custom_attr == "subclass_attr_1" + assert copied_fs.shared_list == ["shared_val_1"] + assert copied_fs.shared_list is not normal_fs.shared_list + copied_alias_dict = snap_sets.additional_properties["sub_fs_alias"] + assert copied_fs.__dict__ is copied_alias_dict + assert copied_fs.__dict__ is not alias_dict_1 + assert len(copied_fs) == 1 + copied_list = snap_sets.additional_properties["fs_list"] + assert copied_list[0] is copied_list[1] + assert copied_list[0] is copied_fs + cloned_member = next(iter(copied_fs)) + assert cloned_member == member_payload + assert cloned_member is not member_payload + assert next(iter(snap_sets.additional_properties["normal_s"])) is cloned_member + assert cloned_member.payload is not member_payload.payload + assert cloned_member in copied_fs + cloned_member.payload.append("mutated_val") + assert member_payload.payload == ["original_val"] + + copied_outer = snap_sets.additional_properties["nested_fs"] + assert isinstance(copied_outer, frozenset) + assert len(copied_outer) == 1 + copied_inner = next(iter(copied_outer)) + assert isinstance(copied_inner, frozenset) + assert len(copied_inner) == 1 + copied_leaf = next(iter(copied_inner)) + assert copied_leaf == leaf_payload + assert copied_leaf is not leaf_payload + assert copied_leaf.payload is not leaf_payload.payload + assert copied_inner in copied_outer + assert copied_leaf in copied_inner + copied_leaf.payload.append("leaf_mutated") + assert leaf_payload.payload == ["leaf_original"] + + # Deque traversed and snapshotted, and weakref inside deque rejected + from collections import deque + + dq: deque[Any] = deque(["elem1", ["nested_list"]]) + detail_dq = CustomerReviewDetail.from_dict(responses[1]) + detail_dq.additional_properties["dq"] = dq + snap_dq = _snapshot_review(detail_dq) + assert snap_dq is not None + copied_dq = snap_dq.additional_properties["dq"] + assert isinstance(copied_dq, deque) + assert copied_dq == dq + assert copied_dq is not dq + assert copied_dq[1] is not dq[1] + + # Deque subclass preserves subclass type, maxlen, and restores attributes + class CustomDeque(deque[Any]): + pass + + c_dq: CustomDeque = CustomDeque(["d1", "d2"], maxlen=5) + c_dq.custom_dq_attr = ["dq_attr"] # type: ignore[attr-defined] + detail_c_dq = CustomerReviewDetail.from_dict(responses[1]) + detail_c_dq.additional_properties["c_dq"] = c_dq + snap_c_dq = _snapshot_review(detail_c_dq) + assert snap_c_dq is not None + copied_c_dq = snap_c_dq.additional_properties["c_dq"] + assert type(copied_c_dq) is CustomDeque + assert copied_c_dq.maxlen == 5 + assert copied_c_dq == c_dq + assert copied_c_dq is not c_dq + assert copied_c_dq.custom_dq_attr is not c_dq.custom_dq_attr + assert copied_c_dq.custom_dq_attr == ["dq_attr"] + + # Deceptive bytearray subclass underreporting len with > 65536 bytes is skipped + class DeceptiveByteArray(bytearray): + def __len__(self) -> int: + return 1 + + huge_ba = bytearray.__new__(DeceptiveByteArray) + bytearray.__init__(huge_ba, b"x" * 65537) + detail_huge_ba = CustomerReviewDetail.from_dict(responses[1]) + detail_huge_ba.additional_properties["huge_ba"] = huge_ba + assert _snapshot_review(detail_huge_ba) is None + + # Weak references in snapshot extension data are rejected with TypeError + # import weakref (already imported at module level) + + class TargetObj: + pass + + dummy_target = TargetObj() + weak_ref_obj = weakref.ref(dummy_target) + detail_weakref = CustomerReviewDetail.from_dict(responses[1]) + detail_weakref.additional_properties["weakref"] = weak_ref_obj + with pytest.raises(TypeError, match="Unsupported mutable value"): + _snapshot_review(detail_weakref) + + # Deque containing weak reference rejected with TypeError + dq_weakref: deque[Any] = deque([weak_ref_obj]) + detail_dq_weakref = CustomerReviewDetail.from_dict(responses[1]) + detail_dq_weakref.additional_properties["dq_weakref"] = dq_weakref + with pytest.raises(TypeError, match="Unsupported mutable value"): + _snapshot_review(detail_dq_weakref) + + # Synchronous wait_for_review passed an async callback raises TypeError + async def async_cb_on_sync_client(r: Any) -> None: + pass + + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("sync", server.url) as sync_cli: + with pytest.raises(TypeError, match="AsyncProoflineClient"): + await call( + sync_cli, + "wait_for_review", + ORG, + REVIEW, + timeout=10, + on_poll=async_cb_on_sync_client, + ) + + # Synchronous wait_for_review rejecting non-None return with TypeError + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("sync", server.url) as sync_cli: + with pytest.raises(TypeError, match="Callbacks must return None"): + await call( + sync_cli, + "wait_for_review", + ORG, + REVIEW, + timeout=10, + on_poll=lambda r: False, + ) + + # Synchronous wait_for_review closes generator awaitable in GEN_CREATED + import types + + @types.coroutine + def gen_coro_cb(r: Any) -> Any: + yield + + gen_cb_inst = gen_coro_cb(None) + + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("sync", server.url) as sync_cli: + with pytest.raises(TypeError, match="AsyncProoflineClient"): + await call( + sync_cli, + "wait_for_review", + ORG, + REVIEW, + timeout=10, + on_poll=lambda r: gen_cb_inst, + ) + assert inspect.getgeneratorstate(gen_cb_inst) == inspect.GEN_CLOSED + + # wait_for_review cleans up already-failed foreign-loop future returned when budget is exhausted + class TrackedForeignFuture(asyncio.Future[None]): + def __init__(self, loop: asyncio.AbstractEventLoop) -> None: + super().__init__(loop=loop) + self.exception_retrieved = False + + def exception(self) -> BaseException | None: + self.exception_retrieved = True + return super().exception() + + # import weakref (already imported at module level) + + foreign_loop_failed = asyncio.new_event_loop() + foreign_reports: list[dict[str, Any]] = [] + foreign_loop_failed.set_exception_handler(lambda _loop, ctx: foreign_reports.append(ctx)) + try: + foreign_failed_box: list[Any] = [TrackedForeignFuture(loop=foreign_loop_failed)] + foreign_failed_box[0].set_exception(RuntimeError("foreign future failed")) + foreign_failed_fut_ref = weakref.ref(foreign_failed_box[0]) + + def foreign_fail_cb(r: Any) -> Any: + clock.now += 150.0 + return foreign_failed_box[0] + + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("async", server.url) as client: + res_foreign = await client.wait_for_review( + ORG, + REVIEW, + timeout=100.0, + on_poll=foreign_fail_cb, + ) + assert res_foreign.status == "COMPLETED" + tracked_ref = foreign_failed_fut_ref() + assert tracked_ref is not None + assert tracked_ref.exception_retrieved is True + del tracked_ref + foreign_failed_box.clear() + import gc + + gc.collect() + assert foreign_failed_fut_ref() is None + assert foreign_reports == [] + finally: + if foreign_failed_box: + try: + ff = foreign_failed_box[0] + if not ff.cancelled(): + ff.exception() + except BaseException: + pass + foreign_failed_box.clear() + foreign_loop_failed.close() + + # Datetime subclass whose replace fails and fallback lacks copied_tz raises TypeError + class BrokenSubDatetime(datetime): + def __new__(cls, y: int, m: int, d: int, tzinfo: Any = None) -> Any: + return super().__new__(cls, y, m, d, tzinfo=tzinfo) + + def replace(self, **kwargs: Any) -> Any: + raise RuntimeError("cannot replace") + + def __copy__(self) -> Any: + return super().__new__( + BrokenSubDatetime, self.year, self.month, self.day, tzinfo=self.tzinfo + ) + + broken_dt = BrokenSubDatetime(2026, 11, 22, tzinfo=custom_tz_inst) + detail_broken_dt = CustomerReviewDetail.from_dict(responses[1]) + detail_broken_dt.additional_properties["broken_dt"] = broken_dt + with pytest.raises(TypeError, match="Unsupported mutable value"): + _snapshot_review(detail_broken_dt) + + # Slotted extension object with class-level mutable list such as __shared__ is rejected + class SlottedSharedAttrObj: + __slots__ = ("instance_attr",) + __shared__ = [1, 2, 3] + + def __init__(self, val: Any) -> None: + self.instance_attr = val + + slotted_shared = SlottedSharedAttrObj("val") + detail_slotted = CustomerReviewDetail.from_dict(responses[1]) + detail_slotted.additional_properties["shared"] = slotted_shared + with pytest.raises(TypeError, match="Unsupported mutable value"): + _snapshot_review(detail_slotted) + + # Set member is a tuple containing an object with a custom hash that reads an inner dict + class CustomHashWithDict: + def __init__(self, d: dict[str, Any]) -> None: + self.d = d + + def __hash__(self) -> int: + return hash(tuple(sorted(self.d.items()))) + + def __eq__(self, other: Any) -> bool: + return isinstance(other, CustomHashWithDict) and self.d == other.d + + inner_dict = {"key": "val"} + custom_obj = CustomHashWithDict(inner_dict) + tuple_with_custom = (custom_obj,) + set_with_tuple = {tuple_with_custom} + detail_tuple_custom = CustomerReviewDetail.from_dict(responses[1]) + detail_tuple_custom.additional_properties["set_tuple"] = set_with_tuple + snap_tc = _snapshot_review(detail_tuple_custom) + assert snap_tc is not None + copied_set = snap_tc.additional_properties["set_tuple"] + copied_tuple = next(iter(copied_set)) + assert copied_tuple[0].d == {"key": "val"} + assert copied_tuple[0].d is not inner_dict + assert copied_tuple in copied_set + + # Snapshot overlapping views of one subclass exporter, visiting a view first: + # assert shared writes, original isolation, copied attributes, and scalar shape () + class SubclassByteArray(bytearray): + pass + + sub_ba = SubclassByteArray(b"0123456789abcdef") + sub_ba.extra_attr = ["isolated_extra"] # type: ignore[attr-defined] + + sub_mv1 = memoryview(sub_ba)[0:10] + sub_scalar = memoryview(sub_ba)[6:7].cast("B", shape=()) + sub_mv2 = memoryview(sub_ba)[5:15] + + detail_view_first = CustomerReviewDetail.from_dict(responses[1]) + detail_view_first.additional_properties = { + "view1": sub_mv1, + "scalar": sub_scalar, + "view2": sub_mv2, + "exporter": sub_ba, + } + + snap_vf = _snapshot_review(detail_view_first) + assert snap_vf is not None + copied_v1 = snap_vf.additional_properties["view1"] + copied_scalar = snap_vf.additional_properties["scalar"] + copied_v2 = snap_vf.additional_properties["view2"] + copied_exp = snap_vf.additional_properties["exporter"] + + assert copied_scalar.shape == () + assert copied_scalar.ndim == 0 + assert copied_scalar[()] == ord("6") + assert type(copied_exp) is SubclassByteArray + assert copied_exp.extra_attr == ["isolated_extra"] + assert copied_exp.extra_attr is not sub_ba.extra_attr + + copied_v1[6] = ord("X") + assert copied_v2[1] == ord("X") + assert copied_scalar[()] == ord("X") + assert copied_exp[6] == ord("X") + assert sub_ba[6] == ord("6") + assert sub_mv1[6] == ord("6") + assert sub_mv2[1] == ord("6") + assert sub_scalar[()] == ord("6") + copied_exp.extra_attr.append("new_val") + assert "new_val" not in sub_ba.extra_attr + + # Also test with array.array subclass exporter and integer views: + class SubclassIntArray(array.array): + pass + + sub_arr = SubclassIntArray("i", [10, 20, 30, 40, 50, 60, 70, 80]) + sub_arr.custom_dict = {"prop": "val"} # type: ignore[attr-defined] + arr_v1 = memoryview(sub_arr)[1:6] + arr_scalar = memoryview(sub_arr).cast("B")[12:16].cast("i", shape=()) + arr_v2 = memoryview(sub_arr)[2:7] + + detail_arr_vf = CustomerReviewDetail.from_dict(responses[1]) + detail_arr_vf.additional_properties = { + "arr_v1": arr_v1, + "arr_scalar": arr_scalar, + "arr_v2": arr_v2, + "arr_exp": sub_arr, + } + + snap_arr = _snapshot_review(detail_arr_vf) + assert snap_arr is not None + c_arr_v1 = snap_arr.additional_properties["arr_v1"] + c_arr_scalar = snap_arr.additional_properties["arr_scalar"] + c_arr_v2 = snap_arr.additional_properties["arr_v2"] + c_arr_exp = snap_arr.additional_properties["arr_exp"] + + assert c_arr_scalar.shape == () + assert c_arr_scalar.ndim == 0 + assert c_arr_scalar[()] == 40 + assert type(c_arr_exp) is SubclassIntArray + assert c_arr_exp.custom_dict == {"prop": "val"} + assert c_arr_exp.custom_dict is not sub_arr.custom_dict + + c_arr_v1[2] = 999 + assert c_arr_v2[1] == 999 + assert c_arr_scalar[()] == 999 + assert c_arr_exp[3] == 999 + assert sub_arr[3] == 40 + assert arr_v1[2] == 40 + + # Class-state rejection for set subclass and views over bytearray and array.array subclasses + class SetWithClassList(set[Any]): + __shared_list__ = [1, 2] + + detail_set_class_list = CustomerReviewDetail.from_dict(responses[1]) + detail_set_class_list.additional_properties["set_sub"] = SetWithClassList([1, 2]) + with pytest.raises(TypeError, match="Unsupported mutable value"): + _snapshot_review(detail_set_class_list) + + class ByteArrayWithClassList(bytearray): + __shared_list__ = [1, 2] + + ba_sub_shared = ByteArrayWithClassList(b"hello") + detail_ba_view = CustomerReviewDetail.from_dict(responses[1]) + detail_ba_view.additional_properties["ba_view"] = memoryview(ba_sub_shared) + with pytest.raises(TypeError, match="Unsupported mutable value"): + _snapshot_review(detail_ba_view) + + class ArrayWithClassList(array.array): + __shared_list__ = [1, 2] + + arr_sub_shared = ArrayWithClassList("i", [1, 2, 3]) + detail_arr_view = CustomerReviewDetail.from_dict(responses[1]) + detail_arr_view.additional_properties["arr_view"] = memoryview(arr_sub_shared) + with pytest.raises(TypeError, match="Unsupported mutable value"): + _snapshot_review(detail_arr_view) + + # Key whose custom hash reads a deferred dictionary through a frozenset member + class DictHolder: + def __init__(self, d: dict[str, Any]) -> None: + self.d = d + + class CustomHashWithFrozenset: + def __init__(self, f_set: frozenset[Any]) -> None: + self.f_set = f_set + + def __hash__(self) -> int: + for item in self.f_set: + if hasattr(item, "d"): + return hash(tuple(sorted((k, str(type(v))) for k, v in item.d.items()))) + return 0 + + def __eq__(self, other: Any) -> bool: + return isinstance(other, CustomHashWithFrozenset) and self.f_set == other.f_set + + class NonemptySubFrozenset2(frozenset[Any]): + pass + + deferred_dict: dict[str, Any] = {"ready_key": "ready_val"} + f_set = NonemptySubFrozenset2([DictHolder(deferred_dict)]) + f_set.custom_attr = "subclass_attr_2" + f_set.shared_list = ["shared_val_2"] + alias_dict_2 = f_set.__dict__ + deferred_dict["pending_val"] = f_set + custom_fs_obj = CustomHashWithFrozenset(f_set) + set_with_fs_custom = {custom_fs_obj} + detail_fs_custom = CustomerReviewDetail.from_dict(responses[1]) + detail_fs_custom.additional_properties["fs_custom"] = set_with_fs_custom + detail_fs_custom.additional_properties["sub_fs_2_alias"] = alias_dict_2 + snap_fsc = _snapshot_review(detail_fs_custom) + assert snap_fsc is not None + copied_fsc_set = snap_fsc.additional_properties["fs_custom"] + assert len(copied_fsc_set) == 1 + copied_obj = next(iter(copied_fsc_set)) + assert copied_obj in copied_fsc_set + copied_f_set = copied_obj.f_set + assert isinstance(copied_f_set, NonemptySubFrozenset2) + assert copied_f_set.custom_attr == "subclass_attr_2" + assert copied_f_set.shared_list == ["shared_val_2"] + assert copied_f_set.shared_list is not f_set.shared_list + copied_alias_2 = snap_fsc.additional_properties["sub_fs_2_alias"] + assert copied_f_set.__dict__ is copied_alias_2 + assert copied_f_set.__dict__ is not alias_dict_2 + copied_holder = next(iter(copied_obj.f_set)) + assert copied_holder.d["ready_key"] == "ready_val" + assert copied_holder.d["pending_val"] is copied_obj.f_set + assert copied_holder.d is not deferred_dict + + # Dictionary key whose hash depends on cloned nested state: snapshot succeeds and lookup works + class NestedStateKey: + def __init__(self, inner: dict[str, Any]) -> None: + self.inner = inner + + def __hash__(self) -> int: + return hash(tuple(sorted((k, str(v)) for k, v in self.inner.items()))) + + def __eq__(self, other: Any) -> bool: + return isinstance(other, NestedStateKey) and self.inner == other.inner + + inner_mapping = {"nested_field": "nested_val"} + nested_key = NestedStateKey(inner_mapping) + dict_with_nested_key = {nested_key: "found_val"} + detail_nested = CustomerReviewDetail.from_dict(responses[1]) + detail_nested.additional_properties["nested_key_dict"] = dict_with_nested_key + + snap_nested = _snapshot_review(detail_nested) + assert snap_nested is not None + copied_nested_dict = snap_nested.additional_properties["nested_key_dict"] + copied_k = next(iter(copied_nested_dict.keys())) + assert copied_k is not nested_key + assert copied_k.inner is not inner_mapping + assert copied_k.inner == {"nested_field": "nested_val"} + assert copied_nested_dict[copied_k] == "found_val" + assert copied_k in copied_nested_dict + + # Dictionary fixture whose key hash changes during clone finalization: snapshot is rejected + class MutatingHashKey: + def __init__(self) -> None: + self.val = 1 + + def __hash__(self) -> int: + h = self.val + self.val += 1 + return h + + def __eq__(self, other: Any) -> bool: + return isinstance(other, MutatingHashKey) + + mutating_k = MutatingHashKey() + dict_with_mutating = {mutating_k: "val"} + detail_dict_mutating = CustomerReviewDetail.from_dict(responses[1]) + detail_dict_mutating.additional_properties["mutating_dict"] = dict_with_mutating + assert _snapshot_review(detail_dict_mutating) is None + + # Strided memoryview over immutable bytes preserves strides and c_contiguous + b_data = b"0123456789abcdef" + mv_strided = memoryview(b_data)[::2] + detail_strided = CustomerReviewDetail.from_dict(responses[1]) + detail_strided.additional_properties["strided"] = mv_strided + snap_strided = _snapshot_review(detail_strided) + assert snap_strided is not None + c_strided = snap_strided.additional_properties["strided"] + assert c_strided.strides == mv_strided.strides + assert c_strided.c_contiguous == mv_strided.c_contiguous + assert bytes(c_strided) == bytes(mv_strided) + + # Empty strided memoryview over immutable bytes preserves strides + mv_empty_strided = memoryview(b_data)[0:0:2] + detail_empty_strided = CustomerReviewDetail.from_dict(responses[1]) + detail_empty_strided.additional_properties["empty_strided"] = mv_empty_strided + snap_empty_strided = _snapshot_review(detail_empty_strided) + assert snap_empty_strided is not None + c_empty_strided = snap_empty_strided.additional_properties["empty_strided"] + assert c_empty_strided.strides == mv_empty_strided.strides + + # 1-byte view over backing buffer > 65,536 bytes skips delivery (returns None) + big_backing = b"A" * 65537 + mv_1byte = memoryview(big_backing)[0:1] + detail_1byte = CustomerReviewDetail.from_dict(responses[1]) + detail_1byte.additional_properties["mv_1b"] = mv_1byte + assert _snapshot_review(detail_1byte) is None + + # Array subclass overriding buffer_info to underreport size skips delivery + class UnderreportingArray(array.array): + def buffer_info(self) -> tuple[int, int]: + return (0, 0) + + big_arr = UnderreportingArray("B", [0] * 65537) + detail_arr_underreport = CustomerReviewDetail.from_dict(responses[1]) + detail_arr_underreport.additional_properties["arr_under"] = big_arr + assert _snapshot_review(detail_arr_underreport) is None + + # Read-only view visited first over writable bytearray exporter followed by writable view + shared_exp = bytearray(b"0123456789") + ro_view = memoryview(shared_exp).toreadonly() + rw_view = memoryview(shared_exp) + detail_sibling_views = CustomerReviewDetail.from_dict(responses[1]) + detail_sibling_views.additional_properties["ro_first"] = ro_view + detail_sibling_views.additional_properties["rw_second"] = rw_view + snap_siblings = _snapshot_review(detail_sibling_views) + assert snap_siblings is not None + copied_ro = snap_siblings.additional_properties["ro_first"] + copied_rw = snap_siblings.additional_properties["rw_second"] + assert copied_ro.readonly is True + assert copied_rw.readonly is False + copied_rw[0] = ord(b"Z") + assert copied_ro[0] == ord(b"Z") + assert shared_exp[0] == ord(b"0") + + # Exact bytes over 65,536 bytes skips delivery (returns None) + detail_big_bytes = CustomerReviewDetail.from_dict(responses[1]) + detail_big_bytes.additional_properties["big_bytes"] = b"A" * 65537 + assert _snapshot_review(detail_big_bytes) is None + + # Bytes subclass whose __bytes__ tries to expand payload beyond native buffer + class CustomBytesWithExpansion(bytes): + def __bytes__(self) -> bytes: + return b"Z" * 70000 + + small_sub_bytes = CustomBytesWithExpansion(b"small_sub") + small_sub_bytes.custom_tag = "tag" # type: ignore[attr-defined] + detail_sub_bytes = CustomerReviewDetail.from_dict(responses[1]) + detail_sub_bytes.additional_properties["sub_bytes"] = small_sub_bytes + snap_sub_bytes = _snapshot_review(detail_sub_bytes) + assert snap_sub_bytes is not None + copied_sub_bytes = snap_sub_bytes.additional_properties["sub_bytes"] + assert type(copied_sub_bytes) is CustomBytesWithExpansion + assert len(copied_sub_bytes) == len(b"small_sub") + assert memoryview(copied_sub_bytes).tobytes() == b"small_sub" + assert copied_sub_bytes.custom_tag == "tag" + + # Deep __self__ chain (exceeding standard recursion limit) walks iteratively + class DeepSelfChain: + pass + + curr_obj = DeepSelfChain() + for _ in range(1500): + prev = curr_obj + curr_obj = DeepSelfChain() + curr_obj.__self__ = prev # type: ignore[attr-defined] + + assert client_module._is_immutable(curr_obj) is False + + # Explicit reference to instance __dict__ points to the exact same cloned dictionary + class SharedDictOwner: + def __init__(self) -> None: + self.x = 42 + + owner = SharedDictOwner() + detail_dict_ref = CustomerReviewDetail.from_dict(responses[1]) + detail_dict_ref.additional_properties["owner"] = owner + detail_dict_ref.additional_properties["owner_dict"] = owner.__dict__ + snap_dict_ref = _snapshot_review(detail_dict_ref) + assert snap_dict_ref is not None + copied_owner = snap_dict_ref.additional_properties["owner"] + copied_dict = snap_dict_ref.additional_properties["owner_dict"] + assert copied_dict is copied_owner.__dict__ + copied_dict["x"] = 99 + assert copied_owner.x == 99 + + # Non-callable object with immutable __self__ is not classified as immutable + class NonCallableWithSelf: + __self__ = 42 + + nc_inst = NonCallableWithSelf() + assert client_module._is_immutable(nc_inst) is False + detail_nc = CustomerReviewDetail.from_dict(responses[1]) + detail_nc.additional_properties["nc"] = nc_inst + snap_nc = _snapshot_review(detail_nc) + assert snap_nc is not None + copied_nc = snap_nc.additional_properties["nc"] + assert copied_nc is not nc_inst + + # Memoryview over bytes subclass exporter whose __bytes__ tries expansion + mv_sub_bytes = memoryview(small_sub_bytes)[1:5] + detail_mv_sub_bytes = CustomerReviewDetail.from_dict(responses[1]) + detail_mv_sub_bytes.additional_properties["mv_sub_bytes"] = mv_sub_bytes + snap_mv_sub = _snapshot_review(detail_mv_sub_bytes) + assert snap_mv_sub is not None + copied_mv_sub = snap_mv_sub.additional_properties["mv_sub_bytes"] + assert copied_mv_sub.tobytes() == b"mall" + assert type(copied_mv_sub.obj) is CustomBytesWithExpansion + assert memoryview(copied_mv_sub.obj).tobytes() == b"small_sub" + + # Tuple containing oversized bytes (> 65536) returns None + detail_tuple_big_bytes = CustomerReviewDetail.from_dict(responses[1]) + detail_tuple_big_bytes.additional_properties["tuple_big"] = (b"A" * 65537,) + assert _snapshot_review(detail_tuple_big_bytes) is None + + # Invalid callback return raising property on close + class RaisingCloseReturn: + @property + def close(self) -> Any: + raise RuntimeError("close property lookup failed") + + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("sync", server.url) as client_sync: + with pytest.raises(TypeError, match="Callbacks must return None"): + await call( + client_sync, + "wait_for_review", + ORG, + REVIEW, + on_poll=lambda r: RaisingCloseReturn(), + ) + + +async def test_wait_for_review_pending_futures_and_clock( + monkeypatch: pytest.MonkeyPatch, +) -> None: + clock = Clock(monkeypatch) + responses = [review("RUNNING"), review()] + + # Assert remaining budget with controlled clock + wait_timeouts: list[float | None] = [] + orig_async_wait = client_module.async_wait + + async def tracking_wait(fs: Any, *, timeout: float | None = None) -> Any: + wait_timeouts.append(timeout) + return await orig_async_wait(fs, timeout=timeout) + + monkeypatch.setattr(client_module, "async_wait", tracking_wait) + + async def dummy_async_cb(r: Any) -> None: + pass + + def cb_consuming_clock(r: Any) -> Any: + clock.now += 35.0 + return dummy_async_cb(r) + + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("async", server.url) as client: + await asyncio.wait_for( + client.wait_for_review( + ORG, + REVIEW, + timeout=100.0, + on_poll=cb_consuming_clock, + ), + timeout=5.0, + ) + assert wait_timeouts == [65.0] + monkeypatch.setattr(client_module, "async_wait", orig_async_wait) + + # Timely pending future failure propagates even if done() lies + class LyingDoneFuture(asyncio.Future[None]): + def done(self) -> bool: + return False + + timely_fut: Any = LyingDoneFuture(loop=asyncio.get_running_loop()) + timely_producer: asyncio.Task[None] | None = None + + async def fail_timely_soon() -> None: + await asyncio.sleep(0.01) + timely_fut.set_exception(ValueError("timely failure")) + + def timely_fail_cb(r: Any) -> asyncio.Future[None]: + nonlocal timely_producer + timely_producer = asyncio.create_task(fail_timely_soon()) + return timely_fut + + try: + with Server(lambda request, index: Reply(body=responses[0])) as server: + async with session("async", server.url) as client: + with pytest.raises(ValueError, match="timely failure"): + await asyncio.wait_for( + client.wait_for_review( + ORG, + REVIEW, + timeout=100.0, + on_poll=timely_fail_cb, + ), + timeout=5.0, + ) + finally: + if timely_producer is not None: + if not timely_producer.done(): + timely_producer.cancel() + try: + await timely_producer + except BaseException: + pass + try: + if not asyncio.Future.done(timely_fut): + timely_fut.cancel() + elif not asyncio.Future.cancelled(timely_fut): + timely_fut.exception() + except BaseException: + pass + + # Timely pending future failure propagates when clock advances after SDK done-callback + # but before waiter resumes + class ClockAdvancingFuture(asyncio.Future[None]): + def add_done_callback(self, fn: Any, *, context: Any = None) -> None: + super().add_done_callback(fn, context=context) + super().add_done_callback(lambda _: setattr(clock, "now", clock.now + 200.0)) + + timely_late_fut: Any = ClockAdvancingFuture(loop=asyncio.get_running_loop()) + timely_late_producer: asyncio.Task[None] | None = None + + async def fail_timely_late_soon() -> None: + await asyncio.sleep(0.01) + timely_late_fut.set_exception(ValueError("timely failure with late resumption")) + + def timely_late_fail_cb(r: Any) -> asyncio.Future[None]: + nonlocal timely_late_producer + timely_late_producer = asyncio.create_task(fail_timely_late_soon()) + return timely_late_fut + + try: + with Server(lambda request, index: Reply(body=responses[0])) as server: + async with session("async", server.url) as client: + with pytest.raises(ValueError, match="timely failure with late resumption"): + await asyncio.wait_for( + client.wait_for_review( + ORG, + REVIEW, + timeout=100.0, + on_poll=timely_late_fail_cb, + ), + timeout=5.0, + ) + assert clock.now >= 200.0 + finally: + if timely_late_producer is not None: + if not timely_late_producer.done(): + timely_late_producer.cancel() + try: + await timely_late_producer + except BaseException: + pass + try: + if not asyncio.Future.done(timely_late_fut): + timely_late_fut.cancel() + elif not asyncio.Future.cancelled(timely_late_fut): + timely_late_fut.exception() + except BaseException: + pass + + # Timely coroutine failure propagates when clock advances after coroutine raises + # but before wrapper done-callback runs or waiter resumes + orig_create_task_timely = client_module.async_create_task + + def advancing_create_task(coro_arg: Any, **kwargs: Any) -> asyncio.Task[Any]: + t = orig_create_task_timely(coro_arg, **kwargs) + orig_add_done = t.add_done_callback + + def wrapped_add_done(fn: Any, *, context: Any = None) -> None: + def advancing_fn(task_arg: Any) -> None: + clock.now += 200.0 + fn(task_arg) + + orig_add_done(advancing_fn, context=context) + + t.add_done_callback = wrapped_add_done + return t + + monkeypatch.setattr(client_module, "async_create_task", advancing_create_task) + + async def timely_fail_coro(r: Any) -> None: + raise ValueError("timely coroutine failure") + + with Server(lambda request, index: Reply(body=responses[0])) as server: + async with session("async", server.url) as client: + with pytest.raises(ValueError, match="timely coroutine failure"): + await asyncio.wait_for( + client.wait_for_review( + ORG, + REVIEW, + timeout=100.0, + on_poll=timely_fail_coro, + ), + timeout=5.0, + ) + assert clock.now >= 200.0 + monkeypatch.setattr(client_module, "async_create_task", orig_create_task_timely) + + # Late-observed pending future failure preserves the classified outcome + class TrackedFuture(asyncio.Future[None]): + def __init__(self, loop: asyncio.AbstractEventLoop) -> None: + super().__init__(loop=loop) + self.exception_retrieved = False + + def exception(self) -> BaseException | None: + self.exception_retrieved = True + return super().exception() + + # import weakref (already imported at module level) + + late_fut: TrackedFuture | None = TrackedFuture(loop=asyncio.get_running_loop()) + late_fut_ref = weakref.ref(late_fut) + late_fut.add_done_callback(lambda _: setattr(clock, "now", clock.now + 200.0)) + producer_task: asyncio.Task[None] | None = None + + async def fail_late_soon() -> None: + await asyncio.sleep(0) + if late_fut is not None: + late_fut.set_exception(ValueError("late failure")) + + def late_fail_cb(r: Any) -> asyncio.Future[None]: + nonlocal producer_task + producer_task = asyncio.create_task(fail_late_soon()) + assert late_fut is not None + return late_fut + + unhandled_reports: list[dict[str, Any]] = [] + loop = asyncio.get_running_loop() + old_h = loop.get_exception_handler() + loop.set_exception_handler(lambda _loop, ctx: unhandled_reports.append(ctx)) + try: + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("async", server.url) as client: + res_late = await asyncio.wait_for( + client.wait_for_review( + ORG, + REVIEW, + timeout=100.0, + on_poll=late_fail_cb, + ), + timeout=5.0, + ) + assert res_late.status == "COMPLETED" + assert late_fut is not None + assert late_fut.done() + assert clock.now >= 200.0 + if producer_task is not None: + await producer_task + await asyncio.sleep(0) + assert late_fut.exception_retrieved is True + assert id(late_fut) not in client_module._DETACHED_FUTURES + late_fut = None + import gc + + gc.collect() + assert late_fut_ref() is None + assert unhandled_reports == [] + finally: + if producer_task is not None: + if not producer_task.done(): + producer_task.cancel() + try: + await producer_task + except (asyncio.CancelledError, BaseException): + pass + if late_fut is not None: + if not late_fut.done(): + late_fut.cancel() + elif not late_fut.cancelled(): + try: + late_fut.exception() + except BaseException: + pass + loop.set_exception_handler(old_h) + + # Overdue pending future failure where failure occurs after deadline + late_failing_fut: TrackedFuture | None = TrackedFuture(loop=asyncio.get_running_loop()) + late_failing_fut_ref = weakref.ref(late_failing_fut) + late_failing_producer: asyncio.Task[None] | None = None + + async def fail_after_deadline() -> None: + await asyncio.sleep(0) + clock.now += 200.0 + if late_failing_fut is not None: + late_failing_fut.set_exception(ValueError("failure after deadline")) + + def late_failing_cb(r: Any) -> asyncio.Future[None]: + nonlocal late_failing_producer + late_failing_producer = asyncio.create_task(fail_after_deadline()) + assert late_failing_fut is not None + return late_failing_fut + + unhandled_after_reports: list[dict[str, Any]] = [] + loop.set_exception_handler(lambda _loop, ctx: unhandled_after_reports.append(ctx)) + try: + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("async", server.url) as client: + res_after = await asyncio.wait_for( + client.wait_for_review( + ORG, + REVIEW, + timeout=100.0, + on_poll=late_failing_cb, + ), + timeout=5.0, + ) + assert res_after.status == "COMPLETED" + assert late_failing_fut is not None + assert late_failing_fut.done() + assert clock.now >= 200.0 + if late_failing_producer is not None: + await late_failing_producer + await asyncio.sleep(0) + assert late_failing_fut.exception_retrieved is True + assert id(late_failing_fut) not in client_module._DETACHED_FUTURES + late_failing_fut = None + gc.collect() + assert late_failing_fut_ref() is None + assert unhandled_after_reports == [] + finally: + if late_failing_producer is not None: + if not late_failing_producer.done(): + late_failing_producer.cancel() + try: + await late_failing_producer + except (asyncio.CancelledError, BaseException): + pass + if late_failing_fut is not None: + if not late_failing_fut.done(): + late_failing_fut.cancel() + elif not late_failing_fut.cancelled(): + try: + late_failing_fut.exception() + except BaseException: + pass + loop.set_exception_handler(old_h) + + # Pending future failing at exact deadline for completed review preserves COMPLETED + exact_call_start = 0.0 + async_wait_calls_exact = 0 + orig_async_wait_exact = client_module.async_wait + + async def counting_async_wait_exact(*args: Any, **kwargs: Any) -> Any: + nonlocal async_wait_calls_exact + async_wait_calls_exact += 1 + return await orig_async_wait_exact(*args, **kwargs) + + monkeypatch.setattr(client_module, "async_wait", counting_async_wait_exact) + + class ExactDeadlineCompletedFuture(TrackedFuture): + def add_done_callback(self, fn: Any, *, context: Any = None) -> None: + super().add_done_callback(fn, context=context) + clock.now = exact_call_start + 100.0 + + exact_dl_fut: ExactDeadlineCompletedFuture | None = ExactDeadlineCompletedFuture( + loop=asyncio.get_running_loop() + ) + exact_dl_ref = weakref.ref(exact_dl_fut) + + def exact_dl_cb(r: Any) -> asyncio.Future[None]: + nonlocal exact_call_start + exact_call_start = clock.now + assert exact_dl_fut is not None + return exact_dl_fut + + unhandled_exact_dl_reports: list[dict[str, Any]] = [] + loop.set_exception_handler(lambda _loop, ctx: unhandled_exact_dl_reports.append(ctx)) + try: + with Server(lambda request, index: Reply(body=responses[1])) as server: + async with session("async", server.url) as client: + res_exact_dl = await asyncio.wait_for( + client.wait_for_review( + ORG, + REVIEW, + timeout=100.0, + on_poll=exact_dl_cb, + ), + timeout=5.0, + ) + assert res_exact_dl.status == "COMPLETED" + assert exact_dl_fut is not None + assert not exact_dl_fut.done() + assert async_wait_calls_exact == 0 + assert id(exact_dl_fut) in client_module._DETACHED_FUTURES + assert exact_dl_fut.exception_retrieved is False + exact_dl_fut.cancel() + await asyncio.sleep(0) + exact_dl_fut = None + gc.collect() + assert exact_dl_ref() is None + assert unhandled_exact_dl_reports == [] + finally: + monkeypatch.setattr(client_module, "async_wait", orig_async_wait_exact) + if exact_dl_fut is not None: + if not exact_dl_fut.done(): + exact_dl_fut.cancel() + elif not exact_dl_fut.cancelled(): + try: + exact_dl_fut.exception() + except BaseException: + pass + loop.set_exception_handler(old_h) + + # Late SystemExit inside callback wrapper is suppressed after deadline + exit_task_done = asyncio.Event() + + async def late_system_exit_coro(r: Any) -> None: + try: + clock.now += 200.0 + raise SystemExit(42) + finally: + exit_task_done.set() + + with Server(lambda request, index: Reply(body=responses[0])) as server: + async with session("async", server.url) as client: + res_exit = await client.wait_for_review( + ORG, + REVIEW, + timeout=10.0, + on_poll=late_system_exit_coro, + ) + assert res_exit.status == "TIMED_OUT" + await asyncio.wait_for(exit_task_done.wait(), timeout=1.0) + + # In-budget SystemExit inside callback wrapper propagates + async def in_budget_system_exit_coro(r: Any) -> None: + raise SystemExit(42) + + with Server(lambda request, index: Reply(body=responses[0])) as server: + async with session("async", server.url) as client: + with pytest.raises(SystemExit) as exc_info: + await client.wait_for_review( + ORG, + REVIEW, + timeout=100.0, + on_poll=in_budget_system_exit_coro, + ) + assert exc_info.value.code == 42 + + # Closing loop releases strong retention from _DETACHED_FUTURES and _DETACHED_TASKS + def test_loop_close_in_thread() -> None: + t_loop = asyncio.new_event_loop() + + class TrackingFuture(asyncio.Future[Any]): + def __init__(self, loop: asyncio.AbstractEventLoop) -> None: + super().__init__(loop=loop) + self.exception_retrieved = False + + def exception(self) -> Any: + self.exception_retrieved = True + return super().exception() + + class TrackingTask(asyncio.Task[Any]): + def __init__(self, coro: Any, *, loop: asyncio.AbstractEventLoop) -> None: + super().__init__(coro, loop=loop) + self.exception_retrieved = False + + def exception(self) -> Any: + self.exception_retrieved = True + return super().exception() + + try: + asyncio.set_event_loop(t_loop) + t_fut = TrackingFuture(loop=t_loop) + t_task: TrackingTask | None = None + task_start = asyncio.Event() + + async def fail_task() -> None: + await task_start.wait() + t_loop.stop() + raise RuntimeError("task failed") + + async def run_in_t() -> None: + nonlocal t_task + t_task = TrackingTask(fail_task(), loop=t_loop) + client_module._detach_overdue_future(t_fut) + client_module._detach_overdue_task(t_task) + + t_loop.run_until_complete(run_in_t()) + assert id(t_fut) in client_module._DETACHED_FUTURES + assert getattr(t_loop, "_proofline_detached_futures", {}).get(id(t_fut)) is t_fut + assert t_task is not None + assert id(t_task) in client_module._DETACHED_TASKS + assert getattr(t_loop, "_proofline_detached_tasks", {}).get(id(t_task)) is t_task + + # Fail both targets before their done callbacks run: + task_start.set() + t_loop.run_forever() + t_fut.set_exception(RuntimeError("fut failed before close")) + + assert t_fut.done() + assert t_task.done() + assert not t_fut.exception_retrieved + assert not t_task.exception_retrieved + assert id(t_fut) in client_module._DETACHED_FUTURES + assert id(t_task) in client_module._DETACHED_TASKS + finally: + t_loop.close() + asyncio.set_event_loop(None) + assert t_fut.exception_retrieved + assert t_task is not None + assert t_task.exception_retrieved + assert id(t_fut) not in client_module._DETACHED_FUTURES + assert not getattr(t_loop, "_proofline_detached_futures", {}) + assert id(t_task) not in client_module._DETACHED_TASKS + assert not getattr(t_loop, "_proofline_detached_tasks", {}) + + # Loop that rejects assignment to close: assert future never enters _DETACHED_FUTURES + u_loop = asyncio.new_event_loop() + + class UnhookableLoopType(type(u_loop)): # type: ignore[misc] + @property + def close(self) -> Any: + return super().close + + u_loop.__class__ = UnhookableLoopType + try: + asyncio.set_event_loop(u_loop) + u_fut = u_loop.create_future() + + async def run_in_u() -> None: + client_module._detach_overdue_future(u_fut) + + u_loop.run_until_complete(run_in_u()) + assert id(u_fut) not in client_module._DETACHED_FUTURES + loop_futs = getattr(u_loop, "_proofline_detached_futures", {}) + assert loop_futs.get(id(u_fut)) is u_fut + finally: + UnhookableLoopType.close.fget(u_loop)() # type: ignore[attr-defined] + asyncio.set_event_loop(None) + assert id(u_fut) not in client_module._DETACHED_FUTURES + + await asyncio.to_thread(test_loop_close_in_thread)