Exception: Rupy::PythonError
- Inherits:
-
Exception
- Object
- Exception
- Rupy::PythonError
- Defined in:
- lib/rupy/pythonerror.rb
Overview
Raised when an error occurs in the Python interpreter.
Class Method Summary collapse
-
.clear ⇒ void
Resets the Python interpreter error flag.
-
.error? ⇒ Boolean
Determines whether an error has occured in the python interpreter.
-
.fetch ⇒ Array<PyObject>
A wrapper to the Python C API PyErr_Fetch function.
-
.handle_error ⇒ PythonError
This method should be called when an error has occured in the Python interpreter.
Instance Method Summary collapse
-
#initialize(typeName, msg) ⇒ PythonError
constructor
A new instance of PythonError.
Constructor Details
#initialize(typeName, msg) ⇒ PythonError
Returns a new instance of PythonError.
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
.clear ⇒ void
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.
68 69 70 |
# File 'lib/rupy/pythonerror.rb', line 68 def self.error? !Python.PyErr_Occurred.null? end |
.fetch ⇒ Array<PyObject>
A wrapper to the Python C API PyErr_Fetch function.
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_error ⇒ PythonError
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
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 |