Class: RubyPython::PyObject
- Inherits:
-
Object
- Object
- RubyPython::PyObject
- Defined in:
- lib/rubypython/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
-
#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.
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rubypython/pyobject.rb', line 53 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*.
48 49 50 |
# File 'lib/rubypython/pyobject.rb', line 48 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.
206 207 208 209 210 211 |
# File 'lib/rubypython/pyobject.rb', line 206 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.
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/rubypython/pyobject.rb', line 190 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 RubyPython::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.
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/rubypython/pyobject.rb', line 162 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.
178 179 180 181 182 183 184 185 186 |
# File 'lib/rubypython/pyobject.rb', line 178 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?
144 145 146 |
# File 'lib/rubypython/pyobject.rb', line 144 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).
101 102 103 104 |
# File 'lib/rubypython/pyobject.rb', line 101 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).
150 151 152 153 154 |
# File 'lib/rubypython/pyobject.rb', line 150 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
125 126 127 |
# File 'lib/rubypython/pyobject.rb', line 125 def cmp(other) Python.PyObject_Compare @pointer, other.pointer 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.
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rubypython/pyobject.rb', line 131 def function_or_method? [ Python.PyFunction_Type, Python.PyMethod_Type, Python.PyCFunction_Type ].each do |type| return true if Macros.PyObject_TypeCheck(@pointer, type.to_ptr) != 0 end false end |
#getAttr(attrName) ⇒ PyObject
Retrieves an object from the wrapped python object
82 83 84 85 |
# File 'lib/rubypython/pyobject.rb', line 82 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
75 76 77 |
# File 'lib/rubypython/pyobject.rb', line 75 def hasAttr(attrName) Python.PyObject_HasAttrString(@pointer, attrName) == 1 end |
#null? ⇒ Boolean
Tests whether the wrapped object is NULL.
120 121 122 |
# File 'lib/rubypython/pyobject.rb', line 120 def null? @pointer.null? end |
#rubify ⇒ Object
Attempts to convert the wrapped object to a native ruby type.
68 69 70 |
# File 'lib/rubypython/pyobject.rb', line 68 def rubify Conversion.ptorObject @pointer end |
#setAttr(attrName, rbPyAttr) ⇒ Boolean
Sets the an attribute of the wrapped Python object set the attribute to.
92 93 94 |
# File 'lib/rubypython/pyobject.rb', line 92 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
108 109 110 111 |
# File 'lib/rubypython/pyobject.rb', line 108 def xDecref AutoPyPointer.release(@pointer) @pointer.free end |
#xIncref ⇒ void
This method returns an undefined value.
Increase the reference count of the wrapped object
115 116 117 |
# File 'lib/rubypython/pyobject.rb', line 115 def xIncref Python.Py_IncRef @pointer end |