Update of /cvs-repository/Products/ZCatalog In directory cvs.zope.org:/tmp/cvs-serv32028/lib/python/Products/ZCatalog
Modified Files: Tag: tseaver-hasattr_geddon-branch Catalog.py CatalogAwareness.py CatalogPathAwareness.py ZCatalog.py ZCatalogIndexes.py Log Message: - Removed all uses of the 'hasattr' builtin from the core, where the object being tested derives (or might) from Persistent. XXX: currently, this branch imports a 'safe_hasattr' from ZODB.utils, which adds a dependency on ZODB for some packages; we probably need a better location, and perhas a C implementation? === Products/ZCatalog/Catalog.py 1.111.2.7 => 1.111.2.7.2.1 === --- Products/ZCatalog/Catalog.py:1.111.2.7 Fri Mar 11 11:24:51 2005 +++ Products/ZCatalog/Catalog.py Sat May 28 20:41:34 2005 @@ -25,6 +25,7 @@ from BTrees.OIBTree import OIBTree from BTrees.IOBTree import IOBTree import BTrees.Length +from ZODB.utils import safe_hasattr import time, sys, types from bisect import bisect @@ -36,8 +37,9 @@ # Fallback to python implementation to avoid dependancy on DocumentTemplate def safe_callable(ob): # Works with ExtensionClasses and Acquisition. - if hasattr(ob, '__class__'): - return hasattr(ob, '__call__') or isinstance(ob, types.ClassType) + if safe_hasattr(ob, '__class__'): + return safe_hasattr(ob, '__call__') or isinstance(ob, + types.ClassType) else: return callable(ob) @@ -119,7 +121,7 @@ for index in self.indexes.values(): - if hasattr(index, '__of__'): index=index.__of__(self) + if safe_hasattr(index, '__of__'): index=index.__of__(self) index._convertBTrees(threshold) def __len__(self): @@ -377,7 +379,7 @@ for name in use_indexes: x = self.getIndex(name) - if hasattr(x, 'index_object'): + if safe_hasattr(x, 'index_object'): blah = x.index_object(index, object, threshold) total = total + blah else: @@ -407,7 +409,7 @@ if rid is not None: for name in indexes: x = self.getIndex(name) - if hasattr(x, 'unindex_object'): + if safe_hasattr(x, 'unindex_object'): x.unindex_object(rid) del data[rid] del paths[rid] @@ -511,7 +513,7 @@ elif rs: # We got some results from the indexes. # Sort and convert to sequences. - if sort_index is None and hasattr(rs, 'values'): + if sort_index is None and safe_hasattr(rs, 'values'): # having a 'values' means we have a data structure with # scores. Build a new result set, sort it by score, reverse # it, compute the normalized score, and Lazify it. @@ -546,9 +548,9 @@ return LazyMap(getScoredResult, rs, len(rs)) - elif sort_index is None and not hasattr(rs, 'values'): + elif sort_index is None and not safe_hasattr(rs, 'values'): # no scores - if hasattr(rs, 'keys'): + if safe_hasattr(rs, 'keys'): rs = rs.keys() return LazyMap(self.__getitem__, rs, len(rs)) else: @@ -579,7 +581,7 @@ _keyerror = KeyError result = [] append = result.append - if hasattr(rs, 'keys'): + if safe_hasattr(rs, 'keys'): rs = rs.keys() rlen = len(rs) @@ -724,12 +726,13 @@ # self.indexes is always a dict, so get() w/ 1 arg works sort_index = self.indexes.get(sort_index_name) if sort_index is None: - raise CatalogError, 'Unknown sort_on index (%s)' % sort_index_name + raise (CatalogError, + 'Unknown sort_on index (%s)' % sort_index_name) else: - if not hasattr(sort_index, 'keyForDocument'): + if not safe_hasattr(sort_index, 'keyForDocument'): raise CatalogError( - 'The index chosen for sort_on (%s) is not capable of being' - ' used as a sort index.' % sort_index_name + 'The index chosen for sort_on (%s) is not capable of ' + 'being used as a sort index.' % sort_index_name ) return sort_index else: === Products/ZCatalog/CatalogAwareness.py 1.17.68.1 => 1.17.68.1.38.1 === --- Products/ZCatalog/CatalogAwareness.py:1.17.68.1 Mon Jul 21 12:36:25 2003 +++ Products/ZCatalog/CatalogAwareness.py Sat May 28 20:41:34 2005 @@ -19,6 +19,7 @@ import urllib from Globals import DTMLFile +from ZODB.utils import safe_hasattr class CatalogAware: """ This is a Mix-In class to make objects automaticly catalog and @@ -83,7 +84,7 @@ def url(self, ftype=urllib.splittype, fhost=urllib.splithost): """Return a SCRIPT_NAME-based url for an object.""" - if hasattr(self, 'DestinationURL') and \ + if safe_hasattr(self, 'DestinationURL') and \ callable(self.DestinationURL): url='%s/%s' % (self.DestinationURL(), self.id) else: url=self.absolute_url() @@ -101,7 +102,7 @@ def summary(self, num=200): """Return a summary of the text content of the object.""" - if not hasattr(self, 'text_content'): + if not safe_hasattr(self, 'text_content'): return '' attr=getattr(self, 'text_content') if callable(attr): @@ -112,12 +113,12 @@ def index_object(self): """A common method to allow Findables to index themselves.""" - if hasattr(self, self.default_catalog): + if safe_hasattr(self, self.default_catalog): getattr(self, self.default_catalog).catalog_object(self, self.url()) def unindex_object(self): """A common method to allow Findables to unindex themselves.""" - if hasattr(self, self.default_catalog): + if safe_hasattr(self, self.default_catalog): getattr(self, self.default_catalog).uncatalog_object(self.url()) def reindex_object(self): @@ -128,9 +129,9 @@ def reindex_all(self, obj=None): """ """ if obj is None: obj=self - if hasattr(aq_base(obj), 'index_object'): + if safe_hasattr(aq_base(obj), 'index_object'): obj.index_object() - if hasattr(aq_base(obj), 'objectValues'): + if safe_hasattr(aq_base(obj), 'objectValues'): sub=obj.objectValues() for item in obj.objectValues(): self.reindex_all(item) === Products/ZCatalog/CatalogPathAwareness.py 1.11.16.1 => 1.11.16.1.38.1 === --- Products/ZCatalog/CatalogPathAwareness.py:1.11.16.1 Mon Jul 21 12:36:25 2003 +++ Products/ZCatalog/CatalogPathAwareness.py Sat May 28 20:41:34 2005 @@ -16,6 +16,7 @@ import urllib from Globals import DTMLFile from Acquisition import aq_base +from ZODB.utils import safe_hasattr class CatalogAware: """ This is a Mix-In class to make objects automaticly catalog and @@ -84,7 +85,7 @@ def summary(self, num=200): """Return a summary of the text content of the object.""" - if not hasattr(self, 'text_content'): + if not safe_hasattr(self, 'text_content'): return '' attr=getattr(self, 'text_content') if callable(attr): @@ -95,13 +96,13 @@ def index_object(self): """A common method to allow Findables to index themselves.""" - if hasattr(self, self.default_catalog): + if safe_hasattr(self, self.default_catalog): getattr(self, self.default_catalog).catalog_object(self, self.getPath()) def unindex_object(self): """A common method to allow Findables to unindex themselves.""" - if hasattr(self, self.default_catalog): + if safe_hasattr(self, self.default_catalog): getattr(self, self.default_catalog).uncatalog_object(self.getPath()) @@ -113,9 +114,9 @@ def reindex_all(self, obj=None): """ """ if obj is None: obj=self - if hasattr(aq_base(obj), 'index_object'): + if safe_hasattr(aq_base(obj), 'index_object'): obj.index_object() - if hasattr(aq_base(obj), 'objectValues'): + if safe_hasattr(aq_base(obj), 'objectValues'): sub=obj.objectValues() for item in obj.objectValues(): self.reindex_all(item) === Products/ZCatalog/ZCatalog.py 1.126.2.6 => 1.126.2.6.24.1 === --- Products/ZCatalog/ZCatalog.py:1.126.2.6 Wed May 26 02:47:07 2004 +++ Products/ZCatalog/ZCatalog.py Sat May 28 20:41:34 2005 @@ -32,6 +32,7 @@ manage_zcatalog_entries, manage_zcatalog_indexes, search_zcatalog from ZCatalogIndexes import ZCatalogIndexes from ZODB.POSException import ConflictError +from ZODB.utils import safe_hasattr from Products.PluginIndexes.common.PluggableIndex \ import PluggableIndexInterface from Products.PluginIndexes.TextIndex import Splitter @@ -687,12 +688,12 @@ dup =dict.has_key x=0 while x < 100: - if hasattr(obj, '__ac_roles__'): + if safe_hasattr(obj, '__ac_roles__'): roles=obj.__ac_roles__ for role in roles: if not dup(role): dict[role]=1 - if not hasattr(obj, 'aq_parent'): + if not safe_hasattr(obj, 'aq_parent'): break obj=obj.aq_parent x=x+1 @@ -737,10 +738,10 @@ obj_expr=(Eval(obj_expr), md, md._push, md._pop) base=obj - if hasattr(obj, 'aq_base'): + if safe_hasattr(obj, 'aq_base'): base=obj.aq_base - if not hasattr(base, 'objectItems'): + if not safe_hasattr(base, 'objectItems'): return result try: items=obj.objectItems() except: return result @@ -754,21 +755,21 @@ else: p=id dflag=0 - if hasattr(ob, '_p_changed') and (ob._p_changed == None): + if safe_hasattr(ob, '_p_changed') and (ob._p_changed == None): dflag=1 - if hasattr(ob, 'aq_base'): + if safe_hasattr(ob, 'aq_base'): bs=ob.aq_base else: bs=ob if ( (not obj_ids or absattr(bs.id) in obj_ids) and - (not obj_metatypes or (hasattr(bs, 'meta_type') and + (not obj_metatypes or (safe_hasattr(bs, 'meta_type') and bs.meta_type in obj_metatypes)) and (not obj_searchterm or - (hasattr(ob, 'PrincipiaSearchSource') and + (safe_hasattr(ob, 'PrincipiaSearchSource') and ob.PrincipiaSearchSource().find(obj_searchterm) >= 0 )) and @@ -786,7 +787,7 @@ add_result((p, ob)) dflag=0 - if search_sub and hasattr(bs, 'objectItems'): + if search_sub and safe_hasattr(bs, 'objectItems'): self.ZopeFindAndApply(ob, obj_ids, obj_metatypes, obj_searchterm, obj_expr, obj_mtime, obj_mspec, @@ -987,7 +988,7 @@ pop() return r -def mtime_match(ob, t, q, fn=hasattr): +def mtime_match(ob, t, q, fn=safe_hasattr): if not fn(ob, '_p_mtime'): return 0 return q=='<' and (ob._p_mtime < t) or (ob._p_mtime > t) @@ -997,11 +998,11 @@ fn=pr.append while 1: - if hasattr(ob, permission): + if safe_hasattr(ob, permission): p=getattr(ob, permission) if type(p) is lt: map(fn, p) - if hasattr(ob, 'aq_parent'): + if safe_hasattr(ob, 'aq_parent'): ob=ob.aq_parent continue break @@ -1012,7 +1013,7 @@ map(fn, ('Manager', 'Anonymous')) break - if hasattr(ob, 'aq_parent'): + if safe_hasattr(ob, 'aq_parent'): ob=ob.aq_parent continue break === Products/ZCatalog/ZCatalogIndexes.py 1.8 => 1.8.136.1 === --- Products/ZCatalog/ZCatalogIndexes.py:1.8 Wed Aug 14 18:25:15 2002 +++ Products/ZCatalog/ZCatalogIndexes.py Sat May 28 20:41:34 2005 @@ -23,6 +23,7 @@ from OFS.History import Historical from OFS.SimpleItem import SimpleItem from OFS.ObjectManager import ObjectManager, IFAwareObjectManager +from ZODB.utils import safe_hasattr import os, sys, time @@ -90,7 +91,8 @@ for ob in indexes.keys(): o = indexes.get(ob) - if hasattr(o, 'meta_type') and getattr(o,'meta_type') in spec: + if (safe_hasattr(o, 'meta_type') + and getattr(o,'meta_type') in spec): set.append(ob) return set _______________________________________________ Zope-Checkins maillist - Zope-Checkins@zope.org http://mail.zope.org/mailman/listinfo/zope-checkins