(
value: Any, handles: List[Channel], visitor_info: Optional[VisitorInfo] = None
)
| 130 | |
| 131 | |
| 132 | def serialize_value( |
| 133 | value: Any, handles: List[Channel], visitor_info: Optional[VisitorInfo] = None |
| 134 | ) -> Any: |
| 135 | if visitor_info is None: |
| 136 | visitor_info = VisitorInfo() |
| 137 | if isinstance(value, JSHandle): |
| 138 | h = len(handles) |
| 139 | handles.append(value._channel) |
| 140 | return dict(h=h) |
| 141 | if value is None: |
| 142 | return dict(v="null") |
| 143 | if isinstance(value, float): |
| 144 | if value == float("inf"): |
| 145 | return dict(v="Infinity") |
| 146 | if value == float("-inf"): |
| 147 | return dict(v="-Infinity") |
| 148 | if value == float("-0"): |
| 149 | return dict(v="-0") |
| 150 | if math.isnan(value): |
| 151 | return dict(v="NaN") |
| 152 | if isinstance(value, datetime.datetime): |
| 153 | # Node.js Date objects are always in UTC. |
| 154 | return { |
| 155 | "d": datetime.datetime.strftime( |
| 156 | value.astimezone(datetime.timezone.utc), "%Y-%m-%dT%H:%M:%S.%fZ" |
| 157 | ) |
| 158 | } |
| 159 | if isinstance(value, Exception): |
| 160 | return { |
| 161 | "e": { |
| 162 | "m": str(value), |
| 163 | "n": ( |
| 164 | (value.name or "") |
| 165 | if isinstance(value, Error) |
| 166 | else value.__class__.__name__ |
| 167 | ), |
| 168 | "s": ( |
| 169 | (value.stack or "") |
| 170 | if isinstance(value, Error) |
| 171 | else "".join( |
| 172 | traceback.format_exception(type(value), value=value, tb=None) |
| 173 | ) |
| 174 | ), |
| 175 | } |
| 176 | } |
| 177 | if isinstance(value, bool): |
| 178 | return {"b": value} |
| 179 | if isinstance(value, (int, float)): |
| 180 | return {"n": value} |
| 181 | if isinstance(value, str): |
| 182 | return {"s": value} |
| 183 | if isinstance(value, ParseResult): |
| 184 | return {"u": urlunparse(value)} |
| 185 | |
| 186 | if value in visitor_info.visited: |
| 187 | return dict(ref=visitor_info.visited[value]) |
| 188 | |
| 189 | if isinstance(value, collections.abc.Sequence) and not isinstance(value, str): |
no test coverage detected