pycall 0.1.0.alpha.20170309 → 0.1.0.alpha.20170311
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pycall/conversion.rb +50 -43
- data/lib/pycall/pyerror.rb +5 -5
- data/lib/pycall/pyobject_wrapper.rb +2 -2
- data/lib/pycall/types.rb +4 -0
- data/lib/pycall/utils.rb +9 -2
- data/lib/pycall/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0fa9f2771f8c95a9d13d448e0369e9b7e9480f6
|
4
|
+
data.tar.gz: 4995b342604508cb01faadde8d6c90f35a402783
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 107f426b7baa846f92dee733e1e2fa797f8a8fe9690e6690257254d3bf9e0d996f83b733c5d31c71136880fa9d1872c22bc4e8d3290319ff19b21c8844a63ccf
|
7
|
+
data.tar.gz: 31a974c8e52b665b61b73078edb3f366f8ff736d1828762f6ca4f1ad103e42208362a2a202b9aaa7d30c8189fcd00d6a3d08a12c24830e51da480a98e35ad745
|
data/lib/pycall/conversion.rb
CHANGED
@@ -26,8 +26,54 @@ module PyCall
|
|
26
26
|
@python_type_map << TypePair.new(pytype, rbtype)
|
27
27
|
end
|
28
28
|
|
29
|
-
|
30
|
-
|
29
|
+
# Convert a PyCall::PyObjectStruct object to a Ruby object
|
30
|
+
#
|
31
|
+
# @param [PyCall::PyObjectStruct] pyptr a PyObjectStruct object.
|
32
|
+
#
|
33
|
+
# @return a Ruby object converted from `pyptr`.
|
34
|
+
def self.to_ruby(pyptr)
|
35
|
+
return nil if pyptr.null? || PyCall.none?(pyptr)
|
36
|
+
|
37
|
+
case
|
38
|
+
when PyCall::Types.pyisinstance(pyptr, LibPython.PyType_Type)
|
39
|
+
return TypeObject.new(pyptr)
|
40
|
+
|
41
|
+
when PyCall::Types.pyisinstance(pyptr, LibPython.PyBool_Type)
|
42
|
+
return Conversions.convert_to_boolean(pyptr)
|
43
|
+
|
44
|
+
when PyCall::Types.pyisinstance(pyptr, LibPython.PyInt_Type)
|
45
|
+
return Conversions.convert_to_integer(pyptr)
|
46
|
+
|
47
|
+
when PyCall::Types.pyisinstance(pyptr, LibPython.PyLong_Type)
|
48
|
+
# TODO: should make Bignum
|
49
|
+
|
50
|
+
when PyCall::Types.pyisinstance(pyptr, LibPython.PyFloat_Type)
|
51
|
+
return Conversions.convert_to_float(pyptr)
|
52
|
+
|
53
|
+
when PyCall::Types.pyisinstance(pyptr, LibPython.PyComplex_Type)
|
54
|
+
return Conversions.convert_to_complex(pyptr)
|
55
|
+
|
56
|
+
when PyCall::Types.pyisinstance(pyptr, LibPython.PyString_Type)
|
57
|
+
return Conversions.convert_to_string(pyptr)
|
58
|
+
|
59
|
+
when PyCall::Types.pyisinstance(pyptr, LibPython.PyUnicode_Type)
|
60
|
+
py_str_ptr = LibPython.PyUnicode_AsUTF8String(pyptr)
|
61
|
+
return Conversions.convert_to_string(py_str_ptr).force_encoding(Encoding::UTF_8)
|
62
|
+
|
63
|
+
when PyCall::Types.pyisinstance(pyptr, LibPython.PyList_Type)
|
64
|
+
return PyCall::List.new(pyptr)
|
65
|
+
|
66
|
+
when PyCall::Types.pyisinstance(pyptr, LibPython.PyTuple_Type)
|
67
|
+
return Conversions.convert_to_tuple(pyptr)
|
68
|
+
|
69
|
+
when PyCall::Types.pyisinstance(pyptr, LibPython.PyDict_Type)
|
70
|
+
return PyCall::Dict.new(pyptr)
|
71
|
+
|
72
|
+
when PyCall::Types.pyisinstance(pyptr, LibPython.PySet_Type)
|
73
|
+
return PyCall::Set.new(pyptr)
|
74
|
+
end
|
75
|
+
|
76
|
+
pyobj = PyObject.new(pyptr)
|
31
77
|
each_type_pair do |tp|
|
32
78
|
pytype, rbtype = tp.to_a
|
33
79
|
next unless pyobj.kind_of?(pytype)
|
@@ -67,6 +113,8 @@ module PyCall
|
|
67
113
|
from_ruby(obj.to_s)
|
68
114
|
when Array
|
69
115
|
PyCall::List.new(obj).__pyobj__
|
116
|
+
when Hash
|
117
|
+
PyCall::Dict.new(obj).__pyobj__
|
70
118
|
else
|
71
119
|
PyCall.None
|
72
120
|
end
|
@@ -119,47 +167,6 @@ module PyCall
|
|
119
167
|
|
120
168
|
class LibPython::PyObjectStruct
|
121
169
|
def to_ruby
|
122
|
-
return nil if self.null? || PyCall.none?(self)
|
123
|
-
|
124
|
-
case
|
125
|
-
when PyCall::Types.pyisinstance(self, LibPython.PyType_Type)
|
126
|
-
return TypeObject.new(self)
|
127
|
-
|
128
|
-
when PyCall::Types.pyisinstance(self, LibPython.PyBool_Type)
|
129
|
-
return Conversions.convert_to_boolean(self)
|
130
|
-
|
131
|
-
when PyCall::Types.pyisinstance(self, LibPython.PyInt_Type)
|
132
|
-
return Conversions.convert_to_integer(self)
|
133
|
-
|
134
|
-
when PyCall::Types.pyisinstance(self, LibPython.PyLong_Type)
|
135
|
-
# TODO: should make Bignum
|
136
|
-
|
137
|
-
when PyCall::Types.pyisinstance(self, LibPython.PyFloat_Type)
|
138
|
-
return Conversions.convert_to_float(self)
|
139
|
-
|
140
|
-
when PyCall::Types.pyisinstance(self, LibPython.PyComplex_Type)
|
141
|
-
return Conversions.convert_to_complex(self)
|
142
|
-
|
143
|
-
when PyCall::Types.pyisinstance(self, LibPython.PyString_Type)
|
144
|
-
return Conversions.convert_to_string(self)
|
145
|
-
|
146
|
-
when PyCall::Types.pyisinstance(self, LibPython.PyUnicode_Type)
|
147
|
-
py_str_ptr = LibPython.PyUnicode_AsUTF8String(self)
|
148
|
-
return Conversions.convert_to_string(py_str_ptr).force_encoding(Encoding::UTF_8)
|
149
|
-
|
150
|
-
when PyCall::Types.pyisinstance(self, LibPython.PyList_Type)
|
151
|
-
return PyCall::List.new(self)
|
152
|
-
|
153
|
-
when PyCall::Types.pyisinstance(self, LibPython.PyTuple_Type)
|
154
|
-
return Conversions.convert_to_tuple(self)
|
155
|
-
|
156
|
-
when PyCall::Types.pyisinstance(self, LibPython.PyDict_Type)
|
157
|
-
return PyCall::Dict.new(self)
|
158
|
-
|
159
|
-
when PyCall::Types.pyisinstance(self, LibPython.PySet_Type)
|
160
|
-
return PyCall::Set.new(self)
|
161
|
-
end
|
162
|
-
|
163
170
|
Conversions.to_ruby(self)
|
164
171
|
end
|
165
172
|
end
|
data/lib/pycall/pyerror.rb
CHANGED
@@ -7,9 +7,9 @@ module PyCall
|
|
7
7
|
ptraceback = ptrs + 2 * ptrs.type_size
|
8
8
|
LibPython.PyErr_Fetch(ptype, pvalue, ptraceback)
|
9
9
|
LibPython.PyErr_NormalizeException(ptype, pvalue, ptraceback)
|
10
|
-
type = Conversions.to_ruby(
|
11
|
-
value = Conversions.to_ruby(
|
12
|
-
traceback = Conversions.to_ruby(
|
10
|
+
type = Conversions.to_ruby(ptype.read(:pointer))
|
11
|
+
value = Conversions.to_ruby(pvalue.read(:pointer))
|
12
|
+
traceback = Conversions.to_ruby(ptraceback.read(:pointer))
|
13
13
|
new(type, value, traceback)
|
14
14
|
end
|
15
15
|
|
@@ -22,9 +22,9 @@ module PyCall
|
|
22
22
|
|
23
23
|
attr_reader :type, :value, :traceback
|
24
24
|
|
25
|
-
def
|
25
|
+
def to_s
|
26
26
|
"#{type}: #{value}".tap do |msg|
|
27
|
-
unless traceback
|
27
|
+
unless traceback&.null?
|
28
28
|
if (o = PyCall.format_traceback(traceback))
|
29
29
|
msg.concat("\n", *o)
|
30
30
|
end
|
@@ -49,6 +49,7 @@ module PyCall
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def initialize(pyobj)
|
52
|
+
pyobj = LibPython::PyObjectStruct.new(pyobj) if pyobj.kind_of? FFI::Pointer
|
52
53
|
pyobj = pyobj.__pyobj__ unless pyobj.kind_of? LibPython::PyObjectStruct
|
53
54
|
@__pyobj__ = pyobj
|
54
55
|
end
|
@@ -86,8 +87,7 @@ module PyCall
|
|
86
87
|
opcode = RICH_COMPARISON_OPCODES[op]
|
87
88
|
raise ArgumentError, "Unknown comparison op: #{op}" unless opcode
|
88
89
|
|
89
|
-
other =
|
90
|
-
other = Conversions.from_ruby(other) unless other.kind_of?(LibPython::PyObjectStruct)
|
90
|
+
other = Conversions.from_ruby(other)
|
91
91
|
return other.null? if __pyobj__.null?
|
92
92
|
return false if other.null?
|
93
93
|
|
data/lib/pycall/types.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
module PyCall
|
2
2
|
module Types
|
3
3
|
def self.pyisinstance(pyobj, pytype)
|
4
|
+
pyobj = LibPython::PyObjectStruct.new(pyobj) if pyobj.kind_of? FFI::Pointer
|
4
5
|
pyobj = pyobj.__pyobj__ unless pyobj.kind_of? LibPython::PyObjectStruct
|
6
|
+
|
7
|
+
pytype = LibPython::PyObjectStruct.new(pytype) if pytype.kind_of? FFI::Pointer
|
5
8
|
pytype = ptype.__pyobj__ unless pytype.kind_of? LibPython::PyObjectStruct
|
9
|
+
|
6
10
|
LibPython.PyObject_IsInstance(pyobj, pytype) == 1
|
7
11
|
end
|
8
12
|
|
data/lib/pycall/utils.rb
CHANGED
@@ -46,8 +46,15 @@ module PyCall
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def none?(pyobj)
|
49
|
-
|
50
|
-
|
49
|
+
case pyobj
|
50
|
+
when FFI::Pointer
|
51
|
+
ptr = pyobj
|
52
|
+
when LibPython::PyObjectStruct
|
53
|
+
ptr = pyobj.to_ptr
|
54
|
+
else
|
55
|
+
pyobj = pyobj.__pyobj__.to_ptr
|
56
|
+
end
|
57
|
+
ptr == self.None.to_ptr
|
51
58
|
end
|
52
59
|
|
53
60
|
def slice(*args)
|
data/lib/pycall/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pycall
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.alpha.
|
4
|
+
version: 0.1.0.alpha.20170311
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenta Murata
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ffi
|