Apport API users: Watch your data types / Python 3 porting

Martin Pitt martin.pitt at ubuntu.com
Fri May 18 14:41:09 UTC 2012


Hello Ubuntu developers,

I just uploaded Apport 2.1 to Quantal. A big change in that version is
that the whole code now works with both Python 2 and 3, except for the
launchpadlib crash database backend (as we do not yet have a
python3-launchpadlib package).

I took some care that apport report objects get along with both
strings ("unicode" type in Python 2) and byte arrays ("str" type in
Python 2) in values, so most package hooks should still work. However,
now is the time to check whether they also work with Python 3, to make
the impending transition to Python 3 easier.

However, you need to watch out if you use projects or scripts which
directly use python-apport to process reports: The open(), write(),
and write_mime() methods now require the passed file descriptors to be
open in binary mode. You will get an exception otherwise.

A common pattern so far has been code like

  report = apport.Report()
  report.load(open('myfile.crash'))

This needs to be changed to

  report = apport.Report()
  with open('myfile.crash', 'rb') as f:
      report.load(f)

The "with" context is not strictly required, but it takes care of
timely closing the files again. This avoids ResourceWarning spew
when you run this in test suites or enable warnings.

Thanks,

Martin
 
-- 
Martin Pitt                        | http://www.piware.de
Ubuntu Developer (www.ubuntu.com)  | Debian Developer  (www.debian.org)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <https://lists.ubuntu.com/archives/ubuntu-devel/attachments/20120518/cc98b969/attachment.pgp>


More information about the ubuntu-devel mailing list