MCPcopy Index your code
hub / github.com/RustPython/RustPython / attr_matches

Method attr_matches

Lib/rlcompleter.py:140–202  ·  view source on GitHub ↗

Compute matches when text contains a dot. Assuming the text is of the form NAME.NAME....[NAME], and is evaluable in self.namespace, it will be evaluated and its attributes (as revealed by dir()) are used as possible completions. (For class instances, class members a

(self, text)

Source from the content-addressed store, hash-verified

138 return matches
139
140 def attr_matches(self, text):
141 """Compute matches when text contains a dot.
142
143 Assuming the text is of the form NAME.NAME....[NAME], and is
144 evaluable in self.namespace, it will be evaluated and its attributes
145 (as revealed by dir()) are used as possible completions. (For class
146 instances, class members are also considered.)
147
148 WARNING: this can still invoke arbitrary C code, if an object
149 with a __getattr__ hook is evaluated.
150
151 """
152 m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
153 if not m:
154 return []
155 expr, attr = m.group(1, 3)
156 try:
157 thisobject = eval(expr, self.namespace)
158 except Exception:
159 return []
160
161 # get the content of the object, except __builtins__
162 words = set(dir(thisobject))
163 words.discard("__builtins__")
164
165 if hasattr(thisobject, '__class__'):
166 words.add('__class__')
167 words.update(get_class_members(thisobject.__class__))
168 matches = []
169 n = len(attr)
170 if attr == '':
171 noprefix = '_'
172 elif attr == '_':
173 noprefix = '__'
174 else:
175 noprefix = None
176 while True:
177 for word in words:
178 if (word[:n] == attr and
179 not (noprefix and word[:n+1] == noprefix)):
180 match = "%s.%s" % (expr, word)
181 if isinstance(getattr(type(thisobject), word, None),
182 property):
183 # bpo-44752: thisobject.word is a method decorated by
184 # `@property`. What follows applies a postfix if
185 # thisobject.word is callable, but know we know that
186 # this is not callable (because it is a property).
187 # Also, getattr(thisobject, word) will evaluate the
188 # property method, which is not desirable.
189 matches.append(match)
190 continue
191 if (value := getattr(thisobject, word, None)) is not None:
192 matches.append(self._callable_postfix(value, match))
193 else:
194 matches.append(match)
195 if matches or not noprefix:
196 break
197 if noprefix == '_':

Callers 2

completeMethod · 0.95
test_attr_matchesMethod · 0.80

Calls 15

_callable_postfixMethod · 0.95
setFunction · 0.85
dirFunction · 0.85
hasattrFunction · 0.85
get_class_membersFunction · 0.85
lenFunction · 0.85
isinstanceFunction · 0.85
getattrFunction · 0.85
evalFunction · 0.50
matchMethod · 0.45
groupMethod · 0.45
discardMethod · 0.45

Tested by 1

test_attr_matchesMethod · 0.64