Exception: RubyPython::PythonError

Inherits:
RuntimeError
  • Object
show all
Defined in:
lib/rubypython/pythonerror.rb

Overview

Raised when an error occurs in the Python interpreter.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(typeName, msg, traceback = nil) ⇒ PythonError

Creates the PythonError.

typeName

The class name of the Python error.

msg

The message attached to the Python error.

traceback

The traceback, if any, associated with the Python error.


14
15
16
17
18
# File 'lib/rubypython/pythonerror.rb', line 14

def initialize(typeName, msg, traceback = nil)
  @type = typeName
  @traceback = traceback
  super([typeName, msg].join(': '))
end

Instance Attribute Details

#tracebackObject (readonly)

The Python traceback object associated with this error. This will be a RubyPython::RubyPyProxy object.


8
9
10
# File 'lib/rubypython/pythonerror.rb', line 8

def traceback
  @traceback
end

Class Method Details

.clearObject

Resets the Python interpreter error flag


77
78
79
# File 'lib/rubypython/pythonerror.rb', line 77

def self.clear
  RubyPython::Python.PyErr_Clear
end

.error?Boolean

Determines whether an error has occurred in the Python interpreter.

Returns:

  • (Boolean)

72
73
74
# File 'lib/rubypython/pythonerror.rb', line 72

def self.error?
  !RubyPython::Python.PyErr_Occurred.null?
end

.fetchObject

A wrapper to the Python C API PyErr_Fetch function. Returns an array with three PyObject instances, representing the Type, the Value, and the stack trace of the Python error.


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

def self.fetch
  typePointer = ::FFI::MemoryPointer.new :pointer
  valuePointer = ::FFI::MemoryPointer.new :pointer
  tracebackPointer = ::FFI::MemoryPointer.new :pointer

  RubyPython::Python.PyErr_Fetch typePointer, valuePointer, tracebackPointer

  rbType = RubyPython::PyObject.new typePointer.read_pointer
  rbValue = RubyPython::PyObject.new valuePointer.read_pointer
  rbTraceback = RubyPython::PyObject.new tracebackPointer.read_pointer
  [rbType, rbValue, rbTraceback]
end

.handle_errorObject

This method should be called when an error has occurred in the Python interpreter. This acts as factory function for PythonError objects. The function fetches calls #fetch to get the error information from the Python interpreter and uses this to build a PythonError object. It then calls +#clear to clear the error flag in the python interpreter. After the error flag has been cleared, the PythonError object is returned.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubypython/pythonerror.rb', line 26

def self.handle_error
  rbType, rbValue, rbTraceback = fetch()

  if not rbValue.null?
    msg = rbValue.getAttr("__str__").callObject RubyPython::PyObject.buildArgTuple
    msg = msg.rubify
  else
    msg = nil
  end

  if not rbTraceback.null?
    traceback = RubyPython::RubyPyProxy.new rbTraceback
  else
    traceback = nil
  end

  # Decrease the reference count. This will happen anyway when they go out
  # of scope but might as well.
  rbValue.xDecref if not rbValue.null?
  pyName = rbType.getAttr("__name__")

  rbType.xDecref
  rbName = pyName.rubify
  pyName.xDecref

  RubyPython::PythonError.clear
  RubyPython::PythonError.new(rbName, msg, traceback)
end