LazyCat.__getitem__() called via __repr__() cripples LazyCat.__add__()

Bug #143919 reported by Sean Upton
2
Affects Status Importance Assigned to Milestone
Zope 2
Fix Released
Medium
Unassigned

Bug Description

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'

Tags: bugday catalog
Revision history for this message
Sean Upton (seanupton) wrote :

agreed - I'll write a test tomorrow and submit patch with test added to tests/testLazySequences.py

Revision history for this message
Sean Upton (seanupton) wrote :

Uploaded: fix-for-2326.diff

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]

Revision history for this message
Andreas Jung (ajung) wrote :

The patch requires a unittest.

Revision history for this message
Tres Seaver (tseaver) wrote :

Constructing a test from the code in the description should be trivial.

Changed in zope2:
status: New → Confirmed
tags: added: bugday
removed: bug
Revision history for this message
Hanno Schlichting (hannosch) wrote :

This was fixed in r121708 for Products.ZCatalog, released in 2.13.14 (2011-05-19).

Changed in zope2:
status: Confirmed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.