Module: PyBind::Utils

Included in:
PyBind
Defined in:
lib/pybind/utils.rb

Constant Summary collapse

BUILTIN_FUNCS =
%w[
  getattr hasattr setattr delattr
  id type dir len iter next
]
MODULE_SHORTCUTS =
%w[
  types traceback
]

Instance Method Summary collapse

Instance Method Details

#callable?(pyobj) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/pybind/utils.rb', line 55

def callable?(pyobj)
  pystruct = pyobj.to_python_struct
  LibPython.PyCallable_Check(pystruct) == 1
end

#decref(pyobj) ⇒ Object



66
67
68
69
70
71
# File 'lib/pybind/utils.rb', line 66

def decref(pyobj)
  pystruct = pyobj.to_python_struct
  LibPython.Py_DecRef(pystruct)
  pystruct.send :pointer=, FFI::Pointer::NULL
  pyobj
end

#eval(str) ⇒ Object



36
37
38
39
40
# File 'lib/pybind/utils.rb', line 36

def eval(str)
  dict = main_dict
  eval_func = PyBind.builtin.get_attribute('eval')
  eval_func.call(str, dict, dict)
end

#execfile(filename) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pybind/utils.rb', line 42

def execfile(filename)
  dict = main_dict
  if PyBind.builtin.has_attribute?('execfile')
    execfile_func = PyBind.builtin.get_attribute('execfile')
    execfile_func.call(filename, dict, dict)
  else
    open_func = PyBind.builtin.get_attribute('open')
    exec_func = PyBind.builtin.get_attribute('exec')
    content = open_func.call(filename).get_attribute('read').call()
    exec_func.(content, dict, dict)
  end
end

#FalseObject



32
33
34
# File 'lib/pybind/utils.rb', line 32

def False
  LibPython.Py_False
end

#incref(pyobj) ⇒ Object



60
61
62
63
64
# File 'lib/pybind/utils.rb', line 60

def incref(pyobj)
  pystruct = pyobj.to_python_struct
  LibPython.Py_IncRef(pystruct)
  pyobj
end

#NoneObject



24
25
26
# File 'lib/pybind/utils.rb', line 24

def None
  LibPython.Py_None
end

#TrueObject



28
29
30
# File 'lib/pybind/utils.rb', line 28

def True
  LibPython.Py_True
end