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

Brian C. Lane bcl at redhat.com
Sat Jun 30 00:16:49 BST 2018


On Fri, Jun 29, 2018 at 09:59:22AM +0100, Colin Watson wrote:
> 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.

Thanks for the review, I'm on vacation all next week so I likely won't
get back to this after the 9th.

> 
> > 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.

Note that it's a % not a , so adding extra args would require  "%s %s" %
(a, b) instead. I don't really have a problem adding the future
import, but I don't think it really makes any difference.

> 
> >      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?

Oh dangit, you're right. I'll fix this up after I get back.

Thanks again!

-- 
Brian C. Lane (PST8PDT)



More information about the parted-devel mailing list