Planet maemo: category "feed:b1704e4f997dfb2448842ae2dc57ed3f"

Marius Gedminas

Surprising old-style class behaviour

2009-05-21 19:59 UTC  by  Marius Gedminas
0
0

Some anonymous Planet Python poster (at least I couldn't find the author's name on the blog) Christian Wyglendowski asks about a surprising difference between old-style and new-style classes. Since the comments on their blog are closed (which you find out only after pressing Submit), I'll answer here.

The question, slightly paraphrased: given a class

class LameContainerOld:
    def __init__(self):
        self._items = {'bar':'test'}
 
    def __getitem__(self, name):
        return self._items[name]
 
    def __getattr__(self, attr):
        return getattr(self._items, attr)

why does the 'in' operator work

>>> container = LameContainerOld()
>>> 'foo' in container
False
>>> 'bar' in container
True

when the equivalent new-style class raises a KeyError: 0 exception? Also, why does __getattr__ appear to be called to get the bound __getitem__ method of the dict?

>>> container.__getitem__
<bound method LameContainerNew.__getitem__ of {'bar': 'test'}>

What actually happens here is that LameOldContainer.__getattr__ gets called for special methods such as __contains__ and __repr__. This is why (1) the 'in' check works, and (2) it appears, at first glance, that you get the wrong __getitem__ bound method. If you pay close attention to the output, you'll see that it's the __getitem__ of LameOldContainer; it's just that repr(LameOldContainer()) gets proxied through to the dict.__repr__ when you don't expect it:

>>> container
{'bar': 'test'}

Special methods never go through __getattr__ for new-style classes, therefore neither __contains__ nor __repr__ are proxied if you make the container inherit object. If there's no __contains__ method, Python falls back to the sequence protocol and starts calling __getitem__ for numbers 0 through infinity, or until it gets an IndexError exception.

Marius Gedminas

Enabling comments in PyBlosxom

2009-05-16 03:46 UTC  by  Marius Gedminas
0
0

I've just spent the whole night setting up blog comments. PyBlosxom doesn't make it painless, sadly, more like the opposite.

First: don't be scared by the list of comment-related plugins on the PyBlosxom site. There's only one important plugin: comments. All others depend on it and enhance its functionality. The last three or four times I was about to add comments to my blog I got scared at step one: evaluate the available plugins. Don't repeat my mistake!

Second, follow the instructions carefully. There's no shortcut.

Third, fix what's broken. Be prepared to debug the source code. print >> sys.stderr, "message" is your friend.

Fourth, fiddle with the look (CSS and HTML).

Fifth, write a blog post and eagerly await your first comments.

Step 3 screams for an explanation, doesn't it? Problem 1: the comments plugin requires that you use categories in your blog. I'm not (I'm holding out for tags). Workaround: comment out if entry['absolute_path'] check in cb_story and cb_story_end.

Problem 2: the AJAX post returns "Empty response from server". Workaround: modify cb_story_end to call readComments directly if entry['num_comments'] is None, since cb_story, which usually does the read, is not called during the AJAX post.

Problem 3: if you enable comment moderation (by setting comment_draft_ext to a different value from comment_ext), the AJAX post returns "Empty response from server" once more. Workaround: modify cb_prepare to notice this case and set data['moderated'] = True, create a new template comment-moderated and render it in cb_story_end just like the preview template is rendered; also modify __shouldOutput to return True when rendering comment-moderated.

I'll post patches to the pyblosxom mailing tomorrow, unless I forget. It's 6 am already, and I'm kind of sleepy. I just hope I haven't inadvertently broken my RSS feed or flooded any planets.

Oh, and a helpful hint: don't name the post you're writing comments.txt, or the #comments anchor will point to the start of the story instead of the comments.

Marius Gedminas

Buildbot issues on Ubuntu Hardy

2009-05-15 12:43 UTC  by  Marius Gedminas
0
0

Update: The story continues, but solution is not in sight yet.

I upgraded a buildbot slave to Ubuntu 8.04 (Hardy) recently and now I'm getting a strange intermittent failure: sometimes cp -r /local/dir /nfs/mounted/dir fails ("process killed by signal 1", i.e. SIGHUP).

I wonder if NFS is relevant or incidental to the issue?

Google finds an old thread from 2005, with a workaround (usepty=False), but I'd like to understand the problem before applying random fixes.

So far three different build steps doing cp -r have failed during 10 days. I've now changed them all to cp -rv, so I can at least see if the failure is in the middle of the copy or at the end, if it fails again.

Update: so far 4 build steps have failed on 6 separate occasions:

May  5 02:31: cp -r local-dir1 nfs-mounted-dir1  
May  6 02:31: cp -r local-dir1 nfs-mounted-dir1  
May  6 04:33: cp -r local-dir2 nfs-mounted-dir2  
May 15 02:00: cp -r local-dir3 nfs-mounted-dir3  
May 17 04:32: rm -rf nfs-mounted-dir4            
May 20 04:31: rm -rf nfs-mounted-dir4            

I see no particular correlation between step duration and results, e.g. the rm -rf step usually takes between 2.2 and 4.6 seconds. The two SIGHUPs happened after 2.4 seconds.

They all make no output. When I changed the cp steps and added a -v, they stopped failing, but that could be just a coincidence.

We're having an email conversation with Jean-Paul Calderone ("exarkun") about the possibility of this being PTY-related, with no clear resolution so far.

And, hey, now this blog supports comments ;)

Marius Gedminas

Expert Python Programming

2009-05-08 03:46 UTC  by  Marius Gedminas
0
0

It's been a while since the last Expert Python Programming review on Planet Python. Y'all might've forgotten about this book by now. Time for a reminder? (Actually, I'm just lazy busy, and this is why this review hasn't appeared sooner.)

Click to read 1548 more words
Marius Gedminas

Filing bugs on GUI apps

2009-04-09 19:41 UTC  by  Marius Gedminas
0
0

Ubuntu people want users to use apport to report bugs. There's a command-line tool called 'ubuntu-bug' that you can use if you know the name of the package or at least the name of executable. There's a "Report a problem" menu item in many, but not all GUI apps.

Here's what you can do if the GUI app in question doesn't have that menu item, and you don't remember what it's called, and you're the same sort of a crazy command-line person that I am:

$ xprop|grep PID

Click on the app's window. Watch that shell command return a line that looks like

_NET_WM_PID(CARDINAL) = 807
Now run
$ ps 807  # substitute the real number

You'll see the command name, e.g.

  PID TTY      STAT   TIME COMMAND
  807 ?        S      0:02 /usr/lib/indicator-applet/indicator-applet --oaf-acti

Now you can run

$ ubuntu-bug /usr/lib/indicator-applet/indicator-applet

If you're not running Jaunty, you'll need to do one more step to find the name of the Ubuntu package:

$ dpkg -S /usr/lib/indicator-applet/indicator-applet
indicator-applet: /usr/lib/indicator-applet/indicator-applet

You can use that with apport-cli or on the Launchpad online bug reporting form.

For some (many?) programs you can short-circuit this trail by looking at the WM_CLASS property instead of the _NET_WM_PID property:

$ xprop|grep CLASS
WM_CLASS(STRING) = "indicator-applet", "Indicator-applet"

While there is no requirement for the window class name to match the name of the Ubuntu package or the name of the executable, it may give you a reasonable guess.

Marius Gedminas

Upgrade to Ubuntu Jaunty

2009-04-05 23:37 UTC  by  Marius Gedminas
0
0

Ubuntu 9.04 is going to be released in around three weeks. As usual I couldn't wait (and saw that some bugs that were irritating me every day were fixed in Jaunty), so I upgraded to the current beta.

Click to read 1164 more words
Marius Gedminas

Submitting patches the Launchpad way

2009-04-05 22:36 UTC  by  Marius Gedminas
0
0

Today I happened to read about lazr.enum in a mailing list. I went to the PyPI page and saw raw ReStructuredText markup instead of a nicely formatted page. Now I know from prior experience that this happens when the package's description has an error in the markup. I thought I'd report a bug and provide a patch.

Leap of knowledge: since I know lazr.enum was created by the Launchpad.net team I could safely assume they were keeping the sources in Launchpad. Therefore I was pretty sure I could get them with

$ bzr branch lp:lazr.enum

so I ran that command and it worked.

Next I looked at setup.py to see how it produces the long_description field. It was reading the contents of a couple of text files, one of them being src/lazr/enum/README.txt. I looked at that and saw a .. toc-tree: directive that does not exists in plain docutils (it's a Sphinx extension).

I added up a couple of lines to setup.py to strip that out, tested it (with setup.py --long-description > test.rst; restview test.rst) committed to my local branch, and created a bug report in Launchpad. Then I was a bit lost, since I didn't know how to make my fix available. Attach a patch? Maybe, but I wanted to see if this distributed version control thing is good for anything else.

I thought that first I'd make that branch public, and then see if there was a way to link it to the bug report. I ran

$ bzr push lp:~mgedmin/lazr.enum/pypi-fix

which took a few seconds to create a new public branch on Launchpad with my fix in it (it would be nice if I didn't have to explicitly specify my Launchpad username and the project name—both of which bzr already knows—and just specify the name of the branch). Then I went back to my bug report and saw an option to link it to a branch. There was a search field in the popup that found my "~mgedmin/lazr.enum/pypi-fix" easily enough when I pasted it into the search box.

After clicking on the branch, I saw a "propose a merge" option. I did that and Launchpad sent an email to the developers asking them to merge my fix.

I made one mistake, I think: I should've created the bug report first, and then mentioned the bug number in my commit message (with bzr commit --fixes=NNN, although here I'm suddenly not sure if the bug number should be left bare, or prefixed with something like "lp" to indicate it was a Launchpad bug number?).

Other than that it was a pretty smooth experience. When will I be able to do that for Ubuntu packages?

Marius Gedminas

Baking CSS into RSS

2009-04-03 07:38 UTC  by  Marius Gedminas
0
0

Wanted: PyBlosxom plugin to bake CSS styles into the RSS feed, i.e. replace things like <span style="keyword"> in the main blog pages with things like <span style="color: #ff7700"> in the RSS.

Also wanted: a spam-resistant comments plugin. (Maybe some/all of them are? I was always afraid to try.) And a tags plugin (categories are too limiting). And useful page titles. And a pony.

Update: Laurence Rowe and Alexander Artemenko suggested I check out cssutils. Alexander also pointed out that a similar question was asked on stackoverflow. Peter Bengtsson recently posted a similar tool built with lxml and regexps. Meanwhile, I took the plunge and installed a comments plugin, and even somehow managed not to get drowned in spam.

Marius Gedminas

Bug of the day

2009-04-02 21:38 UTC  by  Marius Gedminas
0
0

I think I'm going to blog about Ubuntu bugs I encounter during my day. Don't get me wrong—I love Ubuntu and haven't seen a better OS yet. But it has bugs.

Click to read 1894 more words
Marius Gedminas

X ate my keyboard, again

2009-03-20 21:41 UTC  by  Marius Gedminas
0
0

Last night I thought it would be fun to try to suspend my laptop with the 3G USB modem still plugged in and connected. This morning when I woke it up I had no keyboard.

Ctrl+Alt+F1 worked to switch to a text console (when I pressed it twice). The text console wasn't garbled, which was nice. killall gnome-settings-daemon or gnome-screensaver didn't fix anything. Suspending and resuming again didn't fix things either (not that there's any reason why it should). When I noticed that Ctrl+Alt+Backspace didn't work in X, I realized this wasn't an X grab issue.

/var/log/Xorg.0.log has a series of errors like this:

(EE) AT Translated Set 2 keyboard: Device has changed - disabling.
(WW) AT Translated Set 2 keyboard: Release failed (Invalid argument)

Sadly, there are no timestamps. I think this was printed once on resume, and then once on every VT switch.

xinput still lists "AT Translated Set 2 Keyboard" among its devices.

I remember now -- before suspending I had used sudo showkeys -s in an xterm (trying to see the scan code of the ThinkVantage button). Could've mucked up the keyboard mode?

Alt+SysRq+R (raw mode) didn't help, even when followed by more VT switches. I think I'm gonna restart the X server. Or reboot the whole PC, Ubuntu notifier is showing its reboot icon again...

Meta: I wrote this post on March 6, and then decided it wasn't interesting enough to post. And then posted it accidentally on March 20.

Update: looks like bug 327175.

Marius Gedminas

X.org and stuck Ctrl

2009-02-26 23:42 UTC  by  Marius Gedminas
0
0

Warning: this is going to be a long story with a semi-happy ending. Skip it unless you enjoy tales of woe and debugging.

Click to read 2174 more words
Marius Gedminas

Largest apps on my laptop #2

2009-02-22 10:40 UTC  by  Marius Gedminas
0
0

I recently posted some data about applications taking up the most RAM on my laptop. That was after 9 days of uptime, while this is after 12 hours:

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
16033 root      20   0  527m 109m  11m S    3  5.5   6:52.82 Xorg
17834 mg        20   0  244m 105m  24m S    6  5.3   3:26.49 firefox
26425 mg        20   0 96872  58m  12m S    0  2.9   0:01.71 evince
16747 mg        20   0 90704  54m  19m S    0  2.8   0:10.70 tomboy
27169 mg        20   0  166m  53m  23m S    0  2.7   0:07.05 banshee-1
27167 mg        20   0 77392  31m  17m S    0  1.6   0:01.16 pidgin
16706 mg        20   0 86508  27m  18m S    0  1.4   0:35.35 gnome-panel
16708 mg        20   0 75196  20m  14m S    0  1.0   0:02.80 nautilus
20092 mg        20   0 61880  19m  11m S    1  1.0   0:07.08 gnome-terminal
16614 mg        20   0 58456  15m 9836 S    0  0.8   0:09.82 gnome-settings-

I don't have GNOME Do any more, and I've only one of the two PDFs open in Evince. I don't see multiload-applet on the first page of top output, which seems to indicate a slow leak. Evince has the same two documents. That concept doesn't quite apply to Banshee or Pidgin, but Pidgin's numbers are quite striking anyway (from 70 megs VIRT to 1.6 gigs VIRT in 9 days; thankfully RES only grows 2x during that time).

OS: Ubuntu 8.10, up-to-date with all the updates from -security, -updates, -proposed-updates and -backports.

Incidentally, I have 12 hours of uptime because my battery died while the laptops was suspended during my flight back home (either that, or it work up in the backpack, which is a scary thought). Apparently Ubuntu tried to hibernate when the battery was very low, which was a nice gesture. This didn't work out so well when resuming, since the kernels didn't match -- I had installed a kernel update, but hadn't rebooted. I don't think I ever used hibernation successfully in Linux.