python: Better check for integer types
authorMathieu Bridon <bochecha@daitauha.fr>
Thu, 9 Aug 2018 08:27:22 +0000 (10:27 +0200)
committerDylan Baker <dylan@pnwbakers.com>
Thu, 9 Aug 2018 23:49:19 +0000 (16:49 -0700)
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@daitauha.fr>
Reviewed-by: Eric Engestrom <eric.engestrom@intel.com>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
src/compiler/nir/nir_algebraic.py
src/gallium/auxiliary/util/u_format_pack.py

index 5baeea88afe056dff47ef3e61b8219c3f7e133b4..3055937029cf8fa14dfc014919c2a946109b8a59 100644 (file)
@@ -36,9 +36,11 @@ import traceback
 from nir_opcodes import opcodes
 
 if sys.version_info < (3, 0):
+    integer_types = (int, long)
     string_type = unicode
 
 else:
+    integer_types = (int, )
     string_type = str
 
 _type_re = re.compile(r"(?P<type>int|uint|bool|float)?(?P<bits>\d+)?")
@@ -81,7 +83,7 @@ class Value(object):
          return val
       elif isinstance(val, string_type):
          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("""
@@ -145,7 +147,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):
          i = struct.unpack('Q', struct.pack('d', self.value))[0]
@@ -164,7 +166,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"
index ad2e49281fb7cccb59391f6abcba4da2cafcaf31..c1307d30c2f49edc080c8d7270b00e4ad4e3e8dc 100644 (file)
 
 from __future__ import division, print_function
 
+import sys
+
 from u_format_parse import *
 
 
+if sys.version_info < (3, 0):
+    integer_types = (int, long)
+
+else:
+    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 +221,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 +245,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: