Fails on Debian 9 with Mercurial 4.0 with message "*** failed to import extension b from [...]: 'module' object has no attribute 'command'"
Trying to install the revision a7366785c330 of B on a server running Debian 9 with Mercurial 4.0 fails with the error message as follows, displayed first by any hg command:
*** failed to import extension b from ~/proj/b/src/b.py: 'module' object has no attribute 'command'
I fixed this with a hack based on a stackoverflow.com question that was about the same incompatibility but in reverse. Here's the exported commit with that hack:
# HG changeset patch
# User C. Masloch <pushbx@ulukai.org>
# Date 1588979195 -7200
# Sat May 09 01:06:35 2020 +0200
# Node ID af75eccb7781d97bcfc5479463aa447fa8b4bdc8
# Parent a7366785c330820c65836f5f4354ca2520a7a27a
hack a bugfix to work on Mercurial 4.0
Before this, enabling the extension with Mercurial 4.0
would lead to the following error:
*** failed to import extension b from ~/proj/b/src/b.py: 'module' object has no attribute 'command'
The following question had the same issue in reverse:
https://stackoverflow.com/questions/52117289/simple-mercurial-extension-fails-to-import
This is a hack and will cause the extension to fail on
newer Mercurial versions. The choice to make should be
detected based on the version but I don't know enough
about Python and Mercurial to add that.
diff --git a/src/b.py b/src/b.py
--- a/src/b.py
+++ b/src/b.py
@@ -48,7 +48,7 @@
from operator import itemgetter
from mercurial.error import Abort
from mercurial.i18n import _
-from mercurial import hg, commands, registrar
+from mercurial import hg, commands, cmdutil, registrar
#
# Version Info
@@ -956,7 +956,7 @@
cmdtable = {}
-command = registrar.command(cmdtable)
+command = cmdutil.command(cmdtable)
testedwith = '4.7' # And others circa 2010, before this variable existed
buglink = 'http://hg.mwdiamond.com/b'
These are the Mercurial and Debian versions:
$ hg --version
Mercurial Distributed SCM (version 4.0)
(see https://mercurial-scm.org for more information)
Copyright (C) 2005-2016 Matt Mackall and others
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
VERSION_CODENAME=stretch
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
$
Although the server likely will be updated to Debian 10 soon and the Mercurial shipped with that may be new enough, I thought this report might be useful for others.