##// END OF EJS Templates
lazymanifest: make __iter__ generate filenames, not 3-tuples...
Martin von Zweigbergk -
r24298:49cd847f default
parent child Browse files
Show More
@@ -234,7 +234,7 b' static line *lmiter_nextline(lmIter *sel'
234 return self->m->lines + self->pos;
234 return self->m->lines + self->pos;
235 }
235 }
236
236
237 static PyObject *lmiter_iternext(PyObject *o)
237 static PyObject *lmiter_iterentriesnext(PyObject *o)
238 {
238 {
239 size_t pl;
239 size_t pl;
240 line *l;
240 line *l;
@@ -261,10 +261,10 b' static PyObject *lmiter_iternext(PyObjec'
261 return NULL;
261 return NULL;
262 }
262 }
263
263
264 static PyTypeObject lazymanifestIterator = {
264 static PyTypeObject lazymanifestEntriesIterator = {
265 PyObject_HEAD_INIT(NULL)
265 PyObject_HEAD_INIT(NULL)
266 0, /*ob_size */
266 0, /*ob_size */
267 "parsers.lazymanifest.iterator", /*tp_name */
267 "parsers.lazymanifest.entriesiterator", /*tp_name */
268 sizeof(lmIter), /*tp_basicsize */
268 sizeof(lmIter), /*tp_basicsize */
269 0, /*tp_itemsize */
269 0, /*tp_itemsize */
270 lmiter_dealloc, /*tp_dealloc */
270 lmiter_dealloc, /*tp_dealloc */
@@ -285,13 +285,13 b' static PyTypeObject lazymanifestIterator'
285 /* tp_flags: Py_TPFLAGS_HAVE_ITER tells python to
285 /* tp_flags: Py_TPFLAGS_HAVE_ITER tells python to
286 use tp_iter and tp_iternext fields. */
286 use tp_iter and tp_iternext fields. */
287 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,
287 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,
288 "Iterator for a lazymanifest.", /* tp_doc */
288 "Iterator for 3-tuples in a lazymanifest.", /* tp_doc */
289 0, /* tp_traverse */
289 0, /* tp_traverse */
290 0, /* tp_clear */
290 0, /* tp_clear */
291 0, /* tp_richcompare */
291 0, /* tp_richcompare */
292 0, /* tp_weaklistoffset */
292 0, /* tp_weaklistoffset */
293 PyObject_SelfIter, /* tp_iter: __iter__() method */
293 PyObject_SelfIter, /* tp_iter: __iter__() method */
294 lmiter_iternext, /* tp_iternext: next() method */
294 lmiter_iterentriesnext, /* tp_iternext: next() method */
295 };
295 };
296
296
297 static PyObject *lmiter_iterkeysnext(PyObject *o)
297 static PyObject *lmiter_iterkeysnext(PyObject *o)
@@ -340,7 +340,7 b' static PyTypeObject lazymanifestKeysIter'
340
340
341 static lazymanifest *lazymanifest_copy(lazymanifest *self);
341 static lazymanifest *lazymanifest_copy(lazymanifest *self);
342
342
343 static PyObject *lazymanifest_getiter(lazymanifest *self)
343 static PyObject *lazymanifest_getentriesiter(lazymanifest *self)
344 {
344 {
345 lmIter *i = NULL;
345 lmIter *i = NULL;
346 lazymanifest *t = lazymanifest_copy(self);
346 lazymanifest *t = lazymanifest_copy(self);
@@ -348,7 +348,7 b' static PyObject *lazymanifest_getiter(la'
348 PyErr_NoMemory();
348 PyErr_NoMemory();
349 return NULL;
349 return NULL;
350 }
350 }
351 i = PyObject_New(lmIter, &lazymanifestIterator);
351 i = PyObject_New(lmIter, &lazymanifestEntriesIterator);
352 if (i) {
352 if (i) {
353 i->m = t;
353 i->m = t;
354 i->pos = -1;
354 i->pos = -1;
@@ -860,6 +860,8 b' static PyObject *lazymanifest_diff(lazym'
860 static PyMethodDef lazymanifest_methods[] = {
860 static PyMethodDef lazymanifest_methods[] = {
861 {"iterkeys", (PyCFunction)lazymanifest_getkeysiter, METH_NOARGS,
861 {"iterkeys", (PyCFunction)lazymanifest_getkeysiter, METH_NOARGS,
862 "Iterate over file names in this lazymanifest."},
862 "Iterate over file names in this lazymanifest."},
863 {"iterentries", (PyCFunction)lazymanifest_getentriesiter, METH_NOARGS,
864 "Iterate over (path, nodeid, flags) typles in this lazymanifest."},
863 {"copy", (PyCFunction)lazymanifest_copy, METH_NOARGS,
865 {"copy", (PyCFunction)lazymanifest_copy, METH_NOARGS,
864 "Make a copy of this lazymanifest."},
866 "Make a copy of this lazymanifest."},
865 {"filtercopy", (PyCFunction)lazymanifest_filtercopy, METH_O,
867 {"filtercopy", (PyCFunction)lazymanifest_filtercopy, METH_O,
@@ -898,7 +900,7 b' static PyTypeObject lazymanifestType = {'
898 0, /* tp_clear */
900 0, /* tp_clear */
899 0, /* tp_richcompare */
901 0, /* tp_richcompare */
900 0, /* tp_weaklistoffset */
902 0, /* tp_weaklistoffset */
901 (getiterfunc)lazymanifest_getiter, /* tp_iter */
903 (getiterfunc)lazymanifest_getkeysiter, /* tp_iter */
902 0, /* tp_iternext */
904 0, /* tp_iternext */
903 lazymanifest_methods, /* tp_methods */
905 lazymanifest_methods, /* tp_methods */
904 0, /* tp_members */
906 0, /* tp_members */
@@ -44,11 +44,14 b' class _lazymanifest(dict):'
44 dict.__setitem__(self, k, (node, flag))
44 dict.__setitem__(self, k, (node, flag))
45
45
46 def __iter__(self):
46 def __iter__(self):
47 return ((f, e[0], e[1]) for f, e in sorted(self.iteritems()))
47 return iter(sorted(dict.keys(self)))
48
48
49 def iterkeys(self):
49 def iterkeys(self):
50 return iter(sorted(dict.keys(self)))
50 return iter(sorted(dict.keys(self)))
51
51
52 def iterentries(self):
53 return ((f, e[0], e[1]) for f, e in sorted(self.iteritems()))
54
52 def copy(self):
55 def copy(self):
53 c = _lazymanifest('')
56 c = _lazymanifest('')
54 c.update(self)
57 c.update(self)
@@ -76,14 +79,14 b' class _lazymanifest(dict):'
76
79
77 def filtercopy(self, filterfn):
80 def filtercopy(self, filterfn):
78 c = _lazymanifest('')
81 c = _lazymanifest('')
79 for f, n, fl in self:
82 for f, n, fl in self.iterentries():
80 if filterfn(f):
83 if filterfn(f):
81 c[f] = n, fl
84 c[f] = n, fl
82 return c
85 return c
83
86
84 def text(self):
87 def text(self):
85 """Get the full data of this manifest as a bytestring."""
88 """Get the full data of this manifest as a bytestring."""
86 fl = sorted(self)
89 fl = sorted(self.iterentries())
87
90
88 _hex = revlog.hex
91 _hex = revlog.hex
89 # if this is changed to support newlines in filenames,
92 # if this is changed to support newlines in filenames,
@@ -119,7 +122,7 b' class manifestdict(object):'
119 del self._lm[key]
122 del self._lm[key]
120
123
121 def __iter__(self):
124 def __iter__(self):
122 return self._lm.iterkeys()
125 return self._lm.__iter__()
123
126
124 def iterkeys(self):
127 def iterkeys(self):
125 return self._lm.iterkeys()
128 return self._lm.iterkeys()
@@ -140,8 +143,8 b' class manifestdict(object):'
140
143
141 def filesnotin(self, m2):
144 def filesnotin(self, m2):
142 '''Set of files in this manifest that are not in the other'''
145 '''Set of files in this manifest that are not in the other'''
143 files = set(self.iterkeys())
146 files = set(self)
144 files.difference_update(m2.iterkeys())
147 files.difference_update(m2)
145 return files
148 return files
146
149
147 def matches(self, match):
150 def matches(self, match):
@@ -196,7 +199,7 b' class manifestdict(object):'
196 return c
199 return c
197
200
198 def iteritems(self):
201 def iteritems(self):
199 return (x[:2] for x in self._lm)
202 return (x[:2] for x in self._lm.iterentries())
200
203
201 def text(self):
204 def text(self):
202 return self._lm.text()
205 return self._lm.text()
@@ -40,7 +40,7 b' class testmanifest(unittest.TestCase):'
40 def testEmptyManifest(self):
40 def testEmptyManifest(self):
41 m = manifestmod._lazymanifest('')
41 m = manifestmod._lazymanifest('')
42 self.assertEqual(0, len(m))
42 self.assertEqual(0, len(m))
43 self.assertEqual([], list(m))
43 self.assertEqual([], list(m.iterentries()))
44
44
45 def testManifest(self):
45 def testManifest(self):
46 m = manifestmod._lazymanifest(A_SHORT_MANIFEST)
46 m = manifestmod._lazymanifest(A_SHORT_MANIFEST)
@@ -49,7 +49,7 b' class testmanifest(unittest.TestCase):'
49 ('foo', binascii.unhexlify(HASH_1), ''),
49 ('foo', binascii.unhexlify(HASH_1), ''),
50 ]
50 ]
51 self.assertEqual(len(want), len(m))
51 self.assertEqual(len(want), len(m))
52 self.assertEqual(want, list(m))
52 self.assertEqual(want, list(m.iterentries()))
53 self.assertEqual((binascii.unhexlify(HASH_1), ''), m['foo'])
53 self.assertEqual((binascii.unhexlify(HASH_1), ''), m['foo'])
54 self.assertRaises(KeyError, lambda : m['wat'])
54 self.assertRaises(KeyError, lambda : m['wat'])
55 self.assertEqual((binascii.unhexlify(HASH_2), 'l'),
55 self.assertEqual((binascii.unhexlify(HASH_2), 'l'),
@@ -88,7 +88,7 b' class testmanifest(unittest.TestCase):'
88 self.assertEqual((h2, ''), m['beta'])
88 self.assertEqual((h2, ''), m['beta'])
89 self.assertRaises(KeyError, lambda : m['foo'])
89 self.assertRaises(KeyError, lambda : m['foo'])
90 w = [('alpha', h1, ''), ('bar/baz/qux.py', h2, 'l'), ('beta', h2, '')]
90 w = [('alpha', h1, ''), ('bar/baz/qux.py', h2, 'l'), ('beta', h2, '')]
91 self.assertEqual(w, list(m))
91 self.assertEqual(w, list(m.iterentries()))
92
92
93 def testSetGetNodeSuffix(self):
93 def testSetGetNodeSuffix(self):
94 clean = manifestmod._lazymanifest(A_SHORT_MANIFEST)
94 clean = manifestmod._lazymanifest(A_SHORT_MANIFEST)
@@ -100,7 +100,7 b' class testmanifest(unittest.TestCase):'
100 self.assertEqual(want, m['foo'])
100 self.assertEqual(want, m['foo'])
101 self.assertEqual([('bar/baz/qux.py', binascii.unhexlify(HASH_2), 'l'),
101 self.assertEqual([('bar/baz/qux.py', binascii.unhexlify(HASH_2), 'l'),
102 ('foo', binascii.unhexlify(HASH_1) + 'a', '')],
102 ('foo', binascii.unhexlify(HASH_1) + 'a', '')],
103 list(m))
103 list(m.iterentries()))
104 # Sometimes it even tries a 22-byte fake hash, but we can
104 # Sometimes it even tries a 22-byte fake hash, but we can
105 # return 21 and it'll work out
105 # return 21 and it'll work out
106 m['foo'] = want[0] + '+', f
106 m['foo'] = want[0] + '+', f
@@ -114,7 +114,7 b' class testmanifest(unittest.TestCase):'
114 self.assertEqual(want, m2['foo'])
114 self.assertEqual(want, m2['foo'])
115 # suffix with iteration
115 # suffix with iteration
116 self.assertEqual([('bar/baz/qux.py', binascii.unhexlify(HASH_2), 'l'),
116 self.assertEqual([('bar/baz/qux.py', binascii.unhexlify(HASH_2), 'l'),
117 ('foo', want[0], '')], list(m))
117 ('foo', want[0], '')], list(m.iterentries()))
118 # shows up in diff
118 # shows up in diff
119 self.assertEqual({'foo': (want, (h, ''))}, m.diff(clean))
119 self.assertEqual({'foo': (want, (h, ''))}, m.diff(clean))
120 self.assertEqual({'foo': ((h, ''), want)}, clean.diff(m))
120 self.assertEqual({'foo': ((h, ''), want)}, clean.diff(m))
General Comments 0
You need to be logged in to leave comments. Login now