Exception: RubyPython::PythonError

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

Overview

Raised when an error occurs in the Python interpreter.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(typeName, msg) ⇒ PythonError

Creates the PythonError.

typeName

The class name of the Python error.

msg

The message attached to the Python error.



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

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

Class Method Details

.clearObject

Resets the Python interpreter error flag



66
67
68
# File 'lib/rubypython/pythonerror.rb', line 66

def self.clear
  RubyPython::Python.PyErr_Clear
end

.error?Boolean

Determines whether an error has occurred in the Python interpreter.

Returns:

  • (Boolean)


61
62
63
# File 'lib/rubypython/pythonerror.rb', line 61

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.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubypython/pythonerror.rb', line 47

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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubypython/pythonerror.rb', line 20

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

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

  rbType.xDecref
  rbName = pyName.rubify
  pyName.xDecref

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