[PATCH 01 of 10] py3: use unicode literals in encoding.py

Pulkit Goyal 7895pulkit at gmail.com
Wed Aug 3 11:27:42 EDT 2016


> diff -r cf6739a27b8f mercurial/__init__.py
> --- a/mercurial/__init__.py     Wed Aug 03 22:34:54 2016 +0900
> +++ b/mercurial/__init__.py     Wed Aug 03 22:47:17 2016 +0900
> @@ -310,6 +310,10 @@
>          The added header has the form ``HG<VERSION>``. That is a literal
>          ``HG`` with 2 binary bytes indicating the transformation version.
>          """
> +        _notransform = set([
> +            'mercurial.demandimport',
> +        ])
> +
>          def get_data(self, path):
>              data = super(hgloader, self).get_data(path)
>
> @@ -336,9 +340,10 @@
>
>          def source_to_code(self, data, path):
>              """Perform token transformation before compilation."""
> -            buf = io.BytesIO(data)
> -            tokens = tokenize.tokenize(buf.readline)
> -            data = tokenize.untokenize(replacetokens(list(tokens)))
> +            if self.name not in self._notransform:
> +                buf = io.BytesIO(data)
> +                tokens = tokenize.tokenize(buf.readline)
> +                data = tokenize.untokenize(replacetokens(list(tokens)))
>              # Python's built-in importer strips frames from exceptions raised
>              # for this code. Unfortunately, that mechanism isn't extensible
>              # and our frame will be blamed for the import failure. There
>
>
> If (almost) all of operations with string literal in target source
> code requires unicode-ness on Python 3.x, this omitting can reduce
> adding explicit 'u' prefix to existing string literals.
>
> For example, all operations with string literal in demandimport.py are
> related to APIs below, which accept only unicode (as str) on Python
> 3.x.
>
>   - manipulate module name
>     split(), formatting with "%s", __contains__(), and so on
>   - access to attributes by name
>   - access to values in os.environ
>   - access to values in sys.builtin_module_names
>
> pycompat.py and i18n.py also seem to work with this omitting. At short
> glance, maybe, pure/osutil.py does, too ? (a few extra explicit 'b'
> prefix might be needed, though)
>
> How about this omitting ?

I think this is good as it increase flexibility to use the module
loader and we can omit files where module loader is creating problems.


More information about the Mercurial-devel mailing list