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

Side by Side Diff: Lib/sunau.py

Issue 18878: Add support of the 'with' statement to sunau.open.
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 | « Doc/whatsnew/3.4.rst ('k') | Lib/test/test_sunau.py » ('j') | 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 Sun and NeXT audio files. 1 """Stuff to parse Sun and NeXT audio files.
2 2
3 An audio file consists of a header followed by the data. The structure 3 An audio file consists of a header followed by the data. The structure
4 of the header is as follows. 4 of the header is as follows.
5 5
6 +---------------+ 6 +---------------+
7 | magic word | 7 | magic word |
8 +---------------+ 8 +---------------+
9 | header size | 9 | header size |
10 +---------------+ 10 +---------------+
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 import builtins 160 import builtins
161 f = builtins.open(f, 'rb') 161 f = builtins.open(f, 'rb')
162 self._opened = True 162 self._opened = True
163 else: 163 else:
164 self._opened = False 164 self._opened = False
165 self.initfp(f) 165 self.initfp(f)
166 166
167 def __del__(self): 167 def __del__(self):
168 if self._file: 168 if self._file:
169 self.close() 169 self.close()
170
171 def __enter__(self):
172 return self
173
174 def __exit__(self, *args):
175 self.close()
170 176
171 def initfp(self, file): 177 def initfp(self, file):
172 self._file = file 178 self._file = file
173 self._soundpos = 0 179 self._soundpos = 0
174 magic = int(_read_u32(file)) 180 magic = int(_read_u32(file))
175 if magic != AUDIO_FILE_MAGIC: 181 if magic != AUDIO_FILE_MAGIC:
176 raise Error('bad magic number') 182 raise Error('bad magic number')
177 self._hdr_size = int(_read_u32(file)) 183 self._hdr_size = int(_read_u32(file))
178 if self._hdr_size < 24: 184 if self._hdr_size < 24:
179 raise Error('header size too small') 185 raise Error('header size too small')
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 f = builtins.open(f, 'wb') 301 f = builtins.open(f, 'wb')
296 self._opened = True 302 self._opened = True
297 else: 303 else:
298 self._opened = False 304 self._opened = False
299 self.initfp(f) 305 self.initfp(f)
300 306
301 def __del__(self): 307 def __del__(self):
302 if self._file: 308 if self._file:
303 self.close() 309 self.close()
304 self._file = None 310 self._file = None
311
312 def __enter__(self):
313 return self
314
315 def __exit__(self, *args):
316 self.close()
305 317
306 def initfp(self, file): 318 def initfp(self, file):
307 self._file = file 319 self._file = file
308 self._framerate = 0 320 self._framerate = 0
309 self._nchannels = 0 321 self._nchannels = 0
310 self._sampwidth = 0 322 self._sampwidth = 0
311 self._framesize = 0 323 self._framesize = 0
312 self._nframes = AUDIO_UNKNOWN_SIZE 324 self._nframes = AUDIO_UNKNOWN_SIZE
313 self._nframeswritten = 0 325 self._nframeswritten = 0
314 self._datawritten = 0 326 self._datawritten = 0
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 self._nframeswritten = self._nframeswritten + nframes 415 self._nframeswritten = self._nframeswritten + nframes
404 self._datawritten = self._datawritten + len(data) 416 self._datawritten = self._datawritten + len(data)
405 417
406 def writeframes(self, data): 418 def writeframes(self, data):
407 self.writeframesraw(data) 419 self.writeframesraw(data)
408 if self._nframeswritten != self._nframes or \ 420 if self._nframeswritten != self._nframes or \
409 self._datalength != self._datawritten: 421 self._datalength != self._datawritten:
410 self._patchheader() 422 self._patchheader()
411 423
412 def close(self): 424 def close(self):
413 self._ensure_header_written() 425 if self._file:
414 if self._nframeswritten != self._nframes or \ 426 try:
415 self._datalength != self._datawritten: 427 self._ensure_header_written()
416 self._patchheader() 428 if self._nframeswritten != self._nframes or \
417 self._file.flush() 429 self._datalength != self._datawritten:
418 if self._opened and self._file: 430 self._patchheader()
419 self._file.close() 431 self._file.flush()
420 self._file = None 432 finally:
433 if self._opened and self._file:
434 self._file.close()
435 self._file = None
421 436
422 # 437 #
423 # private methods 438 # private methods
424 # 439 #
425 440
426 def _ensure_header_written(self): 441 def _ensure_header_written(self):
427 if not self._nframeswritten: 442 if not self._nframeswritten:
428 if not self._nchannels: 443 if not self._nchannels:
429 raise Error('# of channels not specified') 444 raise Error('# of channels not specified')
430 if not self._sampwidth: 445 if not self._sampwidth:
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 else: 496 else:
482 mode = 'rb' 497 mode = 'rb'
483 if mode in ('r', 'rb'): 498 if mode in ('r', 'rb'):
484 return Au_read(f) 499 return Au_read(f)
485 elif mode in ('w', 'wb'): 500 elif mode in ('w', 'wb'):
486 return Au_write(f) 501 return Au_write(f)
487 else: 502 else:
488 raise Error("mode must be 'r', 'rb', 'w', or 'wb'") 503 raise Error("mode must be 'r', 'rb', 'w', or 'wb'")
489 504
490 openfp = open 505 openfp = open
OLDNEW
« no previous file with comments | « Doc/whatsnew/3.4.rst ('k') | Lib/test/test_sunau.py » ('j') | no next file with comments »

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