Backwards incompatibility: obj.has_key(x) -> x in obj

17 views
Skip to first unread message

Jeremy Dunck

unread,
Jul 16, 2007, 3:19:44 AM7/16/07
to django-d...@googlegroups.com
I just ran into a subtle backwards incompatibility introduced in [5091].

I've been pushing an instance of xml.sax.xmlreader.AttributesImpl onto
my Context. AttributesImpl tries to implement a dictionary-like
interface, including __getitem__, but not __contains__.

Somehow, no matter what the value of x is, what's passed to
xml.sax.xmlreader.AttributesImpl is int(0).

This was causing a KeyError in template attribute lookup.

This is clearly a bug in AttributesImple or the xml.sax package in
general, but I'm patching Django back to use .has_key to avoid the
problem.

N.B. Anyone using Ellington news will have this problem.

Cheers,
Jeremy

Malcolm Tredinnick

unread,
Jul 16, 2007, 3:50:45 AM7/16/07
to django-d...@googlegroups.com
On Mon, 2007-07-16 at 02:19 -0500, Jeremy Dunck wrote:
> I just ran into a subtle backwards incompatibility introduced in [5091].
>
> I've been pushing an instance of xml.sax.xmlreader.AttributesImpl onto
> my Context. AttributesImpl tries to implement a dictionary-like
> interface, including __getitem__, but not __contains__.

It looks a bit more subtle than that class being incomplete. The default
xml.sax.xmlreader.AttributesImpl class is fine. All the way back to
Python 2.3. However, if you have PyXML installed for all the extra
goodies that supplies, that is transparently into the xml.* namespace
(via _xmlplus) and, in that case, AttributesImpl is missing a feature.

So this will work for some people, but not others.

> Somehow, no matter what the value of x is, what's passed to
> xml.sax.xmlreader.AttributesImpl is int(0).
>
> This was causing a KeyError in template attribute lookup.
>
> This is clearly a bug in AttributesImple or the xml.sax package in
> general, but I'm patching Django back to use .has_key to avoid the
> problem.
>
> N.B. Anyone using Ellington news will have this problem.

I'll go through that diff and revert the places that are likely to
affect something like this.

Regards,
Malcolm

--
Experience is something you don't get until just after you need it.
http://www.pointy-stick.com/blog/

Jeremy Dunck

unread,
Aug 29, 2007, 5:59:34 PM8/29/07
to django-d...@googlegroups.com
On 7/16/07, Malcolm Tredinnick <mal...@pointy-stick.com> wrote:
...

>
> I'll go through that diff and revert the places that are likely to
> affect something like this.

I know Malcolm's busy with QS refactor, but I wanted to note, I've
made this a ticket so it doesn't get lost:
http://code.djangoproject.com/ticket/5297

Gary Wilson

unread,
Aug 30, 2007, 12:08:40 AM8/30/07
to django-d...@googlegroups.com
Malcolm Tredinnick wrote:
> On Mon, 2007-07-16 at 02:19 -0500, Jeremy Dunck wrote:
>> I just ran into a subtle backwards incompatibility introduced in [5091].
>>
>> I've been pushing an instance of xml.sax.xmlreader.AttributesImpl onto
>> my Context. AttributesImpl tries to implement a dictionary-like
>> interface, including __getitem__, but not __contains__.
>
> It looks a bit more subtle than that class being incomplete. The default
> xml.sax.xmlreader.AttributesImpl class is fine. All the way back to
> Python 2.3. However, if you have PyXML installed for all the extra
> goodies that supplies, that is transparently into the xml.* namespace
> (via _xmlplus) and, in that case, AttributesImpl is missing a feature.

Yes, it looks as if PyXML's _xmlplus.sax.xmlreader.AttributesImpl is
missing __contains__:

$ diff -u /usr/lib/python2.4/xml/sax/xmlreader.py /usr/lib/python2.4/site-packages/_xmlplus/sax/xmlreader.py
--- /usr/lib/python2.4/xml/sax/xmlreader.py 2007-07-18 22:24:05.000000000 -0500
+++ /usr/lib/python2.4/site-packages/_xmlplus/sax/xmlreader.py 2007-08-29 22:39:02.000000000 -0500
@@ -321,9 +321,6 @@
def has_key(self, name):
return self._attrs.has_key(name)

- def __contains__(self, name):
- return self._attrs.has_key(name)
-
def get(self, name, alternative=None):
return self._attrs.get(name, alternative)


Gary

Reply all
Reply to author
Forward
0 new messages