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/tuple.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/conversion.rb,
lib/pycall/iruby_helper.rb,
lib/pycall/pretty_print.rb,
lib/pycall/libpython/finder.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, Conversions, IRubyHelper, Import, LibPython, PyObjectWrapper, PyTypeObjectWrapper Classes: Dict, Error, LibPythonFunctionNotFound, List, PyError, PyPtr, PyRubyPtr, PyTypePtr, PythonNotFound, Set, Slice, WrapperObjectCache

Constant Summary collapse

VERSION =
"1.0.3"
Tuple =
cTuple

Class Method Summary collapse

Class Method Details

.after_forkObject

PyCall ====



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

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

.builtinsObject



11
12
13
# File 'lib/pycall.rb', line 11

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

.callable?(obj) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


201
202
203
204
205
206
# File 'lib/pycall/pyobject_wrapper.rb', line 201

def check_isclass(pyptr)
  pyptr = pyptr.__pyptr__ if pyptr.kind_of? PyObjectWrapper
  return if pyptr.kind_of? LibPython::API::PyType_Type
  return 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)


208
209
210
211
# File 'lib/pycall/pyobject_wrapper.rb', line 208

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

.dir(obj) ⇒ Object



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

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



37
38
39
40
# File 'lib/pycall.rb', line 37

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



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

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

.import_module(name) ⇒ Object



51
52
53
# File 'lib/pycall.rb', line 51

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
50
51
52
53
54
# 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

  ENV['PYTHONPATH'] = [ File.expand_path('../python', __FILE__), ENV['PYTHONPATH'] ].compact.join(File::PATH_SEPARATOR)

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

  begin
    major, minor, _ = RUBY_VERSION.split('.')
    require "#{major}.#{minor}/pycall.so"
  rescue LoadError
    require 'pycall.so'
  end

  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

.len(obj) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/pycall.rb', line 55

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

.sysObject



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

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

.tuple(iterable = nil) ⇒ Object



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

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

.with(ctx) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pycall.rb', line 79

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

.wrap_class(pytypeptr) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/pycall/pytypeobject_wrapper.rb', line 81

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



191
192
193
194
195
196
197
198
199
# File 'lib/pycall/pyobject_wrapper.rb', line 191

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

.wrap_ruby_object(obj) ⇒ Object



401
402
403
404
405
# File 'ext/pycall/ruby_wrapper.c', line 401

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