Invalid syntax (SyntaxError) in except handler when using comma
Asked Answered
N

5

20

I have this code:

@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)

In 2.x, it worked fine, but in 3.x I get an error message like:

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

Why does this occur, and how can I fix it?

Nubile answered 20/9, 2012 at 19:26 Comment(1)
read PEP 3110: Exception-Handling ChangesEckstein
E
33

Change

except InvalidUserPass, e:

to

except InvalidUserPass as e:

See this for more info.

Endocrinology answered 20/9, 2012 at 19:28 Comment(0)
S
7

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')
Shawn answered 20/9, 2012 at 19:27 Comment(0)
E
3

In python3 it's:

except InvalidUserPass as e:
Ennead answered 20/9, 2012 at 19:28 Comment(0)
A
1

In Python 2.x, the syntax except ExampleError, e: means that exceptions of the type ExampleError will be caught, and the name e will be used for that exception inside the except block.

In 3.x, the closest equivalent syntax is except ExampleError as e:. (This will also explicitly delete the name e after the except block has ended, unlike in 2.x where it will remain defined.)

If this error occurs in your own code, simply fix it accordingly.

If this error occurs in library code (example, example), this indicates that either the library does not support modern versions of Python, or else the installation is out of date and upgrading to a newer library version is necessary. Please read the documentation for the library in order to check version compatibility, and do not try to fix it yourself (unless you intend to take over the entire project.)

Arrow answered 19/1, 2023 at 6:18 Comment(0)
I
-3

When getting the error

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

modify /usr/bin/yum and /usr/libexec/urlgrabber-ext-dow files by changing #!/usr/bin/python to #!/usr/bin/python2.

issue would be resolved.

Isma answered 21/8, 2018 at 7:58 Comment(2)
the issue would be resolved... Why? Please explain your answer a bit more also for future visitors of the pageAaron
This isn't just wrong, it's completely nonsensical - and it comes years after the one correct answer was given by multiple other people.Arrow

© 2022 - 2024 — McMap. All rights reserved.