[PATCH 5 of 5] check-code: handle py3 open divergence
Martijn Pieters
mj at zopatista.com
Mon May 16 12:22:18 EDT 2016
Rather than make Python 3 behave as Python 2, why not use `io.open()`
in Python 2? The *exact same behaviour* is available in Python 2, with
the only difference really being that `io.open()` is the built-in
`open()` in Python 3.
On 11 May 2016 at 05:12, timeless <timeless at fmr.im> wrote:
> # HG changeset patch
> # User timeless <timeless at mozdev.org>
> # Date 1462931171 0
> # Wed May 11 01:46:11 2016 +0000
> # Node ID c5dfd864d81f5d1f435ff376ea44b3ea82b12575
> # Parent ed2dbfec165a0de79c93d7fa1ea9cb5f36b10c29
> # EXP-Topic runtests
> # Available At bb://timeless/mercurial-crew
> # hg pull bb://timeless/mercurial-crew -r c5dfd864d81f
> check-code: handle py3 open divergence
>
> open() really wants an encoding attribute
>
> diff -r ed2dbfec165a -r c5dfd864d81f contrib/check-code.py
> --- a/contrib/check-code.py Wed May 11 01:44:39 2016 +0000
> +++ b/contrib/check-code.py Wed May 11 01:46:11 2016 +0000
> @@ -26,6 +26,11 @@
> import os
> import re
> import sys
> +if sys.version_info[0] < 3:
> + opentext = open
> +else:
> + def opentext(f):
> + return open(f, encoding='ascii')
> try:
> xrange
> except NameError:
> @@ -493,8 +498,12 @@
> result = True
>
> try:
> - with open(f) as fp:
> - pre = post = fp.read()
> + with opentext(f) as fp:
> + try:
> + pre = post = fp.read()
> + except UnicodeDecodeError as e:
> + print("%s while reading %s" % (e, f))
> + return result
> except IOError as e:
> print("Skipping %s, %s" % (f, str(e).split(':', 1)[0]))
> return result
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
--
Martijn Pieters
More information about the Mercurial-devel
mailing list