[Mesa-dev] [PATCH 18/26] python: Better check for integer types
Eric Engestrom
eric.engestrom at intel.com
Thu Jul 5 16:52:52 UTC 2018
On Thursday, 2018-07-05 09:34:54 -0700, Dylan Baker wrote:
> Quoting Mathieu Bridon (2018-07-05 06:17:49)
> > Python 3 lost the long type: now everything is an int, with the right
> > size.
> >
> > This commit makes the script compatible with Python 2 (where we check
> > for both int and long) and Python 3 (where we only check for int).
> >
> > Signed-off-by: Mathieu Bridon <bochecha at daitauha.fr>
> > ---
> > src/compiler/nir/nir_algebraic.py | 8 +++++---
> > src/gallium/auxiliary/util/u_format_pack.py | 12 ++++++++++--
> > 2 files changed, 15 insertions(+), 5 deletions(-)
> >
> > diff --git a/src/compiler/nir/nir_algebraic.py b/src/compiler/nir/nir_algebraic.py
> > index e17e2d26b9..63a7cb5ad1 100644
> > --- a/src/compiler/nir/nir_algebraic.py
> > +++ b/src/compiler/nir/nir_algebraic.py
> > @@ -36,10 +36,12 @@ import traceback
> > from nir_opcodes import opcodes
> >
>
> how about
> import sys
>
> if sys.version < (3, 0):
> ...
> else:
> ...
>
> Since we expect the exception to be hit at least 50% of the time.
Agreed; with that:
Reviewed-by: Eric Engestrom <eric.engestrom at intel.com>
>
> with that,
> Reviewed-by: Dylan Baker <dylan at pnwbakers.com>
>
> > try:
> > + integer_types = (int, long)
> > string_types = (str, unicode)
> >
> > except NameError:
> > # This is Python 3
> > + integer_types = (int, )
> > string_types = (bytes, str)
> >
> > _type_re = re.compile(r"(?P<type>int|uint|bool|float)?(?P<bits>\d+)?")
> > @@ -79,7 +81,7 @@ class Value(object):
> > return val
> > elif isinstance(val, string_types):
> > return Variable(val, name_base, varset)
> > - elif isinstance(val, (bool, int, long, float)):
> > + elif isinstance(val, (bool, float) + integer_types):
> > return Constant(val, name_base)
> >
> > __template = mako.template.Template("""
> > @@ -143,7 +145,7 @@ class Constant(Value):
> > def __hex__(self):
> > if isinstance(self.value, (bool)):
> > return 'NIR_TRUE' if self.value else 'NIR_FALSE'
> > - if isinstance(self.value, (int, long)):
> > + if isinstance(self.value, integer_types):
> > return hex(self.value)
> > elif isinstance(self.value, float):
> > return hex(struct.unpack('Q', struct.pack('d', self.value))[0])
> > @@ -153,7 +155,7 @@ class Constant(Value):
> > def type(self):
> > if isinstance(self.value, (bool)):
> > return "nir_type_bool32"
> > - elif isinstance(self.value, (int, long)):
> > + elif isinstance(self.value, integer_types):
> > return "nir_type_int"
> > elif isinstance(self.value, float):
> > return "nir_type_float"
> > diff --git a/src/gallium/auxiliary/util/u_format_pack.py b/src/gallium/auxiliary/util/u_format_pack.py
> > index 1cfd85fb7f..c753336a84 100644
> > --- a/src/gallium/auxiliary/util/u_format_pack.py
> > +++ b/src/gallium/auxiliary/util/u_format_pack.py
> > @@ -41,6 +41,14 @@ from __future__ import print_function
> > from u_format_parse import *
> >
> >
> > +try:
> > + integer_types = (int, long)
> > +
> > +except NameError:
> > + # This is Python 3
> > + integer_types = (int, )
> > +
> > +
> > def inv_swizzles(swizzles):
> > '''Return an array[4] of inverse swizzle terms'''
> > '''Only pick the first matching value to avoid l8 getting blue and i8 getting alpha'''
> > @@ -212,7 +220,7 @@ def truncate_mantissa(x, bits):
> > '''Truncate an integer so it can be represented exactly with a floating
> > point mantissa'''
> >
> > - assert isinstance(x, (int, long))
> > + assert isinstance(x, integer_types)
> >
> > s = 1
> > if x < 0:
> > @@ -236,7 +244,7 @@ def value_to_native(type, value):
> > '''Get the value of unity for this type.'''
> > if type.type == FLOAT:
> > if type.size <= 32 \
> > - and isinstance(value, (int, long)):
> > + and isinstance(value, integer_types):
> > return truncate_mantissa(value, 23)
> > return value
> > if type.type == FIXED:
> > --
> > 2.17.1
> >
> > _______________________________________________
> > mesa-dev mailing list
> > mesa-dev at lists.freedesktop.org
> > https://lists.freedesktop.org/mailman/listinfo/mesa-dev
More information about the mesa-dev
mailing list