[ZCM] [ZC] 2326/ 3 Comment "LazyCat.__getitem__() called via __repr__() cripples LazyCat.__add__()"

"Collector: Zope Bugs, Features, and Patches ..." <[email protected]> Mon, 21 May 2007 02:52:12 -0400
Newsgroups gmane.comp.web.zope.devel.collector-monitor
Message-ID <[email protected]>
Issue #2326 Update (Comment) "LazyCat.__getitem__() called via __repr__() cripples LazyCat.__add__()"
 Status Pending, Catalog/bug medium
To followup, visit:
  http://www.zope.org/Collectors/Zope/2326

==============================================================
= Comment - Entry #3 by ajung on May 21, 2007 2:52 am

The patch requires a unittest.
________________________________________
= Comment - Entry #2 by seanupton on May 21, 2007 2:48 am


Uploaded:  "fix-for-2326.diff"
 - http://www.zope.org/Collectors/Zope/2326/fix-for-2326.diff/view
Fix / patch attached as diff to latest svn://svn.zope.org/repos/main/Zope/trunk/lib/python/Products/ZCatalog.  

Call to _seq is avoided in __init__() when adding LazyCats together, working around the way that __getitem__() is implemented.  Appears to work correctly:

>>> lazy1 = LazyCat( [ [1,2] ] )
>>> lazy1 += LazyCat( [ [3,4] ])
>>> lazy1
[1, 2, 3, 4]
>>> lazy1 += LazyCat( [ [3,4] ])
>>> lazy1
[1, 2, 3, 4, 3, 4]

________________________________________
= Request - Entry #1 by seanupton on May 20, 2007 8:26 pm

You can add LazyCats together, but only before __repr__() is called or an instance is cast to another sequence type: __getitem__() removes self._seq on line 106 of r40218 in HEAD.  To duplicate:

>>> from Products.ZCatalog.Lazy import LazyCat
>>> lazy1 = LazyCat([ [1,2], ])
>>> lazy1._seq
[[1, 2]]
>>> lazy1 += LazyCat([ [3,4], ])
>>> lazy1._seq
[[1, 2], [3, 4]]
>>> print lazy1 # __repr__ casts to list, calls getitem, removes lazy1._seq
[1, 2, 3, 4]
>>> lazy1._seq #this is bad
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: LazyCat instance has no attribute '_seq'
>>> lazy1 += LazyCat([ [5,6], ]) #this fails because of this bug!
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/home/upton/eg/swhome/lib/python/Products/ZCatalog/Lazy.py", line 43, in __add__
    return LazyCat([self, other])
  File "/home/upton/eg/swhome/lib/python/Products/ZCatalog/Lazy.py", line 68, in __init__
    flattened_seq.extend(s._seq)
AttributeError: LazyCat instance has no attribute '_seq'

==============================================================