django signals: fail silently? any better way of debugging mistakes?

☆樱花仙子☆ 提交于 2019-12-11 03:49:38

问题


It seems that django signals has a "fail silently" paradigm.

When I make a small spelling error in my signals function, for example:-

def new_users_handler(send, user, response, details, **kwargs):
    print "new_users_handler function executes"
    user.is_new = True
    if user.is_new:
        if "id" in response:
            from urllib2 import urlopen, HTTPError
            from django.template.defaultfilters import slugify
            from django.core.files.base import ContentFile

            try:
                url = None
                if sender == FacebookBackend:
                    url = "http://graph.facebook.com/%s/picture?type=large" \
                            % response["id"]
                elif sender == google.GoogleOAuth2Backend and "picture" in response:
                    url = response["picture"]
    ......
    socialauth_registered.connect(new_users_handler, sender=None)

I use "send" as my argument instead of "sender", I don't really get any useful error message or debug information in my devserver stdout.

Is there a good way of making sure that signal failures/error messages get shown loud and clear?

In my example above, this could have been a "5 minutes" fix if there was a proper error message telling me that

name "sender" is not defined

but instead, there wasn't any error messages and I was looking everywhere in my code base to try to figure out why my signals isn't getting called... not cool.

Any advice welcome!


回答1:


Not quite what you are asking for, but your problem could also have been solved with a static analyser like pyflakes.

From pypi:

Pyflakes is program to analyze Python programs and detect various errors. It works by parsing the source file, not importing it, so it is safe to use on modules with side effects. It's also much faster.

sample output:

tmp.py:9: 'ContentFile' imported but unused
tmp.py:13: undefined name 'sender'

I have it integrated into my editor (vim, but i've also seen it in several others), highlighting my typos as i go, or on save.



来源:https://stackoverflow.com/questions/9511297/django-signals-fail-silently-any-better-way-of-debugging-mistakes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!