NameError: 全局名称 'has_no_changeset' 未定义

1 投票
1 回答
1098 浏览
提问于 2025-04-15 12:21

好的,我是个Python新手,我想我可能做了什么很傻的事情,你能告诉我是什么吗,这样我们都能继续过我们的生活了?

我在第55行遇到了一个错误,错误信息是 NameError: global name 'has_no_changeset' is not defined(意思是找不到名为'has_no_changeset'的全局变量),这行代码是我在尝试调用函数 has_no_changeset 的地方。

from genshi.builder import tag

from trac.core import implements,Component
from trac.ticket.api import ITicketManipulator
from trac.ticket.default_workflow import ConfigurableTicketWorkflow
from trac.perm import IPermissionRequestor
from trac.config import Option, ListOption
import re

revision = "$Rev$"
url = "$URL$"

class CloseActionController(Component):
    """Support for close checking.

    If a ticket is closed, it is NOT allowed if ALL the following conditions apply: 
     a) ticket is 'bug' ticket
     b) resolution status is 'fixed'
     c) none of the ticket's changes include a comment containing a changeset, i.e. regex "\[\d+\]"
     d) the ticket does not have the keyword 'web'
    """

    implements(ITicketManipulator)

    # ITicketManipulator methods

    def prepare_ticket(req, ticket, fields, actions):
        """Not currently called, but should be provided for future
        compatibility."""
        return


    def has_no_changeset(ticket):
        db = self.env.get_db_cnx()
        cursor = db.cursor()

        cursor.execute("SELECT newvalue FROM ticket_change WHERE ticket=%s AND field='comment'", (str(ticket.id).encode('ascii','replace'),))

        for newvalue, in cursor:
            if re.search("\[\d{5,}\]", newvalue):
                return False

        return True

    def validate_ticket(me, req, ticket):
        """Validate a ticket after it's been populated from user input.

        Must return a list of `(field, message)` tuples, one for each problem
        detected. `field` can be `None` to indicate an overall problem with the

        ticket. Therefore, a return value of `[]` means everything is OK."""

        if ticket['type'] == 'bug' and ticket['resolution'] == 'fixed':
          if ticket['keywords'] == None or ticket['keywords'].find('web') == -1:
            if has_no_changeset(ticket):
              return [(None, 'You can only close a bug ticket as "fixed" if you refer to a changeset somewhere within the ticket, e.g. with [12345]!')]

        return[]

1 个回答

4

当你想调用当前类的方法时,需要明确地写出 self(在你的例子中是 me)。

if me.has_no_changeset(ticket):

你使用了 me 而不是 self,这样是可以的,但不推荐这么做。类里面的方法的第一个参数应该叫 self

def validate_ticket(self, req, ticket):
    # [...]
    if self.has_no_changeset(ticket):

撰写回答