pycall 0.1.0.alpha.20170307 → 0.1.0.alpha.20170308

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bff90008a927b2cb383fe47fa5b7c9e4447c01ec
4
- data.tar.gz: 4de66d04dad27fc929fe5071bc9ce8bcf06019cd
3
+ metadata.gz: 438d4095d0ef876b3a9593ee86a5608851cc5384
4
+ data.tar.gz: 044d40dcceed8e34a5c3a701a60fa7baf7879d5f
5
5
  SHA512:
6
- metadata.gz: 9fbb882c47f5a771f58b83e56e92deae332b5ca5348bc205594c0bbc5f4c3141d13bb121459b320790d1bdda2e98134323bd70498a94cf7650bb71d377be8128
7
- data.tar.gz: de2fd8a4eceaa84b3ce58918f4fd15cd19b5f3f8256ab1e44abdb32c3e48eeed8527ee853f08b828aadb78635fb0c3b2552a75a1da297b3f45c52a3399fd0226
6
+ metadata.gz: 9928a513029558ef850a99c209ab9a821037a9473406f5f6159d873d535a72ca9869ccd82a241444bcf64a25909b1c6c41e1b2a4393a7cd89645f3063efb9a7e
7
+ data.tar.gz: e04f844e7d2d239556ca4e32c82a6ea4e8a40bc7f53dcd9bf30700bdd02269abed6c63a81d060a36954d87897db170021c0da4fee7a80740eace43df65c4b023
@@ -27,9 +27,7 @@ module PyCall
27
27
  end
28
28
 
29
29
  def self.to_ruby(pyobj)
30
- unless pyobj.kind_of? PyObject
31
- raise
32
- end
30
+ pyobj = PyObject.new(pyobj) if pyobj.kind_of? LibPython::PyObjectStruct
33
31
  each_type_pair do |tp|
34
32
  pytype, rbtype = tp.to_a
35
33
  next unless pyobj.kind_of?(pytype)
@@ -47,9 +45,7 @@ module PyCall
47
45
 
48
46
  def self.from_ruby(obj)
49
47
  case obj
50
- when PyObject
51
- obj
52
- when PyObjectWrapper
48
+ when PyObject, PyObjectWrapper
53
49
  obj.__pyobj__
54
50
  when TrueClass, FalseClass
55
51
  LibPython.PyBool_FromLong(obj ? 1 : 0)
@@ -119,43 +115,46 @@ module PyCall
119
115
  end
120
116
  end
121
117
 
122
- class PyObject
118
+ class LibPython::PyObjectStruct
123
119
  def to_ruby
124
- return nil if self.null? || self.py_none?
120
+ return nil if self.null? || PyCall.none?(self)
121
+
122
+ case
123
+ when PyCall::Types.pyisinstance(self, LibPython.PyType_Type)
124
+ return TypeObject.new(self)
125
125
 
126
- case self
127
- when LibPython.PyBool_Type
126
+ when PyCall::Types.pyisinstance(self, LibPython.PyBool_Type)
128
127
  return Conversions.convert_to_boolean(self)
129
128
 
130
- when LibPython.PyInt_Type
129
+ when PyCall::Types.pyisinstance(self, LibPython.PyInt_Type)
131
130
  return Conversions.convert_to_integer(self)
132
131
 
133
- when LibPython.PyLong_Type
132
+ when PyCall::Types.pyisinstance(self, LibPython.PyLong_Type)
134
133
  # TODO: should make Bignum
135
134
 
136
- when LibPython.PyFloat_Type
135
+ when PyCall::Types.pyisinstance(self, LibPython.PyFloat_Type)
137
136
  return Conversions.convert_to_float(self)
138
137
 
139
- when LibPython.PyComplex_Type
138
+ when PyCall::Types.pyisinstance(self, LibPython.PyComplex_Type)
140
139
  return Conversions.convert_to_complex(self)
141
140
 
142
- when LibPython.PyString_Type
141
+ when PyCall::Types.pyisinstance(self, LibPython.PyString_Type)
143
142
  return Conversions.convert_to_string(self)
144
143
 
145
- when LibPython.PyUnicode_Type
144
+ when PyCall::Types.pyisinstance(self, LibPython.PyUnicode_Type)
146
145
  py_str_ptr = LibPython.PyUnicode_AsUTF8String(self)
147
146
  return Conversions.convert_to_string(py_str_ptr).force_encoding(Encoding::UTF_8)
148
147
 
149
- when LibPython.PyList_Type
148
+ when PyCall::Types.pyisinstance(self, LibPython.PyList_Type)
150
149
  return PyCall::List.new(self)
151
150
 
152
- when LibPython.PyTuple_Type
151
+ when PyCall::Types.pyisinstance(self, LibPython.PyTuple_Type)
153
152
  return Conversions.convert_to_tuple(self)
154
153
 
155
- when LibPython.PyDict_Type
154
+ when PyCall::Types.pyisinstance(self, LibPython.PyDict_Type)
156
155
  return PyCall::Dict.new(self)
157
156
 
158
- when LibPython.PySet_Type
157
+ when PyCall::Types.pyisinstance(self, LibPython.PySet_Type)
159
158
  return PyCall::Set.new(self)
160
159
  end
161
160
 
data/lib/pycall/dict.rb CHANGED
@@ -4,7 +4,7 @@ module PyCall
4
4
 
5
5
  def self.new(init=nil)
6
6
  case init
7
- when PyObject
7
+ when LibPython::PyObjectStruct
8
8
  super
9
9
  when nil
10
10
  new(LibPython.PyDict_New())
@@ -19,10 +19,6 @@ module PyCall
19
19
  end
20
20
  end
21
21
 
22
- def initialize(pyobj)
23
- super(pyobj, LibPython.PyDict_Type)
24
- end
25
-
26
22
  def [](key)
27
23
  key = key.to_s if key.is_a? Symbol
28
24
  value = if key.is_a? String
@@ -32,7 +28,7 @@ module PyCall
32
28
  end
33
29
  ensure
34
30
  case value
35
- when PyObject
31
+ when LibPython::PyObjectStruct
36
32
  PyCall.incref(value)
37
33
  when PyObjectWrapper
38
34
  PyCall.incref(value.__pyobj__)
@@ -42,6 +38,7 @@ module PyCall
42
38
  def []=(key, value)
43
39
  key = key.to_s if key.is_a? Symbol
44
40
  value = Conversions.from_ruby(value)
41
+ value = value.__pyobj__ unless value.kind_of? LibPython::PyObjectStruct
45
42
  if key.is_a? String
46
43
  LibPython.PyDict_SetItemString(__pyobj__, key, value)
47
44
  else
data/lib/pycall/eval.rb CHANGED
@@ -3,8 +3,8 @@ module PyCall
3
3
  Py_eval_input = 258
4
4
 
5
5
  def self.eval(str, filename: "pycall")
6
- globals_ptr = main_dict
7
- locals_ptr = main_dict
6
+ globals_ptr = main_dict.__pyobj__
7
+ locals_ptr = main_dict.__pyobj__
8
8
  defer_sigint do
9
9
  py_code_ptr = LibPython.Py_CompileString(str, filename, Py_eval_input)
10
10
  raise PyError.fetch if py_code_ptr.null?
@@ -17,7 +17,7 @@ module PyCall
17
17
 
18
18
  def main_dict
19
19
  @main_dict ||= PyCall.import_module("__main__") do |main_module|
20
- PyCall.incref(LibPython.PyModule_GetDict(main_module))
20
+ PyCall.incref(LibPython.PyModule_GetDict(main_module.__pyobj__)).to_ruby
21
21
  end
22
22
  end
23
23
 
@@ -38,7 +38,7 @@ module PyCall
38
38
  begin
39
39
  yield value
40
40
  ensure
41
- PyCall.decref(value)
41
+ PyCall.decref(value.__pyobj__)
42
42
  end
43
43
  end
44
44
 
@@ -1,20 +1,28 @@
1
1
  require 'ffi'
2
2
 
3
3
  module PyCall
4
- class PyObject < FFI::Struct
5
- layout ob_refcnt: :ssize_t,
6
- ob_type: :pointer
7
- end
8
-
9
- class PyTypeObject < FFI::Struct
10
- layout ob_base: PyObject,
11
- ob_size: :ssize_t,
12
- tp_name: :string
13
- end
14
-
15
4
  module LibPython
16
5
  extend FFI::Library
17
6
 
7
+ class PyObjectStruct < FFI::Struct
8
+ layout ob_refcnt: :ssize_t,
9
+ ob_type: PyObjectStruct.by_ref
10
+
11
+ def self.null
12
+ new(FFI::Pointer::NULL)
13
+ end
14
+
15
+ def py_none?
16
+ PyCall.none?(self)
17
+ end
18
+
19
+ def kind_of?(klass)
20
+ klass = klass.__pyobj__ if klass.kind_of? PyObjectWrapper
21
+ return super unless klass.kind_of? PyObjectStruct
22
+ PyCall::Types.pyisinstance(self, klass)
23
+ end
24
+ end
25
+
18
26
  private_class_method
19
27
 
20
28
  def self.find_libpython(python = nil)
@@ -95,41 +103,43 @@ module PyCall
95
103
 
96
104
  # --- global variables ---
97
105
 
98
- attach_variable :_Py_NoneStruct, PyObject
106
+ attach_variable :_Py_NoneStruct, PyObjectStruct
99
107
 
100
108
  def self.Py_None
101
109
  _Py_NoneStruct
102
110
  end
103
111
 
112
+ attach_variable :PyType_Type, PyObjectStruct
113
+
104
114
  if libpython.find_variable('PyInt_Type')
105
115
  has_PyInt_Type = true
106
- attach_variable :PyInt_Type, PyTypeObject
116
+ attach_variable :PyInt_Type, PyObjectStruct
107
117
  else
108
118
  has_PyInt_Type = false
109
- attach_variable :PyInt_Type, :PyLong_Type, PyTypeObject
119
+ attach_variable :PyInt_Type, :PyLong_Type, PyObjectStruct
110
120
  end
111
121
 
112
- attach_variable :PyLong_Type, PyTypeObject
113
- attach_variable :PyBool_Type, PyTypeObject
114
- attach_variable :PyFloat_Type, PyTypeObject
115
- attach_variable :PyComplex_Type, PyTypeObject
116
- attach_variable :PyUnicode_Type, PyTypeObject
122
+ attach_variable :PyLong_Type, PyObjectStruct
123
+ attach_variable :PyBool_Type, PyObjectStruct
124
+ attach_variable :PyFloat_Type, PyObjectStruct
125
+ attach_variable :PyComplex_Type, PyObjectStruct
126
+ attach_variable :PyUnicode_Type, PyObjectStruct
117
127
 
118
128
  if libpython.find_symbol('PyString_FromStringAndSize')
119
129
  string_as_bytes = false
120
- attach_variable :PyString_Type, PyTypeObject
130
+ attach_variable :PyString_Type, PyObjectStruct
121
131
  else
122
132
  string_as_bytes = true
123
- attach_variable :PyString_Type, :PyBytes_Type, PyTypeObject
133
+ attach_variable :PyString_Type, :PyBytes_Type, PyObjectStruct
124
134
  end
125
135
 
126
- attach_variable :PyList_Type, PyTypeObject
127
- attach_variable :PyTuple_Type, PyTypeObject
128
- attach_variable :PyDict_Type, PyTypeObject
129
- attach_variable :PySet_Type, PyTypeObject
136
+ attach_variable :PyList_Type, PyObjectStruct
137
+ attach_variable :PyTuple_Type, PyObjectStruct
138
+ attach_variable :PyDict_Type, PyObjectStruct
139
+ attach_variable :PySet_Type, PyObjectStruct
130
140
 
131
- attach_variable :PyFunction_Type, PyTypeObject
132
- attach_variable :PyMethod_Type, PyTypeObject
141
+ attach_variable :PyFunction_Type, PyObjectStruct
142
+ attach_variable :PyMethod_Type, PyObjectStruct
133
143
 
134
144
  # --- functions ---
135
145
 
@@ -139,67 +149,67 @@ module PyCall
139
149
 
140
150
  # Reference count
141
151
 
142
- attach_function :Py_IncRef, [PyObject.by_ref], :void
143
- attach_function :Py_DecRef, [PyObject.by_ref], :void
152
+ attach_function :Py_IncRef, [PyObjectStruct.by_ref], :void
153
+ attach_function :Py_DecRef, [PyObjectStruct.by_ref], :void
144
154
 
145
155
  # Object
146
156
 
147
- attach_function :PyObject_RichCompare, [PyObject.by_ref, PyObject.by_ref, :int], PyObject.by_ref
148
- attach_function :PyObject_GetAttrString, [PyObject.by_ref, :string], PyObject.by_ref
149
- attach_function :PyObject_SetAttrString, [PyObject.by_ref, :string, PyObject.by_ref], :int
150
- attach_function :PyObject_HasAttrString, [PyObject.by_ref, :string], :int
151
- attach_function :PyObject_GetItem, [PyObject.by_ref, PyObject.by_ref], PyObject.by_ref
152
- attach_function :PyObject_SetItem, [PyObject.by_ref, PyObject.by_ref, PyObject.by_ref], :int
153
- attach_function :PyObject_DelItem, [PyObject.by_ref, PyObject.by_ref], :int
154
- attach_function :PyObject_Call, [PyObject.by_ref, PyObject.by_ref, PyObject.by_ref], PyObject.by_ref
155
- attach_function :PyObject_IsInstance, [PyObject.by_ref, PyObject.by_ref], :int
156
- attach_function :PyObject_Dir, [PyObject.by_ref], PyObject.by_ref
157
- attach_function :PyObject_Repr, [PyObject.by_ref], PyObject.by_ref
158
- attach_function :PyObject_Str, [PyObject.by_ref], PyObject.by_ref
159
- attach_function :PyObject_Type, [PyObject.by_ref], PyTypeObject.by_ref
160
- attach_function :PyCallable_Check, [PyObject.by_ref], :int
157
+ attach_function :PyObject_RichCompare, [PyObjectStruct.by_ref, PyObjectStruct.by_ref, :int], PyObjectStruct.by_ref
158
+ attach_function :PyObject_GetAttrString, [PyObjectStruct.by_ref, :string], PyObjectStruct.by_ref
159
+ attach_function :PyObject_SetAttrString, [PyObjectStruct.by_ref, :string, PyObjectStruct.by_ref], :int
160
+ attach_function :PyObject_HasAttrString, [PyObjectStruct.by_ref, :string], :int
161
+ attach_function :PyObject_GetItem, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], PyObjectStruct.by_ref
162
+ attach_function :PyObject_SetItem, [PyObjectStruct.by_ref, PyObjectStruct.by_ref, PyObjectStruct.by_ref], :int
163
+ attach_function :PyObject_DelItem, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], :int
164
+ attach_function :PyObject_Call, [PyObjectStruct.by_ref, PyObjectStruct.by_ref, PyObjectStruct.by_ref], PyObjectStruct.by_ref
165
+ attach_function :PyObject_IsInstance, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], :int
166
+ attach_function :PyObject_Dir, [PyObjectStruct.by_ref], PyObjectStruct.by_ref
167
+ attach_function :PyObject_Repr, [PyObjectStruct.by_ref], PyObjectStruct.by_ref
168
+ attach_function :PyObject_Str, [PyObjectStruct.by_ref], PyObjectStruct.by_ref
169
+ attach_function :PyObject_Type, [PyObjectStruct.by_ref], PyObjectStruct.by_ref
170
+ attach_function :PyCallable_Check, [PyObjectStruct.by_ref], :int
161
171
 
162
172
  # Bool
163
173
 
164
- attach_function :PyBool_FromLong, [:long], PyObject.by_ref
174
+ attach_function :PyBool_FromLong, [:long], PyObjectStruct.by_ref
165
175
 
166
176
  # Integer
167
177
 
168
178
  if has_PyInt_Type
169
- attach_function :PyInt_AsSsize_t, [PyObject.by_ref], :ssize_t
179
+ attach_function :PyInt_AsSsize_t, [PyObjectStruct.by_ref], :ssize_t
170
180
  else
171
- attach_function :PyInt_AsSsize_t, :PyLong_AsSsize_t, [PyObject.by_ref], :ssize_t
181
+ attach_function :PyInt_AsSsize_t, :PyLong_AsSsize_t, [PyObjectStruct.by_ref], :ssize_t
172
182
  end
173
183
 
174
184
  if has_PyInt_Type
175
- attach_function :PyInt_FromSsize_t, [:ssize_t], PyObject.by_ref
185
+ attach_function :PyInt_FromSsize_t, [:ssize_t], PyObjectStruct.by_ref
176
186
  else
177
- attach_function :PyInt_FromSsize_t, :PyLong_FromSsize_t, [:ssize_t], PyObject.by_ref
187
+ attach_function :PyInt_FromSsize_t, :PyLong_FromSsize_t, [:ssize_t], PyObjectStruct.by_ref
178
188
  end
179
189
 
180
190
  # Float
181
191
 
182
- attach_function :PyFloat_FromDouble, [:double], PyObject.by_ref
183
- attach_function :PyFloat_AsDouble, [PyObject.by_ref], :double
192
+ attach_function :PyFloat_FromDouble, [:double], PyObjectStruct.by_ref
193
+ attach_function :PyFloat_AsDouble, [PyObjectStruct.by_ref], :double
184
194
 
185
195
  # Complex
186
196
 
187
- attach_function :PyComplex_RealAsDouble, [PyObject.by_ref], :double
188
- attach_function :PyComplex_ImagAsDouble, [PyObject.by_ref], :double
197
+ attach_function :PyComplex_RealAsDouble, [PyObjectStruct.by_ref], :double
198
+ attach_function :PyComplex_ImagAsDouble, [PyObjectStruct.by_ref], :double
189
199
 
190
200
  # String
191
201
 
192
202
  if string_as_bytes
193
- attach_function :PyString_FromStringAndSize, :PyBytes_FromStringAndSize, [:string, :ssize_t], PyObject.by_ref
203
+ attach_function :PyString_FromStringAndSize, :PyBytes_FromStringAndSize, [:string, :ssize_t], PyObjectStruct.by_ref
194
204
  else
195
- attach_function :PyString_FromStringAndSize, [:string, :ssize_t], PyObject.by_ref
205
+ attach_function :PyString_FromStringAndSize, [:string, :ssize_t], PyObjectStruct.by_ref
196
206
  end
197
207
 
198
208
  # PyString_AsStringAndSize :: (PyPtr, char**, int*) -> int
199
209
  if string_as_bytes
200
- attach_function :PyString_AsStringAndSize, :PyBytes_AsStringAndSize, [PyObject.by_ref, :pointer, :pointer], :int
210
+ attach_function :PyString_AsStringAndSize, :PyBytes_AsStringAndSize, [PyObjectStruct.by_ref, :pointer, :pointer], :int
201
211
  else
202
- attach_function :PyString_AsStringAndSize, [PyObject.by_ref, :pointer, :pointer], :int
212
+ attach_function :PyString_AsStringAndSize, [PyObjectStruct.by_ref, :pointer, :pointer], :int
203
213
  end
204
214
 
205
215
  # Unicode
@@ -207,96 +217,96 @@ module PyCall
207
217
  # PyUnicode_DecodeUTF8
208
218
  case
209
219
  when libpython.find_symbol('PyUnicode_DecodeUTF8')
210
- attach_function :PyUnicode_DecodeUTF8, [:string, :ssize_t, :string], PyObject.by_ref
220
+ attach_function :PyUnicode_DecodeUTF8, [:string, :ssize_t, :string], PyObjectStruct.by_ref
211
221
  when libpython.find_symbol('PyUnicodeUCS4_DecodeUTF8')
212
- attach_function :PyUnicode_DecodeUTF8, :PyUnicodeUCS4_DecodeUTF8, [:string, :ssize_t, :string], PyObject.by_ref
222
+ attach_function :PyUnicode_DecodeUTF8, :PyUnicodeUCS4_DecodeUTF8, [:string, :ssize_t, :string], PyObjectStruct.by_ref
213
223
  when libpython.find_symbol('PyUnicodeUCS2_DecodeUTF8')
214
- attach_function :PyUnicode_DecodeUTF8, :PyUnicodeUCS2_DecodeUTF8, [:string, :ssize_t, :string], PyObject.by_ref
224
+ attach_function :PyUnicode_DecodeUTF8, :PyUnicodeUCS2_DecodeUTF8, [:string, :ssize_t, :string], PyObjectStruct.by_ref
215
225
  end
216
226
 
217
227
  # PyUnicode_AsUTF8String
218
228
  case
219
229
  when libpython.find_symbol('PyUnicode_AsUTF8String')
220
- attach_function :PyUnicode_AsUTF8String, [PyObject.by_ref], PyObject.by_ref
230
+ attach_function :PyUnicode_AsUTF8String, [PyObjectStruct.by_ref], PyObjectStruct.by_ref
221
231
  when libpython.find_symbol('PyUnicodeUCS4_AsUTF8String')
222
- attach_function :PyUnicode_AsUTF8String, :PyUnicodeUCS4_AsUTF8String, [PyObject.by_ref], PyObject.by_ref
232
+ attach_function :PyUnicode_AsUTF8String, :PyUnicodeUCS4_AsUTF8String, [PyObjectStruct.by_ref], PyObjectStruct.by_ref
223
233
  when libpython.find_symbol('PyUnicodeUCS2_AsUTF8String')
224
- attach_function :PyUnicode_AsUTF8String, :PyUnicodeUCS2_AsUTF8String, [PyObject.by_ref], PyObject.by_ref
234
+ attach_function :PyUnicode_AsUTF8String, :PyUnicodeUCS2_AsUTF8String, [PyObjectStruct.by_ref], PyObjectStruct.by_ref
225
235
  end
226
236
 
227
237
  # Tuple
228
238
 
229
- attach_function :PyTuple_New, [:ssize_t], PyObject.by_ref
230
- attach_function :PyTuple_GetItem, [PyObject.by_ref, :ssize_t], PyObject.by_ref
231
- attach_function :PyTuple_SetItem, [PyObject.by_ref, :ssize_t, PyObject.by_ref], :int
232
- attach_function :PyTuple_Size, [PyObject.by_ref], :ssize_t
239
+ attach_function :PyTuple_New, [:ssize_t], PyObjectStruct.by_ref
240
+ attach_function :PyTuple_GetItem, [PyObjectStruct.by_ref, :ssize_t], PyObjectStruct.by_ref
241
+ attach_function :PyTuple_SetItem, [PyObjectStruct.by_ref, :ssize_t, PyObjectStruct.by_ref], :int
242
+ attach_function :PyTuple_Size, [PyObjectStruct.by_ref], :ssize_t
233
243
 
234
244
  # Slice
235
245
 
236
- attach_function :PySlice_New, [PyObject.by_ref, PyObject.by_ref, PyObject.by_ref], PyObject.by_ref
246
+ attach_function :PySlice_New, [PyObjectStruct.by_ref, PyObjectStruct.by_ref, PyObjectStruct.by_ref], PyObjectStruct.by_ref
237
247
 
238
248
  # List
239
249
 
240
- attach_function :PyList_New, [:ssize_t], PyObject.by_ref
241
- attach_function :PyList_Size, [PyObject.by_ref], :ssize_t
242
- attach_function :PyList_GetItem, [PyObject.by_ref, :ssize_t], PyObject.by_ref
243
- attach_function :PyList_SetItem, [PyObject.by_ref, :ssize_t, PyObject.by_ref], :int
244
- attach_function :PyList_Append, [PyObject.by_ref, PyObject.by_ref], :int
250
+ attach_function :PyList_New, [:ssize_t], PyObjectStruct.by_ref
251
+ attach_function :PyList_Size, [PyObjectStruct.by_ref], :ssize_t
252
+ attach_function :PyList_GetItem, [PyObjectStruct.by_ref, :ssize_t], PyObjectStruct.by_ref
253
+ attach_function :PyList_SetItem, [PyObjectStruct.by_ref, :ssize_t, PyObjectStruct.by_ref], :int
254
+ attach_function :PyList_Append, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], :int
245
255
 
246
256
  # Sequence
247
257
 
248
- attach_function :PySequence_Size, [PyObject.by_ref], :ssize_t
249
- attach_function :PySequence_GetItem, [PyObject.by_ref, :ssize_t], PyObject.by_ref
250
- attach_function :PySequence_Contains, [PyObject.by_ref, PyObject.by_ref], :int
258
+ attach_function :PySequence_Size, [PyObjectStruct.by_ref], :ssize_t
259
+ attach_function :PySequence_GetItem, [PyObjectStruct.by_ref, :ssize_t], PyObjectStruct.by_ref
260
+ attach_function :PySequence_Contains, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], :int
251
261
 
252
262
  # Dict
253
263
 
254
- attach_function :PyDict_New, [], PyObject.by_ref
255
- attach_function :PyDict_GetItem, [PyObject.by_ref, PyObject.by_ref], PyObject.by_ref
256
- attach_function :PyDict_GetItemString, [PyObject.by_ref, :string], PyObject.by_ref
257
- attach_function :PyDict_SetItem, [PyObject.by_ref, PyObject.by_ref, PyObject.by_ref], :int
258
- attach_function :PyDict_SetItemString, [PyObject.by_ref, :string, PyObject.by_ref], :int
259
- attach_function :PyDict_DelItem, [PyObject.by_ref, PyObject.by_ref], :int
260
- attach_function :PyDict_DelItem, [PyObject.by_ref, :string], :int
261
- attach_function :PyDict_Size, [PyObject.by_ref], :ssize_t
262
- attach_function :PyDict_Keys, [PyObject.by_ref], PyObject.by_ref
263
- attach_function :PyDict_Values, [PyObject.by_ref], PyObject.by_ref
264
- attach_function :PyDict_Items, [PyObject.by_ref], PyObject.by_ref
265
- attach_function :PyDict_Contains, [PyObject.by_ref, PyObject.by_ref], :int
264
+ attach_function :PyDict_New, [], PyObjectStruct.by_ref
265
+ attach_function :PyDict_GetItem, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], PyObjectStruct.by_ref
266
+ attach_function :PyDict_GetItemString, [PyObjectStruct.by_ref, :string], PyObjectStruct.by_ref
267
+ attach_function :PyDict_SetItem, [PyObjectStruct.by_ref, PyObjectStruct.by_ref, PyObjectStruct.by_ref], :int
268
+ attach_function :PyDict_SetItemString, [PyObjectStruct.by_ref, :string, PyObjectStruct.by_ref], :int
269
+ attach_function :PyDict_DelItem, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], :int
270
+ attach_function :PyDict_DelItem, [PyObjectStruct.by_ref, :string], :int
271
+ attach_function :PyDict_Size, [PyObjectStruct.by_ref], :ssize_t
272
+ attach_function :PyDict_Keys, [PyObjectStruct.by_ref], PyObjectStruct.by_ref
273
+ attach_function :PyDict_Values, [PyObjectStruct.by_ref], PyObjectStruct.by_ref
274
+ attach_function :PyDict_Items, [PyObjectStruct.by_ref], PyObjectStruct.by_ref
275
+ attach_function :PyDict_Contains, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], :int
266
276
 
267
277
  # Set
268
278
 
269
- attach_function :PySet_Size, [PyObject.by_ref], :ssize_t
270
- attach_function :PySet_Contains, [PyObject.by_ref, PyObject.by_ref], :int
271
- attach_function :PySet_Add, [PyObject.by_ref, PyObject.by_ref], :int
272
- attach_function :PySet_Discard, [PyObject.by_ref, PyObject.by_ref], :int
279
+ attach_function :PySet_Size, [PyObjectStruct.by_ref], :ssize_t
280
+ attach_function :PySet_Contains, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], :int
281
+ attach_function :PySet_Add, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], :int
282
+ attach_function :PySet_Discard, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], :int
273
283
 
274
284
  # Module
275
285
 
276
- attach_function :PyModule_GetDict, [PyObject.by_ref], PyObject.by_ref
286
+ attach_function :PyModule_GetDict, [PyObjectStruct.by_ref], PyObjectStruct.by_ref
277
287
 
278
288
  # Import
279
289
 
280
- attach_function :PyImport_ImportModule, [:string], PyObject.by_ref
290
+ attach_function :PyImport_ImportModule, [:string], PyObjectStruct.by_ref
281
291
 
282
292
  # Operators
283
293
 
284
- attach_function :PyNumber_Add, [PyObject.by_ref, PyObject.by_ref], PyObject.by_ref
285
- attach_function :PyNumber_Subtract, [PyObject.by_ref, PyObject.by_ref], PyObject.by_ref
286
- attach_function :PyNumber_Multiply, [PyObject.by_ref, PyObject.by_ref], PyObject.by_ref
287
- attach_function :PyNumber_TrueDivide, [PyObject.by_ref, PyObject.by_ref], PyObject.by_ref
288
- attach_function :PyNumber_Power, [PyObject.by_ref, PyObject.by_ref, PyObject.by_ref], PyObject.by_ref
294
+ attach_function :PyNumber_Add, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], PyObjectStruct.by_ref
295
+ attach_function :PyNumber_Subtract, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], PyObjectStruct.by_ref
296
+ attach_function :PyNumber_Multiply, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], PyObjectStruct.by_ref
297
+ attach_function :PyNumber_TrueDivide, [PyObjectStruct.by_ref, PyObjectStruct.by_ref], PyObjectStruct.by_ref
298
+ attach_function :PyNumber_Power, [PyObjectStruct.by_ref, PyObjectStruct.by_ref, PyObjectStruct.by_ref], PyObjectStruct.by_ref
289
299
 
290
300
  # Compiler
291
301
 
292
- attach_function :Py_CompileString, [:string, :string, :int], PyObject.by_ref
293
- attach_function :PyEval_EvalCode, [PyObject.by_ref, PyObject.by_ref, PyObject.by_ref], PyObject.by_ref
302
+ attach_function :Py_CompileString, [:string, :string, :int], PyObjectStruct.by_ref
303
+ attach_function :PyEval_EvalCode, [PyObjectStruct.by_ref, PyObjectStruct.by_ref, PyObjectStruct.by_ref], PyObjectStruct.by_ref
294
304
 
295
305
  # Error
296
306
 
297
307
  attach_function :PyErr_Clear, [], :void
298
308
  attach_function :PyErr_Print, [], :void
299
- attach_function :PyErr_Occurred, [], PyObject.by_ref
309
+ attach_function :PyErr_Occurred, [], PyObjectStruct.by_ref
300
310
  attach_function :PyErr_Fetch, [:pointer, :pointer, :pointer], :void
301
311
  attach_function :PyErr_NormalizeException, [:pointer, :pointer, :pointer], :void
302
312
 
data/lib/pycall/list.rb CHANGED
@@ -5,7 +5,7 @@ module PyCall
5
5
 
6
6
  def self.new(init=nil)
7
7
  case init
8
- when PyObject
8
+ when LibPython::PyObjectStruct
9
9
  super
10
10
  when nil
11
11
  new(0)
@@ -22,20 +22,6 @@ module PyCall
22
22
  end
23
23
  end
24
24
 
25
- def initialize(pyobj)
26
- super(pyobj, LibPython.PyList_Type)
27
- end
28
-
29
- def [](index)
30
- LibPython.PyList_GetItem(__pyobj__, index).to_ruby
31
- end
32
-
33
- def []=(index, value)
34
- value = Conversions.from_ruby(value)
35
- LibPython.PyList_SetItem(__pyobj__, index, value)
36
- value
37
- end
38
-
39
25
  def <<(value)
40
26
  value = Conversions.from_ruby(value)
41
27
  LibPython.PyList_Append(__pyobj__, value)
@@ -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 = PyTypeObject.new(ptype.read(:pointer))
11
- value = PyObject.new(pvalue.read(:pointer))
12
- traceback = PyObject.new(ptraceback.read(:pointer))
10
+ type = PyObject.new(LibPython::PyObjectStruct.new(ptype.read(:pointer)))
11
+ value = PyObject.new(LibPython::PyObjectStruct.new(pvalue.read(:pointer)))
12
+ traceback = PyObject.new(LibPython::PyObjectStruct.new(ptraceback.read(:pointer)))
13
13
  new(type, value, traceback)
14
14
  end
15
15
 
@@ -1,171 +1,15 @@
1
1
  module PyCall
2
- Py_LT = 0
3
- Py_LE = 1
4
- Py_EQ = 2
5
- Py_NE = 3
6
- Py_GT = 4
7
- Py_GE = 5
8
-
9
- RICH_COMPARISON_OPCODES = {
10
- :< => Py_LT,
11
- :<= => Py_LE,
12
- :== => Py_EQ,
13
- :!= => Py_NE,
14
- :> => Py_GT,
15
- :>= => Py_GE
16
- }.freeze
17
-
18
- module PyObjectMethods
19
- def rich_compare(other, op)
20
- opcode = RICH_COMPARISON_OPCODES[op]
21
- raise ArgumentError, "Unknown comparison op: #{op}" unless opcode
22
-
23
- other = Conversions.from_ruby(other) unless other.kind_of?(PyObject)
24
- return other.null? if self.null?
25
- return false if other.null?
26
-
27
- value = LibPython.PyObject_RichCompare(self, other, opcode)
28
- raise "Unable to compare: #{self} #{op} #{other}" if value.null?
29
- value.to_ruby
30
- end
31
-
32
- RICH_COMPARISON_OPCODES.keys.each do |op|
33
- define_method(op) {|other| rich_compare(other, op) }
34
- end
35
-
36
- def py_none?
37
- to_ptr == PyCall.None.to_ptr
38
- end
39
-
40
- def kind_of?(klass)
41
- case klass
42
- when PyObject, PyTypeObject
43
- Types.pyisinstance(self, klass)
44
- else
45
- super
46
- end
47
- end
48
- end
49
-
50
- class PyObject < FFI::Struct
51
- include PyObjectMethods
2
+ class PyObject
3
+ include PyObjectWrapper
52
4
 
53
5
  def self.null
54
- new(FFI::Pointer::NULL)
55
- end
56
-
57
- alias __aref__ []
58
- alias __aset__ []=
59
-
60
- def [](*indices)
61
- if indices.length == 1
62
- indices = indices[0]
63
- else
64
- indices = PyCall.tuple(*indices)
65
- end
66
- PyCall.getitem(self, indices)
67
- end
68
-
69
- def []=(*indices_and_value)
70
- value = indices_and_value.pop
71
- indices = indices_and_value
72
- if indices.length == 1
73
- indices = indices[0]
74
- else
75
- indices = PyCall.tuple(*indices)
76
- end
77
- PyCall.setitem(self, indices, value)
78
- end
79
-
80
- def +(other)
81
- other = Conversions.from_ruby(other)
82
- value = LibPython.PyNumber_Add(self, other)
83
- return value.to_ruby unless value.null?
84
- raise PyError.fetch
85
- end
86
-
87
- def -(other)
88
- other = Conversions.from_ruby(other)
89
- value = LibPython.PyNumber_Subtract(self, other)
90
- return value.to_ruby unless value.null?
91
- raise PyError.fetch
92
- end
93
-
94
- def *(other)
95
- other = Conversions.from_ruby(other)
96
- value = LibPython.PyNumber_Multiply(self, other)
97
- return value.to_ruby unless value.null?
98
- raise PyError.fetch
99
- end
100
-
101
- def /(other)
102
- other = Conversions.from_ruby(other)
103
- value = LibPython.PyNumber_TrueDivide(self, other)
104
- return value.to_ruby unless value.null?
105
- raise PyError.fetch
106
- end
107
-
108
- def **(other)
109
- other = Conversions.from_ruby(other)
110
- value = LibPython.PyNumber_Power(self, other, PyCall.None)
111
- return value.to_ruby unless value.null?
112
- raise PyError.fetch
113
- end
114
-
115
- def coerce(other)
116
- [Conversions.from_ruby(other), self]
117
- end
118
-
119
- def call(*args, **kwargs)
120
- args = PyCall::Tuple[*args]
121
- kwargs = kwargs.empty? ? PyObject.null : PyCall::Dict.new(kwargs).__pyobj__
122
- res = LibPython.PyObject_Call(self, args.__pyobj__, kwargs)
123
- return res.to_ruby if LibPython.PyErr_Occurred().null?
124
- raise PyError.fetch
125
- end
126
-
127
- def method_missing(name, *args, **kwargs)
128
- if PyCall.hasattr?(self, name)
129
- PyCall.getattr(self, name)
130
- else
131
- super
132
- end
133
- end
134
-
135
- def to_s
136
- s = LibPython.PyObject_Repr(self)
137
- if s.null?
138
- LibPython.PyErr_Clear()
139
- s = LibPython.PyObject_Str(self)
140
- if s.null?
141
- LibPython.PyErr_Clear()
142
- return super
143
- end
144
- end
145
- s.to_ruby
146
- end
147
-
148
- alias inspect to_s
149
-
150
- def type
151
- LibPython.PyObject_Type(self)
152
- end
153
- end
154
-
155
- class PyTypeObject < FFI::Struct
156
- include PyObjectMethods
157
-
158
- def ===(obj)
159
- obj.kind_of? self
160
- end
161
-
162
- def inspect
163
- "pytype(#{self[:tp_name]})"
6
+ new(LibPython::PyObjectStruct.new(FFI::Pointer::NULL))
164
7
  end
165
8
  end
166
9
 
167
10
  def self.getattr(pyobj, name, default=nil)
168
11
  name = check_attr_name(name)
12
+ pyobj = pyobj.__pyobj__ unless pyobj.kind_of? LibPython::PyObjectStruct
169
13
  value = LibPython.PyObject_GetAttrString(pyobj, name)
170
14
  if value.null?
171
15
  return default if default
@@ -177,12 +21,15 @@ module PyCall
177
21
  def self.setattr(pyobj, name, value)
178
22
  name = check_attr_name(name)
179
23
  value = Conversions.from_ruby(value)
24
+ value = value.__pyobj__ unless pyobj.kind_of? LibPython::PyObjectStruct
25
+ pyobj = pyobj.__pyobj__ unless pyobj.kind_of? LibPython::PyObjectStruct
180
26
  return self unless LibPython.PyObject_SetAttrString(pyobj, name, value) == -1
181
27
  raise PyError.fetch
182
28
  end
183
29
 
184
30
  def self.hasattr?(pyobj, name)
185
31
  name = check_attr_name(name)
32
+ pyobj = pyobj.__pyobj__ unless pyobj.kind_of? LibPython::PyObjectStruct
186
33
  1 == LibPython.PyObject_HasAttrString(pyobj, name)
187
34
  end
188
35
 
@@ -194,6 +41,7 @@ module PyCall
194
41
  private_class_method :check_attr_name
195
42
 
196
43
  def self.getitem(pyobj, key)
44
+ pyobj = pyobj.__pyobj__ unless pyobj.kind_of? LibPython::PyObjectStruct
197
45
  pykey = Conversions.from_ruby(key)
198
46
  value = LibPython.PyObject_GetItem(pyobj, pykey)
199
47
  return value.to_ruby unless value.null?
@@ -201,6 +49,7 @@ module PyCall
201
49
  end
202
50
 
203
51
  def self.setitem(pyobj, key, value)
52
+ pyobj = pyobj.__pyobj__ unless pyobj.kind_of? LibPython::PyObjectStruct
204
53
  pykey = Conversions.from_ruby(key)
205
54
  value = Conversions.from_ruby(value)
206
55
  return self unless LibPython.PyObject_SetItem(pyobj, pykey, value) == -1
@@ -1,10 +1,27 @@
1
1
  module PyCall
2
+ Py_LT = 0
3
+ Py_LE = 1
4
+ Py_EQ = 2
5
+ Py_NE = 3
6
+ Py_GT = 4
7
+ Py_GE = 5
8
+
9
+ RICH_COMPARISON_OPCODES = {
10
+ :< => Py_LT,
11
+ :<= => Py_LE,
12
+ :== => Py_EQ,
13
+ :!= => Py_NE,
14
+ :> => Py_GT,
15
+ :>= => Py_GE
16
+ }.freeze
17
+
2
18
  module PyObjectWrapper
3
19
  module ClassMethods
4
20
  private
5
21
 
6
- def wrap_class(pyobj)
7
- define_singleton_method(:__pyobj__) { pyobj }
22
+ def wrap_class(pyclass)
23
+ pyclass__pyobj__ = pyclass.__pyobj__
24
+ define_singleton_method(:__pyobj__) { pyclass__pyobj__ }
8
25
 
9
26
  PyCall.dir(__pyobj__).each do |name|
10
27
  obj = PyCall.getattr(__pyobj__, name)
@@ -31,27 +48,123 @@ module PyCall
31
48
  mod.extend ClassMethods
32
49
  end
33
50
 
34
- def initialize(pyobj, pytype=nil)
35
- check_type pyobj, pytype
36
- pytype ||= LibPython.PyObject_Type(pyobj)
51
+ def initialize(pyobj)
52
+ pyobj = pyobj.__pyobj__ unless pyobj.kind_of? LibPython::PyObjectStruct
37
53
  @__pyobj__ = pyobj
38
54
  end
39
55
 
40
56
  attr_reader :__pyobj__
41
57
 
42
- def ==(other)
43
- case other
44
- when self.class
45
- __pyobj__ == other.__pyobj__
46
- when PyObject
47
- __pyobj__ == other
58
+ def type
59
+ LibPython.PyObject_Type(__pyobj__).to_ruby
60
+ end
61
+
62
+ def null?
63
+ __pyobj__.null?
64
+ end
65
+
66
+ def to_ptr
67
+ __pyobj__.to_ptr
68
+ end
69
+
70
+ def py_none?
71
+ to_ptr == PyCall.None.to_ptr
72
+ end
73
+
74
+ def kind_of?(klass)
75
+ case klass
76
+ when PyObjectWrapper
77
+ __pyobj__.kind_of? klass.__pyobj__
78
+ when LibPython::PyObjectStruct
79
+ __pyobj__.kind_of? klass
48
80
  else
49
81
  super
50
82
  end
51
83
  end
52
84
 
85
+ def rich_compare(other, op)
86
+ opcode = RICH_COMPARISON_OPCODES[op]
87
+ raise ArgumentError, "Unknown comparison op: #{op}" unless opcode
88
+
89
+ other = other.__pyobj__ unless other.kind_of? LibPython::PyObjectStruct
90
+ other = Conversions.from_ruby(other) unless other.kind_of?(LibPython::PyObjectStruct)
91
+ return other.null? if __pyobj__.null?
92
+ return false if other.null?
93
+
94
+ value = LibPython.PyObject_RichCompare(__pyobj__, other, opcode)
95
+ raise "Unable to compare: #{self} #{op} #{other}" if value.null?
96
+ value.to_ruby
97
+ end
98
+
99
+ RICH_COMPARISON_OPCODES.keys.each do |op|
100
+ define_method(op) {|other| rich_compare(other, op) }
101
+ end
102
+
103
+ def [](*indices)
104
+ if indices.length == 1
105
+ indices = indices[0]
106
+ else
107
+ indices = PyCall.tuple(*indices)
108
+ end
109
+ PyCall.getitem(self, indices)
110
+ end
111
+
112
+ def []=(*indices_and_value)
113
+ value = indices_and_value.pop
114
+ indices = indices_and_value
115
+ if indices.length == 1
116
+ indices = indices[0]
117
+ else
118
+ indices = PyCall.tuple(*indices)
119
+ end
120
+ PyCall.setitem(self, indices, value)
121
+ end
122
+
123
+ def +(other)
124
+ other = Conversions.from_ruby(other)
125
+ value = LibPython.PyNumber_Add(self, other)
126
+ return value.to_ruby unless value.null?
127
+ raise PyError.fetch
128
+ end
129
+
130
+ def -(other)
131
+ other = Conversions.from_ruby(other)
132
+ value = LibPython.PyNumber_Subtract(self, other)
133
+ return value.to_ruby unless value.null?
134
+ raise PyError.fetch
135
+ end
136
+
137
+ def *(other)
138
+ other = Conversions.from_ruby(other)
139
+ value = LibPython.PyNumber_Multiply(self, other)
140
+ return value.to_ruby unless value.null?
141
+ raise PyError.fetch
142
+ end
143
+
144
+ def /(other)
145
+ other = Conversions.from_ruby(other)
146
+ value = LibPython.PyNumber_TrueDivide(self, other)
147
+ return value.to_ruby unless value.null?
148
+ raise PyError.fetch
149
+ end
150
+
151
+ def **(other)
152
+ other = Conversions.from_ruby(other)
153
+ value = LibPython.PyNumber_Power(self, other, PyCall.None)
154
+ return value.to_ruby unless value.null?
155
+ raise PyError.fetch
156
+ end
157
+
158
+ def coerce(other)
159
+ [Conversions.from_ruby(other), self]
160
+ end
161
+
53
162
  def call(*args, **kwargs)
54
- __pyobj__.call(*args, **kwargs)
163
+ args = PyCall::Tuple[*args]
164
+ kwargs = kwargs.empty? ? PyObject.null : PyCall::Dict.new(kwargs)
165
+ res = LibPython.PyObject_Call(__pyobj__, args.__pyobj__, kwargs.__pyobj__)
166
+ return res.to_ruby if LibPython.PyErr_Occurred().null?
167
+ raise PyError.fetch
55
168
  end
56
169
 
57
170
  def method_missing(name, *args, **kwargs)
@@ -63,19 +176,18 @@ module PyCall
63
176
  end
64
177
 
65
178
  def to_s
66
- __pyobj__.to_s
67
- end
68
-
69
- def inspect
70
- __pyobj__.inspect
179
+ s = LibPython.PyObject_Repr(__pyobj__)
180
+ if s.null?
181
+ LibPython.PyErr_Clear()
182
+ s = LibPython.PyObject_Str(__pyobj__)
183
+ if s.null?
184
+ LibPython.PyErr_Clear()
185
+ return super
186
+ end
187
+ end
188
+ s.to_ruby
71
189
  end
72
190
 
73
- private
74
-
75
- def check_type(pyobj, pytype)
76
- return if pyobj.kind_of?(PyObject)
77
- return if pytype.nil? || pyobj.kind_of?(pytype)
78
- raise TypeError, "the argument must be a PyObject of #{pytype}"
79
- end
191
+ alias inspect to_s
80
192
  end
81
193
  end
data/lib/pycall/set.rb CHANGED
@@ -3,7 +3,7 @@ module PyCall
3
3
  include PyObjectWrapper
4
4
 
5
5
  def initialize(pyobj)
6
- super(pyobj, LibPython.PySet_Type)
6
+ super(pyobj)
7
7
  end
8
8
 
9
9
  def length
data/lib/pycall/slice.rb CHANGED
@@ -7,7 +7,7 @@ module PyCall
7
7
  case args.length
8
8
  when 1
9
9
  stop = args[0]
10
- return super(stop) if stop.kind_of?(PyObject)
10
+ return super(stop) if stop.kind_of?(LibPython::PyObjectStruct)
11
11
  when 2
12
12
  start, stop = args
13
13
  when 3
@@ -16,9 +16,9 @@ module PyCall
16
16
  much_or_few = args.length > 3 ? 'much' : 'few'
17
17
  raise ArgumentError, "too #{much_or_few} arguments (#{args.length} for 1..3)"
18
18
  end
19
- start = start ? Conversions.from_ruby(start) : PyObject.null
20
- stop = stop ? Conversions.from_ruby(stop) : PyObject.null
21
- step = step ? Conversions.from_ruby(step) : PyObject.null
19
+ start = start ? Conversions.from_ruby(start) : LibPython::PyObjectStruct.null
20
+ stop = stop ? Conversions.from_ruby(stop) : LibPython::PyObjectStruct.null
21
+ step = step ? Conversions.from_ruby(step) : LibPython::PyObjectStruct.null
22
22
  pyobj = LibPython.PySlice_New(start, stop, step)
23
23
  return pyobj.to_ruby unless pyobj.null?
24
24
  raise PyError.fetch
data/lib/pycall/tuple.rb CHANGED
@@ -12,7 +12,7 @@ module PyCall
12
12
  tuple[index] = obj
13
13
  end
14
14
  tuple
15
- when PyObject
15
+ when LibPython::PyObjectStruct
16
16
  super(init)
17
17
  end
18
18
  end
@@ -22,10 +22,6 @@ module PyCall
22
22
  new(ary)
23
23
  end
24
24
 
25
- def initialize(pyobj)
26
- super(pyobj, LibPython.PyTuple_Type)
27
- end
28
-
29
25
  def length
30
26
  LibPython.PyTuple_Size(__pyobj__)
31
27
  end
@@ -0,0 +1,11 @@
1
+ module PyCall
2
+ class TypeObject
3
+ include PyObjectWrapper
4
+
5
+ def to_s
6
+ return "pytype(#{self.__name__})"
7
+ end
8
+
9
+ alias inspect to_s
10
+ end
11
+ end
data/lib/pycall/types.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  module PyCall
2
2
  module Types
3
3
  def self.pyisinstance(pyobj, pytype)
4
- check_pyobject(pyobj)
5
- pytype = PyObject.new(pytype.to_ptr) if pytype.kind_of?(PyTypeObject)
4
+ pyobj = pyobj.__pyobj__ unless pyobj.kind_of? LibPython::PyObjectStruct
5
+ pytype = ptype.__pyobj__ unless pytype.kind_of? LibPython::PyObjectStruct
6
6
  LibPython.PyObject_IsInstance(pyobj, pytype) == 1
7
7
  end
8
8
 
data/lib/pycall/utils.rb CHANGED
@@ -6,19 +6,15 @@ module PyCall
6
6
  end
7
7
 
8
8
  def callable?(pyobj)
9
- case pyobj
10
- when PyObject
11
- when PyTypeObject
12
- pyobj = PyObject.new(pyobj.to_ptr)
13
- when PyObjectWrapper
9
+ unless pyobj.kind_of? LibPython::PyObjectStruct
10
+ raise TypeError, "the argument must be a Python object" unless pyobj.respond_to? :__pyobj__
14
11
  pyobj = pyobj.__pyobj__
15
- else
16
- raise TypeError, "the argument must be a PyObject"
17
12
  end
18
13
  1 == LibPython.PyCallable_Check(pyobj)
19
14
  end
20
15
 
21
16
  def dir(pyobj)
17
+ pyobj = pyobj.__pyobj__ unless pyobj.kind_of? LibPython::PyObjectStruct
22
18
  value = LibPython.PyObject_Dir(pyobj)
23
19
  return value.to_ruby unless value.null?
24
20
  raise PyError.fetch
@@ -49,6 +45,11 @@ module PyCall
49
45
  LibPython.Py_None
50
46
  end
51
47
 
48
+ def none?(pyobj)
49
+ pyobj = pyobj.__pyobj__ unless pyobj.kind_of? LibPython::PyObjectStruct
50
+ pyobj.to_ptr == self.None.to_ptr
51
+ end
52
+
52
53
  def slice(*args)
53
54
  Slice.new(*args)
54
55
  end
@@ -1,3 +1,3 @@
1
1
  module PyCall
2
- VERSION = "0.1.0.alpha.20170307"
2
+ VERSION = "0.1.0.alpha.20170308"
3
3
  end
data/lib/pycall.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  require "pycall/version"
2
2
  require "pycall/libpython"
3
+ require "pycall/pyobject_wrapper"
3
4
  require "pycall/pyobject"
4
5
  require "pycall/pyerror"
5
6
  require "pycall/eval"
6
7
  require "pycall/types"
7
8
  require "pycall/conversion"
8
- require "pycall/pyobject_wrapper"
9
+ require "pycall/type_object"
9
10
  require "pycall/tuple"
10
11
  require "pycall/list"
11
12
  require "pycall/dict"
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.20170307
4
+ version: 0.1.0.alpha.20170308
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-07 00:00:00.000000000 Z
11
+ date: 2017-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -106,6 +106,7 @@ files:
106
106
  - lib/pycall/set.rb
107
107
  - lib/pycall/slice.rb
108
108
  - lib/pycall/tuple.rb
109
+ - lib/pycall/type_object.rb
109
110
  - lib/pycall/types.rb
110
111
  - lib/pycall/utils.rb
111
112
  - lib/pycall/version.rb