[PATCH v3] command options: abort on unicode defaults

Yuya Nishihara yuya at tcha.org
Fri Sep 1 08:50:06 EDT 2017


On Fri, 01 Sep 2017 09:44:24 +0200, Christophe de Vienne wrote:
> # HG changeset patch
> # User Christophe de Vienne <christophe at cdevienne.info>
> # Date 1504023891 -7200
> #      Tue Aug 29 18:24:51 2017 +0200
> # Node ID 06bc5adebc48cfb657cdefda41704a96f686fae7
> # Parent  b2eb0aa445cbe7cadc8b7691c6266908adfc5057
> command options: abort on unicode defaults
> 
> If the default value of an option is a unicode string (something
> than happen easily when using a 'from __future__ import unicode_literals'),
> any value passed on the command line will be ignored because the fancyopts
> module only checks for byte strings and not unicode strings.
> 
> Changing fancyopts behavior is easy but would make assumptions on how
> the python3 port should be done, which is outside the scope of this patch.
> 
> The chosen approach is to abort when a unicode default value is detected,
> with a hint for the developer.
> 
> diff -r b2eb0aa445cb -r 06bc5adebc48 mercurial/dispatch.py
> --- a/mercurial/dispatch.py	Tue Aug 22 21:21:43 2017 -0400
> +++ b/mercurial/dispatch.py	Tue Aug 29 18:24:51 2017 +0200
> @@ -558,6 +558,14 @@
>              args = pycompat.maplist(
>                  util.expandpath, pycompat.shlexsplit(defaults)) + args
>          c = list(entry[1])
> +        for option in entry[1]:
> +            default = option[2]

Maybe this can be checked at fancyopts where the options table is processed.
Alternatively, it could be added to extensions._validatecmdtable().

> +            if not pycompat.ispy3 and isinstance(default, type(u'')):

This can be enabled on Python 3. unicode strings should be banned.

> +                raise error.Abort(

error.ProgrammingError?

> +                    "A unicode default value (%s) was passed to the "
> +                    "%s.%s option" % (repr(default), cmd, option[1]),
> +                    "You should change the %s.%s default value to a "
> +                    "non-unicode string" % (cmd, option[1]))


More information about the Mercurial-devel mailing list