[Piglit] [PATCH] generated_tests: work around bug in Windows numpy implementation.

Jose Fonseca jfonseca at vmware.com
Thu Sep 8 11:15:35 PDT 2011


I think this is pretty brittle -- who knows when numpy decides to create different type objects.

It's better to use numpy dtypes [1] for this. With your example one gets:

  Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on
  win32
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import numpy
  >>> x = numpy.int32(5)
  >>> y = numpy.abs(x)
  >>> type(x) == type(y)
  False
  >>> x.dtype == y.dtype
  True
  >>>

That is, the type checking code should use

if isinstance(x, numpy.number):
   if x.dtype == numpy.dtype(numpy.int32)
   ...

...



Jose

[1] http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html



----- Original Message -----
> The windows implementation of numpy seems to have multiple
> independent
> copies of the int32 and uint32 types, as illustrated by this
> interactive session:
> 
> Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
> (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more
> information.
> >>> import numpy
> >>> x = numpy.int32(5)
> >>> y = numpy.abs(x)
> >>> print type(x)
> <type 'numpy.int32'>
> >>> print type(y)
> <type 'numpy.int32'>
> >>> print type(x) == type(y)
> False
> >>> exit()
> 
> On Linux, the last "print" yields True, which is clearly the correct
> behavior.
> 
> This duplication of int32 and uint32 classes was preventing
> builtin_function.py's glsl_type_of() function from being able to
> reliably determine the type of its argument, causing an assertion
> failure while building piglit.
> 
> This patch works around the problem by creating tuples INT32_TYPES
> and
> UINT32_TYPES which contain the duplicate types.  These tuples are
> used
> instead of referring directly to numpy.int32 and numpy.uint32.
> 
> On platforms that do not have this bug, the tuples are "oneples" (one
> element each) so there is no change in behavior.
> 
> I've verified that on Linux, this change does not affect the
> generated
> tests.
> 
> Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=40697
> ---
>  generated_tests/builtin_function.py |   27
>  ++++++++++++++++++---------
>  1 files changed, 18 insertions(+), 9 deletions(-)
> 
> diff --git a/generated_tests/builtin_function.py
> b/generated_tests/builtin_function.py
> index f08ce9e..9c79cfc 100644
> --- a/generated_tests/builtin_function.py
> +++ b/generated_tests/builtin_function.py
> @@ -57,6 +57,15 @@ import numpy as np
>  # Floating point types used by Python and numpy
>  FLOATING_TYPES = (float, np.float64, np.float32)
>  
> +# Due to a bug in the Windows implementation of numpy, there are
> +# multiple int32 types (and multiple uint32 types).  So we have to
> +# find them all when doing isinstance checks.  The following code
> will
> +# create two-element tuples on numpy implementations that have the
> +# bug, and one-element tuples on numpy implementations that don't.
> +INT32_TYPES = tuple(set([np.int32, type(np.abs(np.int32(1)))]))
> +UINT32_TYPES = tuple(set([np.uint32,
> +			  type(np.dot(np.uint32(0), np.uint32(0)))]))
> +
>  
>  
>  class GlslBuiltinType(object):
> @@ -212,9 +221,9 @@ def glsl_type_of(value):
>  	return glsl_float
>      elif isinstance(value, (bool, np.bool_)):
>  	return glsl_bool
> -    elif isinstance(value, np.int32):
> +    elif isinstance(value, INT32_TYPES):
>  	return glsl_int
> -    elif isinstance(value, np.uint32):
> +    elif isinstance(value, UINT32_TYPES):
>  	return glsl_uint
>      else:
>  	assert isinstance(value, np.ndarray)
> @@ -226,9 +235,9 @@ def glsl_type_of(value):
>  		return (glsl_vec2, glsl_vec3, glsl_vec4)[vector_length - 2]
>  	    elif value.dtype == bool:
>  		return (glsl_bvec2, glsl_bvec3, glsl_bvec4)[vector_length - 2]
> -	    elif value.dtype == np.int32:
> +	    elif value.dtype in INT32_TYPES:
>  		return (glsl_ivec2, glsl_ivec3, glsl_ivec4)[vector_length - 2]
> -	    elif value.dtype == np.uint32:
> +	    elif value.dtype in UINT32_TYPES:
>  		return (glsl_uvec2, glsl_uvec3, glsl_uvec4)[vector_length - 2]
>  	    else:
>  		raise Exception(
> @@ -264,7 +273,7 @@ def glsl_constant(value):
>      column_major = np.reshape(np.array(value), -1, 'F')
>      if column_major.dtype == bool:
>  	values = ['true' if x else 'false' for x in column_major]
> -    elif column_major.dtype == np.uint32:
> +    elif column_major.dtype in UINT32_TYPES:
>  	values = [repr(x) + 'u' for x in column_major]
>      else:
>  	values = [repr(x) for x in column_major]
> @@ -457,14 +466,14 @@ def _refract(I, N, eta):
>  def _change_signedness(x):
>      """Change signed integer types to unsigned integer types and
>      vice
>      versa."""
> -    if isinstance(x, np.int32):
> +    if isinstance(x, INT32_TYPES):
>  	return np.uint32(x)
> -    elif isinstance(x, np.uint32):
> +    elif isinstance(x, UINT32_TYPES):
>  	return np.int32(x)
>      elif isinstance(x, np.ndarray):
> -	if (x.dtype == np.int32):
> +	if (x.dtype in INT32_TYPES):
>  	    return np.array(x, dtype=np.uint32)
> -	elif (x.dtype == np.uint32):
> +	elif (x.dtype in UINT32_TYPES):
>  	    return np.array(x, dtype=np.int32)
>      raise Exception('Unexpected type passed to _change_signedness')
>  
> --
> 1.7.6
> 
> _______________________________________________
> Piglit mailing list
> Piglit at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/piglit
> 


More information about the Piglit mailing list