Skip to content

Commit ee38363

Browse files
author
Justus Pendleton
committed
Allow Multilinks to take any iterable
Change create_inner & set_inner to allow any iterable for Multilinks. Added a test to make sure they work and that we raise an exception for non-iterables.
1 parent c52fe30 commit ee38363

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

roundup/backends/back_anydbm.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
#$Id: back_anydbm.py,v 1.203 2007-03-14 15:23:11 schlatterbeck Exp $
18+
#$Id: back_anydbm.py,v 1.204 2007-08-29 16:40:20 jpend Exp $
1919
'''This module defines a backend that saves the hyperdatabase in a
2020
database chosen by anydbm. It is guaranteed to always be available in python
2121
versions >2.1.1 (the dumbdbm fallback in 2.1.1 and earlier has several
@@ -840,8 +840,8 @@ def create_inner(self, **propvalues):
840840
(self.classname, newid, key))
841841

842842
elif isinstance(prop, hyperdb.Multilink):
843-
if type(value) != type([]):
844-
raise TypeError, 'new property "%s" not a list of ids'%key
843+
if not hasattr(value, '__iter__'):
844+
raise TypeError, 'new property "%s" not an iterable of ids'%key
845845

846846
# clean up and validate the list of links
847847
link_class = self.properties[key].classname
@@ -1132,8 +1132,8 @@ def set_inner(self, nodeid, **propvalues):
11321132
(self.classname, nodeid, propname))
11331133

11341134
elif isinstance(prop, hyperdb.Multilink):
1135-
if type(value) != type([]):
1136-
raise TypeError, 'new property "%s" not a list of'\
1135+
if not hasattr(value, '__iter__'):
1136+
raise TypeError, 'new property "%s" not an iterable of'\
11371137
' ids'%propname
11381138
link_class = self.properties[propname].classname
11391139
l = []

roundup/backends/back_metakit.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: back_metakit.py,v 1.115 2007-03-14 15:51:03 schlatterbeck Exp $
1+
# $Id: back_metakit.py,v 1.116 2007-08-29 16:40:20 jpend Exp $
22
'''Metakit backend for Roundup, originally by Gordon McMillan.
33
44
Known Current Bugs:
@@ -647,8 +647,8 @@ def set_inner(self, nodeid, **propvalues):
647647
(self.classname, str(row.id), key))
648648

649649
elif isinstance(prop, hyperdb.Multilink):
650-
if value is not None and type(value) != _LISTTYPE:
651-
raise TypeError, 'new property "%s" not a list of ids'%key
650+
if not hasattr(value, '__iter__'):
651+
raise TypeError, 'new property "%s" not an iterable of ids'%key
652652
link_class = prop.classname
653653
l = []
654654
if value is None:

roundup/backends/rdbms_common.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
#$Id: rdbms_common.py,v 1.186 2007-06-21 07:35:50 schlatterbeck Exp $
18+
#$Id: rdbms_common.py,v 1.187 2007-08-29 16:40:20 jpend Exp $
1919
""" Relational database (SQL) backend common code.
2020
2121
Basics:
@@ -1363,8 +1363,8 @@ def create_inner(self, **propvalues):
13631363
(self.classname, newid, key))
13641364

13651365
elif isinstance(prop, Multilink):
1366-
if type(value) != type([]):
1367-
raise TypeError, 'new property "%s" not a list of ids'%key
1366+
if not hasattr(value, '__iter__'):
1367+
raise TypeError, 'new property "%s" not an iterable of ids'%key
13681368

13691369
# clean up and validate the list of links
13701370
link_class = self.properties[key].classname
@@ -1613,8 +1613,8 @@ def set_inner(self, nodeid, **propvalues):
16131613
(self.classname, nodeid, propname))
16141614

16151615
elif isinstance(prop, Multilink):
1616-
if type(value) != type([]):
1617-
raise TypeError, 'new property "%s" not a list of'\
1616+
if not hasattr(value, '__iter__'):
1617+
raise TypeError, 'new property "%s" not an iterable of'\
16181618
' ids'%propname
16191619
link_class = self.properties[propname].classname
16201620
l = []

test/db_test_base.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: db_test_base.py,v 1.85 2007-04-11 20:04:06 forsberg Exp $
18+
# $Id: db_test_base.py,v 1.86 2007-08-29 16:40:20 jpend Exp $
1919

2020
import unittest, os, shutil, errno, imp, sys, time, pprint, sets
2121

@@ -253,6 +253,31 @@ def testMultilinkChange(self):
253253
m = self.db.issue.get(nid, "nosy"); m.sort()
254254
self.assertEqual(l, m)
255255

256+
def testMultilinkChangeIterable(self):
257+
for commit in (0,1):
258+
# invalid nosy value assertion
259+
self.assertRaises(IndexError, self.db.issue.create, title='spam',
260+
nosy=['foo%s'%commit])
261+
# invalid type for nosy create
262+
self.assertRaises(TypeError, self.db.issue.create, title='spam',
263+
nosy=1)
264+
u1 = self.db.user.create(username='foo%s'%commit)
265+
u2 = self.db.user.create(username='bar%s'%commit)
266+
nid = self.db.issue.create(title="spam", nosy=set(u1)) # set
267+
if commit: self.db.commit()
268+
self.assertEqual(self.db.issue.get(nid, "nosy"), [u1])
269+
self.assertRaises(TypeError, self.db.issue.set, nid,
270+
nosy='invalid type')
271+
self.db.issue.set(nid, nosy=tuple()) # tuple
272+
if commit: self.db.commit()
273+
self.assertEqual(self.db.issue.get(nid, "nosy"), [])
274+
self.db.issue.set(nid, nosy=frozenset([u1,u2])) # frozenset
275+
if commit: self.db.commit()
276+
l = [u1,u2]; l.sort()
277+
m = self.db.issue.get(nid, "nosy"); m.sort()
278+
self.assertEqual(l, m)
279+
280+
256281
# XXX one day, maybe...
257282
# def testMultilinkOrdering(self):
258283
# for i in range(10):

0 commit comments

Comments
 (0)