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

Re: [Xen-devel] [PATCH for-next RFC 2/4] libxl: make python scripts work with python 2 and 3



On Tue, Mar 05, 2019 at 05:34:13PM +0000, Andrew Cooper wrote:
> On 05/03/2019 16:42, Wei Liu wrote:
> > All scripts are transformed by 2to3.
> >
> > The only addition is "from __future__ import print_function" so that
> > print("BLAH", file=sys.stderr) can work.
> >
> > https://python-future.org/compatible_idioms.html
> >
> > Tested with 2.7 and 3.5.
> >
> > Signed-off-by: Wei Liu <wei.liu2@xxxxxxxxxx>
> > ---
> > I don't have environment to test 2.4 -- it is almost 15 years old. We
> > may want to consider bumping the minimum requirement to 2.7?
> 
> The compatible way to do this is  sys.stderr.write(msg + "\n") and using
> print() without the future import.
> 
> > @@ -269,7 +271,7 @@ class KeyedUnion(Aggregate):
> >          if not isinstance(keyvar_type, Enumeration):
> >              raise ValueError
> >  
> > -        kv_kwargs = dict([(x.lstrip('keyvar_'),y) for (x,y) in 
> > kwargs.items() if x.startswith('keyvar_')])
> > +        kv_kwargs = dict([(x.lstrip('keyvar_'),y) for (x,y) in 
> > list(kwargs.items()) if x.startswith('keyvar_')])
> 
> This shouldn't need changing.  List comprehensions are one of the few
> uses of .items() which is compatible with older versions of python IIRC.
> 
> > @@ -362,11 +364,10 @@ def parse(f):
> >              globs[n] = t
> >  
> >      try:
> > -        execfile(f, globs, locs)
> > -    except SyntaxError,e:
> > -        raise SyntaxError, \
> > -              "Errors were found at line %d while processing %s:\n\t%s"\
> > -              %(e.lineno,f,e.text)
> > +        exec(compile(open(f).read(), f, 'exec'), globs, locs)
> > +    except SyntaxError as e:
> 
> This is the only really awkward bit, and isn't Py 2.4 compatible.
> 
> The only option here to retain pre 2.6 compatibility is:
> 
> try:
>     ...
> except SyntaxError:
>     _, e = sys.exc_info()[:2]
>     ...

Since we will bump python requirement to 2.6, I think the transformation
made by 2to3 should be fine.

Wei.

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