Module: PyBind::PyObjectWrapper

Extended by:
Forwardable
Includes:
AttrAccessor, Operator, RichComparer
Included in:
PyBool, PyBuiltinFunction, PyComplex, PyDict, PyFloat, PyFunction, PyInt, PyList, PyMethod, PyObject, PySet, PySlice, PyString, PyTuple, PyType, PyUnicode
Defined in:
lib/pybind/wrapper.rb,
lib/pybind/autocall.rb

Constant Summary

Constants included from Operator

Operator::BINARY_OPERATION_OPFUNCS, Operator::UNARY_OPERATION_OPFUNCS

Constants included from RichComparer

RichComparer::Py_EQ, RichComparer::Py_GE, RichComparer::Py_GT, RichComparer::Py_LE, RichComparer::Py_LT, RichComparer::Py_NE, RichComparer::RICH_COMPARISON_OPCODES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Operator

#!, #**, #<=>, #===, #__binary_operate__, #__unary_operate__

Methods included from RichComparer

#__rich_compare__

Methods included from AttrAccessor

#[], #[]=, #get_attribute, #has_attribute?, #remove_attribute, #set_attribute

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs) ⇒ Object (private)



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pybind/wrapper.rb', line 100

def method_missing(name, *args, **kwargs)
  attr_name = name.to_s
  is_setter = attr_name.end_with?("=")
  attr_name = attr_name.chomp('=') if is_setter

  if has_attribute?(attr_name)
    if is_setter
      set_attribute(attr_name, *args)
    else
      autocall_method_missing(get_attribute(attr_name), *args, **kwargs)
    end
  else
    super
  end
end

Class Method Details

.included(mod) ⇒ Object



93
94
95
96
# File 'lib/pybind/wrapper.rb', line 93

def self.included(mod)
  mod.extend(PyObjectClassMethods)
  Types.register_type(mod)
end

Instance Method Details

#autocall_method_missing(value, *args, **kwargs) ⇒ Object



116
117
118
# File 'lib/pybind/wrapper.rb', line 116

def autocall_method_missing(value, *args, **kwargs)
  value
end

#call(*args, **kwargs) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/pybind/wrapper.rb', line 57

def call(*args, **kwargs)
  args = PyTuple[*args]
  kwargs = kwargs.empty? ? PyObject.null : PyDict.new(kwargs)
  res = LibPython.PyObject_Call(@pystruct, args.to_python_struct, kwargs.to_python_struct)
  raise PyError.fetch unless LibPython.PyErr_Occurred().null?
  res.to_ruby
end

#initialize(pystruct) ⇒ Object

Raises:

  • (TypeError)


52
53
54
55
# File 'lib/pybind/wrapper.rb', line 52

def initialize(pystruct)
  raise TypeError, "the argument must be a PyObjectStruct" unless pystruct.kind_of? PyObjectStruct
  @pystruct = pystruct
end

#inspectObject



71
72
73
74
75
# File 'lib/pybind/wrapper.rb', line 71

def inspect
  str = LibPython.PyObject_Repr(@pystruct)
  return "#<#{self.class.name || python_type} #{str.to_ruby}>" unless str.null?
  super
end

#methodsObject



77
78
79
# File 'lib/pybind/wrapper.rb', line 77

def methods
  LibPython.PyObject_Dir(@pystruct).to_ruby.map &:to_sym
end

#python_typeObject



84
85
86
# File 'lib/pybind/wrapper.rb', line 84

def python_type
  LibPython.PyObject_Type(@pystruct).to_ruby
end

#to_python_structObject Also known as: to_python



88
89
90
# File 'lib/pybind/wrapper.rb', line 88

def to_python_struct
  @pystruct
end

#to_sObject



65
66
67
68
69
# File 'lib/pybind/wrapper.rb', line 65

def to_s
  str = LibPython.PyObject_Str(@pystruct)
  return str.to_ruby unless str.null?
  super
end