[Mesa-dev] [PATCH 19/26] python: Don't abuse hex()

2018-07-05 Thread Mathieu Bridon
The hex() builtin returns a string containing the hexa-decimal
representation of an integer.

When the argument is not an integer, then the function calls that
object's __hex__() method, if one is defined. That method is supposed to
return a string.

While that's not explicitly documented, that string is supposed to be a
valid hexa-decimal representation for a number. Python 2 doesn't enforce
this though, which is why we got away with returning things like
'NIR_TRUE' which are not numbers.

In Python 3, the hex() builtin instead calls an object's __index__()
method, which itself must return an integer. That integer is then
automatically converted to a string with its hexa-decimal representation
by the rest of the hex() function.

As a result, we really can't make this compatible with Python 3 as it
is.

The solution is to stop using the hex() builtin, and instead use a hex()
object method, which can return whatever we want, in Python 2 and 3.

Signed-off-by: Mathieu Bridon 
---
 src/compiler/nir/nir_algebraic.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/compiler/nir/nir_algebraic.py 
b/src/compiler/nir/nir_algebraic.py
index 63a7cb5ad1..d53a9869de 100644
--- a/src/compiler/nir/nir_algebraic.py
+++ b/src/compiler/nir/nir_algebraic.py
@@ -88,7 +88,7 @@ class Value(object):
 static const ${val.c_type} ${val.name} = {
{ ${val.type_enum}, ${val.bit_size} },
 % if isinstance(val, Constant):
-   ${val.type()}, { ${hex(val)} /* ${val.value} */ },
+   ${val.type()}, { ${val.hex()} /* ${val.value} */ },
 % elif isinstance(val, Variable):
${val.index}, /* ${val.var_name} */
${'true' if val.is_constant else 'false'},
@@ -142,7 +142,7 @@ class Constant(Value):
  assert self.bit_size == 0 or self.bit_size == 32
  self.bit_size = 32
 
-   def __hex__(self):
+   def hex(self):
   if isinstance(self.value, (bool)):
  return 'NIR_TRUE' if self.value else 'NIR_FALSE'
   if isinstance(self.value, integer_types):
-- 
2.17.1

___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


Re: [Mesa-dev] [PATCH 19/26] python: Don't abuse hex()

2018-07-05 Thread Dylan Baker
This is nice change in itself,
Reviewed-by: Dylan Baker 

Quoting Mathieu Bridon (2018-07-05 06:17:50)
> The hex() builtin returns a string containing the hexa-decimal
> representation of an integer.
> 
> When the argument is not an integer, then the function calls that
> object's __hex__() method, if one is defined. That method is supposed to
> return a string.
> 
> While that's not explicitly documented, that string is supposed to be a
> valid hexa-decimal representation for a number. Python 2 doesn't enforce
> this though, which is why we got away with returning things like
> 'NIR_TRUE' which are not numbers.
> 
> In Python 3, the hex() builtin instead calls an object's __index__()
> method, which itself must return an integer. That integer is then
> automatically converted to a string with its hexa-decimal representation
> by the rest of the hex() function.
> 
> As a result, we really can't make this compatible with Python 3 as it
> is.
> 
> The solution is to stop using the hex() builtin, and instead use a hex()
> object method, which can return whatever we want, in Python 2 and 3.
> 
> Signed-off-by: Mathieu Bridon 
> ---
>  src/compiler/nir/nir_algebraic.py | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/src/compiler/nir/nir_algebraic.py 
> b/src/compiler/nir/nir_algebraic.py
> index 63a7cb5ad1..d53a9869de 100644
> --- a/src/compiler/nir/nir_algebraic.py
> +++ b/src/compiler/nir/nir_algebraic.py
> @@ -88,7 +88,7 @@ class Value(object):
>  static const ${val.c_type} ${val.name} = {
> { ${val.type_enum}, ${val.bit_size} },
>  % if isinstance(val, Constant):
> -   ${val.type()}, { ${hex(val)} /* ${val.value} */ },
> +   ${val.type()}, { ${val.hex()} /* ${val.value} */ },
>  % elif isinstance(val, Variable):
> ${val.index}, /* ${val.var_name} */
> ${'true' if val.is_constant else 'false'},
> @@ -142,7 +142,7 @@ class Constant(Value):
>   assert self.bit_size == 0 or self.bit_size == 32
>   self.bit_size = 32
>  
> -   def __hex__(self):
> +   def hex(self):
>if isinstance(self.value, (bool)):
>   return 'NIR_TRUE' if self.value else 'NIR_FALSE'
>if isinstance(self.value, integer_types):
> -- 
> 2.17.1
> 
> ___
> mesa-dev mailing list
> mesa-dev@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev


signature.asc
Description: signature
___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


Re: [Mesa-dev] [PATCH 19/26] python: Don't abuse hex()

2018-07-05 Thread Eric Engestrom
On Thursday, 2018-07-05 15:17:50 +0200, Mathieu Bridon wrote:
> The hex() builtin returns a string containing the hexa-decimal
> representation of an integer.
> 
> When the argument is not an integer, then the function calls that
> object's __hex__() method, if one is defined. That method is supposed to
> return a string.
> 
> While that's not explicitly documented, that string is supposed to be a
> valid hexa-decimal representation for a number. Python 2 doesn't enforce
> this though, which is why we got away with returning things like
> 'NIR_TRUE' which are not numbers.
> 
> In Python 3, the hex() builtin instead calls an object's __index__()
> method, which itself must return an integer. That integer is then
> automatically converted to a string with its hexa-decimal representation
> by the rest of the hex() function.
> 
> As a result, we really can't make this compatible with Python 3 as it
> is.
> 
> The solution is to stop using the hex() builtin, and instead use a hex()
> object method, which can return whatever we want, in Python 2 and 3.
> 
> Signed-off-by: Mathieu Bridon 

Reviewed-by: Eric Engestrom 

> ---
>  src/compiler/nir/nir_algebraic.py | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/src/compiler/nir/nir_algebraic.py 
> b/src/compiler/nir/nir_algebraic.py
> index 63a7cb5ad1..d53a9869de 100644
> --- a/src/compiler/nir/nir_algebraic.py
> +++ b/src/compiler/nir/nir_algebraic.py
> @@ -88,7 +88,7 @@ class Value(object):
>  static const ${val.c_type} ${val.name} = {
> { ${val.type_enum}, ${val.bit_size} },
>  % if isinstance(val, Constant):
> -   ${val.type()}, { ${hex(val)} /* ${val.value} */ },
> +   ${val.type()}, { ${val.hex()} /* ${val.value} */ },
>  % elif isinstance(val, Variable):
> ${val.index}, /* ${val.var_name} */
> ${'true' if val.is_constant else 'false'},
> @@ -142,7 +142,7 @@ class Constant(Value):
>   assert self.bit_size == 0 or self.bit_size == 32
>   self.bit_size = 32
>  
> -   def __hex__(self):
> +   def hex(self):
>if isinstance(self.value, (bool)):
>   return 'NIR_TRUE' if self.value else 'NIR_FALSE'
>if isinstance(self.value, integer_types):
> -- 
> 2.17.1
> 
> ___
> mesa-dev mailing list
> mesa-dev@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev