Class: Rupy::PyObject

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rObject) ⇒ PyObject

Returns a new instance of PyObject.

Parameters:

  • pointer (FFI::Pointer, other)

    objects passed in to the constructor are just assigned to the pointer attribute of the instance. All other objects are converted via Conversion.rtopObject before being assigned.



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

#pointerObject (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.

Parameters:

  • args (Array<PyObject>)

    the arguments to be inserted into the tuple.

Returns:



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.

Returns:



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`.

Parameters:

  • rbObject (PyObject)

    the argment to be turned into a tuple.

Returns:



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.

Returns:



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?

Returns:

  • (Boolean)


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).

Parameters:

Returns:



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).

Returns:

  • (Boolean)


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

Returns:

  • (Number)


119
120
121
# File 'lib/rupy/pyobject.rb', line 119

def cmp(other)
    Python.PyObject_Compare @pointer, other.pointer
end

#dirObject



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.

Returns:

  • (Boolean)


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

Parameters:

  • the (String)

    name of attribute to fetch

Returns:

  • (PyObject)

    a Ruby wrapper around the fetched attribute



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

Parameters:

  • the (String)

    name of the attribute to look up

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


114
115
116
# File 'lib/rupy/pyobject.rb', line 114

def null?
    @pointer.null?
end

#rubifyObject

Attempts to convert the wrapped object to a native ruby type.

Returns:

  • a ruby version of the wrapped object



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.

Parameters:

Returns:

  • (Boolean)

    returns true if the attribute is sucessfully set.



86
87
88
# File 'lib/rupy/pyobject.rb', line 86

def setAttr(attrName, rbPyAttr)
    Python.PyObject_SetAttrString(@pointer, attrName, rbPyAttr.pointer) != -1
end

#xDecrefvoid

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

#xIncrefvoid

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