Module: PyCall

Defined in:
lib/pycall.rb,
lib/pycall/set.rb,
lib/pycall/dict.rb,
lib/pycall/init.rb,
lib/pycall/list.rb,
lib/pycall/error.rb,
lib/pycall/slice.rb,
lib/pycall/import.rb,
lib/pycall/import.rb,
lib/pycall/pyerror.rb,
lib/pycall/version.rb,
lib/pycall/gc_guard.rb,
lib/pycall/libpython.rb,
lib/pycall/iruby_helper.rb,
lib/pycall/pretty_print.rb,
lib/pycall/iterable_wrapper.rb,
lib/pycall/libpython/finder.rb,
lib/pycall/pymodule_wrapper.rb,
lib/pycall/pyobject_wrapper.rb,
lib/pycall/pytypeobject_wrapper.rb,
lib/pycall/wrapper_object_cache.rb,
lib/pycall/libpython/pyobject_struct.rb,
lib/pycall/libpython/pytypeobject_struct.rb,
ext/pycall/pycall.c

Defined Under Namespace

Modules: Conversion, IRubyHelper, Import, LibPython, PyModuleWrapper, PyObjectWrapper, PyTypeObjectWrapper, Version Classes: Dict, Error, IterableWrapper, LibPythonFunctionNotFound, List, PyError, PyPtr, PyRubyPtr, PyTypePtr, PythonNotFound, Set, Slice, WrapperObjectCache

Constant Summary collapse

VERSION =
"1.5.1"
Tuple =
cTuple

Class Method Summary collapse

Class Method Details

.after_forkObject

PyCall ====



66
67
68
69
70
71
# File 'ext/pycall/pycall.c', line 66

VALUE
pycall_after_fork(VALUE mod)
{
  Py_API(PyOS_AfterFork)();
  return Qnil;
}

.builtinsObject



13
14
15
# File 'lib/pycall.rb', line 13

def builtins
  @builtins ||= wrap_module(LibPython::API.builtins_module_ptr)
end

.callable?(obj) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
# File 'lib/pycall.rb', line 17

def callable?(obj)
  case obj
  when PyObjectWrapper
    builtins.callable(obj.__pyptr__)
  when PyPtr
    builtins.callable(obj)
  else
    raise TypeError, "unexpected argument type #{obj.class} (expected PyCall::PyPtr or its wrapper)"
  end
end

.check_isclass(pyptr) ⇒ Object

Raises:

  • (TypeError)


184
185
186
187
188
189
# File 'lib/pycall/pyobject_wrapper.rb', line 184

def check_isclass(pyptr)
  pyptr = pyptr.__pyptr__ if pyptr.kind_of? PyObjectWrapper
  return if pyptr.kind_of? LibPython::API::PyType_Type
  return if defined?(LibPython::API::PyClass_Type) && pyptr.kind_of?(LibPython::API::PyClass_Type)
  raise TypeError, "PyType object is required"
end

.check_ismodule(pyptr) ⇒ Object

Raises:

  • (TypeError)


179
180
181
182
# File 'lib/pycall/pyobject_wrapper.rb', line 179

def check_ismodule(pyptr)
  return if pyptr.kind_of? LibPython::API::PyModule_Type
  raise TypeError, "PyModule object is required"
end

.const_missing(name) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/pycall/init.rb', line 2

def self.const_missing(name)
  case name
  when :PyPtr, :PyTypePtr, :PyObjectWrapper, :PYTHON_DESCRIPTION, :PYTHON_VERSION
    PyCall.init
    const_get(name)
  else
    super
  end
end

.delattr(obj, name) ⇒ Object



66
67
68
# File 'lib/pycall.rb', line 66

def delattr(obj, name)
  LibPython::Helpers.delattr(obj.__pyptr__, name)
end

.dir(obj) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/pycall.rb', line 28

def dir(obj)
  case obj
  when PyObjectWrapper
    builtins.dir(obj.__pyptr__)
  when PyPtr
    builtins.dir(obj)
  else
    raise TypeError, "unexpected argument type #{obj.class} (expected PyCall::PyPtr or its wrapper)"
  end
end

.eval(expr, globals: nil, locals: nil) ⇒ Object



39
40
41
42
# File 'lib/pycall.rb', line 39

def eval(expr, globals: nil, locals: nil)
  globals ||= import_module(:__main__).__dict__
  builtins.eval(expr, globals, locals)
end

.exec(code, globals: nil, locals: nil) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/pycall.rb', line 44

def exec(code, globals: nil, locals: nil)
  globals ||= import_module(:__main__).__dict__
  if PYTHON_VERSION >= '3'
    builtins.exec(code, globals, locals)
  else
    import_module('PyCall.six').exec_(code, globals, locals)
  end
end

.getattr(*args) ⇒ Object



53
54
55
56
# File 'lib/pycall.rb', line 53

def getattr(*args)
  obj, *rest = args
  LibPython::Helpers.getattr(obj.__pyptr__, *rest)
end

.hasattr?(obj, name) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/pycall.rb', line 58

def hasattr?(obj, name)
  LibPython::Helpers.hasattr?(obj.__pyptr__, name)
end

.import_module(name) ⇒ Object



81
82
83
# File 'lib/pycall.rb', line 81

def import_module(name)
  LibPython::Helpers.import_module(name)
end

.init(python = ENV['PYTHON']) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pycall/init.rb', line 24

def self.init(python = ENV['PYTHON'])
  return false if LibPython.instance_variable_defined?(:@handle)
  class << PyCall
    remove_method :const_missing
  end
  class << PyCall::LibPython
    remove_method :const_missing
  end

  LibPython.instance_variable_set(:@handle, LibPython::Finder.find_libpython(python))
  class << LibPython
    undef_method :handle
    attr_reader :handle
  end

  require 'pycall.so'

  PyCall.sys.path.append(File.expand_path('../python', __FILE__))

  require 'pycall/dict'
  require 'pycall/list'
  require 'pycall/slice'
  const_set(:PYTHON_VERSION, LibPython::PYTHON_VERSION)
  const_set(:PYTHON_DESCRIPTION, LibPython::PYTHON_DESCRIPTION)
  true
end

.iterable(obj) ⇒ Object



85
86
87
# File 'lib/pycall.rb', line 85

def iterable(obj)
  IterableWrapper.new(obj)
end

.len(obj) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/pycall.rb', line 89

def len(obj)
  case obj
  when PyObjectWrapper
    builtins.len(obj.__pyptr__)
  when PyPtr
    builtins.len(obj)
  else
    raise TypeError, "unexpected argument type #{obj.class} (expected PyCall::PyPtr or its wrapper)"
  end
end

.same?(left, right) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
# File 'lib/pycall.rb', line 70

def same?(left, right)
  case left
  when PyObjectWrapper
    case right
    when PyObjectWrapper
      return left.__pyptr__ == right.__pyptr__
    end
  end
  false
end

.setattr(obj, name, val) ⇒ Object



62
63
64
# File 'lib/pycall.rb', line 62

def setattr(obj, name, val)
  LibPython::Helpers.setattr(obj.__pyptr__, name, val)
end

.sysObject



100
101
102
# File 'lib/pycall.rb', line 100

def sys
  @sys ||= import_module('sys')
end

.tuple(iterable = nil) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/pycall.rb', line 104

def tuple(iterable=nil)
  pyptr = if iterable
            builtins.tuple.(iterable)
          else
            builtins.tuple.()
          end
  Tuple.wrap_pyptr(pyptr)
end

.with(ctx) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pycall.rb', line 113

def with(ctx)
  begin
    yield ctx.__enter__
  rescue PyError => err
    raise err unless ctx.__exit__(err.type, err.value, err.traceback)
  rescue Exception => err
    # TODO: support telling what exception has been catched
    raise err unless ctx.__exit__(err.class, err, err.backtrace_locations)
  else
    ctx.__exit__(nil, nil, nil)
  end
end

.without_gvlObject



117
118
119
120
121
# File 'ext/pycall/pycall.c', line 117

static VALUE
pycall_m_without_gvl(VALUE mod)
{
  return pycall_without_gvl(rb_yield, Qnil);
}

.wrap_class(pytypeptr) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/pycall/pytypeobject_wrapper.rb', line 100

def wrap_class(pytypeptr)
  check_isclass(pytypeptr)
  WrapperClassCache.instance.lookup(pytypeptr) do
    Class.new do |cls|
      cls.instance_variable_set(:@__pyptr__, pytypeptr)
      cls.extend PyTypeObjectWrapper
    end
  end
end

.wrap_module(pymodptr) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/pycall/pymodule_wrapper.rb', line 37

def wrap_module(pymodptr)
  check_ismodule(pymodptr)
  WrapperModuleCache.instance.lookup(pymodptr) do
    Module.new do |mod|
      mod.instance_variable_set(:@__pyptr__, pymodptr)
      mod.extend PyModuleWrapper
    end
  end
end

.wrap_ruby_object(obj) ⇒ Object



444
445
446
447
448
# File 'ext/pycall/ruby_wrapper.c', line 444

static VALUE
pycall_m_wrap_ruby_object(VALUE mod, VALUE obj)
{
  return pycall_wrap_ruby_object(obj);
}