Giter VIP home page Giter VIP logo

Comments (4)

mgeier avatar mgeier commented on July 19, 2024

close() should probably also delete all attributes:

vars(self).clear()

from python-soundfile.

bastibe avatar bastibe commented on July 19, 2024

I am kind of on the fence about this idea. It seems to me that in every case where you would use close, it would be more pythonic to use the context manager or del instead. Instead of soundfile.close(), why not soundfile=None? It has the same effect, without introducing a SoundFile zombie that still exists even though it is closed and therefore perfectly useless. What do you think about this?

Flush certainly wouldn't hurt, though.

If I understand the circular-reference-business correctly, it would only happen if one SoundFile referenced another SoundFile, which itself referenced the first one. Not a particularly likely scenario.

from python-soundfile.

mgeier avatar mgeier commented on July 19, 2024

To get a little more concrete, I've created a pull request: #14.

Standard file objects have a close() method, so it is kind of Pythonic ... at least people are used to it.

I agree that a context manager would be the best choice, but sometimes it's impossible (e.g. interactive sessions).

The problem with soundfile=None is that it's not guaranteed that __del__() is called at all.
It's the current behavior of CPython to call it as soon as the reference count goes to zero, but other implementations don't have to do it like that.

The SoundFile zombie is definitely a problem, but to avoid usage afterwards, I removed all attributes in my example implementation. So no matter what you try to do, you will get some error message.
It would be possible to check for the _file attribute in each and every function, but I think that's too much.

BTW, there is a little bug in the current implementation: If there is an error in sf_close(), the exception will prevent self._file = None from being executed and if the exception is caught, self._file will remain in a non-None state.
Or am I missing something?

from python-soundfile.

bastibe avatar bastibe commented on July 19, 2024

This is continued in #14

from python-soundfile.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.