[PATCH RFC-STABLE] hg: improve error message when no `mercurial.util.setbinary` function exists

Martin Geisler mg at aragost.com
Fri May 4 09:27:33 CDT 2012


pierre-yves.david at logilab.fr writes:

> # HG changeset patch
> # User Pierre-Yves David <pierre-yves.david at logilab.fr>
> # Date 1336133022 -7200
> # Branch stable
> # Node ID 9450905de257e1ed09e1855ede0305567cf07b51
> # Parent  91323a78aac252b630144f66e1039321cd41c9ef
> hg: improve error message when no `mercurial.util.setbinary` function exists
>
> When newer binary is used with an older mercurial library, hg invocation fails
> with an AttributeError python traceback.
>
> Such error is common when people misconfigured their PYTHONPATH variable and use
> an old system wide library with a newer executable in PATH.
>
> The new error output will be::
>
>     abort: mercurial executable and python libraries mismatch!
>     executable:
>          /home/babar/bin/hg
>     library:
>          /usr/lib/python/site-packages/mercurial-dev/mercurial/__init__.pyc
>     (check your install, PYTHONPATH and PATH)
>     [255]

We already have some code earlier in the hg script that checks for a
@LIBDIR@ (replaced by setup.py). Should that not be able to catch this
kind of mistake?

Also, checking for mercurial.util.setbinary is just one (quite weak)
signal to the hg script that tells it that it has imported a wrong
version of the mercurial module.

Could it not be done better -- for example by letting setup.py write the
version number it writes to mercurial/__version__.py into hg too? That
would make it very explicit if /usr/local/bin/hg loads the mercurial
module from /usr/lib/pythonX.Y/ (instead of /usr/local/lib/pythonX.Y.

> diff --git a/hg b/hg
> --- a/hg
> +++ b/hg
> @@ -31,8 +31,14 @@ except ImportError:
>  
>  import mercurial.util
>  import mercurial.dispatch
>  
>  for fp in (sys.stdin, sys.stdout, sys.stderr):
> +    if getattr(mercurial.util, 'setbinary', None) is None:

I think you want this check outside of the loop.

> +        sys.stderr.write("abort: mercurial executable and python libraries mismatch!\n")
> +        sys.stderr.write("executable:\n\t%s\n" % sys.argv[0])
> +        sys.stderr.write("library:\n\t%s\n" % mercurial.__file__)
> +        sys.stderr.write("(check your install, PYTHONPATH and PATH)\n")
> +        sys.exit(-1)
>      mercurial.util.setbinary(fp)
>  
>  mercurial.dispatch.run()
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel

-- 
Martin Geisler

aragost Trifork
Commercial Mercurial support
http://aragost.com/mercurial/


More information about the Mercurial-devel mailing list