Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit d7e8062

Browse files
authored
Merge pull request #4 from DataDog/remicalixte/error-handling
Exception handling
2 parents 3d634de + d48b3d3 commit d7e8062

File tree

5 files changed

+588
-1
lines changed

5 files changed

+588
-1
lines changed

errors.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package python3
2+
3+
/*
4+
#include "Python.h"
5+
*/
6+
import "C"
7+
8+
import (
9+
"unsafe"
10+
)
11+
12+
//PyErr_Clear : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Clear
13+
func PyErr_Clear() {
14+
C.PyErr_Clear()
15+
}
16+
17+
//PyErr_PrintEx : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_PrintEx
18+
func PyErr_PrintEx(setSysLastVars bool) {
19+
if setSysLastVars {
20+
C.PyErr_PrintEx(1)
21+
} else {
22+
C.PyErr_PrintEx(0)
23+
}
24+
}
25+
26+
//PyErr_Print : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Print
27+
func PyErr_Print() {
28+
C.PyErr_PrintEx(1)
29+
}
30+
31+
//PyErr_WriteUnraisable : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_WriteUnraisable
32+
func PyErr_WriteUnraisable(obj *PyObject) {
33+
C.PyErr_WriteUnraisable(toc(obj))
34+
}
35+
36+
//PyErr_SetString : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SetString
37+
func PyErr_SetString(pyType *PyObject, message string) {
38+
cmessage := C.CString(message)
39+
defer C.free(unsafe.Pointer(cmessage))
40+
41+
C.PyErr_SetString(toc(pyType), cmessage)
42+
43+
}
44+
45+
//PyErr_SetObject : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SetObject
46+
func PyErr_SetObject(pyType, value *PyObject) {
47+
C.PyErr_SetObject(toc(pyType), toc(value))
48+
}
49+
50+
//PyErr_SetNone : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SetNone
51+
func PyErr_SetNone(pyType *PyObject) {
52+
C.PyErr_SetNone(toc(pyType))
53+
}
54+
55+
//PyErr_BadArgument : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_BadArgument
56+
func PyErr_BadArgument() {
57+
C.PyErr_BadArgument()
58+
}
59+
60+
//PyErr_NoMemory : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_NoMemory
61+
func PyErr_NoMemory() *PyObject {
62+
return togo(C.PyErr_NoMemory())
63+
}
64+
65+
//PyErr_SetImportErrorSubclass : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SetImportErrorSubclass
66+
func PyErr_SetImportErrorSubclass(msg, name, path, subclass *PyObject) *PyObject {
67+
return togo(C.PyErr_SetImportErrorSubclass(toc(msg), toc(name), toc(path), toc(subclass)))
68+
}
69+
70+
//PyErr_SetImportError : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SetImportError
71+
func PyErr_SetImportError(msg, name, path *PyObject) *PyObject {
72+
return togo(C.PyErr_SetImportError(toc(msg), toc(name), toc(path)))
73+
}
74+
75+
//PyErr_SyntaxLocationObject : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SyntaxLocationObject
76+
func PyErr_SyntaxLocationObject(filename *PyObject, lineno, col_offset int) {
77+
C.PyErr_SyntaxLocationObject(toc(filename), C.int(lineno), C.int(col_offset))
78+
}
79+
80+
//PyErr_SyntaxLocationEx : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SyntaxLocationEx
81+
func PyErr_SyntaxLocationEx(filename string, lineno, col_offset int) {
82+
cfilename := C.CString(filename)
83+
defer C.free(unsafe.Pointer(cfilename))
84+
85+
C.PyErr_SyntaxLocationEx(cfilename, C.int(lineno), C.int(col_offset))
86+
}
87+
88+
//PyErr_SyntaxLocation : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SyntaxLocation
89+
func PyErr_SyntaxLocation(filename string, lineno int) {
90+
cfilename := C.CString(filename)
91+
defer C.free(unsafe.Pointer(cfilename))
92+
93+
C.PyErr_SyntaxLocation(cfilename, C.int(lineno))
94+
95+
}
96+
97+
//PyErr_BadInternalCall : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_BadInternalCall
98+
func PyErr_BadInternalCall() {
99+
C.PyErr_BadInternalCall()
100+
}
101+
102+
//PyErr_Occurred : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Occurred
103+
func PyErr_Occurred() *PyObject {
104+
return togo(C.PyErr_Occurred())
105+
}
106+
107+
//PyErr_GivenExceptionMatches : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_GivenExceptionMatches
108+
func PyErr_GivenExceptionMatches(given, exc *PyObject) bool {
109+
ret := C.PyErr_GivenExceptionMatches(toc(given), toc(exc))
110+
return ret == 1
111+
}
112+
113+
//PyErr_ExceptionMatches : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_ExceptionMatches
114+
func PyErr_ExceptionMatches(exc *PyObject) bool {
115+
ret := C.PyErr_ExceptionMatches(toc(exc))
116+
return ret == 1
117+
}
118+
119+
//PyErr_Fetch : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Fetch
120+
func PyErr_Fetch() (*PyObject, *PyObject, *PyObject) {
121+
var pyType, value, traceback *C.PyObject
122+
C.PyErr_Fetch(&pyType, &value, &traceback)
123+
return togo(pyType), togo(value), togo(traceback)
124+
}
125+
126+
//PyErr_Restore : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Restore
127+
func PyErr_Restore(pyType *PyObject, value *PyObject, traceback *PyObject) {
128+
C.PyErr_Restore(toc(pyType), toc(value), toc(traceback))
129+
}
130+
131+
//PyErr_NormalizeException : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_NormalizeException
132+
func PyErr_NormalizeException(exc, val, tb *PyObject) (*PyObject, *PyObject, *PyObject) {
133+
cexc := toc(exc)
134+
cval := toc(val)
135+
ctb := toc(tb)
136+
C.PyErr_NormalizeException(&cexc, &cval, &ctb)
137+
return togo(cexc), togo(cval), togo(ctb)
138+
}
139+
140+
//PyErr_GetExcInfo : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_GetExcInfo
141+
func PyErr_GetExcInfo() (*PyObject, *PyObject, *PyObject) {
142+
var pyType, value, traceback *C.PyObject
143+
C.PyErr_GetExcInfo(&pyType, &value, &traceback)
144+
return togo(pyType), togo(value), togo(traceback)
145+
}
146+
147+
//PyErr_SetExcInfo : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SetExcInfo
148+
func PyErr_SetExcInfo(pyType *PyObject, value *PyObject, traceback *PyObject) {
149+
C.PyErr_SetExcInfo(toc(pyType), toc(value), toc(traceback))
150+
}
151+
152+
//PyErr_CheckSignals : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_CheckSignals
153+
func PyErr_CheckSignals() int {
154+
return int(C.PyErr_CheckSignals())
155+
}
156+
157+
//PyErr_SetInterrupt : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_SetInterrupt
158+
func PyErr_SetInterrupt() {
159+
C.PyErr_SetInterrupt()
160+
}
161+
162+
//PySignal_SetWakeupFd : https://docs.python.org/3/c-api/exceptions.html#c.PySignal_SetWakeupFd
163+
func PySignal_SetWakeupFd(fd uintptr) uintptr {
164+
return uintptr(C.PySignal_SetWakeupFd(C.int(fd)))
165+
}

exceptions.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package python3
2+
3+
/*
4+
#include "Python.h"
5+
*/
6+
import "C"
7+
8+
import (
9+
"unsafe"
10+
)
11+
12+
/*
13+
All standard Python exceptions are available as global variables whose names are PyExc_ followed by the Python exception name.
14+
These have the type PyObject*; they are all class objects.
15+
*/
16+
var (
17+
PyExc_BaseException = togo(C.PyExc_BaseException)
18+
PyExc_Exception = togo(C.PyExc_Exception)
19+
PyExc_ArithmeticError = togo(C.PyExc_ArithmeticError)
20+
PyExc_AssertionError = togo(C.PyExc_AssertionError)
21+
PyExc_AttributeError = togo(C.PyExc_AttributeError)
22+
PyExc_BlockingIOError = togo(C.PyExc_BlockingIOError)
23+
PyExc_BrokenPipeError = togo(C.PyExc_BrokenPipeError)
24+
PyExc_BufferError = togo(C.PyExc_BufferError)
25+
PyExc_ChildProcessError = togo(C.PyExc_ChildProcessError)
26+
PyExc_ConnectionAbortedError = togo(C.PyExc_ConnectionAbortedError)
27+
PyExc_ConnectionError = togo(C.PyExc_ConnectionError)
28+
PyExc_ConnectionRefusedError = togo(C.PyExc_ConnectionRefusedError)
29+
PyExc_ConnectcionResetError = togo(C.PyExc_ConnectionResetError)
30+
PyExc_EOFError = togo(C.PyExc_EOFError)
31+
PyExc_FileExistsError = togo(C.PyExc_FileExistsError)
32+
PyExc_FileNotFoundError = togo(C.PyExc_FileNotFoundError)
33+
PyExc_FloatingPointError = togo(C.PyExc_FloatingPointError)
34+
PyExc_GeneratorExit = togo(C.PyExc_GeneratorExit)
35+
PyExc_ImportError = togo(C.PyExc_ImportError)
36+
PyExc_IndentationError = togo(C.PyExc_IndentationError)
37+
PyExc_IndexError = togo(C.PyExc_IndexError)
38+
PyExc_InterruptedError = togo(C.PyExc_InterruptedError)
39+
PyExc_IsADirectoryError = togo(C.PyExc_IsADirectoryError)
40+
PyExc_KeyError = togo(C.PyExc_KeyError)
41+
PyExc_KeyboardInterrupt = togo(C.PyExc_KeyboardInterrupt)
42+
PyExc_LookupError = togo(C.PyExc_LookupError)
43+
PyExc_MemoryError = togo(C.PyExc_MemoryError)
44+
PyExc_ModuleNotFoundError = togo(C.PyExc_ModuleNotFoundError)
45+
PyExc_NameError = togo(C.PyExc_NameError)
46+
PyExc_NotADirectoryError = togo(C.PyExc_NotADirectoryError)
47+
PyExc_NotImplementedError = togo(C.PyExc_NotImplementedError)
48+
PyExc_OSError = togo(C.PyExc_OSError)
49+
PyExc_OverflowError = togo(C.PyExc_OverflowError)
50+
PyExc_PermissionError = togo(C.PyExc_PermissionError)
51+
PyExc_ProcessLookupError = togo(C.PyExc_ProcessLookupError)
52+
PyExc_RecursionError = togo(C.PyExc_RecursionError)
53+
PyExc_ReferenceError = togo(C.PyExc_ReferenceError)
54+
PyExc_RuntimeError = togo(C.PyExc_RuntimeError)
55+
PyExc_StopAsyncIteration = togo(C.PyExc_StopAsyncIteration)
56+
PyExc_StopIteration = togo(C.PyExc_StopIteration)
57+
PyExc_SyntaxError = togo(C.PyExc_SyntaxError)
58+
PyExc_SystemError = togo(C.PyExc_SystemError)
59+
PyExc_SystemExit = togo(C.PyExc_SystemExit)
60+
PyExc_TabError = togo(C.PyExc_TabError)
61+
PyExc_TimeoutError = togo(C.PyExc_TimeoutError)
62+
PyExc_TypeError = togo(C.PyExc_TypeError)
63+
PyExc_UnboundLocalError = togo(C.PyExc_UnboundLocalError)
64+
PyExc_UnicodeDecodeError = togo(C.PyExc_UnicodeDecodeError)
65+
PyExc_UnicodeEncodeError = togo(C.PyExc_UnicodeEncodeError)
66+
PyExc_UnicodeError = togo(C.PyExc_UnicodeError)
67+
PyExc_UnicodeTranslateError = togo(C.PyExc_UnicodeTranslateError)
68+
PyExc_ValueError = togo(C.PyExc_ValueError)
69+
PyExc_ZeroDivisionError = togo(C.PyExc_ZeroDivisionError)
70+
)
71+
72+
//PyErr_NewException : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_NewException
73+
func PyErr_NewException(name string, base, dict *PyObject) *PyObject {
74+
cname := C.CString(name)
75+
defer C.free(unsafe.Pointer(cname))
76+
77+
return togo(C.PyErr_NewException(cname, toc(base), toc(dict)))
78+
}
79+
80+
//PyErr_NewExceptionWithDoc : https://docs.python.org/3/c-api/exceptions.html#c.PyErr_NewExceptionWithDoc
81+
func PyErr_NewExceptionWithDoc(name, doc string, base, dict *PyObject) *PyObject {
82+
cname := C.CString(name)
83+
defer C.free(unsafe.Pointer(cname))
84+
85+
cdoc := C.CString(doc)
86+
defer C.free(unsafe.Pointer(cdoc))
87+
88+
return togo(C.PyErr_NewExceptionWithDoc(cname, cdoc, toc(base), toc(dict)))
89+
}
90+
91+
//PyException_GetTraceback : https://docs.python.org/3/c-api/exceptions.html#c.PyException_GetTraceback
92+
func PyException_GetTraceback(ex *PyObject) *PyObject {
93+
return togo(C.PyException_GetTraceback(toc(ex)))
94+
}
95+
96+
//PyException_SetTraceback : https://docs.python.org/3/c-api/exceptions.html#c.PyException_SetTraceback
97+
func PyException_SetTraceback(ex, tb *PyObject) {
98+
C.PyException_SetTraceback(toc(ex), toc(tb))
99+
}
100+
101+
//PyException_GetContext : https://docs.python.org/3/c-api/exceptions.html#c.PyException_GetContext
102+
func PyException_GetContext(ex *PyObject) *PyObject {
103+
return togo(C.PyException_GetContext(toc(ex)))
104+
}
105+
106+
//PyException_SetContext : https://docs.python.org/3/c-api/exceptions.html#c.PyException_SetContext
107+
func PyException_SetContext(ex, ctx *PyObject) {
108+
C.PyException_SetContext(toc(ex), toc(ctx))
109+
}
110+
111+
//PyException_GetCause : https://docs.python.org/3/c-api/exceptions.html#c.PyException_GetCause
112+
func PyException_GetCause(ex *PyObject) *PyObject {
113+
return togo(C.PyException_GetCause(toc(ex)))
114+
}
115+
116+
//PyException_SetCause : https://docs.python.org/3/c-api/exceptions.html#c.PyException_SetCause
117+
func PyException_SetCause(ex, cause *PyObject) {
118+
C.PyException_SetCause(toc(ex), toc(cause))
119+
}

helper.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ func togo(cobject *C.PyObject) *PyObject {
1313
func toc(object *PyObject) *C.PyObject {
1414
return (*C.PyObject)(object)
1515
}
16-

0 commit comments

Comments
 (0)