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?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153

5 Answers5

33

Change

except InvalidUserPass, e:

to

except InvalidUserPass as e:

See this for more info.

arshajii
  • 127,459
  • 24
  • 238
  • 287
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')
Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83
3

In python3 it's:

except InvalidUserPass as e:
JFiveTwo
  • 68
  • 3
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.)

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
-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.

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
  • 1
    _the issue would be resolved..._ Why? Please explain your answer a bit more also for future visitors of the page – B001ᛦ Aug 21 '18 at 08:21
  • 1
    This isn't just wrong, it's completely nonsensical - and it comes years after the one correct answer was given by multiple other people. – Karl Knechtel Jan 19 '23 at 04:42