Invalid Syntax in except handler when using comma

时光总嘲笑我的痴心妄想 提交于 2019-12-17 16:32:53

问题


I am a beginner in Python and have been testing different kinds of sample code. When I started using Python3 instead of 2.7; I came upon a syntax error, but I don't understand how to fix that error.

  File "app.py", line 101
    except InvalidUserPass, e:
                          ^
SyntaxError: invalid syntax

Here is more of that code to give some context:

@app.route('/login/', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        session['username'] = request.form['username']
        session['password'] = request.form['password']
        try:
            # use reddit_api's login
            r.login(user=session['username'], password=session['password'])
        except InvalidUserPass, e:
            error = 'Incorrect username or password. '
        if not error:
            subreddits = r.user.get_my_reddits(limit=25)
            my_reddits = []
            for i in range(25):
                my_reddits.append(subreddits.next().display_name)
            session['my_reddits'] = my_reddits
            return redirect(url_for('index'))
    return render_template('login.html', error=error)

回答1:


Change

except InvalidUserPass, e:

to

except InvalidUserPass as e:

See this for more info.




回答2:


Simply except InvalidUserPass as e:. And for heaven's sake, let's get rid of the ugly error thing:

@app.route('/login/', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        session['username'] = request.form['username']
        session['password'] = request.form['password']

        try:
            # use reddit_api's login
            r.login(user=session['username'], password=session['password'])
        except InvalidUserPass as e:
            return render_template('login.html', 
                                   error='Incorrect username or password.')

        subreddits = r.user.get_my_reddits(limit=25)
        my_reddits = []
        for i in range(25):
            my_reddits.append(subreddits.next().display_name)
        session['my_reddits'] = my_reddits
        return redirect(url_for('index'))

    return render_template('login.html')



回答3:


In python3 it's:

except InvalidUserPass as e:



回答4:


file /usr/libexec/urlgrabber-ext-down line 28 except oserror e invalid syntax

modify /usr/bin/yum and /usr/libexec/urlgrabber-ext-dow files

#!/usr/bin/python to #!/usr/bin/python2

the issue would be resolved.



来源:https://stackoverflow.com/questions/12519554/invalid-syntax-in-except-handler-when-using-comma

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