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/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, LibPythonFunctionNotFound, List, PyError, PyPtr, PyRubyPtr, PyTypePtr, PythonNotFound, Set, Slice, WrapperObjectCache
Constant Summary
collapse
- VERSION =
"1.3.1"
- Tuple =
cTuple
Class Method Summary
collapse
Class Method Details
.after_fork ⇒ Object
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;
}
|
.callable?(obj) ⇒ Boolean
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/pycall.rb', line 16
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
178
179
180
181
182
183
|
# File 'lib/pycall/pyobject_wrapper.rb', line 178
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
173
174
175
176
|
# File 'lib/pycall/pyobject_wrapper.rb', line 173
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
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/pycall.rb', line 27
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
38
39
40
41
|
# File 'lib/pycall.rb', line 38
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
43
44
45
46
47
48
49
50
|
# File 'lib/pycall.rb', line 43
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
52
53
54
55
|
# File 'lib/pycall.rb', line 52
def getattr(*args)
obj, *rest = args
LibPython::Helpers.getattr(obj.__pyptr__, *rest)
end
|
.hasattr?(obj, name) ⇒ Boolean
.import_module(name) ⇒ Object
.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
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/pycall.rb', line 65
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
|
.sys ⇒ Object
76
77
78
|
# File 'lib/pycall.rb', line 76
def sys
@sys ||= import_module('sys')
end
|
.tuple(iterable = nil) ⇒ Object
80
81
82
83
84
85
86
87
|
# File 'lib/pycall.rb', line 80
def tuple(iterable=nil)
pyptr = if iterable
builtins.tuple.(iterable)
else
builtins.tuple.()
end
Tuple.wrap_pyptr(pyptr)
end
|
.with(ctx) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/pycall.rb', line 89
def with(ctx)
begin
yield ctx.__enter__
rescue PyError => err
raise err unless ctx.__exit__(err.type, err.value, err.traceback)
rescue Exception => err
raise err unless ctx.__exit__(err.class, err, err.backtrace_locations)
else
ctx.__exit__(nil, nil, nil)
end
end
|
.without_gvl ⇒ Object
113
114
115
116
117
|
# File 'ext/pycall/pycall.c', line 113
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
439
440
441
442
443
|
# File 'ext/pycall/ruby_wrapper.c', line 439
static VALUE
pycall_m_wrap_ruby_object(VALUE mod, VALUE obj)
{
return pycall_wrap_ruby_object(obj);
}
|