Update of /cvs-repository/Packages/ZPublisher In directory cvs.zope.org:/tmp/cvs-serv32028/lib/python/ZPublisher
Modified Files: Tag: tseaver-hasattr_geddon-branch BaseRequest.py Client.py Converters.py HTTPRequest.py HTTPResponse.py mapply.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? === Packages/ZPublisher/BaseRequest.py 1.51.2.4 => 1.51.2.4.6.1 === --- Packages/ZPublisher/BaseRequest.py:1.51.2.4 Thu Dec 2 11:49:28 2004 +++ Packages/ZPublisher/BaseRequest.py Sat May 28 20:41:38 2005 @@ -18,6 +18,7 @@ from urllib import quote import xmlrpc from zExceptions import Forbidden +from ZODB.utils import safe_hasattr UNSPECIFIED_ROLES='' @@ -226,7 +227,7 @@ # if the top object has a __bobo_traverse__ method, then use it # to possibly traverse to an alternate top-level object. - if hasattr(object,'__bobo_traverse__'): + if safe_hasattr(object,'__bobo_traverse__'): try: object=object.__bobo_traverse__(request) roles =getattr(object, '__roles__', UNSPECIFIED_ROLES) @@ -236,7 +237,7 @@ return response.forbiddenError(self['URL']) # Traverse the URL to find the object: - if hasattr(object, '__of__'): + if safe_hasattr(object, '__of__'): # Try to bind the top-level object to the request # This is how you get 'self.REQUEST' object=object.__of__(RequestContainer(REQUEST=request)) @@ -262,7 +263,7 @@ # Check for method: if path: entry_name = path.pop() - elif hasattr(object, '__browser_default__'): + elif safe_hasattr(object, '__browser_default__'): # If we have reached the end of the path. We look to see # if the object implements __browser_default__. If so, we # call it to let the object tell us how to publish it @@ -278,15 +279,15 @@ continue else: entry_name = default_path[0] - elif (method and hasattr(object,method) + elif (method and safe_hasattr(object,method) and entry_name != method and getattr(object, method) is not None): request._hacked_path=1 entry_name = method method = 'index_html' else: - if (hasattr(object, '__call__') and - hasattr(object.__call__,'__roles__')): + if (safe_hasattr(object, '__call__') and + safe_hasattr(object.__call__,'__roles__')): roles=object.__call__.__roles__ if request._hacked_path: i=URL.rfind('/') @@ -302,7 +303,7 @@ "Object name begins with an underscore at: %s" % URL) else: return response.forbiddenError(entry_name) - if hasattr(object,'__bobo_traverse__'): + if safe_hasattr(object,'__bobo_traverse__'): try: subobject=object.__bobo_traverse__(request,entry_name) if type(subobject) is type(()) and len(subobject) > 1: @@ -328,8 +329,8 @@ # existing object :( if (no_acquire_flag and len(path) == 0 and - hasattr(object, 'aq_base')): - if hasattr(object.aq_base, entry_name): + safe_hasattr(object, 'aq_base')): + if safe_hasattr(object.aq_base, entry_name): subobject=getattr(object, entry_name) else: raise AttributeError, entry_name else: subobject=getattr(object, entry_name) @@ -393,13 +394,13 @@ if 1: # Always perform authentication. last_parent_index=len(parents) - if hasattr(object, '__allow_groups__'): + if safe_hasattr(object, '__allow_groups__'): groups=object.__allow_groups__ inext=0 else: inext=None for i in range(last_parent_index): - if hasattr(parents[i],'__allow_groups__'): + if safe_hasattr(parents[i],'__allow_groups__'): groups=parents[i].__allow_groups__ inext=i+1 break @@ -407,20 +408,27 @@ if inext is not None: i=inext - if hasattr(groups, 'validate'): v=groups.validate - else: v=old_validation + if safe_hasattr(groups, 'validate'): + v=groups.validate + else: + v=old_validation auth=request._auth if v is old_validation and roles is UNSPECIFIED_ROLES: # No roles, so if we have a named group, get roles from # group keys - if hasattr(groups,'keys'): roles=groups.keys() + if safe_hasattr(groups,'keys'): + roles=groups.keys() else: - try: groups=groups() - except: pass - try: roles=groups.keys() - except: pass + try: + groups=groups() + except: + pass + try: + roles=groups.keys() + except: + pass if groups is None: # Public group, hack structures to get it to validate @@ -435,11 +443,13 @@ while user is None and i < last_parent_index: parent=parents[i] i=i+1 - if hasattr(parent, '__allow_groups__'): + if safe_hasattr(parent, '__allow_groups__'): groups=parent.__allow_groups__ else: continue - if hasattr(groups,'validate'): v=groups.validate - else: v=old_validation + if safe_hasattr(groups,'validate'): + v=groups.validate + else: + v=old_validation if v is old_validation: user=old_validation(groups, request, auth, roles) elif roles is UNSPECIFIED_ROLES: user=v(request, auth) === Packages/ZPublisher/Client.py 1.45.66.1 => 1.45.66.1.30.1 === --- Packages/ZPublisher/Client.py:1.45.66.1 Mon Nov 17 17:34:19 2003 +++ Packages/ZPublisher/Client.py Sat May 28 20:41:38 2005 @@ -43,6 +43,7 @@ from types import FileType, ListType, DictType, TupleType from string import translate, maketrans from urlparse import urlparse +from ZODB.utils import safe_hasattr class BadReply(Exception): pass @@ -109,7 +110,8 @@ content_type=None if not method or method=='POST': for v in kw.values(): - if hasattr(v,'read'): return self._mp_call(kw) + if safe_hasattr(v,'read'): + return self._mp_call(kw) can_marshal=type2marshal.has_key for k,v in kw.items(): @@ -402,8 +404,8 @@ elif (dt==ListType) or (dt==TupleType): raise ValueError, 'Sorry, nested multipart is not done yet!' - elif dt==FileType or hasattr(val,'read'): - if hasattr(val,'name'): + elif dt==FileType or safe_hasattr(val,'read'): + if safe_hasattr(val,'name'): fn=val.name.replace( '\\', '/') fn=fn[(fn.rfind('/')+1):] ex=(fn[(fn.rfind('.')+1):]).lower() === Packages/ZPublisher/Converters.py 1.23.2.5 => 1.23.2.5.12.1 === --- Packages/ZPublisher/Converters.py:1.23.2.5 Tue Nov 16 08:12:12 2004 +++ Packages/ZPublisher/Converters.py Sat May 28 20:41:38 2005 @@ -16,9 +16,10 @@ from types import ListType, TupleType, UnicodeType from DateTime import DateTime from cgi import escape +from ZODB.utils import safe_hasattr def field2string(v): - if hasattr(v,'read'): return v.read() + if safe_hasattr(v,'read'): return v.read() elif isinstance(v,UnicodeType) : return v.encode('iso-8859-15') else: @@ -129,7 +130,8 @@ # <input name="description:utf8:ustring" ..... # rather than # <input name="description:ustring" ..... - if hasattr(v,'read'): v=v.read() + if safe_hasattr(v,'read'): + v=v.read() v = unicode(v) return self.convert_unicode(v) === Packages/ZPublisher/HTTPRequest.py 1.90.2.9 => 1.90.2.9.2.1 === --- Packages/ZPublisher/HTTPRequest.py:1.90.2.9 Tue Mar 29 12:22:53 2005 +++ Packages/ZPublisher/HTTPRequest.py Sat May 28 20:41:38 2005 @@ -23,6 +23,8 @@ from Converters import get_converter from TaintedString import TaintedString from maybe_lock import allocate_lock +from ZODB.utils import safe_hasattr + xmlrpc=None # Placeholder for module that we'll import if we have to. isCGI_NAME = { @@ -360,7 +362,7 @@ REC=12, # RECORD|RECORDS EMPTY=16, CONVERTED=32, - hasattr=hasattr, + hasattr=safe_hasattr, getattr=getattr, setattr=setattr, search_type=re.compile('(:[a-zA-Z][-a-zA-Z0-9_]+|\\.[xy])$').search, @@ -1047,11 +1049,11 @@ # in the context of the resolve_url method so we need # to ensure we are getting the actual object named by # the given url, and not some kind of default object. - if hasattr(object, 'id'): + if safe_hasattr(object, 'id'): if callable(object.id): name=object.id() else: name=object.id - elif hasattr(object, '__name__'): + elif safe_hasattr(object, '__name__'): name=object.__name__ else: name='' if name != os.path.split(path)[-1]: @@ -1409,22 +1411,27 @@ def __init__(self, aFieldStorage): file=aFieldStorage.file - if hasattr(file, '__methods__'): methods=file.__methods__ - else: methods= ['close', 'fileno', 'flush', 'isatty', + if safe_hasattr(file, '__methods__'): + methods=file.__methods__ + else: + methods= ['close', 'fileno', 'flush', 'isatty', 'read', 'readline', 'readlines', 'seek', 'tell', 'truncate', 'write', 'writelines'] d=self.__dict__ for m in methods: - if hasattr(file,m): d[m]=getattr(file,m) + if safe_hasattr(file,m): + d[m]=getattr(file,m) self.headers=aFieldStorage.headers self.filename=aFieldStorage.filename # Add an assertion to the rfc822.Message object that implements # self.headers so that managed code can access them. - try: self.headers.__allow_access_to_unprotected_subobjects__ = 1 - except: pass + try: + self.headers.__allow_access_to_unprotected_subobjects__ = 1 + except: + pass def __nonzero__(self): """FileUpload objects are considered false if their === Packages/ZPublisher/HTTPResponse.py 1.75.2.9 => 1.75.2.9.6.1 === --- Packages/ZPublisher/HTTPResponse.py:1.75.2.9 Wed Dec 22 10:51:28 2004 +++ Packages/ZPublisher/HTTPResponse.py Sat May 28 20:41:38 2005 @@ -21,6 +21,7 @@ from BaseResponse import BaseResponse from zExceptions import Unauthorized, Redirect from zExceptions.ExceptionFormatter import format_exception +from ZODB.utils import safe_hasattr from ZPublisher import BadRequest, InternalError, NotFound from cgi import escape @@ -292,7 +293,7 @@ title,body = body if not isinstance(body, str): - if hasattr(body,'asHTML'): + if safe_hasattr(body,'asHTML'): body = body.asHTML() if isinstance(body, unicode): === Packages/ZPublisher/mapply.py 1.6.66.1 => 1.6.66.1.34.1 === --- Packages/ZPublisher/mapply.py:1.6.66.1 Mon Jul 21 12:37:22 2003 +++ Packages/ZPublisher/mapply.py Sat May 28 20:41:38 2005 @@ -12,6 +12,7 @@ ############################################################################## """Provide an apply-like facility that works with any mapping object """ +from ZODB.utils import safe_hasattr def default_call_object(object, args, context): result=object(*args) # Type s<cr> to step into published object. @@ -21,7 +22,7 @@ raise TypeError, 'argument %s was ommitted' % name def default_handle_class(klass, context): - if hasattr(klass,'__init__'): + if safe_hasattr(klass,'__init__'): f=klass.__init__.im_func c=f.func_code names=c.co_varnames[1:c.co_argcount] @@ -36,19 +37,20 @@ context=None, bind=0, ): - if hasattr(object,'__bases__'): + if safe_hasattr(object,'__bases__'): f, names, defaults = handle_class(object, context) else: f=object im=0 - if hasattr(f, 'im_func'): + if safe_hasattr(f, 'im_func'): im=1 - elif not hasattr(f,'func_defaults'): - if hasattr(f, '__call__'): + elif not safe_hasattr(f,'func_defaults'): + if safe_hasattr(f, '__call__'): f=f.__call__ - if hasattr(f, 'im_func'): + if safe_hasattr(f, 'im_func'): im=1 - elif not hasattr(f,'func_defaults') and maybe: return object + elif not safe_hasattr(f,'func_defaults') and maybe: + return object elif maybe: return object if im: _______________________________________________ Zope-Checkins maillist - Zope-Checkins@zope.org http://mail.zope.org/mailman/listinfo/zope-checkins