How to make cleaning reliable in python?

I have ctypes bindings, and for each body.New I have to name body.Free. The library I'm attached to does not have isolation procedures isolated from the rest of the code (you can call them anywhere), and to use several useful functions I need to make circular links.

I think this would solve if I found a reliable way to bind a destructor to an object. (weakrefs will help if they give me a callback just before the data is deleted.

Thus, it is obvious that this code fires when you enter speed_func:

class Body(object):
    def __init__(self, mass, inertia):
        self._body = body.New(mass, inertia)

    def __del__(self):
        print '__del__ %r' % self
        if body:
            body.Free(self._body)

    ...        

    def set_velocity_func(self, func):
        self._body.contents.velocity_func = ctypes_wrapping(func)

I also tried to solve this through weakrefs, with those things that seem worse, only more more unpredictable.

velocity_func, , , , :

class Toy(object):
    def __init__(self, body):
        self.body.owner = self

...

def collision(a, b, contacts):
    whatever(a.body.owner)

, , Structures , / ?

, : http://bitbucket.org/cheery/ctypes-chipmunk/

+3
3

, , :

from weakref import ref

pointers = set()

class Pointer(object):
    def __init__(self, cfun, ptr):
        pointers.add(self)
        self.ref = ref(ptr, self.cleanup)
        self.data = cast(ptr, c_void_p).value # python cast it so smart, but it can't be smarter than this.
        self.cfun = cfun

    def cleanup(self, obj):
        print 'cleanup 0x%x' % self.data
        self.cfun(self.data)
        pointers.remove(self)

def cleanup(cfun, ptr):
    Pointer(cfun, ptr)

. , - , . , ctypes , . , , , , , , , .

: , , , . , del , , , . , .

, - - , .

: .. weakrefs ! , , python, , .

0

, , , , , Python, , . del , .

Python - :

try:
    allocate()
    dostuff()
finally:
    cleanup()

, 2.5, with, .

, / . , , , , . , , . , . , Python , . , , , , , , , Python.

+3

CPython __del__ , , ( : - __del__ - , ).

, : - , , . ; - , , - .

-

class Body(object):
    def __init__(self, mass, inertia):
        self._bodyref = body
        self._body = body.New(mass, inertia)

    def __del__(self):
        print '__del__ %r' % self
        if body:
            body.Free(self._body)

...        

def set_velocity_func(self, func):
    self._body.contents.velocity_func = ctypes_wrapping(func)

:

  • , , , , , .
  • , - ; factory , "" , unit test . .
0

Source: https://habr.com/ru/post/1711268/


All Articles