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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/rubypython/conversion.rb', line 164
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
153
154
155
|
# File 'lib/rubypython/conversion.rb', line 153
def self.ptorFloat(pNum)
Python.PyFloat_AsDouble(pNum)
end
|
.ptorInt(pNum) ⇒ Object
144
145
146
|
# File 'lib/rubypython/conversion.rb', line 144
def self.ptorInt(pNum)
Python.PyInt_AsLong pNum
end
|
.ptorList(pList) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/rubypython/conversion.rb', line 130
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
148
149
150
151
|
# File 'lib/rubypython/conversion.rb', line 148
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.
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/rubypython/conversion.rb', line 189
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
126
127
128
|
# File 'lib/rubypython/conversion.rb', line 126
def self.ptorString(pString)
Python.PyString_AsString(pString)
end
|
.ptorTuple(pTuple) ⇒ Object
157
158
159
160
161
162
|
# File 'lib/rubypython/conversion.rb', line 157
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.
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/rubypython/conversion.rb', line 89
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
when Proc, Method
raise UnsupportedConversion.new("Python to Ruby callbacks not suppported in legacy mode") if RubyPython.legacy_mode
rtopProc rObj
else
raise UnsupportedConversion.new("Unsupported type for RTOP conversion." )
end
end
|
.rtopProc(rObj) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/rubypython/conversion.rb', line 68
def self.rtopProc(rObj)
pyMethodDef = Python::PyMethodDef.new
callback = Proc.new do |py_self, py_args|
ret = rObj.call(*RubyPyProxy.new(py_args).to_a)
PyObject.convert(ret)[0].pointer
end
pyMethodDef[:ml_name] = FFI::MemoryPointer.from_string "Proc::#{rObj.object_id}"
pyMethodDef[:ml_meth] = callback
pyMethodDef[:ml_flags] = Python::METH_VARARGS
pyMethodDef[:ml_doc] = nil
Python::PyCFunction_New pyMethodDef, nil
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
|