The first batch of changes recommended by the fixdiv tool. These are

mostly changes of / operators into //.  Once or twice I did more or
less than recommended.
This commit is contained in:
Guido van Rossum 2001-09-04 19:14:14 +00:00
parent b8f2274985
commit 54e54c6877
19 changed files with 38 additions and 38 deletions

View file

@ -9,7 +9,7 @@ import binascii
__all__ = ["encode","decode","encodestring","decodestring"] __all__ = ["encode","decode","encodestring","decodestring"]
MAXLINESIZE = 76 # Excluding the CRLF MAXLINESIZE = 76 # Excluding the CRLF
MAXBINSIZE = (MAXLINESIZE/4)*3 MAXBINSIZE = (MAXLINESIZE//4)*3
def encode(input, output): def encode(input, output):
"""Encode a file.""" """Encode a file."""

View file

@ -128,7 +128,7 @@ class _Hqxcoderengine:
def write(self, data): def write(self, data):
self.data = self.data + data self.data = self.data + data
datalen = len(self.data) datalen = len(self.data)
todo = (datalen/3)*3 todo = (datalen//3)*3
data = self.data[:todo] data = self.data[:todo]
self.data = self.data[todo:] self.data = self.data[todo:]
if not data: if not data:
@ -292,7 +292,7 @@ class _Hqxdecoderengine:
# much to decode: there may be newlines in the incoming data. # much to decode: there may be newlines in the incoming data.
while wtd > 0: while wtd > 0:
if self.eof: return decdata if self.eof: return decdata
wtd = ((wtd+2)/3)*4 wtd = ((wtd+2)//3)*4
data = self.ifp.read(wtd) data = self.ifp.read(wtd)
# #
# Next problem: there may not be a complete number of # Next problem: there may not be a complete number of

View file

@ -12,7 +12,7 @@ def insort_right(a, x, lo=0, hi=None):
if hi is None: if hi is None:
hi = len(a) hi = len(a)
while lo < hi: while lo < hi:
mid = (lo+hi)/2 mid = (lo+hi)//2
if x < a[mid]: hi = mid if x < a[mid]: hi = mid
else: lo = mid+1 else: lo = mid+1
a.insert(lo, x) a.insert(lo, x)
@ -33,7 +33,7 @@ def bisect_right(a, x, lo=0, hi=None):
if hi is None: if hi is None:
hi = len(a) hi = len(a)
while lo < hi: while lo < hi:
mid = (lo+hi)/2 mid = (lo+hi)//2
if x < a[mid]: hi = mid if x < a[mid]: hi = mid
else: lo = mid+1 else: lo = mid+1
return lo return lo
@ -52,7 +52,7 @@ def insort_left(a, x, lo=0, hi=None):
if hi is None: if hi is None:
hi = len(a) hi = len(a)
while lo < hi: while lo < hi:
mid = (lo+hi)/2 mid = (lo+hi)//2
if a[mid] < x: lo = mid+1 if a[mid] < x: lo = mid+1
else: hi = mid else: hi = mid
a.insert(lo, x) a.insert(lo, x)
@ -72,7 +72,7 @@ def bisect_left(a, x, lo=0, hi=None):
if hi is None: if hi is None:
hi = len(a) hi = len(a)
while lo < hi: while lo < hi:
mid = (lo+hi)/2 mid = (lo+hi)//2
if a[mid] < x: lo = mid+1 if a[mid] < x: lo = mid+1
else: hi = mid else: hi = mid
return lo return lo

View file

@ -87,7 +87,7 @@ class _Database:
## Does not work under MW compiler ## Does not work under MW compiler
## pos = ((pos + _BLOCKSIZE - 1) / _BLOCKSIZE) * _BLOCKSIZE ## pos = ((pos + _BLOCKSIZE - 1) / _BLOCKSIZE) * _BLOCKSIZE
## f.seek(pos) ## f.seek(pos)
npos = ((pos + _BLOCKSIZE - 1) / _BLOCKSIZE) * _BLOCKSIZE npos = ((pos + _BLOCKSIZE - 1) // _BLOCKSIZE) * _BLOCKSIZE
f.write('\0'*(npos-pos)) f.write('\0'*(npos-pos))
pos = npos pos = npos

View file

@ -590,7 +590,7 @@ def getframeinfo(frame, context=1):
filename = getsourcefile(frame) filename = getsourcefile(frame)
lineno = getlineno(frame) lineno = getlineno(frame)
if context > 0: if context > 0:
start = lineno - 1 - context/2 start = lineno - 1 - context//2
try: try:
lines, lnum = findsource(frame) lines, lnum = findsource(frame)
except IOError: except IOError:

View file

@ -27,7 +27,7 @@ def needsquoting(c, quotetabs):
def quote(c): def quote(c):
"""Quote a single character.""" """Quote a single character."""
i = ord(c) i = ord(c)
return ESCAPE + HEX[i/16] + HEX[i%16] return ESCAPE + HEX[i//16] + HEX[i%16]

View file

@ -65,7 +65,7 @@ class Repr:
def repr_str(self, x, level): def repr_str(self, x, level):
s = `x[:self.maxstring]` s = `x[:self.maxstring]`
if len(s) > self.maxstring: if len(s) > self.maxstring:
i = max(0, (self.maxstring-3)/2) i = max(0, (self.maxstring-3)//2)
j = max(0, self.maxstring-3-i) j = max(0, self.maxstring-3-i)
s = `x[:i] + x[len(x)-j:]` s = `x[:i] + x[len(x)-j:]`
s = s[:i] + '...' + s[len(s)-j:] s = s[:i] + '...' + s[len(s)-j:]
@ -73,7 +73,7 @@ class Repr:
def repr_long(self, x, level): def repr_long(self, x, level):
s = `x` # XXX Hope this isn't too slow... s = `x` # XXX Hope this isn't too slow...
if len(s) > self.maxlong: if len(s) > self.maxlong:
i = max(0, (self.maxlong-3)/2) i = max(0, (self.maxlong-3)//2)
j = max(0, self.maxlong-3-i) j = max(0, self.maxlong-3-i)
s = s[:i] + '...' + s[len(s)-j:] s = s[:i] + '...' + s[len(s)-j:]
return s return s
@ -86,7 +86,7 @@ class Repr:
return '<' + x.__class__.__name__ + ' instance at ' + \ return '<' + x.__class__.__name__ + ' instance at ' + \
hex(id(x))[2:] + '>' hex(id(x))[2:] + '>'
if len(s) > self.maxstring: if len(s) > self.maxstring:
i = max(0, (self.maxstring-3)/2) i = max(0, (self.maxstring-3)//2)
j = max(0, self.maxstring-3-i) j = max(0, self.maxstring-3-i)
s = s[:i] + '...' + s[len(s)-j:] s = s[:i] + '...' + s[len(s)-j:]
return s return s

View file

@ -927,7 +927,7 @@ def parsedate_tz(data):
tzoffset = -tzoffset tzoffset = -tzoffset
else: else:
tzsign = 1 tzsign = 1
tzoffset = tzsign * ( (tzoffset/100)*3600 + (tzoffset % 100)*60) tzoffset = tzsign * ( (tzoffset//100)*3600 + (tzoffset % 100)*60)
tuple = (yy, mm, dd, thh, tmm, tss, 0, 0, 0, tzoffset) tuple = (yy, mm, dd, thh, tmm, tss, 0, 0, 0, tzoffset)
return tuple return tuple

View file

@ -116,8 +116,8 @@ def testlin2lin(data):
# too simple: we test only the size # too simple: we test only the size
for d1 in data: for d1 in data:
for d2 in data: for d2 in data:
got = len(d1)/3 got = len(d1)//3
wtd = len(d2)/3 wtd = len(d2)//3
if len(audioop.lin2lin(d1, got, wtd)) != len(d2): if len(audioop.lin2lin(d1, got, wtd)) != len(d2):
return 0 return 0
return 1 return 1

View file

@ -5,7 +5,7 @@ x += 1
x *= 2 x *= 2
x **= 2 x **= 2
x -= 8 x -= 8
x /= 2 x //= 2
x //= 1 x //= 1
x %= 12 x %= 12
x &= 2 x &= 2
@ -19,7 +19,7 @@ x[0] += 1
x[0] *= 2 x[0] *= 2
x[0] **= 2 x[0] **= 2
x[0] -= 8 x[0] -= 8
x[0] /= 2 x[0] //= 2
x[0] //= 2 x[0] //= 2
x[0] %= 12 x[0] %= 12
x[0] &= 2 x[0] &= 2
@ -33,7 +33,7 @@ x[0] += 1
x[0] *= 2 x[0] *= 2
x[0] **= 2 x[0] **= 2
x[0] -= 8 x[0] -= 8
x[0] /= 2 x[0] //= 2
x[0] //= 1 x[0] //= 1
x[0] %= 12 x[0] %= 12
x[0] &= 2 x[0] &= 2

View file

@ -413,7 +413,7 @@ else:
# Worked by accident in Windows release build, but failed in debug build. # Worked by accident in Windows release build, but failed in debug build.
# Failed in all Linux builds. # Failed in all Linux builds.
x = -1-sys.maxint x = -1-sys.maxint
if x >> 1 != x/2: if x >> 1 != x//2:
raise TestFailed("x >> 1 != x/2 when x == -1-sys.maxint") raise TestFailed("x >> 1 != x/2 when x == -1-sys.maxint")
print 'isinstance' print 'isinstance'

View file

@ -54,10 +54,10 @@ for i in range(256):
fillers = fillers + c fillers = fillers + c
def addnoise(line): def addnoise(line):
noise = fillers noise = fillers
ratio = len(line) / len(noise) ratio = len(line) // len(noise)
res = "" res = ""
while line and noise: while line and noise:
if len(line) / len(noise) > ratio: if len(line) // len(noise) > ratio:
c, line = line[0], line[1:] c, line = line[0], line[1:]
else: else:
c, noise = noise[0], noise[1:] c, noise = noise[0], noise[1:]

View file

@ -42,8 +42,8 @@ class Rat(object):
if den == 0: if den == 0:
raise ZeroDivisionError, "zero denominator" raise ZeroDivisionError, "zero denominator"
g = gcd(den, num) g = gcd(den, num)
self.__num = long(num/g) self.__num = long(num//g)
self.__den = long(den/g) self.__den = long(den//g)
def _get_num(self): def _get_num(self):
"""Accessor function for read-only 'num' attribute of Rat.""" """Accessor function for read-only 'num' attribute of Rat."""

View file

@ -47,7 +47,7 @@ def test():
# Ensure default comparison compares id() of args # Ensure default comparison compares id() of args
L = [] L = []
for i in range(10): for i in range(10):
L.insert(len(L)/2, Empty()) L.insert(len(L)//2, Empty())
for a in L: for a in L:
for b in L: for b in L:
if cmp(a, b) != cmp(id(a), id(b)): if cmp(a, b) != cmp(id(a), id(b)):

View file

@ -76,7 +76,7 @@ def getran2(ndigits):
def test_division_2(x, y): def test_division_2(x, y):
q, r = divmod(x, y) q, r = divmod(x, y)
q2, r2 = x/y, x%y q2, r2 = x//y, x%y
pab, pba = x*y, y*x pab, pba = x*y, y*x
check(pab == pba, "multiplication does not commute for", x, y) check(pab == pba, "multiplication does not commute for", x, y)
check(q == q2, "divmod returns different quotient than / for", x, y) check(q == q2, "divmod returns different quotient than / for", x, y)
@ -117,7 +117,7 @@ def test_bitop_identities_1(x):
for n in range(2*SHIFT): for n in range(2*SHIFT):
p2 = 2L ** n p2 = 2L ** n
check(x << n >> n == x, "x << n >> n != x for", x, n) check(x << n >> n == x, "x << n >> n != x for", x, n)
check(x / p2 == x >> n, "x / p2 != x >> n for x n p2", x, n, p2) check(x // p2 == x >> n, "x // p2 != x >> n for x n p2", x, n, p2)
check(x * p2 == x << n, "x * p2 != x << n for x n p2", x, n, p2) check(x * p2 == x << n, "x * p2 != x << n for x n p2", x, n, p2)
check(x & -p2 == x >> n << n == x & ~(p2 - 1), check(x & -p2 == x >> n << n == x & ~(p2 - 1),
"not x & -p2 == x >> n << n == x & ~(p2 - 1) for x n p2", "not x & -p2 == x >> n << n == x & ~(p2 - 1) for x n p2",
@ -161,7 +161,7 @@ def test_bitop_identities(maxdigits=MAXDIGITS):
for leny in digits: for leny in digits:
y = getran(leny) y = getran(leny)
test_bitop_identities_2(x, y) test_bitop_identities_2(x, y)
test_bitop_identities_3(x, y, getran((lenx + leny)/2)) test_bitop_identities_3(x, y, getran((lenx + leny)//2))
# ------------------------------------------------- hex oct repr str atol # ------------------------------------------------- hex oct repr str atol
@ -296,8 +296,8 @@ def test_auto_overflow():
checkit(x, '*', y) checkit(x, '*', y)
if y: if y:
expected = longx / longy expected = longx // longy
got = x / y got = x // y
checkit(x, '/', y) checkit(x, '/', y)
expected = longx // longy expected = longx // longy

View file

@ -82,7 +82,7 @@ class OperatorTestCase(unittest.TestCase):
self.assert_(a == [0, 1, 8, 9]) self.assert_(a == [0, 1, 8, 9])
def test_div(self): def test_div(self):
self.failUnless(operator.div(5, 2) == 2) self.failUnless(operator.floordiv(5, 2) == 2)
def test_floordiv(self): def test_floordiv(self):
self.failUnless(operator.floordiv(5, 2) == 2) self.failUnless(operator.floordiv(5, 2) == 2)

View file

@ -86,7 +86,7 @@ if pid == pty.CHILD:
else: else:
debug("Waiting for child (%d) to finish."%pid) debug("Waiting for child (%d) to finish."%pid)
(pid, status) = os.waitpid(pid, 0) (pid, status) = os.waitpid(pid, 0)
res = status / 256 res = status >> 8
debug("Child (%d) exited with status %d (%d)."%(pid, res, status)) debug("Child (%d) exited with status %d (%d)."%(pid, res, status))
if res == 1: if res == 1:
raise TestFailed, "Child raised an unexpected exception in os.setsid()" raise TestFailed, "Child raised an unexpected exception in os.setsid()"

View file

@ -64,10 +64,10 @@ def strftest(now):
('%M', '%02d' % now[4], 'minute, (00-59)'), ('%M', '%02d' % now[4], 'minute, (00-59)'),
('%p', ampm, 'AM or PM as appropriate'), ('%p', ampm, 'AM or PM as appropriate'),
('%S', '%02d' % now[5], 'seconds of current time (00-60)'), ('%S', '%02d' % now[5], 'seconds of current time (00-60)'),
('%U', '%02d' % ((now[7] + jan1[6])/7), ('%U', '%02d' % ((now[7] + jan1[6])//7),
'week number of the year (Sun 1st)'), 'week number of the year (Sun 1st)'),
('%w', '0?%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'), ('%w', '0?%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'),
('%W', '%02d' % ((now[7] + (jan1[6] - 1)%7)/7), ('%W', '%02d' % ((now[7] + (jan1[6] - 1)%7)//7),
'week number of the year (Mon 1st)'), 'week number of the year (Mon 1st)'),
# %x see below # %x see below
('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'), ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),

View file

@ -146,7 +146,7 @@ class Wave_read:
if not self._fmt_chunk_read: if not self._fmt_chunk_read:
raise Error, 'data chunk before fmt chunk' raise Error, 'data chunk before fmt chunk'
self._data_chunk = chunk self._data_chunk = chunk
self._nframes = chunk.chunksize / self._framesize self._nframes = chunk.chunksize // self._framesize
self._data_seek_needed = 0 self._data_seek_needed = 0
break break
chunk.skip() chunk.skip()
@ -248,7 +248,7 @@ class Wave_read:
data = self._data_chunk.read(nframes * self._framesize) data = self._data_chunk.read(nframes * self._framesize)
if self._convert and data: if self._convert and data:
data = self._convert(data) data = self._convert(data)
self._soundpos = self._soundpos + len(data) / (self._nchannels * self._sampwidth) self._soundpos = self._soundpos + len(data) // (self._nchannels * self._sampwidth)
return data return data
# #
@ -259,7 +259,7 @@ class Wave_read:
wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack('<hhllh', chunk.read(14)) wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack('<hhllh', chunk.read(14))
if wFormatTag == WAVE_FORMAT_PCM: if wFormatTag == WAVE_FORMAT_PCM:
sampwidth = struct.unpack('<h', chunk.read(2))[0] sampwidth = struct.unpack('<h', chunk.read(2))[0]
self._sampwidth = (sampwidth + 7) / 8 self._sampwidth = (sampwidth + 7) // 8
else: else:
raise Error, 'unknown format: ' + `wFormatTag` raise Error, 'unknown format: ' + `wFormatTag`
self._framesize = self._nchannels * self._sampwidth self._framesize = self._nchannels * self._sampwidth
@ -403,7 +403,7 @@ class Wave_write:
def writeframesraw(self, data): def writeframesraw(self, data):
self._ensure_header_written(len(data)) self._ensure_header_written(len(data))
nframes = len(data) / (self._sampwidth * self._nchannels) nframes = len(data) // (self._sampwidth * self._nchannels)
if self._convert: if self._convert:
data = self._convert(data) data = self._convert(data)
if self._sampwidth > 1 and big_endian: if self._sampwidth > 1 and big_endian: