[parted-devel] [PATCH 1/2] Modify gpt-header-move and msdos-overlap to work with py2 or py3

Colin Watson cjwatson at debian.org
Fri Jun 29 09:59:22 BST 2018


On Wed, Jun 27, 2018 at 02:53:45PM -0700, Brian C. Lane wrote:
> diff --git a/tests/gpt-header-move b/tests/gpt-header-move
> index 05cdc65..3dda5cb 100755
> --- a/tests/gpt-header-move
> +++ b/tests/gpt-header-move

The changes to this file look fine.

> diff --git a/tests/msdos-overlap b/tests/msdos-overlap
> index 5bddfb0..48cfa7f 100755
> --- a/tests/msdos-overlap
> +++ b/tests/msdos-overlap
> @@ -14,12 +14,11 @@ BAD_ENTRY = (0x72, 0xf5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
>  OFFSET = 0x1b8
>  
>  if len(sys.argv) < 2:
> -    print "%s: <image or device>"
> +    print("%s: <image or device>" % sys.argv[0])

I'd add "from __future__ import print_function" to the top of the file.
The code you have happens to work without that, since an expression
consisting of parentheses around a single expression just yields that
single expression, but it would go wrong on Python 2 if anyone followed
Python 3 syntax and tried to add more than one argument to the print
function; the __future__ import makes this more robust.

>      sys.exit(1)
>  
> -data = "".join(chr(c) for c in BAD_ENTRY)
>  with open(sys.argv[1], "rb+") as f:
>      f.seek(OFFSET, 0)
> -    f.write(data)
> +    f.write(bytes(BAD_ENTRY))

This is wrong if you're aiming for bilingual Python 2/3 code.  On Python
2 (truncating BAD_ENTRY for brevity):

  >>> bytes((0x72, 0xf5, 0x00, 0x00))
  '(114, 245, 0, 0)'

... while on Python 3:

  >>> bytes((0x72, 0xf5, 0x00, 0x00))
  b'r\xf5\x00\x00'

(This makes sense when you remember that bytes == str in Python 2, so in
that case you're just stringifying a tuple.)

I'm on my first cup of coffee, but how about bytes(bytearray(BAD_ENTRY))
instead, which I believe works as desired in both Python 2 and 3?

-- 
Colin Watson                                       [cjwatson at debian.org]



More information about the parted-devel mailing list