Class: RubyPython::PyObject

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

Class Method Summary collapse

Instance Method Summary collapse

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.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rubypython/pyobject.rb', line 58

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

#pointerObject (readonly)

The AutoPyPointer object which represents the RubyPython::Python Py…Object.



51
52
53
# File 'lib/rubypython/pyobject.rb', line 51

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.



225
226
227
228
229
230
# File 'lib/rubypython/pyobject.rb', line 225

def self.buildArgTuple(*args)
  pList = newList(*args)
  pTuple = makeTuple(pList)
  pList.xDecref
  pTuple
end

.convert(*args) ⇒ Object

Converts the supplied arguments to PyObject instances.



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rubypython/pyobject.rb', line 209

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.



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/rubypython/pyobject.rb', line 182

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.



197
198
199
200
201
202
203
204
205
206
# File 'lib/rubypython/pyobject.rb', line 197

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?

Returns:

  • (Boolean)


154
155
156
# File 'lib/rubypython/pyobject.rb', line 154

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.



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

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.



105
106
107
108
# File 'lib/rubypython/pyobject.rb', line 105

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

Returns:

  • (Boolean)


168
169
170
171
172
173
174
# File 'lib/rubypython/pyobject.rb', line 168

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



138
139
140
# File 'lib/rubypython/pyobject.rb', line 138

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

#dirObject

Returns the ‘directory’ of the RubyPython::Python object; similar to #methods in Ruby.



160
161
162
163
164
# File 'lib/rubypython/pyobject.rb', line 160

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.

Returns:

  • (Boolean)


144
145
146
147
148
149
150
151
# File 'lib/rubypython/pyobject.rb', line 144

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.



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

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.



80
81
82
# File 'lib/rubypython/pyobject.rb', line 80

def hasAttr(attrName)
  RubyPython::Python.PyObject_HasAttrString(@pointer, attrName) == 1
end

#null?Boolean

Tests whether the wrapped object is NULL.

Returns:

  • (Boolean)


132
133
134
# File 'lib/rubypython/pyobject.rb', line 132

def null?
  @pointer.null?
end

#rubifyObject

Attempts to convert the wrapped object to a native ruby type. Returns either the Ruby object or the unmodified Python object.



73
74
75
# File 'lib/rubypython/pyobject.rb', line 73

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.



96
97
98
# File 'lib/rubypython/pyobject.rb', line 96

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

#xDecrefObject

Decrease the reference count of the wrapped object.



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

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

#xIncrefObject

Increase the reference count of the wrapped object



126
127
128
129
# File 'lib/rubypython/pyobject.rb', line 126

def xIncref
  RubyPython::Python.Py_IncRef @pointer
  nil
end