Merge lp:~jameinel/bzr/2.1-st-from-iterable into lp:bzr

Proposed by John A Meinel
Status: Merged
Merged at revision: not available
Proposed branch: lp:~jameinel/bzr/2.1-st-from-iterable
Merge into: lp:bzr
Diff against target: 86 lines
2 files modified
bzrlib/_static_tuple_c.c (+14/-8)
bzrlib/tests/test__static_tuple.py (+15/-0)
To merge this branch: bzr merge lp:~jameinel/bzr/2.1-st-from-iterable
Reviewer Review Type Date Requested Status
Vincent Ladeuil Approve
Review via email: mp+14027@code.launchpad.net
To post a comment you must log in.
Revision history for this message
John A Meinel (jameinel) wrote :

This is a small cleanup of Matt Nordhoff's update for 'from_sequence'. Basically, it changes the internals such that if an object isn't a simple sequence, we cast it to a tuple and then use that instead.

It means you can do "StaticTuple.from_sequence(generator)" which is what they had been doing in loggerhead. (using tuple(generator).)

I don't think we *need* this, but if it helps someone, I'm ok with it.

Revision history for this message
Vincent Ladeuil (vila) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'bzrlib/_static_tuple_c.c'
--- bzrlib/_static_tuple_c.c 2009-10-26 15:59:54 +0000
+++ bzrlib/_static_tuple_c.c 2009-10-27 14:10:22 +0000
@@ -183,7 +183,8 @@
183static StaticTuple *183static StaticTuple *
184StaticTuple_FromSequence(PyObject *sequence)184StaticTuple_FromSequence(PyObject *sequence)
185{185{
186 StaticTuple *new;186 StaticTuple *new = NULL;
187 PyObject *as_tuple = NULL;
187 PyObject *item;188 PyObject *item;
188 Py_ssize_t i, size;189 Py_ssize_t i, size;
189190
@@ -192,16 +193,18 @@
192 return (StaticTuple *)sequence;193 return (StaticTuple *)sequence;
193 }194 }
194 if (!PySequence_Check(sequence)) {195 if (!PySequence_Check(sequence)) {
195 PyErr_Format(PyExc_TypeError, "Type %s is not a sequence type",196 as_tuple = PySequence_Tuple(sequence);
196 Py_TYPE(sequence)->tp_name);197 if (as_tuple == NULL)
197 return NULL;198 goto done;
199 sequence = as_tuple;
198 }200 }
199 size = PySequence_Size(sequence);201 size = PySequence_Size(sequence);
200 if (size == -1)202 if (size == -1) {
201 return NULL;203 goto done;
204 }
202 new = StaticTuple_New(size);205 new = StaticTuple_New(size);
203 if (new == NULL) {206 if (new == NULL) {
204 return NULL;207 goto done;
205 }208 }
206 for (i = 0; i < size; ++i) {209 for (i = 0; i < size; ++i) {
207 // This returns a new reference, which we then 'steal' with 210 // This returns a new reference, which we then 'steal' with
@@ -209,10 +212,13 @@
209 item = PySequence_GetItem(sequence, i);212 item = PySequence_GetItem(sequence, i);
210 if (item == NULL) {213 if (item == NULL) {
211 Py_DECREF(new);214 Py_DECREF(new);
212 return NULL;215 new = NULL;
216 goto done;
213 }217 }
214 StaticTuple_SET_ITEM(new, i, item);218 StaticTuple_SET_ITEM(new, i, item);
215 }219 }
220done:
221 Py_XDECREF(as_tuple);
216 return (StaticTuple *)new;222 return (StaticTuple *)new;
217}223}
218224
219225
=== modified file 'bzrlib/tests/test__static_tuple.py'
--- bzrlib/tests/test__static_tuple.py 2009-10-21 17:54:33 +0000
+++ bzrlib/tests/test__static_tuple.py 2009-10-27 14:10:22 +0000
@@ -571,6 +571,8 @@
571 def test_from_sequence_not_sequence(self):571 def test_from_sequence_not_sequence(self):
572 self.assertRaises(TypeError,572 self.assertRaises(TypeError,
573 self.module.StaticTuple.from_sequence, object())573 self.module.StaticTuple.from_sequence, object())
574 self.assertRaises(TypeError,
575 self.module.StaticTuple.from_sequence, 10)
574576
575 def test_from_sequence_incorrect_args(self):577 def test_from_sequence_incorrect_args(self):
576 self.assertRaises(TypeError,578 self.assertRaises(TypeError,
@@ -578,6 +580,19 @@
578 self.assertRaises(TypeError,580 self.assertRaises(TypeError,
579 self.module.StaticTuple.from_sequence, foo='a')581 self.module.StaticTuple.from_sequence, foo='a')
580582
583 def test_from_sequence_iterable(self):
584 st = self.module.StaticTuple.from_sequence(iter(['foo', 'bar']))
585 self.assertIsInstance(st, self.module.StaticTuple)
586 self.assertEqual(('foo', 'bar'), st)
587
588 def test_from_sequence_generator(self):
589 def generate_tuple():
590 yield 'foo'
591 yield 'bar'
592 st = self.module.StaticTuple.from_sequence(generate_tuple())
593 self.assertIsInstance(st, self.module.StaticTuple)
594 self.assertEqual(('foo', 'bar'), st)
595
581 def test_pickle(self):596 def test_pickle(self):
582 st = self.module.StaticTuple('foo', 'bar')597 st = self.module.StaticTuple('foo', 'bar')
583 pickled = cPickle.dumps(st)598 pickled = cPickle.dumps(st)