From b6714352ab0d0ea85734dabce744bfb4f2dc4d76 Mon Sep 17 00:00:00 2001 From: Marcel Keller Date: Sun, 12 Jan 2020 00:10:40 +0100 Subject: [PATCH] Python 3.8 warnings. --- Compiler/GC/types.py | 4 ++-- Compiler/library.py | 4 ++-- Compiler/ml.py | 5 +++-- Compiler/types.py | 19 ++++++++++--------- Compiler/util.py | 8 ++++---- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Compiler/GC/types.py b/Compiler/GC/types.py index 2f95a774..a6d01a5f 100644 --- a/Compiler/GC/types.py +++ b/Compiler/GC/types.py @@ -419,7 +419,7 @@ class sbitvec(object): self.v = sbits.trans(elements) def popcnt(self): res = sbitint.wallace_tree([[b] for b in self.v]) - while res[-1] is 0: + while util.is_zero(res[-1]): del res[-1] return self.from_vec(res) def elements(self, start=None, stop=None): @@ -622,7 +622,7 @@ class sbitint(_bitint, _number, sbits): class sbitintvec(sbitvec): def __add__(self, other): - if other is 0: + if util.is_zero(other): return self assert(len(self.v) == len(other.v)) v = sbitint.bit_adder(self.v, other.v) diff --git a/Compiler/library.py b/Compiler/library.py index 99345bb1..758b9242 100644 --- a/Compiler/library.py +++ b/Compiler/library.py @@ -1,6 +1,6 @@ from Compiler.types import cint,sint,cfix,sfix,sfloat,MPCThread,Array,MemValue,cgf2n,sgf2n,_number,_mem,_register,regint,Matrix,_types, cfloat, _single, localint from Compiler.instructions import * -from Compiler.util import tuplify,untuplify +from Compiler.util import tuplify,untuplify,is_zero from Compiler import instructions,instructions_base,comparison,program,util import inspect,math import random @@ -834,7 +834,7 @@ def map_reduce_single(n_parallel, n_loops, initializer=lambda *x: [], if not (isinstance(n_parallel, int) or n_parallel is None): raise CompilerException('Number of parallel executions' \ 'must be constant') - n_parallel = 1 if n_parallel is 0 else n_parallel + n_parallel = 1 if is_zero(n_parallel) else n_parallel if mem_state is None: # default to list of MemValues to allow varying types mem_state = [MemValue(x) for x in initializer()] diff --git a/Compiler/ml.py b/Compiler/ml.py index 9d579055..3cd73846 100644 --- a/Compiler/ml.py +++ b/Compiler/ml.py @@ -3,6 +3,7 @@ import mpc_math, math from Compiler.types import * from Compiler.types import _unreduced_squant from Compiler.library import * +from Compiler.util import is_zero from functools import reduce def log_e(x): @@ -470,7 +471,7 @@ class QuantConv2d(QuantConvBase): in_x = in_x_origin + filter_x inside_x = (0 <= in_x) * (in_x < inputs_w) inside = inside_y * inside_x - if inside is 0: + if is_zero(inside): continue for in_c in range(n_channels_in): iv += [self.X[0][in_y * inside_y] @@ -530,7 +531,7 @@ class QuantDepthwiseConv2d(QuantConvBase): in_y = in_y_origin + filter_y inside = (0 <= in_x) * (in_x < inputs_w) * \ (0 <= in_y) * (in_y < inputs_h) - if inside is 0: + if is_zero(inside): continue iv += [self.X[0][in_y][in_x][in_c]] wv += [self.weights[0][filter_y][filter_x][oc]] diff --git a/Compiler/types.py b/Compiler/types.py index 14562e2c..efe81c0c 100644 --- a/Compiler/types.py +++ b/Compiler/types.py @@ -6,6 +6,7 @@ from .floatingpoint import two_power from . import comparison, floatingpoint import math from . import util +from .util import is_zero, is_one import operator from functools import reduce @@ -128,15 +129,15 @@ class _number(object): return self * self def __add__(self, other): - if other is 0: + if is_zero(other): return self else: return self.add(other) def __mul__(self, other): - if other is 0: + if is_zero(other): return 0 - elif other is 1: + elif is_one(other): return self else: return self.mul(other) @@ -1487,7 +1488,7 @@ class sgf2n(_secret, _gf2n): return self ^ cgf2n(2**program.galois_length - 1) def __xor__(self, other): - if other is 0: + if is_zero(other): return self else: return super(sgf2n, self).add(other) @@ -1588,7 +1589,7 @@ class _bitint(object): get_carry=False): lower = [] for (ai,bi) in zip(a,b): - if ai is 0 or bi is 0: + if is_zero(ai) or is_zero(bi): lower.append(ai + bi) a.pop(0) b.pop(0) @@ -2590,7 +2591,7 @@ class unreduced_sfix(_single): self.kappa = kappa def __add__(self, other): - if other is 0: + if is_zero(other): return self assert self.k == other.k assert self.m == other.m @@ -2722,7 +2723,7 @@ class _unreduced_squant(object): self.res_params = res_params or params[0] def __add__(self, other): - if other is 0: + if is_zero(other): return self assert self.params == other.params assert self.res_params == other.res_params @@ -3340,7 +3341,7 @@ class Array(object): self.assign(self.value_type.get_input_from(player, size=len(self))) def __add__(self, other): - if other is 0: + if is_zero(other): return self assert len(self) == len(other) return self.get_vector() + other @@ -3451,7 +3452,7 @@ class SubMultiArray(object): return res def __add__(self, other): - if other is 0: + if is_zero(other): return self assert self.sizes == other.sizes if len(self.sizes) == 2: diff --git a/Compiler/util.py b/Compiler/util.py index 1583b86e..57e875fe 100644 --- a/Compiler/util.py +++ b/Compiler/util.py @@ -151,10 +151,10 @@ def is_constant_float(x): return isinstance(x, float) or is_constant(x) def is_zero(x): - try: - return int(x) is 0 - except: - return False + return is_constant(x) and x == 0 + +def is_one(x): + return is_constant(x) and x == 1 def is_all_ones(x, n): if is_constant(x):