[pacman-dev] [PATCH 8/8] Allow tests to be run with either version 2 or 3 of Python.

Martin Panter vadmium+patch at gmail.com
Thu Oct 10 00:50:22 EDT 2013


On 10 October 2013 00:35, Jeremy Heiner <scalaprotractor at gmail.com> wrote:
> Discovered by running with 3.3.2. One test changed to avoid a lexical
> scoping change (fortunately the standard zip function is supported by
> all necessary versions and does what is needed). The other changes
> were to adjust to the more strict separation Python 3 puts between
> Unicode and byte data.
>
> Signed-off-by: Jeremy Heiner <ScalaProtractor at gmail.com>
>
> Conflicts:
>         test/pacman/util.py
> ---
>  test/pacman/tests/epoch005.py | 2 +-
>  test/pacman/util.py           | 7 +++++--
>  2 files changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/test/pacman/tests/epoch005.py b/test/pacman/tests/epoch005.py
> index 6215170..3e2073a 100644
> --- a/test/pacman/tests/epoch005.py
> +++ b/test/pacman/tests/epoch005.py
> @@ -8,7 +8,7 @@
>         "1234327518932650063289125782697892:1.0-1",
>  )
>
> -pkgvers = [(n, versions[n]) for n in range(len(versions))]
> +pkgvers = zip(range(len(versions)), versions)
>  for k, v in pkgvers:
>         sp = pmpkg("pkg_%d" % k, v)
>         self.addpkg2db("sync", sp)
> diff --git a/test/pacman/util.py b/test/pacman/util.py
> index 86d0eb2..9283852 100644
> --- a/test/pacman/util.py
> +++ b/test/pacman/util.py
> @@ -71,11 +71,14 @@ def itemsview(x):
>  def new_StringIO(source):
>      if sys.hexversion >= 0x03000000:
>          import io
> -        return io.StringIO(source)
> +        return io.BytesIO(source.encode("utf8"))

To me, having learnt Python 3 before Python 2, I found the name
new_StringIO() misleading, since you are actually returning a BytesIO
object. Anyway, the “io” module was added in Python 2.6, so maybe you
can get rid of the whole workaround.

>      else:
>          import StringIO
>          return StringIO.StringIO(source)
>
> +def chars2bytes(source):
> +    return source.encode("utf8") if sys.hexversion >= 0x03000000 else source
> +

Python 2 can also do str.encode("utf8"), why only do it for Python 3?

>
>  #
>  # Methods to generate files
> @@ -181,7 +184,7 @@ def getmd5sum(filename):
>
>  def mkmd5sum(data):
>      checksum = hashlib.md5()
> -    checksum.update("%s\n" % data)
> +    checksum.update(chars2bytes("%s\n" % data))
>      return checksum.hexdigest()
>
>
> --
> 1.8.4


More information about the pacman-dev mailing list