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 ...@@ -62,6 +62,12 @@ Zope Changes
- Collector #741: Applied patch to provide better FTP error messages. - 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 Bugs Fixed
......
...@@ -11,14 +11,15 @@ ...@@ -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 DateTime.DateTime import DateTime
from Products.PluginIndexes import PluggableIndex from Products.PluginIndexes import PluggableIndex
from Products.PluginIndexes.common.UnIndex import UnIndex from Products.PluginIndexes.common.UnIndex import UnIndex
from Products.PluginIndexes.common.util import parseIndexRequest 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 Globals import DTMLFile
from BTrees.IOBTree import IOBTree from BTrees.IOBTree import IOBTree
...@@ -63,7 +64,7 @@ class DateIndex(UnIndex): ...@@ -63,7 +64,7 @@ class DateIndex(UnIndex):
try: try:
date_attr = getattr( obj, self.id ) date_attr = getattr( obj, self.id )
if callable( date_attr ): if safe_callable( date_attr ):
date_attr = date_attr() date_attr = date_attr()
ConvertedDate = self._convert( value=date_attr, default=_marker ) ConvertedDate = self._convert( value=date_attr, default=_marker )
......
...@@ -11,12 +11,15 @@ ...@@ -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 import PluggableIndex
from Products.PluginIndexes.common.UnIndex import UnIndex from Products.PluginIndexes.common.UnIndex import UnIndex
from Products.PluginIndexes.common.util import parseIndexRequest from Products.PluginIndexes.common.util import parseIndexRequest
from Products.PluginIndexes.common import safe_callable
from OFS.SimpleItem import SimpleItem from OFS.SimpleItem import SimpleItem
from BTrees.IOBTree import IOBTree from BTrees.IOBTree import IOBTree
...@@ -25,7 +28,6 @@ from BTrees.IIBTree import IISet, IITreeSet, union, intersection, multiunion ...@@ -25,7 +28,6 @@ from BTrees.IIBTree import IISet, IITreeSet, union, intersection, multiunion
from Globals import package_home, DTMLFile, InitializeClass from Globals import package_home, DTMLFile, InitializeClass
from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo
from DateTime.DateTime import DateTime from DateTime.DateTime import DateTime
import os
_dtmldir = os.path.join( package_home( globals() ), 'dtml' ) _dtmldir = os.path.join( package_home( globals() ), 'dtml' )
...@@ -158,12 +160,12 @@ class DateRangeIndex(UnIndex): ...@@ -158,12 +160,12 @@ class DateRangeIndex(UnIndex):
return 0 return 0
since = getattr( obj, self._since_field, None ) since = getattr( obj, self._since_field, None )
if callable( since ): if safe_callable( since ):
since = since() since = since()
since = self._convertDateTime( since ) since = self._convertDateTime( since )
until = getattr( obj, self._until_field, None ) until = getattr( obj, self._until_field, None )
if callable( until ): if safe_callable( until ):
until = until() until = until()
until = self._convertDateTime( until ) until = self._convertDateTime( until )
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
# #
############################################################################## ##############################################################################
from types import StringType, UnicodeType
from zLOG import LOG, ERROR from zLOG import LOG, ERROR
from BTrees.OOBTree import OOSet, difference from BTrees.OOBTree import OOSet, difference
...@@ -18,6 +20,7 @@ from Globals import DTMLFile ...@@ -18,6 +20,7 @@ from Globals import DTMLFile
from Products.PluginIndexes import PluggableIndex from Products.PluginIndexes import PluggableIndex
from Products.PluginIndexes.common.UnIndex import UnIndex from Products.PluginIndexes.common.UnIndex import UnIndex
from Products.PluginIndexes.common import safe_callable
class KeywordIndex(UnIndex): class KeywordIndex(UnIndex):
...@@ -87,17 +90,21 @@ class KeywordIndex(UnIndex): ...@@ -87,17 +90,21 @@ class KeywordIndex(UnIndex):
def _get_object_keywords(self, obj, attr): def _get_object_keywords(self, obj, attr):
newKeywords = getattr(obj, attr, ()) newKeywords = getattr(obj, attr, ())
if callable(newKeywords): if safe_callable(newKeywords):
newKeywords = newKeywords() newKeywords = newKeywords()
if hasattr(newKeywords,'capitalize'): # is it string-like ? if (isinstance(newKeywords, StringType)
newKeywords = [newKeywords] or isinstance(newKeywords, UnicodeType)): #Python 2.1 compat isinstance
return (newKeywords,)
else: else:
# Uniqueify keywords
unique = {} unique = {}
try:
for k in newKeywords: for k in newKeywords:
unique[k] = None unique[k] = None
newKeywords = unique.keys() except TypeError:
return newKeywords # Not a sequence
return (newKeywords,)
else:
return unique.keys()
def unindex_objectKeywords(self, documentId, keywords): def unindex_objectKeywords(self, documentId, keywords):
""" carefully unindex the object with integer id 'documentId'""" """ carefully unindex the object with integer id 'documentId'"""
......
...@@ -11,10 +11,11 @@ ...@@ -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 import PluggableIndex
from Products.PluginIndexes.common.util import parseIndexRequest from Products.PluginIndexes.common.util import parseIndexRequest
from Products.PluginIndexes.common import safe_callable
from Globals import Persistent, DTMLFile from Globals import Persistent, DTMLFile
from Acquisition import Implicit from Acquisition import Implicit
...@@ -103,7 +104,7 @@ class PathIndex(Persistent, Implicit, SimpleItem): ...@@ -103,7 +104,7 @@ class PathIndex(Persistent, Implicit, SimpleItem):
if hasattr(obj, self.id): if hasattr(obj, self.id):
f = getattr(obj, self.id) f = getattr(obj, self.id)
if callable(f): if safe_callable(f):
try: try:
path = f() path = f()
except AttributeError: except AttributeError:
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
"""Text Index """Text Index
""" """
__version__ = '$Revision: 1.35 $'[11:-2] __version__ = '$Revision: 1.36 $'[11:-2]
import re import re
...@@ -31,6 +31,7 @@ from BTrees.IOBTree import IOBTree ...@@ -31,6 +31,7 @@ from BTrees.IOBTree import IOBTree
from BTrees.OIBTree import OIBTree from BTrees.OIBTree import OIBTree
from BTrees.IIBTree import IIBTree, IIBucket, IISet, IITreeSet from BTrees.IIBTree import IIBTree, IIBucket, IISet, IITreeSet
from BTrees.IIBTree import difference, weightedIntersection from BTrees.IIBTree import difference, weightedIntersection
from Products.PluginIndexes.common import safe_callable
from Lexicon import Lexicon from Lexicon import Lexicon
...@@ -282,7 +283,7 @@ class TextIndex(Persistent, Implicit, SimpleItem): ...@@ -282,7 +283,7 @@ class TextIndex(Persistent, Implicit, SimpleItem):
# index is this attribute. If it smells callable, call it. # index is this attribute. If it smells callable, call it.
try: try:
source = getattr(obj, self.id) source = getattr(obj, self.id)
if callable(source): if safe_callable(source):
source = source() source = source()
if not isinstance(source, UnicodeType): if not isinstance(source, UnicodeType):
...@@ -295,7 +296,7 @@ class TextIndex(Persistent, Implicit, SimpleItem): ...@@ -295,7 +296,7 @@ class TextIndex(Persistent, Implicit, SimpleItem):
try: try:
encoding = getattr(obj, self.id+'_encoding') encoding = getattr(obj, self.id+'_encoding')
if callable(encoding ): if safe_callable(encoding ):
encoding = str(encoding()) encoding = str(encoding())
else: else:
encoding = str(encoding) encoding = str(encoding)
......
...@@ -13,14 +13,17 @@ ...@@ -13,14 +13,17 @@
"""Simple column indices""" """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 Globals import Persistent
from Acquisition import Implicit from Acquisition import Implicit
import BTree import BTree
import IOBTree import IOBTree
from zLOG import LOG, ERROR from zLOG import LOG, ERROR
from types import StringType, ListType, IntType, TupleType
from BTrees.OOBTree import OOBTree, OOSet from BTrees.OOBTree import OOBTree, OOSet
from BTrees.IOBTree import IOBTree from BTrees.IOBTree import IOBTree
...@@ -29,8 +32,8 @@ from OFS.SimpleItem import SimpleItem ...@@ -29,8 +32,8 @@ from OFS.SimpleItem import SimpleItem
import BTrees.Length import BTrees.Length
from Products.PluginIndexes.common.util import parseIndexRequest from Products.PluginIndexes.common.util import parseIndexRequest
import sys from Products.PluginIndexes.common import safe_callable
from cgi import escape
_marker = [] _marker = []
...@@ -279,7 +282,7 @@ class UnIndex(Persistent, Implicit, SimpleItem): ...@@ -279,7 +282,7 @@ class UnIndex(Persistent, Implicit, SimpleItem):
# we'll do so. # we'll do so.
try: try:
datum = getattr(obj, attr) datum = getattr(obj, attr)
if callable(datum): if safe_callable(datum):
datum = datum() datum = datum()
except AttributeError: except AttributeError:
datum = _marker 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 ...@@ -30,6 +30,7 @@ from AccessControl.Permissions import manage_zcatalog_indexes, search_zcatalog
from Products.PluginIndexes.common.PluggableIndex import \ from Products.PluginIndexes.common.PluggableIndex import \
PluggableIndexInterface PluggableIndexInterface
from Products.PluginIndexes.common.util import parseIndexRequest from Products.PluginIndexes.common.util import parseIndexRequest
from Products.PluginIndexes.common import safe_callable
from Products.ZCTextIndex.ILexicon import ILexicon from Products.ZCTextIndex.ILexicon import ILexicon
from Products.ZCTextIndex.Lexicon import \ from Products.ZCTextIndex.Lexicon import \
...@@ -168,7 +169,7 @@ class ZCTextIndex(Persistent, Acquisition.Implicit, SimpleItem): ...@@ -168,7 +169,7 @@ class ZCTextIndex(Persistent, Acquisition.Implicit, SimpleItem):
text = getattr(obj, self._fieldname, None) text = getattr(obj, self._fieldname, None)
if text is None: if text is None:
return 0 return 0
if callable(text): if safe_callable(text):
text = text() text = text()
if text is None: if text is None:
return 0 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