| 241 | # DictResolver |
| 242 | # ======================================================================================================================= |
| 243 | class DictResolver: |
| 244 | sort_keys = not IS_PY36_OR_GREATER |
| 245 | |
| 246 | def resolve(self, dct, key): |
| 247 | if key in (GENERATED_LEN_ATTR_NAME, TOO_LARGE_ATTR): |
| 248 | return None |
| 249 | |
| 250 | if "(" not in key: |
| 251 | # we have to treat that because the dict resolver is also used to directly resolve the global and local |
| 252 | # scopes (which already have the items directly) |
| 253 | try: |
| 254 | return dct[key] |
| 255 | except: |
| 256 | return getattr(dct, key) |
| 257 | |
| 258 | # ok, we have to iterate over the items to find the one that matches the id, because that's the only way |
| 259 | # to actually find the reference from the string we have before. |
| 260 | expected_id = int(key.split("(")[-1][:-1]) |
| 261 | for key, val in dct.items(): |
| 262 | if id(key) == expected_id: |
| 263 | return val |
| 264 | |
| 265 | raise UnableToResolveVariableException() |
| 266 | |
| 267 | def key_to_str(self, key, fmt=None): |
| 268 | if fmt is not None: |
| 269 | if fmt.get("hex", False): |
| 270 | safe_repr = SafeRepr() |
| 271 | safe_repr.convert_to_hex = True |
| 272 | return safe_repr(key) |
| 273 | return "%r" % (key,) |
| 274 | |
| 275 | def init_dict(self): |
| 276 | return {} |
| 277 | |
| 278 | def get_contents_debug_adapter_protocol(self, dct, fmt=None): |
| 279 | """ |
| 280 | This method is to be used in the case where the variables are all saved by its id (and as |
| 281 | such don't need to have the `resolve` method called later on, so, keys don't need to |
| 282 | embed the reference in the key). |
| 283 | |
| 284 | Note that the return should be ordered. |
| 285 | |
| 286 | :return list(tuple(name:str, value:object, evaluateName:str)) |
| 287 | """ |
| 288 | ret = [] |
| 289 | |
| 290 | i = 0 |
| 291 | |
| 292 | found_representations = set() |
| 293 | |
| 294 | for key, val in dct.items(): |
| 295 | i += 1 |
| 296 | key_as_str = self.key_to_str(key, fmt) |
| 297 | |
| 298 | if key_as_str not in found_representations: |
| 299 | found_representations.add(key_as_str) |
| 300 | else: |
no outgoing calls