getattr()可以找到一些函数,但不能找到其他函数

时间:2014-09-26 00:45:00

标签: python getattr heisenbug

作为学习项目的一部分,我正在编写一个简单的IRC机器人,并且我已经实现了简单的文本功能,如!auth!join!say等。为了方便添加功能,我已经在python中实现了bot_auth()bot_join()bot_say()等每个功能,并对bot_NOCOMMAND()进行了规避。记录错误。

我遇到的问题是虽然getattr()按预期找到了大多数功能,但joinpart功能却没有,我无法理解为什么。

代码:

def process_bot_command(self, prefix, params, trailing):
        # eg: !command some arguments
        try:
            bot_command,bot_args = trailing[1:].split(' ',1)
            bot_command = bot_command.lower().strip()
            bot_args = bot_args.strip()
        except ValueError:
            bot_command = trailing[1:].split(' ')[0].lower().strip()
            bot_args = ''

        try:
            function = getattr(self, 'bot_'+bot_command)
            function(prefix, params[0], bot_command, bot_args)
        except AttributeError:
            self.bot_NOCOMMAND(prefix, params[0], bot_command, bot_args)

def bot_NOCOMMAND(self, c_from, c_to, command, args):
    self.log('Undefined command: %s' % ','.join([c_from, c_to, command, args]), 1)

def bot_auth(self, c_from, c_to, command, args):
    user = self.parsePrefix(c_from)['name']
    if self.authenticate(user, args):
        self.irc_PRIVMSG(user, 'Authentication successful.')
        self.admins.append(c_from)
        self.log('Added %s to active admin list.' % c_from)
    else:
        self.irc_PRIVMSG(user, 'Authentication failed.')

# TODO: doesn't work for some reason
def bot_join(self, c_from, c_to, command, args):
    if not self.isAdmin(c_from):
        self.respond(c_from, c_to, self.str['unauth'])
        return
    channel = args.split(' ')[0].strip()
    self.irc_JOIN(channel)
    self.respond(c_from, c_to, 'Joining %s' % channel)

# TODO: doesn't work for some reason
def bot_part(self, c_from, c_to, command, args):
    if not self.isAdmin(c_from):
        self.respond(c_from, c_to, self.str['unauth'])
        return
    channel = args.split(' ')[0].strip()
    if channel in self.subscribed_channels:
        self.irc_PART(channel, 'Leaving channel')
        self.respond(c_from, c_to, 'Left %s' % channel)

def bot_say(self, c_from, c_to, command, args):
    if not self.isAdmin(c_from):
        self.respond(c_from, c_to, self.str['unauth'])
        return
    if args[0] == '#':
        channel,text = args.split(' ', 1)
        self.irc_PRIVMSG(channel, text)
    else:
        self.respond(c_from, c_to, args)

基本流程:

  1. 如果处理传入的PRIVMSG的函数在!数据的第一个字符中找到trailing,则将数据发送到process_bot_command()
  2. process_bot_command()弹出trailing字符串的第一部分,删除!,预先bot_,然后调用getattr()以查找具有该功能的函数名称
  3. 如果找到某个函数,则将输入传递给函数。
  4. 如果找不到函数,则输入传递给bot_NOCOMMAND(),它将输入作为错误转储到日志中。
  5. 示例输出:

    > Sammitch!~Sammitch@hostname.tld PRIVMSG JerkfaceMcAssbot :!auth password
    * Authentication request for user: Sammitch succeeded.
    < PRIVMSG Sammitch :Authentication successful.
    * Added Sammitch!~Sammitch@hostname.tld to active admin list.
    > Sammitch!~Sammitch@hostname.tld PRIVMSG JerkfaceMcAssbot :!identify
    < PRIVMSG nickserv :IDENTIFY password
    < PRIVMSG Sammitch :Sent identify command.
    > NickServ!NickServ@services. NOTICE JerkfaceMcAssbot :You are now identified for JerkfaceMcAssbot.
    > Sammitch!~Sammitch@hostname.tld PRIVMSG JerkfaceMcAssbot :!join ##systemadmins
    ! Undefined command: Sammitch!~Sammitch@hostname.tld,JerkfaceMcAssbot,join,##systemadmins
    > Sammitch!~Sammitch@hostname.tld PRIVMSG JerkfaceMcAssbot :!part KitchenCommandCenter
    ! Undefined command: Sammitch!~Sammitch@hostname.tld,JerkfaceMcAssbot,part,KitchenCommandCenter
    

    例如:

    ! Undefined command: Sammitch!~Sammitch@hostname.tld,JerkfaceMcAssbot,join,##systemadmins
    

    表示get_attr()无法找到名为bot_join()的函数。

    我无法弄清楚为什么只有bot_join()bot_part()函数不想工作而其他所有函数都运行得很好。任何人都可以告诉我这里可能出现的问题吗?

    更新

    我从process_bot_command()注释掉了try / catch以使其死亡,并在评论中为你们提供了例外,并且join / part函数开始工作。然后我把它添加回去,看看我是否能弄清楚它为什么会破坏,但代码仍在继续工作。

    我不知道为什么,但我猜问题已经解决了?

0 个答案:

没有答案
相关问题