Where is the syntax error with **finally:* clause?
Question: I'm trying to run Selenium tests for a Django app on production server.

I am getting a syntax error on the **finally:** clause.

I don't see where the error is and all the tests ran fine in development.

Here is the code:

    
    
     def activate_revision(self, user, revision):  
        self.title = revision.title  
        self.tagnames = revision.tagnames  
    
        self.body = self.rendered(revision.body)  
    
        self.active_revision = revision  
    
        # Try getting the previous revision  
        try:  
            prev_revision = NodeRevision.objects.get(node=self, revision=revision.revision-1)  
    
            update_activity = True  
    
            # Do not update the activity if only the tags are changed  
            if prev_revision.title == revision.title and prev_revision.body == revision.body \  
            and prev_revision.tagnames != revision.tagnames and not settings.UPDATE_LATEST_ACTIVITY_ON_TAG_EDIT:  
                update_activity = False  
        except NodeRevision.DoesNotExist:  
            update_activity = True  
        finally:  
            if update_activity:  
                self.update_last_activity(user)  
        self.save()  
    

Here is the traceback:

    
    
    $ python manage.py test forum  
    Traceback (most recent call last):  
      File "/usr/lib/python2.4/logging/__init__.py", line 731, in emit  
        msg = self.format(record)  
      File "/usr/lib/python2.4/logging/__init__.py", line 617, in format  
        return fmt.format(record)  
      File "/usr/lib/python2.4/logging/__init__.py", line 408, in format  
        s = self._fmt % record.__dict__  
    KeyError: 'funcName'  
    /home/spirituality/lib/python2.7/Django-1.3.1-py2.7.egg/django/db/models/fields/subclassing.py:80: DeprecationWarning: A Field class whose get_db_prep_lookup method hasn't been updated to take `connection` and `prepared` arguments.  
      new_class = super(SubfieldBase, cls).__new__(cls, name, bases, attrs)  
    /home/spirituality/lib/python2.7/Django-1.3.1-py2.7.egg/django/db/models/fields/subclassing.py:80: DeprecationWarning: A Field class whose get_db_prep_value method hasn't been updated to take `connection` and `prepared` arguments.  
      new_class = super(SubfieldBase, cls).__new__(cls, name, bases, attrs)  
    Traceback (most recent call last):  
      File "manage.py", line 13, in ?  
        execute_manager(settings)  
      File "/home/spirituality/lib/python2.7/Django-1.3.1-py2.7.egg/django/core/management/__init__.py", line 438, in execute_manager  
        utility.execute()  
      File "/home/spirituality/lib/python2.7/Django-1.3.1-py2.7.egg/django/core/management/__init__.py", line 379, in execute  
        self.fetch_command(subcommand).run_from_argv(self.argv)  
      File "/home/spirituality/lib/python2.7/Django-1.3.1-py2.7.egg/django/core/management/base.py", line 191, in run_from_argv  
        self.execute(*args, **options.__dict__)  
      File "/home/spirituality/lib/python2.7/Django-1.3.1-py2.7.egg/django/core/management/base.py", line 220, in execute  
        output = self.handle(*args, **options)  
      File "/home/spirituality/lib/python2.7/South-0.7.3-py2.7.egg/south/management/commands/test.py", line 8, in handle  
        super(Command, self).handle(*args, **kwargs)  
      File "/home/spirituality/lib/python2.7/Django-1.3.1-py2.7.egg/django/core/management/commands/test.py", line 37, in handle  
        failures = test_runner.run_tests(test_labels)  
      File "/home/spirituality/lib/python2.7/Django-1.3.1-py2.7.egg/django/test/simple.py", line 358, in run_tests  
        suite = self.build_suite(test_labels, extra_tests)  
      File "/home/spirituality/lib/python2.7/Django-1.3.1-py2.7.egg/django/test/simple.py", line 247, in build_suite  
        app = get_app(label)  
      File "/home/spirituality/lib/python2.7/Django-1.3.1-py2.7.egg/django/db/models/loading.py", line 129, in get_app  
        self._populate()  
      File "/home/spirituality/lib/python2.7/Django-1.3.1-py2.7.egg/django/db/models/loading.py", line 61, in _populate  
        self.load_app(app_name, True)  
      File "/home/spirituality/lib/python2.7/Django-1.3.1-py2.7.egg/django/db/models/loading.py", line 78, in load_app  
        models = import_module('.models', app_name)  
      File "/home/spirituality/lib/python2.7/Django-1.3.1-py2.7.egg/django/utils/importlib.py", line 35, in import_module  
        __import__(name)  
      File "/home/spirituality/webapps/spirituality/spirit_app/forum/models/__init__.py", line 2, in ?  
        from question import Question ,QuestionRevision, QuestionSubscription  
      File "/home/spirituality/webapps/spirituality/spirit_app/forum/models/question.py", line 1, in ?  
        from base import *  
      File "/home/spirituality/webapps/spirituality/spirit_app/forum/models/base.py", line 349, in ?  
        from node import Node, NodeRevision, NodeManager  
      File "/home/spirituality/webapps/spirituality/spirit_app/forum/models/node.py", line 383  
        finally:  
              ^  
    SyntaxError: invalid syntax  
    


Answer: First part of the traceback suggests that it's Python 2.4 on production. As
per my comment above, the problem is that `try..except..finally` is only for
Python 2.5 and newer. Upgrade production or rewrite the code to nest
`try..except` inside an outer `try..finally`.

