Module: RubyPython::Conversion
- Defined in:
- lib/rubypython/conversion.rb
Overview
This modules encapsulates the work of converting between native Ruby and Python types. Unsupported conversions raise UnsupportedConversion.
Defined Under Namespace
Classes: UnsupportedConversion
Class Method Summary
collapse
Class Method Details
.ptorDict(pDict) ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/rubypython/conversion.rb', line 147
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 Python.PyDict_Next(pDict, pos, key, val) != 0
pKey = key.read_pointer
pVal = val.read_pointer
rKey = ptorObject(pKey)
rVal = ptorObject(pVal)
rb_hash[rKey] = rVal
end
rb_hash
end
|
.ptorFloat(pNum) ⇒ Object
136
137
138
|
# File 'lib/rubypython/conversion.rb', line 136
def self.ptorFloat(pNum)
Python.PyFloat_AsDouble(pNum)
end
|
.ptorInt(pNum) ⇒ Object
127
128
129
|
# File 'lib/rubypython/conversion.rb', line 127
def self.ptorInt(pNum)
Python.PyInt_AsLong pNum
end
|
.ptorList(pList) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/rubypython/conversion.rb', line 113
def self.ptorList(pList)
rb_array = []
list_size = Python.PyList_Size(pList)
list_size.times do |i|
element = Python.PyList_GetItem(pList, i)
Python.Py_IncRef element
rObject = ptorObject(element)
rb_array.push rObject
end
rb_array
end
|
.ptorLong(pNum) ⇒ Object
131
132
133
134
|
# File 'lib/rubypython/conversion.rb', line 131
def self.ptorLong(pNum)
Python.PyLong_AsLong(pNum)
end
|
.ptorObject(pObj) ⇒ Object
Converts a pointer to a Python object into a native ruby type, if possible. Otherwise raises an error.
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/rubypython/conversion.rb', line 172
def self.ptorObject(pObj)
if Macros.PyObject_TypeCheck(pObj, Python.PyString_Type.to_ptr) != 0
ptorString pObj
elsif Macros.PyObject_TypeCheck(pObj, Python.PyList_Type.to_ptr) != 0
ptorList pObj
elsif Macros.PyObject_TypeCheck(pObj, Python.PyInt_Type.to_ptr) != 0
ptorInt pObj
elsif Macros.PyObject_TypeCheck(pObj, Python.PyLong_Type.to_ptr) != 0
ptorLong pObj
elsif Macros.PyObject_TypeCheck(pObj, Python.PyFloat_Type.to_ptr) != 0
ptorFloat pObj
elsif Macros.PyObject_TypeCheck(pObj, Python.PyTuple_Type.to_ptr) != 0
ptorTuple pObj
elsif Macros.PyObject_TypeCheck(pObj, Python.PyDict_Type.to_ptr) != 0
ptorDict pObj
elsif pObj == Macros.Py_True
true
elsif pObj == Macros.Py_False
false
elsif pObj == Macros.Py_None
nil
else
pObj
end
end
|
.ptorString(pString) ⇒ Object
109
110
111
|
# File 'lib/rubypython/conversion.rb', line 109
def self.ptorString(pString)
Python.PyString_AsString(pString)
end
|
.ptorTuple(pTuple) ⇒ Object
140
141
142
143
144
145
|
# File 'lib/rubypython/conversion.rb', line 140
def self.ptorTuple(pTuple)
pList = Python.PySequence_List pTuple
rArray = ptorList pList
Python.Py_DecRef pList
rArray
end
|
.rtopArrayToList(rArray) ⇒ Object
16
17
18
19
20
21
22
23
|
# File 'lib/rubypython/conversion.rb', line 16
def self.rtopArrayToList(rArray)
size = rArray.length
pList = Python.PyList_New size
rArray.each_with_index do |el, i|
Python.PyList_SetItem pList, i, rtopObject(el)
end
pList
end
|
.rtopArrayToTuple(rArray) ⇒ Object
25
26
27
28
29
30
|
# File 'lib/rubypython/conversion.rb', line 25
def self.rtopArrayToTuple(rArray)
pList = rtopArrayToList(rArray)
pTuple = Python.PySequence_Tuple(pList)
Python.Py_DecRef(pList)
pTuple
end
|
.rtopBigNum(rNum) ⇒ Object
44
45
46
|
# File 'lib/rubypython/conversion.rb', line 44
def self.rtopBigNum(rNum)
Python.PyLong_FromLong(rNum)
end
|
.rtopFalse ⇒ Object
52
53
54
|
# File 'lib/rubypython/conversion.rb', line 52
def self.rtopFalse
Macros.Py_RETURN_FALSE
end
|
.rtopFixnum(rNum) ⇒ Object
40
41
42
|
# File 'lib/rubypython/conversion.rb', line 40
def self.rtopFixnum(rNum)
Python.PyInt_FromLong(rNum)
end
|
.rtopFloat(rNum) ⇒ Object
48
49
50
|
# File 'lib/rubypython/conversion.rb', line 48
def self.rtopFloat(rNum)
Python.PyFloat_FromDouble(rNum)
end
|
.rtopHash(rHash) ⇒ Object
32
33
34
35
36
37
38
|
# File 'lib/rubypython/conversion.rb', line 32
def self.rtopHash(rHash)
pDict = Python.PyDict_New
rHash.each do |k,v|
Python.PyDict_SetItem pDict, rtopObject(k, key=true), rtopObject(v)
end
pDict
end
|
.rtopNone ⇒ Object
60
61
62
|
# File 'lib/rubypython/conversion.rb', line 60
def self.rtopNone
Macros.Py_RETURN_NONE
end
|
.rtopObject(rObj, is_key = false) ⇒ FFI::Pointer
If possible converts a ruby type to an equivalent python native type.
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/rubypython/conversion.rb', line 75
def self.rtopObject(rObj, is_key=false)
case rObj
when String
rtopString rObj
when Array
if is_key
rtopArrayToTuple rObj
else
rtopArrayToList rObj
end
when Hash
rtopHash rObj
when Fixnum
rtopFixnum rObj
when Bignum
rtopBignum rObj
when Float
rtopFloat rObj
when true
rtopTrue
when false
rtopFalse
when Symbol
rtopSymbol rObj
when nil
rtopNone
else
raise UnsupportedConversion.new("Unsupported type for RTOP conversion." )
end
end
|
.rtopString(rString) ⇒ Object
12
13
14
|
# File 'lib/rubypython/conversion.rb', line 12
def self.rtopString(rString)
Python.PyString_FromString(rString)
end
|
.rtopSymbol(rSymbol) ⇒ Object
64
65
66
|
# File 'lib/rubypython/conversion.rb', line 64
def self.rtopSymbol(rSymbol)
Python.PyString_FromString rSymbol.to_s
end
|
.rtopTrue ⇒ Object
56
57
58
|
# File 'lib/rubypython/conversion.rb', line 56
def self.rtopTrue
Macros.Py_RETURN_TRUE
end
|