Class: RubyPython::PyObject

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

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.



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

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

Parameters:

  • args (Array<PyObject>)

    the arguments to be inserted into the tuple.

Returns:



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.

Returns:



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.

Parameters:

  • rbObject (PyObject)

    the argment to be turned into a tuple.

Returns:



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.

Returns:



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?

Returns:

  • (Boolean)


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

Parameters:

Returns:



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

Returns:

  • (Boolean)


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

Returns:

  • (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.

Returns:

  • (Boolean)


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

Parameters:

  • the (String)

    name of attribute to fetch

Returns:

  • (PyObject)

    a Ruby wrapper around the fetched attribute



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

Parameters:

  • the (String)

    name of the attribute to look up

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


120
121
122
# File 'lib/rubypython/pyobject.rb', line 120

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



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.

Parameters:

Returns:

  • (Boolean)

    returns true if the attribute is sucessfully set.



92
93
94
# File 'lib/rubypython/pyobject.rb', line 92

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



108
109
110
111
# File 'lib/rubypython/pyobject.rb', line 108

def xDecref
  AutoPyPointer.release(@pointer)
  @pointer.free
end

#xIncrefvoid

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