Closed Bug 599177 Opened 14 years ago Closed 14 years ago

Python FutureWarning about behavior of has_key method on Python 2.7

Categories

(Add-on SDK Graveyard :: General, defect)

x86
Linux
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: myk, Assigned: warner)

Details

(Whiteboard: [mozmill-1.5.1+])

Attachments

(1 file)

over in bug 596528, comment 7, Matej reported that every time he runs cfx he sees the following warnings:

/home/matej/projekty/jetpack-sdk/python-lib/mozrunner/__init__.py:221:
FutureWarning: The behavior of this method will change in future versions.  Use
specific 'len(elem)' or 'elem is not None' test instead.
  if desc and desc.attrib.has_key('{http://www.mozilla.org/2004/em-rdf#}id'):
/home/matej/projekty/jetpack-sdk/python-lib/mozrunner/__init__.py:223:
FutureWarning: The behavior of this method will change in future versions.  Use
specific 'len(elem)' or 'elem is not None' test instead.
  elif desc and desc.find('.//{http://www.mozilla.org/2004/em-rdf#}id') is not
None:

Matej is using Python 2.7 on Fedora14Beta.  I don't see this on my Linux installation running Python 2.6.4, but presumably it's something new in Python 2.7.  To avoid showing users these warnings (and continue working on future versions of Python), the SDK should implement the recommended changes and push the changes upstream.

Brian: you are our resident Python expert; might this be something you could tackle?
sure, I'll look at it.
Assignee: nobody → warner-bugzilla
Status: NEW → ASSIGNED
Some digging revealed that the actual issue is that the new version of
ElementTree (included in python2.7 for the first time) has deprecated the use
of boolean(element), in preparation for changing the way it works in some
future version. So in the line:

  if desc and desc.attrib.has_key('{http://www.mozilla.org/2004/em-rdf#}id'):

the actual problem was the "if desc" part (which is equivalent to "if
bool(desc)"), not the call to has_key().

http://effbot.org/zone/elementtree-13-intro.htm#truth-testing has the release
notes about the change.

My best guess is that this code is testing whether the "Description" XML
element contains non-"targetApplication" children, so the implicit bool(desc)
test is checking whether there are any child elements. If that's correct,
then replacing "desc" with "len(desc)" is the right fix.

This patch also changes a "len(about) is 0" into "len(about) == 0", since
numbers can't be compared by identity. This happens to work because Python
optimizes some math by interning a bunch of small integers, but e.g. "1000 is
1001-1" evaluates as False. Comparing with == is more robust.
copying Clint to see how he wants to handle this upstream
Attachment #478121 - Flags: review?(avarma)
Attachment #478121 - Flags: feedback?(ctalbert)
Comment on attachment 478121 [details] [diff] [review]
mozrunner patch to hush FutureWarning on py2.7

Looks good to me!
Attachment #478121 - Flags: review?(avarma) → review+
Status: ASSIGNED → RESOLVED
Closed: 14 years ago
Resolution: --- → FIXED
Comment on attachment 478121 [details] [diff] [review]
mozrunner patch to hush FutureWarning on py2.7

Let's take this for 1.5.1
Attachment #478121 - Flags: feedback?(ctalbert) → feedback+
Heather, can you shepherd this change into 1.5.1 hotfix, please?
Whiteboard: [mozmill-1.5.1+]
(In reply to comment #7)
> Heather, can you shepherd this change into 1.5.1 hotfix, please?

Was this pushed? (Just wondering...)
(In reply to comment #7)
> Heather, can you shepherd this change into 1.5.1 hotfix, please?

Looks like that has been forgotten. I had to update the patch for the hotfix-1.5.1 and master branch. Both have been landed now:

http://github.com/mozautomation/mozmill/commit/a8ca453b8a90cb3bf545fc9d25bae44e283f744c (master)
http://github.com/mozautomation/mozmill/commit/1aa3a377642ae02913a8a02c1e989c268ecfab73 (hotfix-1.5.1)
The Add-on SDK is no longer a Mozilla Labs experiment and has become a big enough project to warrant its own Bugzilla product, so the "Add-on SDK" product has been created for it, and I am moving its bugs to that product.

To filter bugmail related to this change, filter on the word "looptid".
Component: Jetpack SDK → General
Product: Mozilla Labs → Add-on SDK
QA Contact: jetpack-sdk → general
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: