[Python-Dev] Catching "return" and "return expr" at compile time

Barry A. Warsaw bwarsaw@cnri.reston.va.us (Barry A. Warsaw)
Tue, 7 Sep 1999 14:07:01 -0400 (EDT)

>>>>> "GW" == Greg Ward <gward@cnri.reston.va.us> writes:

    GW> However, one could certainly envision a world where Python
    GW> issues runtime warnings.  If my time machine were working, I'd
    GW> zip back and suggest to Guido that mistakes with the %
    GW> operator should issue warnings rather than raising exceptions.
    GW> (Ignore the language philosophy issue and presume this would
    GW> be worthwhile.)

Moderately off-topic, but since you brought it up, here's what I use
in Mailman (since site-admins can make mistakes editing their
templates, which contains %(keys)s... we'd like to make Mailman more
robust so it doesn't totally crap out when that happens).

We (hopefully) always interpolate with a SafeDict instead of a raw
Python dictionary.


class SafeDict(UserDict):
    """Dictionary which returns a default value for unknown keys.

    This is used in maketext so that editing templates is a bit more robust.
    def __init__(self, d):
        # optional initial dictionary is a Python 1.5.2-ism.  Do it this way
        # for portability

    def __getitem__(self, key):
            return self.data[key]
        except KeyError:
            if type(key) == StringType:
                return '%('+key+')s'
                return '<Missing key: %s>' % `key`