7

I am trying to use finally in the following function, however, Python reports a Syntax error. I'm sure I'm doing something silly, but I can't seem to spot it ...

Snippet follows below:

# Store ids with key
# Returns GUID (used to clear table after words)
def storeIdsInTemporaryTable(dbinfo, id_list):
    conn = dbinfo['db_connection']

    guid = genutils.getGUID()
    orig_tableinfo = dbinfo['table']
    orig_datarows = dbinfo['datarows']

    metadata = dbinfo['metadata']

    sql = "INSERT INTO temporary_data_key (key) VALUES ({0}) RETURNING id".format(guid)
    key_id = executeSQLonDbConnection(dbinfo, sql, return_field='id')

    tableinfo = Table('temporary_data', metadata, autoload=True)
    datarows = []

    for id_value in id_list:
        datarows.append( { 'key_id': key_id, 'id_value': id_value} )

    try:
        insertToDb(dbinfo)
    except:
        guid = None # to indicate an error occured
        if key_id:
            conn.execute("DELETE FROM temporary_data_key WHERE key={0}".format(guid)

    finally:
        dbinfo['table'] = orig_tableinfo
        dbinfo['datarows'] = orig_datarows

    return guid

What is causing the syntax error?

As an aside, I am aware that I need to wrap the two inserts in a transaction, but for some reason, I can't get transactions to work (SQLALchemy throws a transaction related error) - so thats for another question another time..

[[Edit]]

The exception error (now fixed) was:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/script.py", line 1632
    finally:
          ^
SyntaxError: invalid syntax
4
  • 1
    Can you post the exception as well please? Commented Jun 8, 2012 at 9:39
  • 3
    @vartec CodeReview is for improving efficiency/style/etc of working code - OP has a SyntaxError, making it belong firmly here. Commented Jun 8, 2012 at 9:40
  • @lvc: it sure doesn't belong here: meta.stackexchange.com/a/87164/147630 Commented Jun 8, 2012 at 9:42
  • @vartec I'll take the point that says syntax error questions should be closed and possibly deleted as 'too trivial'. But that is a far step from 'belongs on CodeReview'. Of all *.SE, it fits here the closest - even if it does or should get closed, here was the best (or at least the least worst) place for the OP to ask it. Commented Jun 8, 2012 at 9:51

2 Answers 2

17

Are you using a Python < 2.5? try except finally was only added in 2.5 and before you had to wrap try except in a try finally.

Sign up to request clarification or add additional context in comments.

Comments

7

You're missing a closing parenthesis in your call to execute(). It should be:

conn.execute("DELETE FROM temporary_data_key WHERE key={0}".format(guid))

Instead of:

conn.execute("DELETE FROM temporary_data_key WHERE key={0}".format(guid)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.