Replace .seek() magic numbers with io.SEEK_* constants

A bit more readable.

https://docs.python.org/3/library/io.html#io.IOBase.seek
This commit is contained in:
Jon Dufresne 2019-01-12 18:05:46 -08:00
parent 503138c857
commit a00fc33c04
10 changed files with 22 additions and 16 deletions

View File

@ -18,6 +18,8 @@
# A file object that provides read access to a part of an existing # A file object that provides read access to a part of an existing
# file (for example a TAR file). # file (for example a TAR file).
import io
class ContainerIO(object): class ContainerIO(object):
@ -41,7 +43,7 @@ class ContainerIO(object):
def isatty(self): def isatty(self):
return 0 return 0
def seek(self, offset, mode=0): def seek(self, offset, mode=io.SEEK_SET):
""" """
Move file pointer. Move file pointer.

View File

@ -102,7 +102,7 @@ def Ghostscript(tile, size, fp, scale=1):
# Copy whole file to read in Ghostscript # Copy whole file to read in Ghostscript
with open(infile_temp, 'wb') as f: with open(infile_temp, 'wb') as f:
# fetch length of fp # fetch length of fp
fp.seek(0, 2) fp.seek(0, io.SEEK_END)
fsize = fp.tell() fsize = fp.tell()
# ensure start position # ensure start position
# go back # go back
@ -167,7 +167,7 @@ class PSFile(object):
self.fp = fp self.fp = fp
self.char = None self.char = None
def seek(self, offset, whence=0): def seek(self, offset, whence=io.SEEK_SET):
self.char = None self.char = None
self.fp.seek(offset, whence) self.fp.seek(offset, whence)
@ -310,7 +310,7 @@ class EpsImageFile(ImageFile.ImageFile):
if s[:4] == b"%!PS": if s[:4] == b"%!PS":
# for HEAD without binary preview # for HEAD without binary preview
fp.seek(0, 2) fp.seek(0, io.SEEK_END)
length = fp.tell() length = fp.tell()
offset = 0 offset = 0
elif i32(s[0:4]) == 0xC6D3D0C5: elif i32(s[0:4]) == 0xC6D3D0C5:

View File

@ -195,7 +195,7 @@ class IcnsFile(object):
i += HEADERSIZE i += HEADERSIZE
blocksize -= HEADERSIZE blocksize -= HEADERSIZE
dct[sig] = (i, blocksize) dct[sig] = (i, blocksize)
fobj.seek(blocksize, 1) fobj.seek(blocksize, io.SEEK_CUR)
i += blocksize i += blocksize
def itersizes(self): def itersizes(self):

View File

@ -491,7 +491,7 @@ def _save(im, fp, tile, bufsize=0):
for e, b, o, a in tile: for e, b, o, a in tile:
e = Image._getencoder(im.mode, e, a, im.encoderconfig) e = Image._getencoder(im.mode, e, a, im.encoderconfig)
if o > 0: if o > 0:
fp.seek(o, 0) fp.seek(o)
e.setimage(im.im, b) e.setimage(im.im, b)
if e.pushes_fd: if e.pushes_fd:
e.setfd(fp) e.setfd(fp)
@ -510,7 +510,7 @@ def _save(im, fp, tile, bufsize=0):
for e, b, o, a in tile: for e, b, o, a in tile:
e = Image._getencoder(im.mode, e, a, im.encoderconfig) e = Image._getencoder(im.mode, e, a, im.encoderconfig)
if o > 0: if o > 0:
fp.seek(o, 0) fp.seek(o)
e.setimage(im.im, b) e.setimage(im.im, b)
if e.pushes_fd: if e.pushes_fd:
e.setfd(fp) e.setfd(fp)

View File

@ -191,9 +191,9 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
fd = -1 fd = -1
try: try:
pos = self.fp.tell() pos = self.fp.tell()
self.fp.seek(0, 2) self.fp.seek(0, io.SEEK_END)
length = self.fp.tell() length = self.fp.tell()
self.fp.seek(pos, 0) self.fp.seek(pos)
except Exception: except Exception:
length = -1 length = -1

View File

@ -16,6 +16,7 @@
# See the README file for information on usage and redistribution. # See the README file for information on usage and redistribution.
# #
import io
from . import Image, FontFile from . import Image, FontFile
from ._binary import i8, i16le as l16, i32le as l32, i16be as b16, i32be as b32 from ._binary import i8, i16le as l16, i32le as l32, i16be as b16, i32be as b32
@ -117,7 +118,7 @@ class PcfFontFile(FontFile.FontFile):
for i in range(nprops): for i in range(nprops):
p.append((i32(fp.read(4)), i8(fp.read(1)), i32(fp.read(4)))) p.append((i32(fp.read(4)), i8(fp.read(1)), i32(fp.read(4))))
if nprops & 3: if nprops & 3:
fp.seek(4 - (nprops & 3), 1) # pad fp.seek(4 - (nprops & 3), io.SEEK_CUR) # pad
data = fp.read(i32(fp.read(4))) data = fp.read(i32(fp.read(4)))

View File

@ -25,6 +25,7 @@
# See the README file for information on usage and redistribution. # See the README file for information on usage and redistribution.
# #
import io
import logging import logging
from . import Image, ImageFile, ImagePalette from . import Image, ImageFile, ImagePalette
from ._binary import i8, i16le as i16, o8, o16le as o16 from ._binary import i8, i16le as i16, o8, o16le as o16
@ -80,7 +81,7 @@ class PcxImageFile(ImageFile.ImageFile):
elif version == 5 and bits == 8 and planes == 1: elif version == 5 and bits == 8 and planes == 1:
mode = rawmode = "L" mode = rawmode = "L"
# FIXME: hey, this doesn't work with the incremental loader !!! # FIXME: hey, this doesn't work with the incremental loader !!!
self.fp.seek(-769, 2) self.fp.seek(-769, io.SEEK_END)
s = self.fp.read(769) s = self.fp.read(769)
if len(s) == 769 and i8(s[0]) == 12: if len(s) == 769 and i8(s[0]) == 12:
# check if the palette is linear greyscale # check if the palette is linear greyscale

View File

@ -18,6 +18,7 @@
__version__ = "0.4" __version__ = "0.4"
import io
from . import Image, ImageFile, ImagePalette from . import Image, ImageFile, ImagePalette
from ._binary import i8, i16be as i16, i32be as i32 from ._binary import i8, i16be as i16, i32be as i32
@ -214,12 +215,12 @@ def _layerinfo(file):
if size: if size:
length = i32(read(4)) length = i32(read(4))
if length: if length:
file.seek(length - 16, 1) file.seek(length - 16, io.SEEK_CUR)
combined += length + 4 combined += length + 4
length = i32(read(4)) length = i32(read(4))
if length: if length:
file.seek(length, 1) file.seek(length, io.SEEK_CUR)
combined += length + 4 combined += length + 4
length = i8(read(1)) length = i8(read(1))
@ -229,7 +230,7 @@ def _layerinfo(file):
name = read(length).decode('latin-1', 'replace') name = read(length).decode('latin-1', 'replace')
combined += length + 1 combined += length + 1
file.seek(size - combined, 1) file.seek(size - combined, io.SEEK_CUR)
layers.append((name, mode, (x0, y0, x1, y1))) layers.append((name, mode, (x0, y0, x1, y1)))
# get tiles # get tiles

View File

@ -14,6 +14,7 @@
# See the README file for information on usage and redistribution. # See the README file for information on usage and redistribution.
# #
import io
import sys import sys
from . import ContainerIO from . import ContainerIO
@ -51,7 +52,7 @@ class TarIO(ContainerIO.ContainerIO):
if file == name: if file == name:
break break
self.fh.seek((size + 511) & (~511), 1) self.fh.seek((size + 511) & (~511), io.SEEK_CUR)
# Open region # Open region
ContainerIO.ContainerIO.__init__(self, self.fh, self.fh.tell(), size) ContainerIO.ContainerIO.__init__(self, self.fh, self.fh.tell(), size)

View File

@ -1678,7 +1678,7 @@ class AppendingTiffWriter:
def tell(self): def tell(self):
return self.f.tell() - self.offsetOfNewPage return self.f.tell() - self.offsetOfNewPage
def seek(self, offset, whence): def seek(self, offset, whence=io.SEEK_SET):
if whence == os.SEEK_SET: if whence == os.SEEK_SET:
offset += self.offsetOfNewPage offset += self.offsetOfNewPage