Merge lp:~mhall119/apps-brancher/fix-1010507 into lp:~dholbach/apps-brancher/trunk

Proposed by Michael Hall
Status: Merged
Merged at revision: 43
Proposed branch: lp:~mhall119/apps-brancher/fix-1010507
Merge into: lp:~dholbach/apps-brancher/trunk
Diff against target: 12 lines (+1/-1)
1 file modified
brancher/appcache.py (+1/-1)
To merge this branch: bzr merge lp:~mhall119/apps-brancher/fix-1010507
Reviewer Review Type Date Requested Status
Daniel Holbach Pending
Review via email: mp+109441@code.launchpad.net

Commit message

Use a copy of the app data, not the app data itself, before converting the timestamp to a serializable form

Description of the change

Well this was a fun one to track down, and has everything to do with how Python objects work.

When you use:
 data = app.__dict__
You aren't getting new dictionary with a copy of app's fields and data, it's actually the app itself in dictionary form.

So when you do:
 data['tarball_timestamp'] = 'foo'
You're actually changing app.tarball_timestamp as well.

Now, app was placed into the self.cache list. Not a copy of app, but a reference to the actual app instance. So, when you changed the dictionary form of the app instance, you also changed it in the cache.

So essentially, when saving the cache to a file, you were changing the in-memory cache so that the app instance it held had it's tarball_timestamp field converted from a datetime to an str.

Fun huh?

To post a comment you must log in.
Revision history for this message
Daniel Holbach (dholbach) wrote :

WOW. Let me try this out.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'brancher/appcache.py'
2--- brancher/appcache.py 2012-06-08 16:25:59 +0000
3+++ brancher/appcache.py 2012-06-08 22:01:17 +0000
4@@ -24,7 +24,7 @@
5 def save(self):
6 data = []
7 for app in self.cache:
8- d = app.__dict__
9+ d = dict(app.__dict__)
10 if isinstance(d['tarball_timestamp'], datetime):
11 d['tarball_timestamp'] = app.tarball_timestamp.strftime("%Y-%m-%d %H:%M:%S")
12 data += [ d ]

Subscribers

People subscribed via source and target branches

to all changes: