Class: RubyPython::PyObject
- Inherits:
-
Object
- Object
- RubyPython::PyObject
- Defined in:
- lib/rubypython/pyobject.rb
Overview
This object is an opaque wrapper around the C Py…Object types used by the Python C API.
This class is only for RubyPython internal use.
Defined Under Namespace
Classes: AutoPyPointer
Instance Attribute Summary collapse
-
#pointer ⇒ Object
readonly
The AutoPyPointer object which represents the RubyPython::Python Py…Object.
Class Method Summary collapse
-
.buildArgTuple(*args) ⇒ Object
Takes an array of wrapped Python objects and wraps them in a Tuple such that they may be passed to #callObject.
-
.convert(*args) ⇒ Object
Converts the supplied arguments to PyObject instances.
-
.makeTuple(rbObject) ⇒ Object
Manipulates the supplied PyObject instance such that it is suitable to passed to #callObject or #callObjectKeywords.
-
.newList(*args) ⇒ Object
Wraps up the supplied arguments in a Python List.
Instance Method Summary collapse
-
#callable? ⇒ Boolean
Is the wrapped object callable?.
-
#callObject(rbPyArgs) ⇒ Object
Calls the wrapped Python object with the supplied arguments.
-
#callObjectKeywords(rbPyArgs, rbPyKeywords) ⇒ Object
Calls the wrapped Python object with the supplied arguments and keyword arguments.
-
#class? ⇒ Boolean
Tests whether the wrapped object is a RubyPython::Python class (both new and old style).
-
#cmp(other) ⇒ Object
Performs a compare on two Python objects.
-
#dir ⇒ Object
Returns the ‘directory’ of the RubyPython::Python object; similar to #methods in Ruby.
-
#function_or_method? ⇒ Boolean
Tests whether the wrapped object is a function or a method.
-
#getAttr(attrName) ⇒ Object
Retrieves an object from the wrapped Python object.
-
#hasAttr(attrName) ⇒ Object
Tests whether the wrapped Python object has a given attribute.
-
#initialize(rObject) ⇒ PyObject
constructor
- rObject
-
FFI Pointer objects passed into the constructor are wrapped in an AutoPyPointer and assigned to the
#pointer
attribute.
-
#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) ⇒ Object
Sets an attribute of the wrapped Python object.
-
#xDecref ⇒ Object
Decrease the reference count of the wrapped object.
-
#xIncref ⇒ Object
Increase the reference count of the wrapped object.
Constructor Details
#initialize(rObject) ⇒ PyObject
- rObject
-
FFI Pointer objects passed into the constructor are wrapped in
an AutoPyPointer and assigned to the #pointer
attribute. Other objects are converted, if possible, from their Ruby types to their Python types and wrapped in an AutoPyPointer. The conversion is done with RubyPython::Conversion.rtopObject
.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rubypython/pyobject.rb', line 59 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 RubyPython::Conversion.rtopObject(rObject) end AutoPyPointer.current_pointers[@pointer.object_id] = true end |
Instance Attribute Details
#pointer ⇒ Object (readonly)
The AutoPyPointer object which represents the RubyPython::Python Py…Object.
52 53 54 |
# File 'lib/rubypython/pyobject.rb', line 52 def pointer @pointer end |
Class Method Details
.buildArgTuple(*args) ⇒ Object
Takes an array of wrapped Python objects and wraps them in a Tuple such that they may be passed to #callObject.
- args
-
An array of PyObjects; the arguments to be inserted into the
Tuple.
226 227 228 229 230 231 |
# File 'lib/rubypython/pyobject.rb', line 226 def self.buildArgTuple(*args) pList = newList(*args) pTuple = makeTuple(pList) pList.xDecref pTuple end |
.convert(*args) ⇒ Object
Converts the supplied arguments to PyObject instances.
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/rubypython/pyobject.rb', line 210 def self.convert(*args) args.map do |arg| if arg.kind_of? RubyPython::PyObject arg elsif arg.kind_of? RubyPython::RubyPyProxy arg.pObject else RubyPython::PyObject.new arg end end end |
.makeTuple(rbObject) ⇒ Object
Manipulates the supplied PyObject instance such that it is suitable to passed to #callObject or #callObjectKeywords. 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
.
- rbObject
-
The argument to be turned into a Tuple.
183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/rubypython/pyobject.rb', line 183 def self.makeTuple(rbObject) pTuple = nil if RubyPython::Macros.PyObject_TypeCheck(rbObject.pointer, RubyPython::Python.PyList_Type.to_ptr) != 0 pTuple = RubyPython::Python.PySequence_Tuple(rbObject.pointer) elsif RubyPython::Macros.PyObject_TypeCheck(rbObject.pointer, RubyPython::Python.PyTuple_Type.to_ptr) != 0 pTuple = rbObject.pointer else pTuple = RubyPython::Python.PyTuple_Pack(1, :pointer, rbObject.pointer) end self.new pTuple end |
.newList(*args) ⇒ Object
Wraps up the supplied arguments in a Python List.
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/rubypython/pyobject.rb', line 198 def self.newList(*args) rbList = self.new RubyPython::Python.PyList_New(args.length) args.each_with_index do |el, i| el.xIncref # PyList_SetItem steals references! RubyPython::Python.PyList_SetItem rbList.pointer, i, el.pointer end rbList end |
Instance Method Details
#callable? ⇒ Boolean
Is the wrapped object callable?
155 156 157 |
# File 'lib/rubypython/pyobject.rb', line 155 def callable? RubyPython::Python.PyCallable_Check(@pointer) != 0 end |
#callObject(rbPyArgs) ⇒ Object
Calls the wrapped Python object with the supplied arguments. Returns a PyObject wrapper around the returned object, which may be NULL
.
- rbPyArgs
-
A PyObject wrapping a Tuple of the supplied arguments.
114 115 116 117 |
# File 'lib/rubypython/pyobject.rb', line 114 def callObject(rbPyArgs) pyReturn = RubyPython::Python.PyObject_CallObject(@pointer, rbPyArgs.pointer) self.class.new pyReturn end |
#callObjectKeywords(rbPyArgs, rbPyKeywords) ⇒ Object
Calls the wrapped Python object with the supplied arguments and keyword arguments. Returns a PyObject wrapper around the returned object, which may be NULL
.
- rbPyArgs
-
A PyObject wrapping a Tuple of the supplied arguments.
- rbPyKeywords
-
A PyObject wrapping a Dict of keyword arguments.
106 107 108 109 |
# File 'lib/rubypython/pyobject.rb', line 106 def callObjectKeywords(rbPyArgs, rbPyKeywords) pyReturn = RubyPython::Python.PyObject_Call(@pointer, rbPyArgs.pointer, rbPyKeywords.pointer) self.class.new pyReturn end |
#class? ⇒ Boolean
Tests whether the wrapped object is a RubyPython::Python class (both new and old style).
169 170 171 172 173 174 175 |
# File 'lib/rubypython/pyobject.rb', line 169 def class? check = RubyPython::Macros.PyObject_TypeCheck(@pointer, [ RubyPython::Python.PyClass_Type.to_ptr, RubyPython::Python.PyType_Type.to_ptr ]) check != 0 end |
#cmp(other) ⇒ Object
Performs a compare on two Python objects. Returns a value similar to that of the spaceship operator (<=>).
139 140 141 |
# File 'lib/rubypython/pyobject.rb', line 139 def cmp(other) RubyPython::Python.PyObject_Compare @pointer, other.pointer end |
#dir ⇒ Object
Returns the ‘directory’ of the RubyPython::Python object; similar to #methods in Ruby.
161 162 163 164 165 |
# File 'lib/rubypython/pyobject.rb', line 161 def dir return self.class.new(RubyPython::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.
145 146 147 148 149 150 151 152 |
# File 'lib/rubypython/pyobject.rb', line 145 def function_or_method? check = RubyPython::Macros.PyObject_TypeCheck(@pointer, [ RubyPython::Python.PyFunction_Type.to_ptr, RubyPython::Python.PyCFunction_Type.to_ptr, RubyPython::Python.PyMethod_Type.to_ptr ]) check != 0 end |
#getAttr(attrName) ⇒ Object
Retrieves an object from the wrapped Python object.
- attrName
-
The name of the attribute to fetch.
87 88 89 90 |
# File 'lib/rubypython/pyobject.rb', line 87 def getAttr(attrName) pyAttr = RubyPython::Python.PyObject_GetAttrString(@pointer, attrName) self.class.new pyAttr end |
#hasAttr(attrName) ⇒ Object
Tests whether the wrapped Python object has a given attribute. Returns true
if the attribute exists.
- attrName
-
The name of the attribute to look up.
81 82 83 |
# File 'lib/rubypython/pyobject.rb', line 81 def hasAttr(attrName) RubyPython::Python.PyObject_HasAttrString(@pointer, attrName) == 1 end |
#null? ⇒ Boolean
Tests whether the wrapped object is NULL
.
133 134 135 |
# File 'lib/rubypython/pyobject.rb', line 133 def null? @pointer.null? end |
#rubify ⇒ Object
Attempts to convert the wrapped object to a native ruby type. Returns either the Ruby object or the unmodified Python object.
74 75 76 |
# File 'lib/rubypython/pyobject.rb', line 74 def rubify RubyPython::Conversion.ptorObject @pointer end |
#setAttr(attrName, rbPyAttr) ⇒ Object
Sets an attribute of the wrapped Python object. Returns true
if the attribute was successfully set.
- attrName
-
The name of the attribute to set.
- rbPyAttr
-
A PyObject wrapper around the value that we wish to set the
attribute to.
97 98 99 |
# File 'lib/rubypython/pyobject.rb', line 97 def setAttr(attrName, rbPyAttr) RubyPython::Python.PyObject_SetAttrString(@pointer, attrName, rbPyAttr.pointer) != -1 end |
#xDecref ⇒ Object
Decrease the reference count of the wrapped object.
120 121 122 123 124 |
# File 'lib/rubypython/pyobject.rb', line 120 def xDecref AutoPyPointer.release(@pointer) @pointer.free nil end |
#xIncref ⇒ Object
Increase the reference count of the wrapped object
127 128 129 130 |
# File 'lib/rubypython/pyobject.rb', line 127 def xIncref RubyPython::Python.Py_IncRef @pointer nil end |