Skip to content

Commit 31812c0

Browse files
committed
Further modeling of exception raising
1 parent d937325 commit 31812c0

File tree

9 files changed

+49
-52
lines changed

9 files changed

+49
-52
lines changed

psycopg/adapter_qstring.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
/* qstring_quote - do the quote process on plain and unicode strings */
3737

38-
static PyObject *
38+
BORROWED static PyObject *
3939
qstring_quote(qstringObject *self)
4040
{
4141
PyObject *str;

psycopg/lobject.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,19 @@ typedef struct {
5151

5252
/* functions exported from lobject_int.c */
5353

54-
HIDDEN int lobject_open(lobjectObject *self, connectionObject *conn,
55-
Oid oid, const char *smode, Oid new_oid,
56-
const char *new_file);
57-
HIDDEN int lobject_unlink(lobjectObject *self);
58-
HIDDEN int lobject_export(lobjectObject *self, const char *filename);
59-
60-
HIDDEN Py_ssize_t lobject_read(lobjectObject *self, char *buf, size_t len);
61-
HIDDEN Py_ssize_t lobject_write(lobjectObject *self, const char *buf,
54+
RAISES_NEG HIDDEN int lobject_open(lobjectObject *self, connectionObject *conn,
55+
Oid oid, const char *smode, Oid new_oid,
56+
const char *new_file);
57+
RAISES_NEG HIDDEN int lobject_unlink(lobjectObject *self);
58+
RAISES_NEG HIDDEN int lobject_export(lobjectObject *self, const char *filename);
59+
60+
RAISES_NEG HIDDEN Py_ssize_t lobject_read(lobjectObject *self, char *buf, size_t len);
61+
RAISES_NEG HIDDEN Py_ssize_t lobject_write(lobjectObject *self, const char *buf,
6262
size_t len);
63-
HIDDEN int lobject_seek(lobjectObject *self, int pos, int whence);
64-
HIDDEN int lobject_tell(lobjectObject *self);
65-
HIDDEN int lobject_truncate(lobjectObject *self, size_t len);
66-
HIDDEN int lobject_close(lobjectObject *self);
63+
RAISES_NEG HIDDEN int lobject_seek(lobjectObject *self, int pos, int whence);
64+
RAISES_NEG HIDDEN int lobject_tell(lobjectObject *self);
65+
RAISES_NEG HIDDEN int lobject_truncate(lobjectObject *self, size_t len);
66+
RAISES_NEG HIDDEN int lobject_close(lobjectObject *self);
6767

6868
#define lobject_is_closed(self) \
6969
((self)->fd < 0 || !(self)->conn || (self)->conn->closed)

psycopg/lobject_int.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ collect_error(connectionObject *conn, char **error)
5050
*
5151
* Valid mode are [r|w|rw|n][t|b]
5252
*/
53-
static int
53+
RAISES_NEG static int
5454
_lobject_parse_mode(const char *mode)
5555
{
5656
int rv = 0;
@@ -147,7 +147,7 @@ _lobject_unparse_mode(int mode)
147147

148148
/* lobject_open - create a new/open an existing lo */
149149

150-
int
150+
RAISES_NEG int
151151
lobject_open(lobjectObject *self, connectionObject *conn,
152152
Oid oid, const char *smode, Oid new_oid, const char *new_file)
153153
{
@@ -237,7 +237,7 @@ lobject_open(lobjectObject *self, connectionObject *conn,
237237

238238
/* lobject_close - close an existing lo */
239239

240-
static int
240+
RAISES_NEG static int
241241
lobject_close_locked(lobjectObject *self, char **error)
242242
{
243243
int retvalue;
@@ -270,7 +270,7 @@ lobject_close_locked(lobjectObject *self, char **error)
270270
return retvalue;
271271
}
272272

273-
int
273+
RAISES_NEG int
274274
lobject_close(lobjectObject *self)
275275
{
276276
PGresult *pgres = NULL;
@@ -292,7 +292,7 @@ lobject_close(lobjectObject *self)
292292

293293
/* lobject_unlink - remove an lo from database */
294294

295-
int
295+
RAISES_NEG int
296296
lobject_unlink(lobjectObject *self)
297297
{
298298
PGresult *pgres = NULL;
@@ -326,7 +326,7 @@ lobject_unlink(lobjectObject *self)
326326

327327
/* lobject_write - write bytes to a lo */
328328

329-
Py_ssize_t
329+
RAISES_NEG Py_ssize_t
330330
lobject_write(lobjectObject *self, const char *buf, size_t len)
331331
{
332332
Py_ssize_t written;
@@ -353,7 +353,7 @@ lobject_write(lobjectObject *self, const char *buf, size_t len)
353353

354354
/* lobject_read - read bytes from a lo */
355355

356-
Py_ssize_t
356+
RAISES_NEG Py_ssize_t
357357
lobject_read(lobjectObject *self, char *buf, size_t len)
358358
{
359359
Py_ssize_t n_read;
@@ -377,7 +377,7 @@ lobject_read(lobjectObject *self, char *buf, size_t len)
377377

378378
/* lobject_seek - move the current position in the lo */
379379

380-
int
380+
RAISES_NEG int
381381
lobject_seek(lobjectObject *self, int pos, int whence)
382382
{
383383
PGresult *pgres = NULL;
@@ -405,7 +405,7 @@ lobject_seek(lobjectObject *self, int pos, int whence)
405405

406406
/* lobject_tell - tell the current position in the lo */
407407

408-
int
408+
RAISES_NEG int
409409
lobject_tell(lobjectObject *self)
410410
{
411411
PGresult *pgres = NULL;
@@ -432,7 +432,7 @@ lobject_tell(lobjectObject *self)
432432

433433
/* lobject_export - export to a local file */
434434

435-
int
435+
RAISES_NEG int
436436
lobject_export(lobjectObject *self, const char *filename)
437437
{
438438
PGresult *pgres = NULL;
@@ -461,7 +461,7 @@ lobject_export(lobjectObject *self, const char *filename)
461461

462462
#if PG_VERSION_HEX >= 0x080300
463463

464-
int
464+
RAISES_NEG int
465465
lobject_truncate(lobjectObject *self, size_t len)
466466
{
467467
int retvalue;

psycopg/psycopgmodule.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,6 @@ psyco_connect(PyObject *self, PyObject *args, PyObject *keywds)
143143
" * `name`: Name for the new type\n" \
144144
" * `baseobj`: Adapter to perform type conversion of a single array item."
145145

146-
static void
147-
_psyco_register_type_set(PyObject **dict, PyObject *type)
148-
{
149-
if (*dict == NULL)
150-
*dict = PyDict_New();
151-
typecast_add(type, *dict, 0);
152-
}
153-
154146
static PyObject *
155147
psyco_register_type(PyObject *self, PyObject *args)
156148
{
@@ -162,10 +154,16 @@ psyco_register_type(PyObject *self, PyObject *args)
162154

163155
if (obj != NULL && obj != Py_None) {
164156
if (PyObject_TypeCheck(obj, &cursorType)) {
165-
_psyco_register_type_set(&(((cursorObject*)obj)->string_types), type);
157+
PyObject **dict = &(((cursorObject*)obj)->string_types);
158+
if (*dict == NULL) {
159+
if (!(*dict = PyDict_New())) { return NULL; }
160+
}
161+
if (0 > typecast_add(type, *dict, 0)) { return NULL; }
166162
}
167163
else if (PyObject_TypeCheck(obj, &connectionType)) {
168-
typecast_add(type, ((connectionObject*)obj)->string_types, 0);
164+
if (0 > typecast_add(type, ((connectionObject*)obj)->string_types, 0)) {
165+
return NULL;
166+
}
169167
}
170168
else {
171169
PyErr_SetString(PyExc_TypeError,
@@ -174,7 +172,7 @@ psyco_register_type(PyObject *self, PyObject *args)
174172
}
175173
}
176174
else {
177-
typecast_add(type, NULL, 0);
175+
if (0 > typecast_add(type, NULL, 0)) { return NULL; }
178176
}
179177

180178
Py_INCREF(Py_None);
@@ -1001,7 +999,7 @@ INIT_MODULE(_psycopg)(void)
1001999
}
10021000
#endif
10031001
/* initialize default set of typecasters */
1004-
typecast_init(dict);
1002+
if (0 != typecast_init(dict)) { goto exit; }
10051003

10061004
/* initialize microprotocols layer */
10071005
microprotocols_init(dict);

psycopg/typecast.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ PyObject *psyco_default_binary_cast;
250250

251251
/* typecast_init - initialize the dictionary and create default types */
252252

253-
int
253+
RAISES_NEG int
254254
typecast_init(PyObject *dict)
255255
{
256256
int i;
@@ -316,7 +316,7 @@ typecast_init(PyObject *dict)
316316
}
317317

318318
/* typecast_add - add a type object to the dictionary */
319-
int
319+
RAISES_NEG int
320320
typecast_add(PyObject *obj, PyObject *dict, int binary)
321321
{
322322
PyObject *val;

psycopg/typecast.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ extern HIDDEN PyObject *psyco_default_binary_cast;
7171
/** exported functions **/
7272

7373
/* used by module.c to init the type system and register types */
74-
HIDDEN int typecast_init(PyObject *dict);
75-
HIDDEN int typecast_add(PyObject *obj, PyObject *dict, int binary);
74+
RAISES_NEG HIDDEN int typecast_init(PyObject *dict);
75+
RAISES_NEG HIDDEN int typecast_add(PyObject *obj, PyObject *dict, int binary);
7676

7777
/* the C callable typecastObject creator function */
7878
HIDDEN PyObject *typecast_from_c(typecastObject_initlist *type, PyObject *d);

psycopg/typecast_array.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ typecast_array_tokenize(const char *str, Py_ssize_t strlength,
166166
return res;
167167
}
168168

169-
static int
169+
RAISES_NEG static int
170170
typecast_array_scan(const char *str, Py_ssize_t strlength,
171171
PyObject *curs, PyObject *base, PyObject *array)
172172
{
@@ -199,41 +199,41 @@ typecast_array_scan(const char *str, Py_ssize_t strlength,
199199

200200
/* before anything else we free the memory */
201201
if (state == ASCAN_QUOTED) PyMem_Free(token);
202-
if (obj == NULL) return 0;
202+
if (obj == NULL) return -1;
203203

204204
PyList_Append(array, obj);
205205
Py_DECREF(obj);
206206
}
207207

208208
else if (state == ASCAN_BEGIN) {
209209
PyObject *sub = PyList_New(0);
210-
if (sub == NULL) return 0;
210+
if (sub == NULL) return -1;
211211

212212
PyList_Append(array, sub);
213213
Py_DECREF(sub);
214214

215215
if (stack_index == MAX_DIMENSIONS)
216-
return 0;
216+
return -1;
217217

218218
stack[stack_index++] = array;
219219
array = sub;
220220
}
221221

222222
else if (state == ASCAN_ERROR) {
223-
return 0;
223+
return -1;
224224
}
225225

226226
else if (state == ASCAN_END) {
227227
if (--stack_index < 0)
228-
return 0;
228+
return -1;
229229
array = stack[stack_index];
230230
}
231231

232232
else if (state == ASCAN_EOF)
233233
break;
234234
}
235235

236-
return 1;
236+
return 0;
237237
}
238238

239239

@@ -263,9 +263,8 @@ typecast_GENERIC_ARRAY_cast(const char *str, Py_ssize_t len, PyObject *curs)
263263
obj = PyList_New(0);
264264

265265
/* scan the array skipping the first level of {} */
266-
if (typecast_array_scan(&str[1], len-2, curs, base, obj) == 0) {
267-
Py_DECREF(obj);
268-
obj = NULL;
266+
if (typecast_array_scan(&str[1], len-2, curs, base, obj) < 0) {
267+
Py_CLEAR(obj);
269268
}
270269

271270
return obj;

psycopg/typecast_datetime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <math.h>
2727
#include "datetime.h"
2828

29-
static int
29+
RAISES_NEG static int
3030
psyco_typecast_datetime_init(void)
3131
{
3232
Dprintf("psyco_typecast_datetime_init: datetime init");

psycopg/xid_type.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ xid_get_tid(XidObject *self)
486486
*
487487
* Return a borrowed reference. */
488488

489-
static PyObject *
489+
BORROWED static PyObject *
490490
_xid_get_parse_regex(void) {
491491
static PyObject *rv;
492492

0 commit comments

Comments
 (0)