Commit 1133226e authored by Sidnei da Silva's avatar Sidnei da Silva

- Made all PluginIndexes and ZCTextIndex use 'safe_callable',

        which is aware of extension classes that fill 'tp_callable'
        but don't define '__call__'.

      - Made KeywordIndex be more robust about receiving a value that
        is not a string or an iterable type.
parent 47f66299
......@@ -62,6 +62,12 @@ Zope Changes
- Collector #741: Applied patch to provide better FTP error messages.
- Made all PluginIndexes and ZCTextIndex use 'safe_callable',
which is aware of extension classes that fill 'tp_callable'
but don't define '__call__'.
- Made KeywordIndex be more robust about receiving a value that
is not a string or an iterable type.
Bugs Fixed
......
......@@ -11,14 +11,15 @@
#
##############################################################################
"""$Id: DateIndex.py,v 1.11 2003/06/08 08:56:28 andreasjung Exp $
"""$Id: DateIndex.py,v 1.12 2003/06/17 19:01:06 sidnei Exp $
"""
from types import StringType, FloatType, IntType
from DateTime.DateTime import DateTime
from Products.PluginIndexes import PluggableIndex
from Products.PluginIndexes.common.UnIndex import UnIndex
from Products.PluginIndexes.common.util import parseIndexRequest
from types import StringType, FloatType, IntType
from Products.PluginIndexes.common import safe_callable
from Globals import DTMLFile
from BTrees.IOBTree import IOBTree
......@@ -63,7 +64,7 @@ class DateIndex(UnIndex):
try:
date_attr = getattr( obj, self.id )
if callable( date_attr ):
if safe_callable( date_attr ):
date_attr = date_attr()
ConvertedDate = self._convert( value=date_attr, default=_marker )
......
......@@ -11,12 +11,15 @@
#
##############################################################################
"""$Id: DateRangeIndex.py,v 1.5 2002/12/05 21:35:52 caseman Exp $
"""$Id: DateRangeIndex.py,v 1.6 2003/06/17 19:01:06 sidnei Exp $
"""
import os
from Products.PluginIndexes import PluggableIndex
from Products.PluginIndexes.common.UnIndex import UnIndex
from Products.PluginIndexes.common.util import parseIndexRequest
from Products.PluginIndexes.common import safe_callable
from OFS.SimpleItem import SimpleItem
from BTrees.IOBTree import IOBTree
......@@ -25,7 +28,6 @@ from BTrees.IIBTree import IISet, IITreeSet, union, intersection, multiunion
from Globals import package_home, DTMLFile, InitializeClass
from AccessControl import ClassSecurityInfo
from DateTime.DateTime import DateTime
import os
_dtmldir = os.path.join( package_home( globals() ), 'dtml' )
......@@ -158,12 +160,12 @@ class DateRangeIndex(UnIndex):
return 0
since = getattr( obj, self._since_field, None )
if callable( since ):
if safe_callable( since ):
since = since()
since = self._convertDateTime( since )
until = getattr( obj, self._until_field, None )
if callable( until ):
if safe_callable( until ):
until = until()
until = self._convertDateTime( until )
......
......@@ -11,6 +11,8 @@
#
##############################################################################
from types import StringType, UnicodeType
from zLOG import LOG, ERROR
from BTrees.OOBTree import OOSet, difference
......@@ -18,6 +20,7 @@ from Globals import DTMLFile
from Products.PluginIndexes import PluggableIndex
from Products.PluginIndexes.common.UnIndex import UnIndex
from Products.PluginIndexes.common import safe_callable
class KeywordIndex(UnIndex):
......@@ -87,17 +90,21 @@ class KeywordIndex(UnIndex):
def _get_object_keywords(self, obj, attr):
newKeywords = getattr(obj, attr, ())
if callable(newKeywords):
if safe_callable(newKeywords):
newKeywords = newKeywords()
if hasattr(newKeywords,'capitalize'): # is it string-like ?
newKeywords = [newKeywords]
if (isinstance(newKeywords, StringType)
or isinstance(newKeywords, UnicodeType)): #Python 2.1 compat isinstance
return (newKeywords,)
else:
# Uniqueify keywords
unique = {}
for k in newKeywords:
unique[k] = None
newKeywords = unique.keys()
return newKeywords
try:
for k in newKeywords:
unique[k] = None
except TypeError:
# Not a sequence
return (newKeywords,)
else:
return unique.keys()
def unindex_objectKeywords(self, documentId, keywords):
""" carefully unindex the object with integer id 'documentId'"""
......@@ -125,7 +132,7 @@ class KeywordIndex(UnIndex):
manage_addKeywordIndexForm = DTMLFile('dtml/addKeywordIndex', globals())
def manage_addKeywordIndex(self, id, extra=None,
def manage_addKeywordIndex(self, id, extra=None,
REQUEST=None, RESPONSE=None, URL3=None):
"""Add a keyword index"""
return self.manage_addIndex(id, 'KeywordIndex', extra=extra, \
......
......@@ -11,10 +11,11 @@
#
##############################################################################
__version__ = '$Id: PathIndex.py,v 1.34 2003/05/27 05:31:21 caseman Exp $'
__version__ = '$Id: PathIndex.py,v 1.35 2003/06/17 19:01:07 sidnei Exp $'
from Products.PluginIndexes import PluggableIndex
from Products.PluginIndexes.common.util import parseIndexRequest
from Products.PluginIndexes.common import safe_callable
from Globals import Persistent, DTMLFile
from Acquisition import Implicit
......@@ -65,7 +66,7 @@ class PathIndex(Persistent, Implicit, SimpleItem):
self.useOperator = 'or'
self.clear()
def clear(self):
""" clear everything """
......@@ -73,11 +74,11 @@ class PathIndex(Persistent, Implicit, SimpleItem):
self._depth = 0
self._index = OOBTree()
self._unindex = IOBTree()
def insertEntry(self, comp, id, level):
"""Insert an entry.
comp is a path component (generated by splitPath() )
id is the documentId
level is the level of the component inside the path
......@@ -102,8 +103,8 @@ class PathIndex(Persistent, Implicit, SimpleItem):
if hasattr(obj, self.id):
f = getattr(obj, self.id)
if callable(f):
if safe_callable(f):
try:
path = f()
except AttributeError:
......@@ -141,7 +142,7 @@ class PathIndex(Persistent, Implicit, SimpleItem):
'Attempt to unindex nonexistent document'
' with id %s' % documentId)
return
path = self._unindex[documentId]
comps = path.split('/')
......@@ -219,7 +220,7 @@ class PathIndex(Persistent, Implicit, SimpleItem):
if not self._index[comp].has_key(level+i): return IISet()
results.append( self._index[comp][level+i] )
res = results[0]
for i in range(1,len(results)):
......@@ -323,8 +324,8 @@ class PathIndex(Persistent, Implicit, SimpleItem):
def hasUniqueValuesFor(self, name):
"""has unique values for column name"""
return name == self.id
def uniqueValues(self, name=None, withLength=0):
""" needed to be consistent with the interface """
return self._index.keys()
......
......@@ -14,7 +14,7 @@
"""Text Index
"""
__version__ = '$Revision: 1.35 $'[11:-2]
__version__ = '$Revision: 1.36 $'[11:-2]
import re
......@@ -31,6 +31,7 @@ from BTrees.IOBTree import IOBTree
from BTrees.OIBTree import OIBTree
from BTrees.IIBTree import IIBTree, IIBucket, IISet, IITreeSet
from BTrees.IIBTree import difference, weightedIntersection
from Products.PluginIndexes.common import safe_callable
from Lexicon import Lexicon
......@@ -282,7 +283,7 @@ class TextIndex(Persistent, Implicit, SimpleItem):
# index is this attribute. If it smells callable, call it.
try:
source = getattr(obj, self.id)
if callable(source):
if safe_callable(source):
source = source()
if not isinstance(source, UnicodeType):
......@@ -295,7 +296,7 @@ class TextIndex(Persistent, Implicit, SimpleItem):
try:
encoding = getattr(obj, self.id+'_encoding')
if callable(encoding ):
if safe_callable(encoding ):
encoding = str(encoding())
else:
encoding = str(encoding)
......
......@@ -13,14 +13,17 @@
"""Simple column indices"""
__version__='$Revision: 1.19 $'[11:-2]
__version__='$Revision: 1.20 $'[11:-2]
import sys
from cgi import escape
from types import StringType, ListType, IntType, TupleType
from Globals import Persistent
from Acquisition import Implicit
import BTree
import IOBTree
from zLOG import LOG, ERROR
from types import StringType, ListType, IntType, TupleType
from BTrees.OOBTree import OOBTree, OOSet
from BTrees.IOBTree import IOBTree
......@@ -29,8 +32,8 @@ from OFS.SimpleItem import SimpleItem
import BTrees.Length
from Products.PluginIndexes.common.util import parseIndexRequest
import sys
from cgi import escape
from Products.PluginIndexes.common import safe_callable
_marker = []
......@@ -279,7 +282,7 @@ class UnIndex(Persistent, Implicit, SimpleItem):
# we'll do so.
try:
datum = getattr(obj, attr)
if callable(datum):
if safe_callable(datum):
datum = datum()
except AttributeError:
datum = _marker
......
# empty comment for winzip and friends
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
#############################################################################
# This code is duplicated here from Products/ZCatalog/Catalog.py to avoid a
# unnecessary dependency on ZCatalog.
try:
from DocumentTemplate.cDocumentTemplate import safe_callable
except ImportError:
def safe_callable(ob):
# Works with ExtensionClasses and Acquisition.
if hasattr(ob, '__class__'):
if hasattr(ob, '__call__'):
return 1
else:
return isinstance(ob, types.ClassType)
else:
return callable(ob)
......@@ -30,6 +30,7 @@ from AccessControl.Permissions import manage_zcatalog_indexes, search_zcatalog
from Products.PluginIndexes.common.PluggableIndex import \
PluggableIndexInterface
from Products.PluginIndexes.common.util import parseIndexRequest
from Products.PluginIndexes.common import safe_callable
from Products.ZCTextIndex.ILexicon import ILexicon
from Products.ZCTextIndex.Lexicon import \
......@@ -168,7 +169,7 @@ class ZCTextIndex(Persistent, Acquisition.Implicit, SimpleItem):
text = getattr(obj, self._fieldname, None)
if text is None:
return 0
if callable(text):
if safe_callable(text):
text = text()
if text is None:
return 0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment