Skip to content

Commit

Permalink
expand referrer patching to defaultdict
Browse files Browse the repository at this point in the history
  • Loading branch information
smacke committed Nov 8, 2023
1 parent 9fc6643 commit e35ebf2
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions core/superduperreload/patching.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import functools
import gc
import sys
from collections import defaultdict
from enum import Enum
from types import FunctionType, MethodType
from typing import Callable, Dict, List, Optional, Set, Sized, Tuple, Type, Union
Expand Down Expand Up @@ -104,9 +105,11 @@ def __init__(self, patch_referrers: bool, **kwargs) -> None:
]

self._patch_referrers: bool = patch_referrers
self._referrer_patch_rules: List[Tuple[Type[Sized], Callable[..., None]]] = [
(list, self._patch_list_referrer),
(dict, self._patch_dict_referrer),
self._referrer_patch_rules: List[
Tuple[Tuple[Type[Sized], ...], Callable[..., None]]
] = [
((list,), self._patch_list_referrer),
((dict, defaultdict), self._patch_dict_referrer),
]

@classmethod
Expand Down Expand Up @@ -371,8 +374,8 @@ def _patch_referrers_generic(self, old: object, new: object) -> None:
if len(referrers) > _MAX_REFERRERS_FOR_PATCHING:
return
for referrer in referrers:
for typ, referrer_patcher in self._referrer_patch_rules:
if type(referrer) is typ:
for types, referrer_patcher in self._referrer_patch_rules:
if type(referrer) in types:
if len(referrer) <= _MAX_REFERRER_LENGTH_FOR_PATCHING:
referrer_patcher(referrer, old, new)
break

0 comments on commit e35ebf2

Please sign in to comment.