Author drj
Recipients drj
Date 2009-02-10.11:44:58
SpamBayes Score 3.06644e-13
Marked as misclassified No
Message-id <1234266301.49.0.830035453307.issue5202@psf.upfronthosting.co.za>
In-reply-to
Content
When using the wave module to output wave files, the output file cannot 
be a Unix pipeline.

Example.  The following program outputs a (trivial) wave file on stdout:

#!/usr/bin/env python
import sys
import wave
w = wave.open(sys.stdout, 'w')
w.setnchannels(1)
w.setsampwidth(1)
w.setframerate(32000)
w.setnframes(0)
w.close()


It can create a wave file like this:

$ ./bugex > foo.wav

When used in a pipeline we get:

$ ./bugex | wc
Traceback (most recent call last):
  File "./bugex", line 9, in <module>
    w.close()
  File 
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py
", line 437, in close
    self._ensure_header_written(0)
  File 
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py
", line 458, in _ensure_header_written
    self._write_header(datasize)
  File 
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py
", line 465, in _write_header
    self._form_length_pos = self._file.tell()
IOError: [Errno 29] Illegal seek
Exception exceptions.IOError: (29, 'Illegal seek') in <bound method 
Wave_write.__del__ of <wave.Wave_write instance at 0x71418>> ignored
       0       1       8

The wave module has almost all it needs to work around this problem.  
The wave module will only seek the output if it needs to patch the 
header.  If you use setnframes to write the correct number of frames 
before writing them with writeframesraw then the header will not be 
patched upon calling close.  However...

The problem is that the "tell" method is invoked on the output stream 
(to record where the header is, in the event that we need to patch it); 
the "tell" method fails with an exception when the output is a pipeline 
(see example, above).

Exceptions from "tell" when writing the header initially (in 
_write_header) should be ignored.  If _patchheader is later invoked it 
will fail due to lack of pos.
History
Date User Action Args
2009-02-10 11:45:01drjsetrecipients: + drj
2009-02-10 11:45:01drjsetmessageid: <1234266301.49.0.830035453307.issue5202@psf.upfronthosting.co.za>
2009-02-10 11:45:00drjlinkissue5202 messages
2009-02-10 11:44:58drjcreate