lib/pycall/conversion.rb in pycall-0.1.0.alpha.20170307 vs lib/pycall/conversion.rb in pycall-0.1.0.alpha.20170308

- old
+ new

@@ -25,13 +25,11 @@ end @python_type_map << TypePair.new(pytype, rbtype) end def self.to_ruby(pyobj) - unless pyobj.kind_of? PyObject - raise - end + pyobj = PyObject.new(pyobj) if pyobj.kind_of? LibPython::PyObjectStruct each_type_pair do |tp| pytype, rbtype = tp.to_a next unless pyobj.kind_of?(pytype) case when rbtype.kind_of?(Proc) @@ -45,13 +43,11 @@ pyobj end def self.from_ruby(obj) case obj - when PyObject - obj - when PyObjectWrapper + when PyObject, PyObjectWrapper obj.__pyobj__ when TrueClass, FalseClass LibPython.PyBool_FromLong(obj ? 1 : 0) when Integer LibPython.PyInt_FromSsize_t(obj) @@ -117,46 +113,49 @@ def self.convert_to_tuple(py_obj) PyCall::Tuple.new(py_obj) end end - class PyObject + class LibPython::PyObjectStruct def to_ruby - return nil if self.null? || self.py_none? + return nil if self.null? || PyCall.none?(self) - case self - when LibPython.PyBool_Type + case + when PyCall::Types.pyisinstance(self, LibPython.PyType_Type) + return TypeObject.new(self) + + when PyCall::Types.pyisinstance(self, LibPython.PyBool_Type) return Conversions.convert_to_boolean(self) - when LibPython.PyInt_Type + when PyCall::Types.pyisinstance(self, LibPython.PyInt_Type) return Conversions.convert_to_integer(self) - when LibPython.PyLong_Type + when PyCall::Types.pyisinstance(self, LibPython.PyLong_Type) # TODO: should make Bignum - when LibPython.PyFloat_Type + when PyCall::Types.pyisinstance(self, LibPython.PyFloat_Type) return Conversions.convert_to_float(self) - when LibPython.PyComplex_Type + when PyCall::Types.pyisinstance(self, LibPython.PyComplex_Type) return Conversions.convert_to_complex(self) - when LibPython.PyString_Type + when PyCall::Types.pyisinstance(self, LibPython.PyString_Type) return Conversions.convert_to_string(self) - when LibPython.PyUnicode_Type + when PyCall::Types.pyisinstance(self, LibPython.PyUnicode_Type) py_str_ptr = LibPython.PyUnicode_AsUTF8String(self) return Conversions.convert_to_string(py_str_ptr).force_encoding(Encoding::UTF_8) - when LibPython.PyList_Type + when PyCall::Types.pyisinstance(self, LibPython.PyList_Type) return PyCall::List.new(self) - when LibPython.PyTuple_Type + when PyCall::Types.pyisinstance(self, LibPython.PyTuple_Type) return Conversions.convert_to_tuple(self) - when LibPython.PyDict_Type + when PyCall::Types.pyisinstance(self, LibPython.PyDict_Type) return PyCall::Dict.new(self) - when LibPython.PySet_Type + when PyCall::Types.pyisinstance(self, LibPython.PySet_Type) return PyCall::Set.new(self) end Conversions.to_ruby(self) end