Module: PyBind::RichComparer

Included in:
PyObjectWrapper
Defined in:
lib/pybind/wrapper/rich_comparer.rb

Constant Summary collapse

Py_LT =
0
Py_LE =
1
Py_EQ =
2
Py_NE =
3
Py_GT =
4
Py_GE =
5
RICH_COMPARISON_OPCODES =
{
  :<  => Py_LT,
  :<= => Py_LE,
  :== => Py_EQ,
  :!= => Py_NE,
  :>  => Py_GT,
  :>= => Py_GE
}.freeze

Instance Method Summary collapse

Instance Method Details

#__rich_compare__(other, op) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pybind/wrapper/rich_comparer.rb', line 19

def __rich_compare__(other, op)
  opcode = RICH_COMPARISON_OPCODES[op]
  raise ArgumentError, "Unknown comparison op: #{op}" unless opcode

  other = other.to_python
  return other.null? if @pystruct.null?
  return false if other.null?

  value = LibPython.PyObject_RichCompare(@pystruct, other, opcode)
  raise PyError.fetch if value.null?
  value.to_ruby
end