Debian Codegraph @

rpython/rtyper/extregistry.py

raw symbols SCIP
1 import weakref
2 import UserDict
3 from rpython.tool.uid import Hashable
4
5
6 class AutoRegisteringType(type):
7
8 def __init__(selfcls, name, bases, dict):
9 type.__init__(selfcls, name, bases, dict)
10 if '_about_' in dict:
11 selfcls._register_value(dict['_about_'])
12 del selfcls._about_ # avoid keeping a ref
13 if '_type_' in dict:
14 selfcls._register_type(dict['_type_'])
15 del selfcls._type_
16
17 def _register(selfcls, dict, key):
18 if isinstance(key, tuple):
19 for k in key:
20 selfcls._register(dict, k)
21 else:
22 if key in dict:
23 raise ValueError("duplicate extregistry entry %r" % (selfcls,))
24 dict[key] = selfcls
25
26 def _register_value(selfcls, key):
27 selfcls._register(EXT_REGISTRY_BY_VALUE, key)
28
29 def _register_type(selfcls, key):
30 selfcls._register(EXT_REGISTRY_BY_TYPE, key)
31
32
33 class ExtRegistryEntry(object):
34 __metaclass__ = AutoRegisteringType
35
36 def __init__(self, type, instance=None):
37 self.type = type
38 self.instance = instance
39
40 # structural equality, and trying hard to be hashable: Entry instances
41 # are used as keys to map annotations to Reprs in the rtyper.
42 # Warning, it's based on only 'type' and 'instance'.
43 def __eq__(self, other):
44 return (self.__class__ is other.__class__ and
45 self.type == other.type and
46 self.instance == other.instance)
47
48 def __ne__(self, other):
49 return not self.__eq__(other)
50
51 def __hash__(self):
52 return hash((self.__class__, self.type, Hashable(self.instance)))
53
54 def compute_annotation_bk(self, bk):
55 self.bookkeeper = bk
56 return self.compute_annotation()
57
58 def compute_annotation(self):
59 # callers should always use compute_annotation_bk()!
60 # default implementation useful for built-in functions,
61 # can be overriden.
62 func = self.instance
63 assert func is not None
64 from rpython.annotator import model as annmodel
65 analyser = self.compute_result_annotation
66 methodname = getattr(func, '__name__', None)
67 return annmodel.SomeBuiltin(analyser, methodname=methodname)
68
69 def compute_result_annotation(self, *args_s, **kwds_s):
70 # default implementation for built-in functions with a constant
71 # result annotation, can be overriden
72 return self.s_result_annotation
73
74 # ____________________________________________________________
75
76 class FlexibleWeakDict(UserDict.DictMixin):
77 """A WeakKeyDictionary that accepts more or less anything as keys:
78 weakly referenceable objects or not, hashable objects or not.
79 """
80 def __init__(self):
81 self._regdict = {}
82 self._weakdict = weakref.WeakKeyDictionary()
83 self._iddict = {}
84
85 def ref(self, key):
86 try:
87 hash(key)
88 except TypeError:
89 return self._iddict, Hashable(key) # key is not hashable
90 try:
91 weakref.ref(key)
92 except TypeError:
93 return self._regdict, key # key cannot be weakly ref'ed
94 else:
95 return self._weakdict, key # normal case
96
97 def __getitem__(self, key):
98 d, key = self.ref(key)
99 return d[key]
100
101 def __setitem__(self, key, value):
102 d, key = self.ref(key)
103 d[key] = value
104
105 def __delitem__(self, key):
106 d, key = self.ref(key)
107 del d[key]
108
109 def keys(self):
110 return (self._regdict.keys() +
111 self._weakdict.keys() +
112 [hashable.value for hashable in self._iddict])
113
114
115 EXT_REGISTRY_BY_VALUE = FlexibleWeakDict()
116 EXT_REGISTRY_BY_TYPE = weakref.WeakKeyDictionary()
117
118 # ____________________________________________________________
119 # Public interface to access the registry
120
121 def _lookup_type_cls(tp):
122 return EXT_REGISTRY_BY_TYPE[tp]
123
124 def lookup_type(tp):
125 Entry = _lookup_type_cls(tp)
126 return Entry(tp)
127
128 def is_registered_type(tp):
129 return tp in EXT_REGISTRY_BY_TYPE
130
131 def _lookup_cls(instance):
132 try:
133 return EXT_REGISTRY_BY_VALUE[instance]
134 except (KeyError, TypeError):
135 return _lookup_type_cls(type(instance))
136
137 def lookup(instance):
138 Entry = _lookup_cls(instance)
139 return Entry(type(instance), instance)
140
141 def is_registered(instance):
142 try:
143 _lookup_cls(instance)
144 except KeyError:
145 return False
146 return True