Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(164522)

Side by Side Diff: Lib/wave.py

Issue 17616: wave.Wave_read and wave.Wave_write can be context managers
Patch Set: Created 6 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_wave.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Stuff to parse WAVE files. 1 """Stuff to parse WAVE files.
2 2
3 Usage. 3 Usage.
4 4
5 Reading WAVE files: 5 Reading WAVE files:
6 f = wave.open(file, 'r') 6 f = wave.open(file, 'r')
7 where file is either the name of a file or an open file pointer. 7 where file is either the name of a file or an open file pointer.
8 The open file pointer must have methods read(), seek(), and close(). 8 The open file pointer must have methods read(), seek(), and close().
9 When the setpos() and rewind() methods are not used, the seek() 9 When the setpos() and rewind() methods are not used, the seek()
10 method is not necessary. 10 method is not necessary.
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 # else, assume it is an open file object already 161 # else, assume it is an open file object already
162 try: 162 try:
163 self.initfp(f) 163 self.initfp(f)
164 except: 164 except:
165 if self._i_opened_the_file: 165 if self._i_opened_the_file:
166 f.close() 166 f.close()
167 raise 167 raise
168 168
169 def __del__(self): 169 def __del__(self):
170 self.close() 170 self.close()
171
172 def __enter__(self):
173 return self
174
175 def __exit__(self, *args):
176 self.close()
177
171 # 178 #
172 # User visible methods. 179 # User visible methods.
173 # 180 #
174 def getfp(self): 181 def getfp(self):
175 return self._file 182 return self._file
176 183
177 def rewind(self): 184 def rewind(self):
178 self._data_seek_needed = 1 185 self._data_seek_needed = 1
179 self._soundpos = 0 186 self._soundpos = 0
180 187
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 self._nchannels = 0 322 self._nchannels = 0
316 self._sampwidth = 0 323 self._sampwidth = 0
317 self._framerate = 0 324 self._framerate = 0
318 self._nframes = 0 325 self._nframes = 0
319 self._nframeswritten = 0 326 self._nframeswritten = 0
320 self._datawritten = 0 327 self._datawritten = 0
321 self._datalength = 0 328 self._datalength = 0
322 self._headerwritten = False 329 self._headerwritten = False
323 330
324 def __del__(self): 331 def __del__(self):
332 self.close()
333
334 def __enter__(self):
335 return self
336
337 def __exit__(self, *args):
325 self.close() 338 self.close()
326 339
327 # 340 #
328 # User visible methods. 341 # User visible methods.
329 # 342 #
330 def setnchannels(self, nchannels): 343 def setnchannels(self, nchannels):
331 if self._datawritten: 344 if self._datawritten:
332 raise Error('cannot change parameters after starting to write') 345 raise Error('cannot change parameters after starting to write')
333 if nchannels < 1: 346 if nchannels < 1:
334 raise Error('bad # of channels') 347 raise Error('bad # of channels')
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 else: 508 else:
496 mode = 'rb' 509 mode = 'rb'
497 if mode in ('r', 'rb'): 510 if mode in ('r', 'rb'):
498 return Wave_read(f) 511 return Wave_read(f)
499 elif mode in ('w', 'wb'): 512 elif mode in ('w', 'wb'):
500 return Wave_write(f) 513 return Wave_write(f)
501 else: 514 else:
502 raise Error("mode must be 'r', 'rb', 'w', or 'wb'") 515 raise Error("mode must be 'r', 'rb', 'w', or 'wb'")
503 516
504 openfp = open # B/W compatibility 517 openfp = open # B/W compatibility
OLDNEW
« no previous file with comments | « Lib/test/test_wave.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+