[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Xen-devel] python3 issues



On Wed, Mar 27, 2019 at 01:36:10AM +0100, Hans van Kranenburg wrote:
> On 3/26/19 2:16 PM, Wei Liu wrote:
> > On Mon, Mar 25, 2019 at 10:20:05PM +0000, YOUNG, MICHAEL A. wrote:
> >> [...]
> >> --- xen-4.12.0-rc6/tools/pygrub/src/pygrub.orig    2019-03-24 
> >> 22:44:05.503582025 +0000
> >> +++ xen-4.12.0-rc6/tools/pygrub/src/pygrub 2019-03-24 22:48:24.446113809 
> >> +0000
> >> @@ -457,7 +457,7 @@
> >>          # limit read size to avoid pathological cases
> >>          buf = f.read(FS_READ_MAX)
> >>          del f
> >> -        self.cf.parse(buf)
> >> +        self.cf.parse(buf.decode())
> > 
> > Hmm... This could be a bit problematic for 2 compatibility. I will need
> > some time to check the documents.
> 
> The exact moment when you're typing .decode() or .encode() to try fix a
> python2/3 problem, you're entering a world of pain (tm).
> 
> [insert big lebowski walter pointing gun gif here]
> 
> Some of the python 2 and 3 compatibility things can be fixed easily by
> importing things from the future, but when dealing with strings and
> bytes, you're entering the "is it really worth it" department.
> 
> In python 2, strings are bytes, and there's some added duct tape
> available to do something with unicode and utf-8.
> 
> In python 3, strings are unicode and bytes are bytes, which is sooooooo
> much more convenient. If the strings have to be stored in some location
> that stores bytes, you can encode them. If you have some incoming byte
> stream, you can decode it.
> 
> I don't really have recommendations to make both of them work with the
> same lines of code since I tried and gave up when doing it myself.
> 
> What you're looking at is how the in-memory storage for a 'string' is
> done and how to connect input and output to that, while looking at
> python 2 and 3 functions with the same name, doing different things in
> a different context. There is no sane way to do this that works with
> python 2 and 3.

I have tested the following snippet:

    from __future__ import print_function
    import sys
    
    
    s = b'1234'
    if sys.version_info[0] < 3:
        print(s) 
    else:
        print(s.decode())

It seems to be the easiest way to solve this issue.

Wei.

> 
> Hans

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.