** finally:*子句的语法错误在哪里?

时间:2012-06-28 18:30:07

标签: django testing try-catch-finally

我正在尝试为生产服务器上的Django应用程序运行Selenium测试。

我在 finally:子句中遇到语法错误。

我没有看到错误在哪里,所有测试都在开发中运行良好。

以下是代码:

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

这是追溯:

$ 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  

1 个答案:

答案 0 :(得分:1)

回溯的第一部分表明它是生产中的Python 2.4。根据我上面的评论,问题是try..except..finally仅适用于Python 2.5及更高版本。升级生产或重写代码以将try..except嵌套在外部try..finally内。