Method: RubyPython::Conversion.rtopHash

Defined in:
lib/rubypython/conversion.rb

.rtopHash(rHash) ⇒ Object

Convert a Ruby Hash to a Python Dict. Returns an FFI::Pointer to a PyDictObject.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rubypython/conversion.rb', line 60

def self.rtopHash(rHash)
  pDict = RubyPython::Python.PyDict_New
  if pDict.null?
    raise ConversionError.new "Python failed to create new dict"
  end
  rHash.each do |k,v|
    key = rtopObject(k, :key => true)
    value = rtopObject(v)

    # PyDict_SetItem INCREFS both the key and the value passed to it.
    # Since rtopObject already gives us a new reference, this is not necessary.
    # Thus, we decref the passed in objects to balancy things out
    if RubyPython::Python.PyDict_SetItem(pDict, key, value) == -1
      raise ConversionError.new "Python failed to set #{key}, #{value} in dict conversion"
    end

    RubyPython::Python.Py_DecRef key
    RubyPython::Python.Py_DecRef value
  end

  pDict
end