Conversation
The built-in open() function isn't used anymore. The function os.path.isfile() is used to check if a file exists already. For mode 'w+', an existing file is first opened in write-only mode to make sure it is truncated. Afterwards it is re-opened in read/write mode.
This was referenced Nov 27, 2014
Merged
Owner
|
Great work! |
mgeier
added a commit
that referenced
this pull request
Nov 27, 2014
Allow mode='x' even for Python < 3.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-inopen()function.The main point is that
SFM_RDWRdoesn't truncate an existing file, but this would be expected frommode='w+'.As a work-around, the file is first opened with
os.open()using theos.O_TRUNCflag to truncate the file, then immediately closed and opened again with libsndfile inSFM_RDWRmode.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.