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.


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubypython/pyobject.rb', line 63

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.


56
57
58
# File 'lib/rubypython/pyobject.rb', line 56

def pointer
  @pointer
end

Class Method Details

.buildArgTuple(*args) ⇒ Object

Takes an array of objects, converting them to Python objects if necessary, and wraps them in a Tuple such that they may be passed to #callObject.

args

An array; the arguments to be inserted into the

Tuple.


186
187
188
# File 'lib/rubypython/pyobject.rb', line 186

def self.buildArgTuple(*args)
  self.new RubyPython::Conversion.rtopArrayToTuple(args)
end

Instance Method Details

#callable?Boolean

Is the wrapped object callable?

Returns:

  • (Boolean)

159
160
161
# File 'lib/rubypython/pyobject.rb', line 159

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.


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

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.


111
112
113
114
# File 'lib/rubypython/pyobject.rb', line 111

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)

173
174
175
176
177
178
179
# File 'lib/rubypython/pyobject.rb', line 173

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


143
144
145
# File 'lib/rubypython/pyobject.rb', line 143

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.


165
166
167
168
169
# File 'lib/rubypython/pyobject.rb', line 165

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)

149
150
151
152
153
154
155
156
# File 'lib/rubypython/pyobject.rb', line 149

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.


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

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.


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

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

#null?Boolean

Tests whether the wrapped object is NULL.

Returns:

  • (Boolean)

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

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.


78
79
80
# File 'lib/rubypython/pyobject.rb', line 78

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.


101
102
103
104
# File 'lib/rubypython/pyobject.rb', line 101

def setAttr(attrName, rbPyAttr)
  #SetAttrString should incref whatever gets passed to it.
  RubyPython::Python.PyObject_SetAttrString(@pointer, attrName, rbPyAttr.pointer) != -1
end

#xDecrefObject

Decrease the reference count of the wrapped object.


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

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

#xIncrefObject

Increase the reference count of the wrapped object


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

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