Method: RubyPython::Conversion.ptorDict

Defined in:
lib/rubypython/conversion.rb

.ptorDict(pDict) ⇒ Object

Convert an FFI::Pointer to a Python Dictionary (PyDictObject) to a Ruby Hash.



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/rubypython/conversion.rb', line 274

def self.ptorDict(pDict)
  rb_hash = {}

  pos = ::FFI::MemoryPointer.new :ssize_t
  pos.write_int 0
  key = ::FFI::MemoryPointer.new :pointer
  val = ::FFI::MemoryPointer.new :pointer

  while RubyPython::Python.PyDict_Next(pDict, pos, key, val) != 0
    #PyDict_Next sets key and val to borrowed references. We do not care
    #if we are able to convert them to native ruby types, but if we wind up
    #wrapping either in a proxy we better IncRef it to make sure it stays
    #around.
    pKey = key.read_pointer
    pVal = val.read_pointer
    rKey = ptorObject(pKey)
    rVal = ptorObject(pVal)
    RubyPython.Py_IncRef pKey if rKey.kind_of? ::FFI::Pointer
    RubyPython.Py_IncRef pVal if rVal.kind_of? ::FFI::Pointer
    rb_hash[rKey] = rVal
  end

  rb_hash
end