Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do we want to have weakref support? #646

Open
pfalcon opened this issue Jun 1, 2014 · 2 comments

Comments

@pfalcon
Copy link
Contributor

commented Jun 1, 2014

Here's another thing to consider re: completeness of language implementation: do we want to support weak referenced (weakref module)?

On one hand, it's niche feature used rarely enough. On the other hand, it's something which would pretty much help low-memory usage: ability to cache some object in memory, but safely and transparently free it if there's memory pressure.

But efficient implementation of weakrefs is expensive - each object need to grow to contain pointer to its associated weakref. Taking into account that weakrefs are rare, less runtime-efficient, but much more memory-efficient impl of having a separate mapping from obj to its weakref can be used.

Thoughts/comments are welcome.

@pfalcon pfalcon added notimpl labels Jun 1, 2014

@pfalcon

This comment has been minimized.

Copy link
Contributor Author

commented May 8, 2015

I hit weakref usage in 3rd-party code (npyscreen lib), added dummy module to micropython-lib: micropython/micropython-lib@450b8b6

tannewt pushed a commit to tannewt/circuitpython that referenced this issue Feb 27, 2018
tannewt added a commit to tannewt/circuitpython that referenced this issue Feb 27, 2018
Merge pull request micropython#647 from siddacious/samd51g_timer_fix
fixes timer number assumptions for samd51s for issue micropython#646
@pmp-p

This comment has been minimized.

Copy link
Contributor

commented Sep 11, 2019

i think if __del__ (for user defined objects) #1878 is not implemented then weakref should be.

or at least a generic C "uweak" object with a C __del__ ready to use callback function that could be attached to user defined instances so it can react when "host" is collected eg with a callback and id(host) as parameter. it seems there's already some support to do that #245

# cpython demonstration of a what could be a µpy C object
class uweak:
    def __init__(self, host):
        self.host = id(host)

    def __del__(self):
        print(self.host,"is gone")

class user_def:
    def __init__(self):
        self.weak =uweak(self)

def test():
    u = user_def()

test()

Actual gc is out of Python control ( finalizers control - not cycle control ). determinist gc handling is nothing but a niche feature ( RPC / ffi / external c++ libraries )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.