[Urwid] urwid.command_map

Alain Leufroy alain.leufroy at logilab.fr
Thu Sep 15 11:55:56 EDT 2011

>> I found something strange with `urwid.command_map`.
>> It's a module given by the file *urwid/command_map.py*
>> and it is also an  instance of `urwid.command_map.Command_map`.
>> This introduce a problem with lazy import systems (or lazy import introduce
>> the problem :D), mercurial for instance.
> Interesting.  I haven't played with lazy importing much, except for what
> django does.  What exactly causes the problem?

I don't know exactly why. It seems that doing

     >>> from urwid.command_map import CommandMap

is converted to something like:

     >>> import urwid.command_map
     >>> urwid.command_map.CommandMap

if you want to play with it ::

     $ cd /tmp
     $ hg init lazy
     $ cd lazy
     $ cat >> lazy.py << EOF
     from mercurial import hg, commands, dispatch, error
     def do_it(ui, repo, *pats, **opts):
         import cmdmap
     cmdtable = {"lazy": (do_it,[], "do it"),}
     $ cat >> cmdmap.py << EOF
     from urwid.widget import Text
     TEXT = Text("")
     print TEXT
     from urwid.command_map import CommandMap
     MAP = CommandMap()
     print MAP

Add `lazy = /tmp/lazy/lazy.py` in your ~/.hgrc (section `[extensions]`).

    $ cd /tmp/lazy
    $ python cmdmap.py
    <Text flow widget ''>
    <urwid.command_map.CommandMap instance at 0x2802d40>
    $ hg lazy
    <Text flow widget ''>
    Traceback (most recent call last):
    TypeError: <unloaded module 'CommandMap'> object is not callable

>> Does this behaviour is really needed ?
> Well it's been that way for a few releases, so changing it will break
> some people's code.

Perhaps adding "CommandMap" to urwid/__init__.py(__all__) should be enough.

> Ian

Alain Leufroy
LOGILAB, Paris (France)
Informatique scientifique & gestion de connaissances

More information about the Urwid mailing list