Exception: Rupy::PythonError

Inherits:
Exception
  • Object
show all
Defined in:
lib/rupy/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/rupy/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



74
75
76
# File 'lib/rupy/pythonerror.rb', line 74

def self.clear
    Python.PyErr_Clear
end

.error?Boolean

Determines whether an error has occured in the python interpreter.

Returns:

  • (Boolean)


68
69
70
# File 'lib/rupy/pythonerror.rb', line 68

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 Rupy::PyObject instances. representing the Type, Value, and stacktrace of the python error respectively.



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

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
48
# File 'lib/rupy/pythonerror.rb', line 24

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

    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