I see. Thanks for the info about __html__. > I didn't know WebOb itself also did it. It only seems to do it on an HTTP Redirection. In webob.exc, _make_body(self, environ, escape) it loops over environ, calling escape on any values. (i'm still not exactly sure why).
> Or is it just returning something for all .__getattr__ calls > regardless of value? pymongo.database instance always returns a collection object, regardless of the attr name. The collection object is not callable, (well actually it defines __call__, but its implementation throws an exception immediately on purpose). The pymongo database __getattr__ looks like this # in pymongo database def __getattr__(self, name): return Collection(self.db, name) Ok, good to know about the callable deprecation. It looks like I may just subclass pymongo.database and override its __getattr__ to check for __html__. That doesn't feel very clean, but it'll work. On Sep 27, 12:44 am, Mike Orr <[email protected]> wrote: > On Sat, Sep 26, 2009 at 1:44 PM, Chris <[email protected]> wrote: > > > Hi, > > Looking for a little advice on how to solve this. > > Here is the problem: I store an object in environ and this object has > > a special __getattr__ which always returns an object. However, this > > object is *not* callable. > > > On a redirect, WebOb iterates over environ checking for __html__ via > > hasattr (which calls __getattr__). My object in the environ returns > > an object for __html__, but again it is not callable, so an Exception > > is thrown. > > > The object store in environ is a thirdparty MongoDB database instance. > > > So, is this a bug with WebOb, that is, should webob check that > > __html__ is callable? > > # in webob html_escape > > html = getattr(s, '__html__', None) > > if html and callable(html): > > return html() > > > Or should I fix this in the PyMongo Database class? Either way I have > > to change or subclass a 3rd party lib. > > > I'm leaning towards fixing this in webob, because I have other objects > > that have special __getattr__ methods. Any advice is appreciated. > > .__html__() is a convention used for smart escaping. String-like > objects should return self to indicate that they're preformatted and > should not be escaped further. Other objects can define .__html__() to > indicate their preferred HTML format. This convention is used by > literal() in webhelpers.html, and by the render functions that ship > with Pylons (render_mako, etc). I didn't know WebOb itself also did > it. > > Previous implementations of smart escaping (Quixote, Genshi) required > preformatted objects to be a certain class. This made it impossible > for third-party libraries to mark their objects preformatted, because > they'd have to depend on the package with the special class, which > they'd refuse to do or wouldn't know about. Worse, they would be tied > to one specific template library rather than supporting all of them. > The .__html__ strategy allows third-party packages to define their own > string subclass with an .__html__ method rather than having to depend > on a special class in a foreign package. > > So in this case, is the object yours or somebody else's? If it's > yours, define an .__html__ method that returns unicode. If it's > somebody else's, well, what does it think .__html__ is supposed to be? > Or is it just returning something for all .__getattr__ calls > regardless of value? > > callable() has been deprecated by Guido and is not in Python 3. He > says rather than using it, just call the d**n thing and see if you get > an exception or not. > > -- > Mike Orr <[email protected]> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en -~----------~----~----~----~------~----~------~--~---
