annotate Lib/chunk.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 9d29daecb9fa
children 0a389b47f129
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Simple class to read IFF chunks.
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 An IFF chunk (used in formats such as AIFF, TIFF, RMFF (RealMedia File
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 Format)) has the following structure:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 +----------------+
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 | ID (4 bytes) |
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 +----------------+
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 | size (4 bytes) |
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 +----------------+
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 | data |
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 | ... |
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 +----------------+
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 The ID is a 4-byte string which identifies the type of chunk.
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 The size field (a 32-bit value, encoded using big-endian byte order)
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 gives the size of the whole chunk, including the 8-byte header.
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
11050
5a82fb4f78e5 Patch from Sjoerd Mullender:
Fred Drake <fdrake@acm.org>
parents: 10993
diff changeset
20 Usually an IFF-type file consists of one or more chunks. The proposed
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11227
diff changeset
21 usage of the Chunk class defined here is to instantiate an instance at
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 the start of each chunk and read from the instance until it reaches
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 the end, after which a new instance can be instantiated. At the end
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 of the file, creating a new instance will fail with a EOFError
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 exception.
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 Usage:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 17797
diff changeset
28 while True:
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 try:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 chunk = Chunk(file)
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 except EOFError:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 break
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 chunktype = chunk.getname()
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 17797
diff changeset
34 while True:
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 data = chunk.read(nbytes)
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 if not data:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 pass
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 # do something with data
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 The interface is file-like. The implemented methods are:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 read, close, seek, tell, isatty.
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 Extra methods are: skip() (called by close, skips to the end of the chunk),
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 getname() (returns the name (ID) of the chunk)
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 The __init__ method has one required argument, a file-like object
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 (including a chunk instance), and one optional argument, a flag which
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11227
diff changeset
47 specifies whether or not chunks are aligned on 2-byte boundaries. The
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 default is 1, i.e. aligned.
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 """
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 class Chunk:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 17797
diff changeset
52 def __init__(self, file, align=True, bigendian=True, inclheader=False):
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 import struct
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 17797
diff changeset
54 self.closed = False
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11227
diff changeset
55 self.align = align # whether to align to word (2-byte) boundaries
11227
e773d2f0741d Sjoerd Mullender writes:
Guido van Rossum <guido@python.org>
parents: 11050
diff changeset
56 if bigendian:
e773d2f0741d Sjoerd Mullender writes:
Guido van Rossum <guido@python.org>
parents: 11050
diff changeset
57 strflag = '>'
e773d2f0741d Sjoerd Mullender writes:
Guido van Rossum <guido@python.org>
parents: 11050
diff changeset
58 else:
e773d2f0741d Sjoerd Mullender writes:
Guido van Rossum <guido@python.org>
parents: 11050
diff changeset
59 strflag = '<'
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 self.file = file
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 self.chunkname = file.read(4)
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 if len(self.chunkname) < 4:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 raise EOFError
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 try:
36567
9d29daecb9fa Patch #1373643: The chunk module can now read chunks larger than
Georg Brandl <georg@python.org>
parents: 22867
diff changeset
65 self.chunksize = struct.unpack(strflag+'L', file.read(4))[0]
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 except struct.error:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 raise EOFError
11227
e773d2f0741d Sjoerd Mullender writes:
Guido van Rossum <guido@python.org>
parents: 11050
diff changeset
68 if inclheader:
e773d2f0741d Sjoerd Mullender writes:
Guido van Rossum <guido@python.org>
parents: 11050
diff changeset
69 self.chunksize = self.chunksize - 8 # subtract header
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 self.size_read = 0
10993
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
71 try:
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
72 self.offset = self.file.tell()
17797
a8fa9c0f1e0b Clean up bare except: when determining whether a file is seekable.
Fred Drake <fdrake@acm.org>
parents: 17524
diff changeset
73 except (AttributeError, IOError):
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 17797
diff changeset
74 self.seekable = False
10993
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
75 else:
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 17797
diff changeset
76 self.seekable = True
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 def getname(self):
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 """Return the name (ID) of the current chunk."""
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 return self.chunkname
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
11227
e773d2f0741d Sjoerd Mullender writes:
Guido van Rossum <guido@python.org>
parents: 11050
diff changeset
82 def getsize(self):
e773d2f0741d Sjoerd Mullender writes:
Guido van Rossum <guido@python.org>
parents: 11050
diff changeset
83 """Return the size of the current chunk."""
e773d2f0741d Sjoerd Mullender writes:
Guido van Rossum <guido@python.org>
parents: 11050
diff changeset
84 return self.chunksize
e773d2f0741d Sjoerd Mullender writes:
Guido van Rossum <guido@python.org>
parents: 11050
diff changeset
85
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 def close(self):
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 if not self.closed:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 self.skip()
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 17797
diff changeset
89 self.closed = True
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 def isatty(self):
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 if self.closed:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 raise ValueError, "I/O operation on closed file"
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 17797
diff changeset
94 return False
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 17797
diff changeset
96 def seek(self, pos, whence=0):
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 """Seek to specified position into the chunk.
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 Default position is 0 (start of chunk).
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 If the file is not seekable, this will result in an error.
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11227
diff changeset
100 """
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 if self.closed:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 raise ValueError, "I/O operation on closed file"
10993
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
104 if not self.seekable:
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
105 raise IOError, "cannot seek"
11050
5a82fb4f78e5 Patch from Sjoerd Mullender:
Fred Drake <fdrake@acm.org>
parents: 10993
diff changeset
106 if whence == 1:
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 pos = pos + self.size_read
11050
5a82fb4f78e5 Patch from Sjoerd Mullender:
Fred Drake <fdrake@acm.org>
parents: 10993
diff changeset
108 elif whence == 2:
17524
79efbc9a03de Fix typo in attribute name (chunk_size should be chunksize) found by
Guido van Rossum <guido@python.org>
parents: 16657
diff changeset
109 pos = pos + self.chunksize
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 if pos < 0 or pos > self.chunksize:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 raise RuntimeError
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 self.file.seek(self.offset + pos, 0)
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 self.size_read = pos
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
114
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 def tell(self):
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 if self.closed:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 raise ValueError, "I/O operation on closed file"
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 return self.size_read
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 17797
diff changeset
120 def read(self, size=-1):
11050
5a82fb4f78e5 Patch from Sjoerd Mullender:
Fred Drake <fdrake@acm.org>
parents: 10993
diff changeset
121 """Read at most size bytes from the chunk.
5a82fb4f78e5 Patch from Sjoerd Mullender:
Fred Drake <fdrake@acm.org>
parents: 10993
diff changeset
122 If size is omitted or negative, read until the end
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 of the chunk.
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11227
diff changeset
124 """
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 if self.closed:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 raise ValueError, "I/O operation on closed file"
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 if self.size_read >= self.chunksize:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 return ''
11050
5a82fb4f78e5 Patch from Sjoerd Mullender:
Fred Drake <fdrake@acm.org>
parents: 10993
diff changeset
130 if size < 0:
5a82fb4f78e5 Patch from Sjoerd Mullender:
Fred Drake <fdrake@acm.org>
parents: 10993
diff changeset
131 size = self.chunksize - self.size_read
5a82fb4f78e5 Patch from Sjoerd Mullender:
Fred Drake <fdrake@acm.org>
parents: 10993
diff changeset
132 if size > self.chunksize - self.size_read:
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11227
diff changeset
133 size = self.chunksize - self.size_read
11050
5a82fb4f78e5 Patch from Sjoerd Mullender:
Fred Drake <fdrake@acm.org>
parents: 10993
diff changeset
134 data = self.file.read(size)
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 self.size_read = self.size_read + len(data)
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 if self.size_read == self.chunksize and \
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 self.align and \
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 (self.chunksize & 1):
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 dummy = self.file.read(1)
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 self.size_read = self.size_read + len(dummy)
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 return data
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
142
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 def skip(self):
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 """Skip the rest of the chunk.
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 If you are not interested in the contents of the chunk,
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 this method should be called so that the file points to
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 the start of the next chunk.
15867
f5a3d98a35bc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 11227
diff changeset
148 """
10937
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 if self.closed:
e8ff4b1298c2 Patch/new code by Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 raise ValueError, "I/O operation on closed file"
10993
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
152 if self.seekable:
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
153 try:
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
154 n = self.chunksize - self.size_read
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
155 # maybe fix alignment
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
156 if self.align and (self.chunksize & 1):
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
157 n = n + 1
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
158 self.file.seek(n, 1)
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
159 self.size_read = self.size_read + n
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
160 return
17797
a8fa9c0f1e0b Clean up bare except: when determining whether a file is seekable.
Fred Drake <fdrake@acm.org>
parents: 17524
diff changeset
161 except IOError:
10993
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
162 pass
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
163 while self.size_read < self.chunksize:
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
164 n = min(8192, self.chunksize - self.size_read)
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
165 dummy = self.read(n)
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
166 if not dummy:
3e05fcaaaed2 Sjoerd Mullender:
Guido van Rossum <guido@python.org>
parents: 10938
diff changeset
167 raise EOFError