MCPcopy Index your code
hub / github.com/python/cpython / attr_matches

Method attr_matches

Lib/rlcompleter.py:141–213  ·  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

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

Callers 1

completeMethod · 0.95

Calls 11

_callable_postfixMethod · 0.95
setFunction · 0.85
get_class_membersFunction · 0.85
matchMethod · 0.45
groupMethod · 0.45
discardMethod · 0.45
addMethod · 0.45
updateMethod · 0.45
appendMethod · 0.45
getMethod · 0.45
sortMethod · 0.45

Tested by

no test coverage detected