datetime.datetime object as key in BTree

Bug #143766 reported by Kim Nikolay
2
Affects Status Importance Assigned to Milestone
Zope 2
Invalid
Medium
Unassigned

Bug Description

here example:

from datetime import datetime
from BTrees.OOBTree import OOBTree

tree = OOBTree()
tree[datetime.now()] = 'test1'
tree[datetime.now()] = 'test2'
tree.get(None, 'test3')

exceptions.TypeError

TypeError: can't compare datetime.datetime to NoneType
==================================================

but datetime.now() == None work

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

Please include the *full* traceback

Revision history for this message
Kim Nikolay (fafhrd) wrote :

this happens when catalog objects with datetime attributes, but this is common problem in BTrees module

    * Module ZPublisher.Publish, line 115, in publish
    * Module ZPublisher.mapply, line 88, in mapply
    * Module ZPublisher.Publish, line 41, in call_object
    * Module Products.ZCatalog.ZCatalog, line 536, in manage_reindexIndex
    * Module Products.ZCatalog.ZCatalog, line 521, in reindexIndex
    * Module Products.CMFPlone.CatalogTool, line 329, in catalog_object
    * Module Products.ZCatalog.ZCatalog, line 565, in catalog_object
    * Module Products.ZCatalog.Catalog, line 360, in catalogObject
    * Module Products.PluginIndexes.common.UnIndex, line 243, in index_object
    * Module Products.PluginIndexes.common.UnIndex, line 270, in _index_object
    * Module Products.PluginIndexes.common.UnIndex, line 214, in insertForwardIndexEntry

TypeError: can't compare datetime.datetime to NoneType

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

Status: Pending => Rejected

Keys in BTrees must be comparable. But None and datetime instances are not:

>>> cmp(datetime.datetime.now(), None)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: can't compare datetime.datetime to NoneType

Revision history for this message
Tim Peters (tim-one) wrote :

As the Python datetime docs say, the datetime types support __eq__ and __ne__ comparisons, but not __le__, __lt__, __ge__ or __gt__. __eq__ and __ne__ are the only ones used by Python dictionaries, but BTrees require a total ordering and so require all 6 rich comparison operators. IOW, there's nothing to be done about this: you can't mix datetime and non-datetime types (including None) as keys in a BTree.

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.