forked from oracle/python-cx_Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathError.c
More file actions
338 lines (303 loc) · 12 KB
/
Error.c
File metadata and controls
338 lines (303 loc) · 12 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//-----------------------------------------------------------------------------
// Copyright 2016, 2017, Oracle and/or its affiliates. All rights reserved.
//
// Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
//
// Portions Copyright 2001-2007, Computronix (Canada) Ltd., Edmonton, Alberta,
// Canada. All rights reserved.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Error.c
// Error handling.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// structure for the Python type
//-----------------------------------------------------------------------------
typedef struct {
PyObject_HEAD
sb4 code;
ub2 offset;
PyObject *message;
const char *context;
boolean isRecoverable;
} udt_Error;
//-----------------------------------------------------------------------------
// maximum size of error message string in bytes
//-----------------------------------------------------------------------------
#ifdef OCI_ERROR_MAXMSG_SIZE2
#define ERROR_BUF_SIZE OCI_ERROR_MAXMSG_SIZE2
#else
#define ERROR_BUF_SIZE OCI_ERROR_MAXMSG_SIZE
#endif
//-----------------------------------------------------------------------------
// forward declarations
//-----------------------------------------------------------------------------
static void Error_Free(udt_Error*);
static PyObject *Error_Str(udt_Error*);
static PyObject *Error_New(PyTypeObject*, PyObject*, PyObject*);
static PyObject *Error_Reduce(udt_Error*);
//-----------------------------------------------------------------------------
// declaration of methods
//-----------------------------------------------------------------------------
static PyMethodDef g_ErrorMethods[] = {
{ "__reduce__", (PyCFunction) Error_Reduce, METH_NOARGS },
{ NULL, NULL }
};
//-----------------------------------------------------------------------------
// 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 },
{ "isrecoverable", T_BOOL, offsetof(udt_Error, isRecoverable), 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
g_ErrorMethods, // tp_methods
g_ErrorMembers, // tp_members
0, // tp_getset
0, // tp_base
0, // tp_dict
0, // tp_descr_get
0, // tp_descr_set
0, // tp_dictoffset
0, // tp_init
0, // tp_alloc
Error_New, // tp_new
0, // tp_free
0, // tp_is_gc
0 // tp_bases
};
//-----------------------------------------------------------------------------
// 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_New()
// Create a new error object. This is intended to only be used by the
// unpickling routine, and not by direct creation!
//-----------------------------------------------------------------------------
static PyObject *Error_New(
PyTypeObject *type, // type object
PyObject *args, // arguments
PyObject *keywordArgs) // keyword arguments
{
boolean isRecoverable;
PyObject *message;
udt_Error *self;
unsigned offset;
char *context;
int code;
isRecoverable = 0;
if (!PyArg_ParseTuple(args, "OiIs|i", &message, &code, &offset, &context,
&isRecoverable))
return NULL;
self = (udt_Error*) type->tp_alloc(type, 0);
if (!self)
return NULL;
self->context = context;
self->code = code;
self->offset = offset;
self->isRecoverable = isRecoverable;
Py_INCREF(message);
self->message = message;
return (PyObject*) self;
}
//-----------------------------------------------------------------------------
// Error_Str()
// Return a string representation of the error variable.
//-----------------------------------------------------------------------------
static PyObject *Error_Str(
udt_Error *self) // variable to return the string for
{
Py_INCREF(self->message);
return self->message;
}
//-----------------------------------------------------------------------------
// Error_InternalNew()
// Internal method for creating an error object.
//-----------------------------------------------------------------------------
static udt_Error *Error_InternalNew(
udt_Environment *environment, // environment object
const char *context, // context in which error occurred
ub4 handleType, // handle type
dvoid* handle) // handle
{
char errorText[ERROR_BUF_SIZE];
udt_Error *self;
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 (handle) {
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;
}
#if ORACLE_VERSION_HEX >= ORACLE_VERSION(12, 1)
// determine if error is recoverable (Transaction Guard)
// if the attribute cannot be read properly, simply set it to false;
// otherwise, that error will mask the one that we really want to see
if (handleType == OCI_HTYPE_ERROR) {
status = OCIAttrGet(handle, handleType,
(dvoid*) &self->isRecoverable, 0,
OCI_ATTR_ERROR_IS_RECOVERABLE, handle);
if (status != OCI_SUCCESS)
self->isRecoverable = 0;
}
#endif
}
return self;
}
//-----------------------------------------------------------------------------
// Error_Raise()
// Reads the error that was caused by the last Oracle statement and raise an
// exception for Python. Return -1 as a convenience to the caller.
//-----------------------------------------------------------------------------
static int Error_Raise(
udt_Environment *environment, // environment object
const char *context, // context in which error occurred
OCIError* errorHandle) // handle
{
PyObject *exceptionType;
udt_Error *error;
error = Error_InternalNew(environment, context, OCI_HTYPE_ERROR,
errorHandle);
if (error) {
switch (error->code) {
case 1:
case 1400:
case 2290:
case 2291:
case 2292:
exceptionType = g_IntegrityErrorException;
break;
case 22:
case 378:
case 602:
case 603:
case 604:
case 609:
case 1012:
case 1013:
case 1033:
case 1034:
case 1041:
case 1043:
case 1089:
case 1090:
case 1092:
case 3113:
case 3114:
case 3122:
case 3135:
case 12153:
case 12203:
case 12500:
case 12571:
case 27146:
case 28511:
exceptionType = g_OperationalErrorException;
break;
default:
exceptionType = g_DatabaseErrorException;
break;
}
PyErr_SetObject(exceptionType, (PyObject*) error);
Py_DECREF(error);
}
return -1;
}
//-----------------------------------------------------------------------------
// Error_Check()
// Check for an error in the last call and if an error has occurred, raise a
// Python exception.
//-----------------------------------------------------------------------------
static int Error_Check(
udt_Environment *environment, // environment to raise error in
sword status, // status of last call
const char *context, // context
OCIError *errorHandle) // error handle to use
{
udt_Error *error;
if (status != OCI_SUCCESS && status != OCI_SUCCESS_WITH_INFO) {
if (status != OCI_INVALID_HANDLE)
return Error_Raise(environment, context, errorHandle);
error = Error_InternalNew(environment, context, 0, NULL);
if (!error)
return -1;
error->code = 0;
error->message = cxString_FromAscii("Invalid handle!");
if (!error->message)
Py_DECREF(error);
else PyErr_SetObject(g_DatabaseErrorException, (PyObject*) error);
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// Error_Reduce()
// Method provided for pickling/unpickling of Error objects.
//-----------------------------------------------------------------------------
static PyObject *Error_Reduce(
udt_Error *self) // error object
{
return Py_BuildValue("(O(OiIs))", Py_TYPE(self), self->message,
self->code, self->offset, self->context);
}