module PyCall
Constants
- Dict
- List
- Slice
- Tuple
- VERSION
Public Class Methods
after_fork()
click to toggle source
const_missing(name)
click to toggle source
Calls superclass method
# 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
init(python = ENV['PYTHON'])
click to toggle source
# 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
without_gvl()
click to toggle source
static VALUE pycall_m_without_gvl(VALUE mod) { return pycall_without_gvl(rb_yield, Qnil); }
Public Instance Methods
builtins()
click to toggle source
# File lib/pycall.rb, line 13 def builtins @builtins ||= wrap_module(LibPython::API.builtins_module_ptr) end
callable?(obj)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
dir(obj)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/pycall.rb, line 53 def getattr(*args) obj, *rest = args LibPython::Helpers.getattr(obj.__pyptr__, *rest) end
hasattr?(obj, name)
click to toggle source
# File lib/pycall.rb, line 58 def hasattr?(obj, name) LibPython::Helpers.hasattr?(obj.__pyptr__, name) end
import_module(name)
click to toggle source
# File lib/pycall.rb, line 73 def import_module(name) LibPython::Helpers.import_module(name) end
iterable(obj)
click to toggle source
# File lib/pycall.rb, line 77 def iterable(obj) IterableWrapper.new(obj) end
len(obj)
click to toggle source
# File lib/pycall.rb, line 81 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)
click to toggle source
# File lib/pycall.rb, line 62 def same?(left, right) case left when PyObjectWrapper case right when PyObjectWrapper return left.__pyptr__ == right.__pyptr__ end end false end
sys()
click to toggle source
# File lib/pycall.rb, line 92 def sys @sys ||= import_module('sys') end
tuple(iterable=nil)
click to toggle source
# File lib/pycall.rb, line 96 def tuple(iterable=nil) pyptr = if iterable builtins.tuple.(iterable) else builtins.tuple.() end Tuple.wrap_pyptr(pyptr) end
with(ctx) { |__enter__| ... }
click to toggle source
# File lib/pycall.rb, line 105 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)
click to toggle source
# 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)
click to toggle source
# 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