bastibe/PySoundFile — pull request #94 by mgeier (2014-11-27 15:41:39+00:00)
Title: Allow mode='x' even for Python < 3.3

For #77 it would be very helpful if `mode='x'` were supported for all Python versions, not only >= 3.3.

In this PR I tried to implement `mode='x'` "manually" by checking if a file exists before opening it and not by relying on the built-in `open()` function.
The main point is that `SFM_RDWR` doesn't truncate an existing file, but this would be expected from `mode='w+'`.
As a work-around, the file is first opened with `os.open()` using the `os.O_TRUNC` flag to truncate the file, then immediately closed and opened again with libsndfile in `SFM_RDWR` mode.
This only works when specifying a file name, it doesn't work for file descriptors and file-like objects. Those have to be truncated already before using them in `SoundFile`.

This is basically an alternative implementation for #65 (but hopefully better).
It is a combination of point 1 and 3 in #59.
It should have the same behavior as discussed in #60, but with a different implementation.

Along the way, it would automatically solve most of #68.