Class: RubyPython::PyObject::AutoPyPointer

Inherits:
FFI::AutoPointer
  • Object
show all
Defined in:
lib/rubypython/pyobject.rb

Overview

This class wraps C Py…Objects so that the RubyPython::Python reference count is automatically decreased when the Ruby object referencing them goes out of scope.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.current_pointersObject

Keeps track of which objects are associated with the currently running RubyPython::Python interpreter, so that RubyPython knows not to try to decrease the reference counts of the others when garbage collecting.



20
21
22
# File 'lib/rubypython/pyobject.rb', line 20

def current_pointers
  @current_pointers
end

Class Method Details

.python_interpreter_update(status) ⇒ Object

Called by RubyPython when the interpreter is started or stopped so that the necessary preparation or cleanup can be done. For internal use only.



42
43
44
45
46
47
# File 'lib/rubypython/pyobject.rb', line 42

def python_interpreter_update(status)
  case status
  when :stop
    current_pointers.clear
  end
end

.release(pointer) ⇒ Object

When used along with the FFI Library method is executed whenever a pointer is garbage collected so that cleanup can be done. In our case we decrease the reference count of the held pointer as long as the object is still good. There is really no reason the end-user would need to the use this method directly.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubypython/pyobject.rb', line 27

def release(pointer)
  obj_id = pointer.object_id
  deleted = @current_pointers.delete(obj_id)
  if pointer.null?
    puts "Warning: Trying to DecRef NULL pointer" if RubyPython::Python.Py_IsInitialized != 0
    return
  end
  if deleted and (RubyPython::Python.Py_IsInitialized != 0)
    RubyPython::Python.Py_DecRef pointer
  end
end