[Mesa-dev] [PATCH v2 10/26] python: Use explicit integer divisions

Dylan Baker dylan at pnwbakers.com
Tue Jul 24 22:31:29 UTC 2018


Quoting Eric Engestrom (2018-07-24 09:42:57)
> On Wednesday, 2018-07-11 19:52:31 +0200, Mathieu Bridon wrote:
> > In Python 2, divisions return an integer:
                          ^ of integers

Since 32 / 4.0 returns 8.0

> > 
> >     >>> 32 / 4
> >     8
> > 
> > In Python 3 though, they return floats:
> > 
> >     >>> 32 / 4
> >     8.0
> > 
> > However, Python 3 has an explicit integer division operator:
> > 
> >     >>> 32 // 4
> >     8
> > 
> > That operator exists on Python >= 2.2, so let's use it everywhere to
> > make the scripts compatible with both Python 2 and 3.
> > 
> > In addition, using __future__.division tells Python 2 to behave the same
> > way as Python 3, which helps ensure the scripts produce the same output
> > in both versions of Python.
> > 
> > Signed-off-by: Mathieu Bridon <bochecha at daitauha.fr>
> 
> Reviewed-by: Eric Engestrom <eric.engestrom at intel.com>
> 
> > ---
> >  src/gallium/auxiliary/util/u_format_pack.py  | 4 ++--
> >  src/gallium/auxiliary/util/u_format_parse.py | 7 +++++--
> >  src/mapi/glapi/gen/glX_proto_send.py         | 4 ++--
> >  src/mesa/main/format_info.py                 | 4 ++--
> >  src/mesa/main/format_pack.py                 | 8 ++++----
> >  src/mesa/main/format_unpack.py               | 8 ++++----
> >  6 files changed, 19 insertions(+), 16 deletions(-)
> > 
> > diff --git a/src/gallium/auxiliary/util/u_format_pack.py b/src/gallium/auxiliary/util/u_format_pack.py
> > index 7a952a48b3..ad2e49281f 100644
> > --- a/src/gallium/auxiliary/util/u_format_pack.py
> > +++ b/src/gallium/auxiliary/util/u_format_pack.py
> > @@ -36,7 +36,7 @@
> >  '''
> >  
> >  
> > -from __future__ import print_function
> > +from __future__ import division, print_function
> >  
> >  from u_format_parse import *
> >  
> > @@ -240,7 +240,7 @@ def value_to_native(type, value):
> >              return truncate_mantissa(value, 23)
> >          return value
> >      if type.type == FIXED:
> > -        return int(value * (1 << (type.size/2)))
> > +        return int(value * (1 << (type.size // 2)))
> >      if not type.norm:
> >          return int(value)
> >      if type.type == UNSIGNED:
> > diff --git a/src/gallium/auxiliary/util/u_format_parse.py b/src/gallium/auxiliary/util/u_format_parse.py
> > index c0456f6d15..d3874cd895 100644
> > --- a/src/gallium/auxiliary/util/u_format_parse.py
> > +++ b/src/gallium/auxiliary/util/u_format_parse.py
> > @@ -29,6 +29,9 @@
> >  '''
> >  
> >  
> > +from __future__ import division
> > +
> > +
> >  VOID, UNSIGNED, SIGNED, FIXED, FLOAT = range(5)
> >  
> >  SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_0, SWIZZLE_1, SWIZZLE_NONE, = range(7)
> > @@ -76,7 +79,7 @@ class Channel:
> >          if self.type == FLOAT:
> >              return VERY_LARGE
> >          if self.type == FIXED:
> > -            return (1 << (self.size/2)) - 1
> > +            return (1 << (self.size // 2)) - 1
> >          if self.norm:
> >              return 1
> >          if self.type == UNSIGNED:
> > @@ -90,7 +93,7 @@ class Channel:
> >          if self.type == FLOAT:
> >              return -VERY_LARGE
> >          if self.type == FIXED:
> > -            return -(1 << (self.size/2))
> > +            return -(1 << (self.size // 2))
> >          if self.type == UNSIGNED:
> >              return 0
> >          if self.norm:
> > diff --git a/src/mapi/glapi/gen/glX_proto_send.py b/src/mapi/glapi/gen/glX_proto_send.py
> > index a920ecc012..03067d8a3c 100644
> > --- a/src/mapi/glapi/gen/glX_proto_send.py
> > +++ b/src/mapi/glapi/gen/glX_proto_send.py
> > @@ -26,7 +26,7 @@
> >  #    Ian Romanick <idr at us.ibm.com>
> >  #    Jeremy Kolb <jkolb at brandeis.edu>
> >  
> > -from __future__ import print_function
> > +from __future__ import division, print_function
> >  
> >  import argparse
> >  
> > @@ -809,7 +809,7 @@ generic_%u_byte( GLint rop, const void * ptr )
> >                      # Dividing by the array size (1 for
> >                      # non-arrays) gives us this.
> >  
> > -                    s = p.size() / p.get_element_count()
> > +                    s = p.size() // p.get_element_count()
> >                      print("       %s __glXReadReply(dpy, %s, %s, %s);" % (return_str, s, p.name, aa))
> >                      got_reply = 1
> >  
> > diff --git a/src/mesa/main/format_info.py b/src/mesa/main/format_info.py
> > index bbecaa2451..00e27b3fba 100644
> > --- a/src/mesa/main/format_info.py
> > +++ b/src/mesa/main/format_info.py
> > @@ -21,7 +21,7 @@
> >  # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
> >  # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
> >  
> > -from __future__ import print_function
> > +from __future__ import division, print_function
> >  
> >  import format_parser as parser
> >  import sys
> > @@ -198,7 +198,7 @@ for fmat in formats:
> >        chan = fmat.array_element()
> >        norm = chan.norm or chan.type == parser.FLOAT
> >        print('      .ArrayFormat = MESA_ARRAY_FORMAT({0}),'.format(', '.join([
> > -         str(chan.size / 8),
> > +         str(chan.size // 8),
> >           str(int(chan.sign)),
> >           str(int(chan.type == parser.FLOAT)),
> >           str(int(norm)),
> > diff --git a/src/mesa/main/format_pack.py b/src/mesa/main/format_pack.py
> > index d3c8d24acd..05262efb5b 100644
> > --- a/src/mesa/main/format_pack.py
> > +++ b/src/mesa/main/format_pack.py
> > @@ -1,4 +1,4 @@
> > -from __future__ import print_function
> > +from __future__ import division, print_function

Ah, this is tricky one, mako actually does some tricky meta-programming things,
so importing __future__.division like this wont affect it. What you actually
need to do is modify the `template = Template(...)` line to be: `template =
Template(..., future_imports=['division'])`

Dylan

> >  
> >  from mako.template import Template
> >  from sys import argv
> > @@ -356,7 +356,7 @@ _mesa_pack_ubyte_rgba_row(mesa_format format, GLuint n,
> >     case ${f.name}:
> >        for (i = 0; i < n; ++i) {
> >           pack_ubyte_${f.short_name()}(src[i], d);
> > -         d += ${f.block_size() / 8};
> > +         d += ${f.block_size() // 8};
> >        }
> >        break;
> >  %endfor
> > @@ -388,7 +388,7 @@ _mesa_pack_uint_rgba_row(mesa_format format, GLuint n,
> >     case ${f.name}:
> >        for (i = 0; i < n; ++i) {
> >           pack_uint_${f.short_name()}(src[i], d);
> > -         d += ${f.block_size() / 8};
> > +         d += ${f.block_size() // 8};
> >        }
> >        break;
> >  %endfor
> > @@ -418,7 +418,7 @@ _mesa_pack_float_rgba_row(mesa_format format, GLuint n,
> >     case ${f.name}:
> >        for (i = 0; i < n; ++i) {
> >           pack_float_${f.short_name()}(src[i], d);
> > -         d += ${f.block_size() / 8};
> > +         d += ${f.block_size() // 8};
> >        }
> >        break;
> >  %endfor
> > diff --git a/src/mesa/main/format_unpack.py b/src/mesa/main/format_unpack.py
> > index 286c08e621..5d24a79489 100644
> > --- a/src/mesa/main/format_unpack.py
> > +++ b/src/mesa/main/format_unpack.py
> > @@ -1,4 +1,4 @@
> > -from __future__ import print_function
> > +from __future__ import division, print_function
> >  
> >  from mako.template import Template
> >  from sys import argv
> > @@ -322,7 +322,7 @@ _mesa_unpack_rgba_row(mesa_format format, GLuint n,
> >     case ${f.name}:
> >        for (i = 0; i < n; ++i) {
> >           unpack_float_${f.short_name()}(s, dst[i]);
> > -         s += ${f.block_size() / 8};
> > +         s += ${f.block_size() // 8};
> >        }
> >        break;
> >  %endfor
> > @@ -355,7 +355,7 @@ _mesa_unpack_ubyte_rgba_row(mesa_format format, GLuint n,
> >     case ${f.name}:
> >        for (i = 0; i < n; ++i) {
> >           unpack_ubyte_${f.short_name()}(s, dst[i]);
> > -         s += ${f.block_size() / 8};
> > +         s += ${f.block_size() // 8};
> >        }
> >        break;
> >  %endfor
> > @@ -397,7 +397,7 @@ _mesa_unpack_uint_rgba_row(mesa_format format, GLuint n,
> >     case ${f.name}:
> >        for (i = 0; i < n; ++i) {
> >           unpack_int_${f.short_name()}(s, dst[i]);
> > -         s += ${f.block_size() / 8};
> > +         s += ${f.block_size() // 8};
> >        }
> >        break;
> >  %endfor
> > -- 
> > 2.17.1
> > 
> > _______________________________________________
> > mesa-dev mailing list
> > mesa-dev at lists.freedesktop.org
> > https://lists.freedesktop.org/mailman/listinfo/mesa-dev
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: signature
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20180724/ce83196d/attachment.sig>


More information about the mesa-dev mailing list