Class: Rupy::PyObject
- Inherits:
-
Object
- Object
- Rupy::PyObject
- Defined in:
- lib/rupy/pyobject.rb
Overview
This object is an opaque wrapper around the C PyObject* type used by the python C API. This class **should not** be used by the end user. They should instead make use of the RubyPyProxy class and its subclasses.
Defined Under Namespace
Classes: AutoPyPointer
Instance Attribute Summary collapse
-
#pointer ⇒ Object
readonly
The FFI::Pointer object which represents the Python PyObject*.
Class Method Summary collapse
-
.buildArgTuple(*args) ⇒ PyObject<tuple>
Takes an array of wrapped Python objects and wraps them in a tuple such that they may be passed to #callObject.
-
.convert(*args) ⇒ Array<PyObject>
Converts the supplied arguments to PyObject instances.
-
.makeTuple(rbObject) ⇒ PyObject<tuple>
Manipulates the supplied PyObject instance such that it is suitable to passed to #callObject.
-
.newList(*args) ⇒ PyObject<list>
Wraps up the supplied arguments in Python list.
Instance Method Summary collapse
-
#callable? ⇒ Boolean
Is the wrapped object callable?.
-
#callObject(rbPyArgs) ⇒ PyObject
Calls the wrapped Python object with the supplied arguments.
-
#class? ⇒ Boolean
Tests whether the wrapped object is a Python class (both new and old style).
- #cmp(other) ⇒ Number
- #dir ⇒ Object
-
#function_or_method? ⇒ Boolean
Tests whether the wrapped object is a function or a method.
-
#getAttr(attrName) ⇒ PyObject
Retrieves an object from the wrapped python object.
-
#hasAttr(attrName) ⇒ Boolean
Tests whether the wrapped object has a given attribute.
-
#initialize(rObject) ⇒ PyObject
constructor
A new instance of PyObject.
-
#null? ⇒ Boolean
Tests whether the wrapped object is NULL.
-
#rubify ⇒ Object
Attempts to convert the wrapped object to a native ruby type.
-
#setAttr(attrName, rbPyAttr) ⇒ Boolean
Sets the an attribute of the wrapped Python object set the attribute to.
-
#xDecref ⇒ void
Decrease the reference count of the wrapped object.
-
#xIncref ⇒ void
Increase the reference count of the wrapped object.
Constructor Details
#initialize(rObject) ⇒ PyObject
Returns a new instance of PyObject.
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rupy/pyobject.rb', line 47 def initialize(rObject) if rObject.kind_of? FFI::AutoPointer new_pointer = FFI::Pointer.new rObject @pointer = AutoPyPointer.new new_pointer xIncref elsif rObject.kind_of? FFI::Pointer @pointer = AutoPyPointer.new rObject else @pointer = AutoPyPointer.new Conversion.rtopObject(rObject) end AutoPyPointer.current_pointers[@pointer.object_id] = true end |
Instance Attribute Details
#pointer ⇒ Object (readonly)
The FFI::Pointer object which represents the Python PyObject*.
42 43 44 |
# File 'lib/rupy/pyobject.rb', line 42 def pointer @pointer end |
Class Method Details
.buildArgTuple(*args) ⇒ PyObject<tuple>
Takes an array of wrapped Python objects and wraps them in a tuple such that they may be passed to #callObject.
200 201 202 203 204 205 |
# File 'lib/rupy/pyobject.rb', line 200 def self.buildArgTuple(*args) pList = PyObject.newList(*args) pTuple = PyObject.makeTuple(pList) pList.xDecref pTuple end |
.convert(*args) ⇒ Array<PyObject>
Converts the supplied arguments to PyObject instances.
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/rupy/pyobject.rb', line 184 def self.convert(*args) args.map! do |arg| if arg.kind_of? PyObject arg elsif arg.kind_of? RubyPyProxy arg.pObject else PyObject.new arg end end end |
.makeTuple(rbObject) ⇒ PyObject<tuple>
Manipulates the supplied Rupy::PyObject instance such that it is suitable to passed to #callObject. If ‘rbObject` is a tuple then the argument passed in is returned. If it is a list then the list is converted to a tuple. Otherwise returns a tuple with one element: `rbObject`.
156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/rupy/pyobject.rb', line 156 def self.makeTuple(rbObject) pTuple = nil if Macros.PyObject_TypeCheck(rbObject.pointer, Python.PyList_Type.to_ptr) != 0 pTuple = Python.PySequence_Tuple(rbObject.pointer) elsif Macros.PyObject_TypeCheck(rbObject.pointer, Python.PyTuple_Type.to_ptr) != 0 pTuple = rbObject.pointer else pTuple = Python.PyTuple_Pack(1, :pointer, rbObject.pointer) end self.new pTuple end |
.newList(*args) ⇒ PyObject<list>
Wraps up the supplied arguments in Python list.
172 173 174 175 176 177 178 179 180 |
# File 'lib/rupy/pyobject.rb', line 172 def self.newList(*args) rbList = self.new Python.PyList_New(args.length) args.each_with_index do |el, i| Python.PyList_SetItem rbList.pointer, i, el.pointer end rbList end |
Instance Method Details
#callable? ⇒ Boolean
Is the wrapped object callable?
132 133 134 |
# File 'lib/rupy/pyobject.rb', line 132 def callable? Python.PyCallable_Check(@pointer) != 0 end |
#callObject(rbPyArgs) ⇒ PyObject
Calls the wrapped Python object with the supplied arguments. arguments object (this may be NULL).
95 96 97 98 |
# File 'lib/rupy/pyobject.rb', line 95 def callObject(rbPyArgs) pyReturn = Python.PyObject_CallObject(@pointer, rbPyArgs.pointer) self.class.new pyReturn end |
#class? ⇒ Boolean
Tests whether the wrapped object is a Python class (both new and old style).
144 145 146 147 148 |
# File 'lib/rupy/pyobject.rb', line 144 def class? isClassObj = (Macros.PyObject_TypeCheck(@pointer, Python.PyClass_Type.to_ptr) == 1) isTypeObj = (Macros.PyObject_TypeCheck(@pointer, Python.PyType_Type.to_ptr) == 1) isTypeObj or isClassObj end |
#cmp(other) ⇒ Number
119 120 121 |
# File 'lib/rupy/pyobject.rb', line 119 def cmp(other) Python.PyObject_Compare @pointer, other.pointer end |
#dir ⇒ Object
136 137 138 139 140 |
# File 'lib/rupy/pyobject.rb', line 136 def dir return self.class.new(Python.PyObject_Dir(@pointer)).rubify.map do |x| x.to_sym end end |
#function_or_method? ⇒ Boolean
Tests whether the wrapped object is a function or a method. This is not the same as #callable? as many other Python objects are callable.
125 126 127 128 129 |
# File 'lib/rupy/pyobject.rb', line 125 def function_or_method? isFunc = (Macros.PyObject_TypeCheck(@pointer, Python.PyFunction_Type.to_ptr) != 0) isMethod = (Macros.PyObject_TypeCheck(@pointer, Python.PyMethod_Type.to_ptr) != 0) isFunc or isMethod end |
#getAttr(attrName) ⇒ PyObject
Retrieves an object from the wrapped python object
76 77 78 79 |
# File 'lib/rupy/pyobject.rb', line 76 def getAttr(attrName) pyAttr = Python.PyObject_GetAttrString @pointer, attrName self.class.new pyAttr end |
#hasAttr(attrName) ⇒ Boolean
Tests whether the wrapped object has a given attribute
69 70 71 |
# File 'lib/rupy/pyobject.rb', line 69 def hasAttr(attrName) Python.PyObject_HasAttrString(@pointer, attrName) == 1 end |
#null? ⇒ Boolean
Tests whether the wrapped object is NULL.
114 115 116 |
# File 'lib/rupy/pyobject.rb', line 114 def null? @pointer.null? end |
#rubify ⇒ Object
Attempts to convert the wrapped object to a native ruby type.
62 63 64 |
# File 'lib/rupy/pyobject.rb', line 62 def rubify Conversion.ptorObject @pointer end |
#setAttr(attrName, rbPyAttr) ⇒ Boolean
Sets the an attribute of the wrapped Python object set the attribute to.
86 87 88 |
# File 'lib/rupy/pyobject.rb', line 86 def setAttr(attrName, rbPyAttr) Python.PyObject_SetAttrString(@pointer, attrName, rbPyAttr.pointer) != -1 end |
#xDecref ⇒ void
This method returns an undefined value.
Decrease the reference count of the wrapped object
102 103 104 105 |
# File 'lib/rupy/pyobject.rb', line 102 def xDecref AutoPyPointer.release(@pointer) @pointer.free end |
#xIncref ⇒ void
This method returns an undefined value.
Increase the reference count of the wrapped object
109 110 111 |
# File 'lib/rupy/pyobject.rb', line 109 def xIncref Python.Py_IncRef @pointer end |