Hash: SHA1

Dieter Maurer wrote:
> Tres Seaver wrote at 2005-5-27 08:22 -0400:
>>As a local patch, this isn't too bad (one could even package it as a
>>do-nothing-after-initialization product).  However, no redistributed
>>product code should rely on the presence of a patched 'hasattr', but
>>should use the 3 argument getattr instead.
> I think this is *very* bad!
>   It is quite difficult to get the "hasattr" emulation via
>   "getattr" correct.
> If you do not like a modified "__builtin__.hasattr" (which I can understand),
> then provide at least a "__builtin__.zhasattr" (or similarly named) available
> in restricted code and strongly recommend in the documentation to use this
> variant of "hasattr" for any Zope/ZODB object.

As Tim notes, if we aren't replacing the builtin, then we could equally
well recommend the following pattern:

  # in ZODB.utils

  def safe_hasattr(obj, name, _marker=object()):
      """Make sure we don't mask exceptions like hasattr().

      We don't want exceptions other than AttributeError to be masked,
      since that too often masks other programming errors.
      Three-argument getattr() doesn't mask those, so we use that to
      implement our own hasattr() replacement.
      return getattr(obj, name, _marker) is not _marker

  & in any module which uses hasattr
  from ZODB.utils import safe_hasattr
  # was:  if hasattr(object, 'attribute'):
  if safe_hasattr(object, 'attribute'):

The spelling, of course, would be up for endless argument. ;)  Perhaps
we could call it 'persistent_hasattr', and then let folks alias it at
import time as they wish (even to 'hasattr', but not in the core!).

We would then (as you note) need to ensure that we made it available to
restricted code.  For that casee, we might need a variant of it which
used 'guarded_getattr' (but I'm not sure about that).

- --
Tres Seaver                                   [EMAIL PROTECTED]
Palladion Software   "Excellence by Design"    http://palladion.com
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

Zope-Dev maillist  -  Zope-Dev@zope.org
**  No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope )

Reply via email to