Exception: RubyPython::PythonError

Inherits:
Exception
  • 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

Returns a new instance of PythonError.

Parameters:

  • typeName (String)

    the class name of the Python error

  • msg (String)

    the message attached to the Python error



12
13
14
15
# File 'lib/rubypython/pythonerror.rb', line 12

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

Class Method Details

.clearvoid

This method returns an undefined value.

Resets the Python interpreter error flag



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

def self.clear
  Python.PyErr_Clear
end

.error?Boolean

Determines whether an error has occured in the python interpreter.

Returns:

  • (Boolean)


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

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

.fetchArray<PyObject>

A wrapper to the Python C API PyErr_Fetch function.

Returns:

  • (Array<PyObject>)

    an array containing three RubyPython::PyObject instances. representing the Type, Value, and stacktrace of the python error respectively.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubypython/pythonerror.rb', line 53

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

  Python.PyErr_Fetch typePointer, valuePointer, tracebackPointer

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

.handle_errorPythonError

This method should be called when an error has occured in the Python interpreter. This acts as factory function for PythonError objects. The function fetchs 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 of the python interpreter

Returns:

  • (PythonError)

    an error enscapsulating the Python error



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

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

  if not rbValue.null?
    msg = rbValue.getAttr("__str__").callObject 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

  PythonError.clear

  PythonError.new(rbName, msg)
end