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

Re: [Xen-devel] [PATCH for-next v2 v2 2/5] libxl: make python scripts work with python 2.6 and up


  • To: Wei Liu <wei.liu2@xxxxxxxxxx>, xen-devel@xxxxxxxxxxxxxxxxxxxx
  • From: Hans van Kranenburg <hans@xxxxxxxxxxx>
  • Date: Wed, 6 Mar 2019 21:58:19 +0100
  • Autocrypt: addr=hans@xxxxxxxxxxx; prefer-encrypt=mutual; keydata= mQINBFo2pooBEADwTBe/lrCa78zuhVkmpvuN+pXPWHkYs0LuAgJrOsOKhxLkYXn6Pn7e3xm+ ySfxwtFmqLUMPWujQYF0r5C6DteypL7XvkPP+FPVlQnDIifyEoKq8JZRPsAFt1S87QThYPC3 mjfluLUKVBP21H3ZFUGjcf+hnJSN9d9MuSQmAvtJiLbRTo5DTZZvO/SuQlmafaEQteaOswme DKRcIYj7+FokaW9n90P8agvPZJn50MCKy1D2QZwvw0g2ZMR8yUdtsX6fHTe7Ym+tHIYM3Tsg 2KKgt17NTxIqyttcAIaVRs4+dnQ23J98iFmVHyT+X2Jou+KpHuULES8562QltmkchA7YxZpT mLMZ6TPit+sIocvxFE5dGiT1FMpjM5mOVCNOP+KOup/N7jobCG15haKWtu9k0kPz+trT3NOn gZXecYzBmasSJro60O4bwBayG9ILHNn+v/ZLg/jv33X2MV7oYXf+ustwjXnYUqVmjZkdI/pt 30lcNUxCANvTF861OgvZUR4WoMNK4krXtodBoEImjmT385LATGFt9HnXd1rQ4QzqyMPBk84j roX5NpOzNZrNJiUxj+aUQZcINtbpmvskGpJX0RsfhOh2fxfQ39ZP/0a2C59gBQuVCH6C5qsY rc1qTIpGdPYT+J1S2rY88AvPpr2JHZbiVqeB3jIlwVSmkYeB/QARAQABtCZIYW5zIHZhbiBL cmFuZW5idXJnIDxoYW5zQGtub3JyaWUub3JnPokCTgQTAQoAOBYhBOJv1o/B6NS2GUVGTueB VzIYDCpVBQJaNq7KAhsDBQsJCAcDBRUKCQgLBR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E0C7J03ir90PlTEkMEHEzyJMPt OhO05Phy+d51WPTo1VSKxhL4bsWddHLfQoXW8RQ388Q69JG4m+JhNH/XvWe3aQFpYP+GZuzO hkMez0lHCaVOOLBSKHkAHh9i0/pH+/3hfEa4NsoHCpyy
  • Cc: George Dunlap <george.dunlap@xxxxxxxxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>, Ian Jackson <ian.jackson@xxxxxxxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • Delivery-date: Wed, 06 Mar 2019 20:58:29 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Openpgp: preference=signencrypt

On 3/6/19 6:52 PM, Wei Liu wrote:
> Go through the transformations suggested by 2to3 and pick the
> necessary ones.
> 
> Use sys.stderr.write to avoid importing from __future__.
> 
> Signed-off-by: Wei Liu <wei.liu2@xxxxxxxxxx>
> ---
>  tools/libxl/gentest.py  |  2 +-
>  tools/libxl/gentypes.py | 10 +++++-----
>  tools/libxl/idl.py      | 13 ++++++-------
>  3 files changed, 12 insertions(+), 13 deletions(-)
> 
> diff --git a/tools/libxl/gentest.py b/tools/libxl/gentest.py
> index 989959fc68..81e13b437c 100644
> --- a/tools/libxl/gentest.py
> +++ b/tools/libxl/gentest.py
> @@ -86,7 +86,7 @@ def gen_rand_init(ty, v, indent = "    ", parent = None):
>  
>  if __name__ == '__main__':
>      if len(sys.argv) < 3:
> -        print >>sys.stderr, "Usage: gentest.py <idl> <implementation>"
> +        sys.stderr.write("Usage: gentest.py <idl> <implementation>\n")
>          sys.exit(1)
>  
>      random.seed(os.getenv('LIBXL_TESTIDL_SEED'))
> diff --git a/tools/libxl/gentypes.py b/tools/libxl/gentypes.py
> index 88e5c5f30e..656c157c01 100644
> --- a/tools/libxl/gentypes.py
> +++ b/tools/libxl/gentypes.py
> @@ -576,14 +576,14 @@ def libxl_C_enum_from_string(ty, str, e, indent = "    
> "):
>  
>  if __name__ == '__main__':
>      if len(sys.argv) != 6:
> -        print >>sys.stderr, "Usage: gentypes.py <idl> <header> 
> <header-private> <header-json> <implementation>"
> +        sys.stderr.write("Usage: gentypes.py <idl> <header> <header-private> 
> <header-json> <implementation>\n")
>          sys.exit(1)
>  
>      (_, idlname, header, header_private, header_json, impl) = sys.argv
>  
>      (builtins,types) = idl.parse(idlname)
>  
> -    print "outputting libxl type definitions to %s" % header
> +    print("outputting libxl type definitions to %s" % header)

Note that print is *not* a function in python 2, unless you..

  from __future__ import print_function

...which should not be avoided.

It looks like print() just works as a function, but it's not, and I
would recommend against introducing this kind of misleading code.

In print("hallo"), the parentheses are part of an expression, like in
  ((1+ 1) * 2)

Other syntax with parentheses creates tuples.

Python 2.7.13 (>>> replaced with ===)

=== type(())
<type 'tuple'>
=== type((1+1))
<type 'int'>
=== type((1, 2))
<type 'tuple'>

=== print () <- tuple
()
=== print (1+1) <- expression
2
=== print (1, 2) <- tuple
(1, 2)

=== from __future__ import print_function
=== print()

=== print(1+1)
2
=== print(1, 2)
1 2

>      f = open(header, "w")
>  
> @@ -633,7 +633,7 @@ if __name__ == '__main__':
>      f.write("""#endif /* %s */\n""" % (header_define))
>      f.close()
>  
> -    print "outputting libxl JSON definitions to %s" % header_json
> +    print("outputting libxl JSON definitions to %s" % header_json)
>  
>      f = open(header_json, "w")
>  
> @@ -657,7 +657,7 @@ if __name__ == '__main__':
>      f.write("""#endif /* %s */\n""" % header_json_define)
>      f.close()
>  
> -    print "outputting libxl type internal definitions to %s" % header_private
> +    print("outputting libxl type internal definitions to %s" % 
> header_private)
>  
>      f = open(header_private, "w")
>  
> @@ -683,7 +683,7 @@ if __name__ == '__main__':
>      f.write("""#endif /* %s */\n""" % header_json_define)
>      f.close()
>  
> -    print "outputting libxl type implementations to %s" % impl
> +    print("outputting libxl type implementations to %s" % impl)
>  
>      f = open(impl, "w")
>      f.write("""
> diff --git a/tools/libxl/idl.py b/tools/libxl/idl.py
> index 2a7f3c44fe..b5bfc66b50 100644
> --- a/tools/libxl/idl.py
> +++ b/tools/libxl/idl.py
> @@ -11,7 +11,7 @@ DIR_BOTH = 3
>  _default_namespace = ""
>  def namespace(s):
>      if type(s) != str:
> -        raise TypeError, "Require a string for the default namespace."
> +        raise TypeError("Require a string for the default namespace.")
>      global _default_namespace
>      _default_namespace = s
>  
> @@ -346,7 +346,7 @@ class OrderedDict(dict):
>          return [(x,self[x]) for x in self.__ordered]
>  
>  def parse(f):
> -    print >>sys.stderr, "Parsing %s" % f
> +    sys.stderr.write("Parsing %s\n" % f)

And if you have it from future anyway...

print("Parsing", f, file=sys.stderr)

>      globs = {}
>      locs = OrderedDict()
> @@ -362,11 +362,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:
> +        raise SyntaxError("Errors were found at line %d while processing 
> %s:\n\t%s"\
> +              %(e.lineno,f,e.text))
>  
>      types = [t for t in locs.ordered_values() if isinstance(t,Type)]
>  
> 

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