Catching AttributeError exceptions in widget media

32 views
Skip to first unread message

Michael Jones

unread,
Sep 3, 2014, 11:37:40 AM9/3/14
to django...@googlegroups.com
Hi,

I love Django, it has been enormously useful to a new comer to web dev like me. 

One minor issue I've just had is that I've struggled to figure out an issue with my code because an AttributeError I was generating in my media property on my custom widget was being silently swallowed by:

    https://github.com/django/django/blob/09c1f18f223cd60c948f7dae6aedccea70a92274/django/forms/widgets.py#L113

Is there a chance of changing it so that that doesn't happen? Maybe do a `hasattr` test instead of the try-catch? There is no feedback otherwise. You're media just doesn't appear on the page. I guess if I was a more experienced debugger I might find this easier to figure out.

I am inexperienced but I have found that catching such broad exceptions as AttributeError around something that can call other functions causes pain from time to time. I guess `media` was originally meant to be just a class or instance attribute but it seem to frequently be made into a property with the potential for non-trivial code being called from it.

I am not sure if this is best here or suggested on the developers mailing list. I'm happy to repost if someone could offer me guidance.

Thanks,
Michael

Collin Anderson

unread,
Sep 3, 2014, 9:45:22 PM9/3/14
to django...@googlegroups.com
unfortunately hasattr eats even more exceptions than AttributeError.

Michael Jones

unread,
Sep 4, 2014, 11:29:38 AM9/4/14
to django...@googlegroups.com
Hi,

Thanks for the reply, but I'm not sure I follow. I was hoping that the following might work:

    if hasattr(sup_cls, 'media'):
        base = sup_cls.media
    else:
        base = Media()

Would that swallow additional exceptions?

Cheers,
Michael

Collin Anderson

unread,
Sep 4, 2014, 4:04:36 PM9/4/14
to django...@googlegroups.com
believe it or not, hasattr is the same as:

try:
    getattr(obj, name)
    return True
except:  # yikes! naked except!
    return False

So, excepting AttributeError will at least narrow down the error slightly, rather than hasattr accepting _any_ exception.


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/K26vVvKWnTs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9db016be-bc63-44b4-934c-7b2c64ae9863%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Zachary McCord

unread,
Sep 4, 2014, 4:20:18 PM9/4/14
to django...@googlegroups.com
I simply didn't believe it at first, but sure enough, in python 2.7:

>>> class Bomb(object):
...     def __getattr__(self, id):
...         assert False
...  
>>> bomb = Bomb()
>>> bomb.foo
Traceback (most recent call last):
 
File "<stdin>", line 1, in <module>
 
File "<stdin>", line 3, in __getattr__
AssertionError
>>> hasattr(bomb, 'foo')
False

Thankfully Python 3 fixes this. One more reason to switch, I guess.

Michael Jones

unread,
Sep 5, 2014, 9:50:35 AM9/5/14
to django...@googlegroups.com
Thanks for the clarification guys. That is truly horrific :)

Sorry not to have tested my suggestion first. 

Michael
Reply all
Reply to author
Forward
0 new messages