gdude2002
gdude2002

Reputation: 197

Python - Local variable error where variable is an imported module

So, I've been working on an IRC bot lately. It stores the last time someone did something as part of an antispam feature. It used to initialize the lists of users in a WHO reply, which I've been moving to use RPL_NAMREPLY.

It almost works. However, I have a very odd-looking problem..

import time

# ...

    def irc_unknown(self, prefix, command, params):
        """Handle packets that aren't handled by the library."""

        # Prefix: asimov.freenode.net
        # Command: RPL_BANLIST
        # Params: ['MCBans_Testing', '#mcbans-test', 'a!*@*', 'gdude2002!g@unaffiliated/gdude2002', '1330592882']

        self.runHook("unknownMessage", {"prefix": prefix, "command": command, "params": params})

        if command == "RPL_BANLIST":
            channel = params[1]
            mask = params[2]
            owner = params[3]
            time = params[4]

            if channel not in self.banlist.keys():
                done = {"done": False, "total": 1}
                banmask = {"owner": owner.split("!")[0], "ownerhost": owner, "time": time, "mask": mask,
                           "channel": channel}
                done[mask] = banmask
                self.banlist[channel] = done

            else:
                if not self.banlist[channel]["done"]:
                    banmask = {"owner": owner.split("!")[0], "ownerhost": owner, "time": time, "mask": mask,
                               "channel": channel}
                    self.banlist[channel][mask] = banmask
                    self.banlist[channel]["total"] += 1

                else:
                    done = {"done": False, "total": 1}
                    banmask = {"owner": owner.split("!")[0], "ownerhost": owner, "time": time, "mask": mask,
                               "channel": channel}
                    done[mask] = banmask
                    self.banlist[channel] = done

        elif command == "RPL_ENDOFBANLIST":
            channel = params[1]

            if channel in self.banlist.keys():
                self.banlist[channel]["done"] = True
            else:
                self.banlist[channel] = {"done": True, "total": 0}

            self.prnt("|= Got %s bans for %s." % (self.banlist[channel]["total"], channel))

            if self.is_op(channel, self.nickname):
                stuff = self.banlist[channel].keys()
                stuff.remove("done")
                stuff.remove("total")

                for element in stuff:
                    if stuff == "*!*@*":
                        self.send_raw("KICK %s %s :Do not set such ambiguous bans!" % (
                        channel, self.banlist[channel][element]["owner"]))
                        self.send_raw("MODE %s -b *!*@*" % channel)
                        self.send_raw(
                            "MODE %s +b *!*@%s" % (channel, self.banlist[channel][element]["ownerhost"].split("@")[1]))
                    else:
                        self.checkban(channel, element, self.banlist[channel][element]["owner"])

        elif command == "RPL_NAMREPLY":
            me, status, channel, names = params
            users = names.split()
            ranks = "+%@&~"

            if not channel in self.chanlist.keys():
                self.chanlist[channel] = {}

            for element in users:
                rank = ""

                for part in ranks:
                    if part in element:
                        rank = rank + part
                    element = element.strip(part)

                done = { 'server': prefix,
                         'status': rank,
                         'last_time': float( time.time() - 0.25 ) }
                self.chanlist[channel][element] = done

            print "Names for %s%s: %s" % (status, channel, names)

        elif command == "RPL_ENDOFNAMES":
            me, channel, message = params
            ops = 0
            voices = 0
            opers = 0
            aways = 0

            for element in self.chanlist[channel].values():
                status = element["status"]
                if "+" in status:
                    voices += 1
                if "@" in status:
                    ops += 1
                if "*" in status:
                    opers += 1
                if "G" in status:
                    aways += 1
            print("|= %s users on %s (%s voices, %s ops, %s opers, %s away)" % (
            len(self.chanlist[channel]), channel, voices, ops, opers, aways))
            print "[%s] (%s) %s" % (prefix, command, params)

        else:
            print "[%s] (%s) %s" % (prefix, command, params)

With it, I get the following error..

  File "C:\Users\Gareth\Documents\GitHub\McBlockit---Helpbot\system\irc.py", line 1422, in irc_unknown
    done = {"server": prefix, "status": rank, "last_time": float(time.time() - 0.25)}
exceptions.UnboundLocalError: local variable 'time' referenced before assignment

As you can see, I'm importing time at the top of the file. And that code works fine in another function. Any ideas?

Upvotes: 1

Views: 816

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121824

You have a local variable time defined in an if statement in your function:

        time = params[4]

Because it is inside an if statement nothing is assigned to it, but because of that variable, the imported time module is not available in your function.

Rename that variable to resolve the conflict.

Upvotes: 7

Related Questions