-
Notifications
You must be signed in to change notification settings - Fork 2
/
Error.c
153 lines (137 loc) · 5.5 KB
/
Error.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//-----------------------------------------------------------------------------
// Error.c
// Error handling.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// structure for the Python type
//-----------------------------------------------------------------------------
typedef struct {
PyObject_HEAD
sb4 code;
ub4 offset;
PyObject *message;
const char *context;
} udt_Error;
//-----------------------------------------------------------------------------
// forward declarations
//-----------------------------------------------------------------------------
static void Error_Free(udt_Error*);
static PyObject *Error_Str(udt_Error*);
//-----------------------------------------------------------------------------
// declaration of members
//-----------------------------------------------------------------------------
static PyMemberDef g_ErrorMembers[] = {
{ "code", T_INT, offsetof(udt_Error, code), READONLY },
{ "offset", T_INT, offsetof(udt_Error, offset), READONLY },
{ "message", T_OBJECT, offsetof(udt_Error, message), READONLY },
{ "context", T_STRING, offsetof(udt_Error, context), READONLY },
{ NULL }
};
//-----------------------------------------------------------------------------
// declaration of Python type
//-----------------------------------------------------------------------------
static PyTypeObject g_ErrorType = {
PyVarObject_HEAD_INIT(NULL, 0)
"cx_Oracle._Error", // tp_name
sizeof(udt_Error), // tp_basicsize
0, // tp_itemsize
(destructor) Error_Free, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
0, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
(reprfunc) Error_Str, // tp_str
0, // tp_getattro
0, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
0, // tp_doc
0, // tp_traverse
0, // tp_clear
0, // tp_richcompare
0, // tp_weaklistoffset
0, // tp_iter
0, // tp_iternext
0, // tp_methods
g_ErrorMembers, // tp_members
0 // tp_getset
};
//-----------------------------------------------------------------------------
// Error_New()
// Create a new error object.
//-----------------------------------------------------------------------------
static udt_Error *Error_New(
udt_Environment *environment, // environment object
const char *context, // context in which error occurred
int retrieveError) // retrieve error from OCI?
{
char errorText[4096];
udt_Error *self;
ub4 handleType;
dvoid *handle;
sword status;
#if PY_MAJOR_VERSION >= 3
Py_ssize_t len;
#endif
self = (udt_Error*) g_ErrorType.tp_alloc(&g_ErrorType, 0);
if (!self)
return NULL;
self->context = context;
if (retrieveError) {
if (environment->errorHandle) {
handle = environment->errorHandle;
handleType = OCI_HTYPE_ERROR;
} else {
handle = environment->handle;
handleType = OCI_HTYPE_ENV;
}
status = OCIErrorGet(handle, 1, 0, &self->code,
(unsigned char*) errorText, sizeof(errorText), handleType);
if (status != OCI_SUCCESS) {
Py_DECREF(self);
PyErr_SetString(g_InternalErrorException, "No Oracle error?");
return NULL;
}
#if PY_MAJOR_VERSION < 3
self->message = PyBytes_FromString(errorText);
#else
len = strlen(errorText);
self->message = PyUnicode_Decode(errorText, len, environment->encoding,
NULL);
#endif
if (!self->message) {
Py_DECREF(self);
return NULL;
}
}
return self;
}
//-----------------------------------------------------------------------------
// Error_Free()
// Deallocate the environment, disconnecting from the database if necessary.
//-----------------------------------------------------------------------------
static void Error_Free(
udt_Error *self) // error object
{
Py_CLEAR(self->message);
PyObject_Del(self);
}
//-----------------------------------------------------------------------------
// Error_Str()
// Return a string representation of the error variable.
//-----------------------------------------------------------------------------
static PyObject *Error_Str(
udt_Error *self) // variable to return the string for
{
if (self->message) {
Py_INCREF(self->message);
return self->message;
}
return cxString_FromAscii("");
}