Changeset 1343 for framspy


Ignore:
Timestamp:
08/11/25 18:20:48 (5 days ago)
Author:
Maciej Komosinski
Message:

Protect against the "c_api" global variable becoming None when Python runtime is terminating, but some ExtValue? objects are still about to be destroyed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • framspy/frams.py

    r1199 r1343  
    4242                if dontinit:
    4343                        return
     44                       
    4445                if isinstance(arg, int):
    4546                        self._initFromInt(arg)
     
    5354                        raise ctypes.ArgumentError("Can't make ExtValue from '%s' (%s)" % (str(arg), type(arg)))
    5455
     56                # Bypass our custom __setattr__ (just like a regular self.myfield=... assignment here, it would cause infinite recursion because our custom __setattr__ creates another ExtValue object) by calling object.__setattr__ directly:
     57                # object.__setattr__(self, 'debuginfo', "typ=%s, class=%s, arg=%s" % (str(self._type()), str(self._class()), str(arg)))  # for debugging the order of deletion/destruction of ExtValue objects         
     58
    5559
    5660        def __del__(self):
    57                 c_api.extFree(self.__ptr)
     61                # debuginfo = self.__dict__['debuginfo'] if 'debuginfo' in self.__dict__ else '(not-inited)'
     62                if c_api is not None:  # there is some unknown interaction between the native Framsticks library and other native Python libraries (like numpy) which affects the order of Python interpreter's garbage collector and leads to occasional calls of this destructor after c_api becomes None (when the Python interpreter exits). Hence this protection to avoid calling extFree() on None. An alternative would be to use self._finalizer = weakref.finalize(self, c_api.extFree, self.__ptr) instead of __del__.
     63                        #print("\tDeleter of the object with debuginfo='%s'" % debuginfo)
     64                        c_api.extFree(self.__ptr)
     65                #else:
     66                #       print("\t*** The deleter of the object with debuginfo='%s' has c_api==None, so unable to extFree() !" % debuginfo)
    5867
    5968
Note: See TracChangeset for help on using the changeset viewer.