Fixed major bugz0rs with IrcSet and IrcDict.

This commit is contained in:
Jeremy Fincher 2003-11-04 05:50:19 +00:00
parent 6092ce484a
commit 3f8c149047
3 changed files with 45 additions and 6 deletions

View File

@ -182,10 +182,10 @@ class ChannelState(object):
__slots__ = ('users', 'ops', 'halfops', 'voices', 'topic') __slots__ = ('users', 'ops', 'halfops', 'voices', 'topic')
def __init__(self): def __init__(self):
self.topic = '' self.topic = ''
self.users = ircutils.IrcSet() # sets.Set() self.users = ircutils.IrcSet()
self.ops = ircutils.IrcSet() # sets.Set() self.ops = ircutils.IrcSet()
self.halfops = ircutils.IrcSet() # sets.Set() self.halfops = ircutils.IrcSet()
self.voices = ircutils.IrcSet() # sets.Set() self.voices = ircutils.IrcSet()
def addUser(self, user): def addUser(self, user):
"Adds a given user to the ChannelState. Power prefixes are handled." "Adds a given user to the ChannelState. Power prefixes are handled."

View File

@ -39,6 +39,7 @@ IRC-case-insensitive fashion), and numerous other things.
import fix import fix
import re import re
import copy
import sets import sets
import string import string
import fnmatch import fnmatch
@ -355,11 +356,19 @@ class IrcDict(dict):
def __delitem__(self, s): def __delitem__(self, s):
self.__parent.__delitem__(IrcString(s)) self.__parent.__delitem__(IrcString(s))
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, self.__parent.__repr__())
def __reduce__(self):
return (self.__class__, (dict(self),))
class IrcSet(sets.Set): class IrcSet(sets.Set):
"""A sets.Set using IrcStrings instead of regular strings.""" """A sets.Set using IrcStrings instead of regular strings."""
def __init__(self): def __init__(self, seq=()):
self.__parent = super(IrcSet, self) self.__parent = super(IrcSet, self)
self.__parent.__init__() self.__parent.__init__()
for elt in seq:
self.add(elt)
def add(self, s): def add(self, s):
return self.__parent.add(IrcString(s)) return self.__parent.add(IrcString(s))
@ -372,9 +381,12 @@ class IrcSet(sets.Set):
def __contains__(self, s): def __contains__(self, s):
return self.__parent.__contains__(IrcString(s)) return self.__parent.__contains__(IrcString(s))
has_key = __contains__ has_key = __contains__
def __reduce__(self):
return (self.__class__, (list(self),))
if __name__ == '__main__': if __name__ == '__main__':
import sys, doctest import sys, doctest
doctest.testmod(sys.modules['__main__']) doctest.testmod(sys.modules['__main__'])

View File

@ -32,6 +32,7 @@
from test import * from test import *
import copy
import random import random
import ircmsgs import ircmsgs
@ -205,6 +206,12 @@ class IrcDictTestCase(unittest.TestCase):
d['#fOOBAR[]'] = 'blah' d['#fOOBAR[]'] = 'blah'
self.assertEqual('blah', d['#foobar{}']) self.assertEqual('blah', d['#foobar{}'])
def testCopyable(self):
d = ircutils.IrcDict()
d['foo'] = 'bar'
self.failUnless(d == copy.copy(d))
self.failUnless(d == copy.deepcopy(d))
class IrcSetTestCase(unittest.TestCase): class IrcSetTestCase(unittest.TestCase):
def test(self): def test(self):
s = ircutils.IrcSet() s = ircutils.IrcSet()
@ -217,6 +224,26 @@ class IrcSetTestCase(unittest.TestCase):
self.failIf('foo' in s) self.failIf('foo' in s)
self.failIf('FOo' in s) self.failIf('FOo' in s)
def testCopy(self):
s = ircutils.IrcSet()
s.add('foo')
s.add('bar')
s1 = copy.deepcopy(s)
self.failUnless('foo' in s)
self.failUnless('FOO' in s)
s.discard('alfkj')
s.remove('FOo')
self.failIf('foo' in s)
self.failIf('FOo' in s)
self.failUnless('foo' in s1)
self.failUnless('FOO' in s1)
s1.discard('alfkj')
s1.remove('FOo')
self.failIf('foo' in s1)
self.failIf('FOo' in s1)
class IrcStringTestCase(unittest.TestCase): class IrcStringTestCase(unittest.TestCase):
def testEquality(self): def testEquality(self):
self.assertEqual('#foo', ircutils.IrcString('#foo')) self.assertEqual('#foo', ircutils.IrcString('#foo'))