Skip to content

Commit 0bbabde

Browse files
author
bob
committed
I left a "raise" in that was only there for debugging, oops, and I discovered a
problem with my __getattribute__ hackery which is now remedied
1 parent 48c85e3 commit 0bbabde

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

bpython/cli.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,22 +335,38 @@ def attr_matches(self, text):
335335
type_ = type(obj)
336336
f = None
337337
# Dark magic:
338+
# If __getattribute__ doesn't exist on the class and __getattr__ does
339+
# then __getattr__ will be called when doing
340+
# getattr(type_, '__getattribute__', None)
341+
# so we need to first remove the __getattr__, then the
342+
# __getattribute__, then look up the attributes and then restore the
343+
# original methods. :-(
344+
# The upshot being that introspecting on an object to display its
345+
# attributes will avoid unwanted side-effects.
338346
if type_ != types.InstanceType:
339-
f = getattr(type_, '__getattribute__', None)
340-
if f is not None:
347+
__getattr__ = getattr(type_, '__getattr__', None)
348+
if __getattr__ is not None:
349+
try:
350+
setattr(type_, '__getattr__', (lambda _: None))
351+
except TypeError:
352+
__getattr__ = None
353+
__getattribute__ = getattr(type_, '__getattribute__', None)
354+
if __getattribute__ is not None:
341355
try:
342356
setattr(type_, '__getattribute__', object.__getattribute__)
343357
except TypeError:
344358
# XXX: This happens for e.g. built-in types
345-
f = None
359+
__getattribute__ = None
346360
# /Dark magic
347361

348362
try:
349363
matches = self.attr_lookup(obj, expr, attr)
350364
finally:
351365
# Dark magic:
352-
if f is not None:
353-
setattr(type_, '__getattribute__', f)
366+
if __getattribute__ is not None:
367+
setattr(type_, '__getattribute__', __getattribute__)
368+
if __getattr__ is not None:
369+
setattr(type_, '__getattr__', __getattr__)
354370
# /Dark magic
355371
return matches
356372

@@ -519,7 +535,6 @@ def _complete(self, unused_tab=False):
519535
try:
520536
self.completer.complete(cw, 0)
521537
except Exception:
522-
raise
523538
# This sucks, but it's either that or list all the exceptions that could
524539
# possibly be raised here, so if anyone wants to do that, feel free to send me
525540
# a patch. XXX: Make sure you raise here if you're debugging the completion

0 commit comments

Comments
 (0)