Browse code

modules/python3: return NULL on error.

- PyErr_SetString is a void function. It doesn't return NULL.

(cherry picked from commit b7911b63d7a27a4f29acc4bca3101f7857930084)

Matthias Urlichs authored on 02/09/2022 10:29:32 • Daniel-Constantin Mierla committed on 27/09/2022 09:51:22
Showing 1 changed files
... ...
@@ -72,20 +72,17 @@ static PyObject *msg_rewrite_ruri(msgobject *self, PyObject *args)
72 72
 
73 73
 	if (self == NULL) {
74 74
 		PyErr_SetString(PyExc_RuntimeError, "self is NULL");
75
-		Py_INCREF(Py_None);
76
-		return Py_None;
75
+		return NULL;
77 76
 	}
78 77
 
79 78
 	if (self->msg == NULL) {
80 79
 		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
81
-		Py_INCREF(Py_None);
82
-		return Py_None;
80
+		return NULL;
83 81
 	}
84 82
 
85 83
 	if ((self->msg->first_line).type != SIP_REQUEST) {
86 84
 		PyErr_SetString(PyExc_RuntimeError, "Not a request message - rewrite is not possible.\n");
87
-		Py_INCREF(Py_None);
88
-		return Py_None;
85
+		return NULL;
89 86
 	}
90 87
 
91 88
 	if(!PyArg_ParseTuple(args, "s:rewrite_ruri", &nuri.s))
... ...
@@ -107,20 +104,17 @@ static PyObject *msg_set_dst_uri(msgobject *self, PyObject *args)
107 104
 
108 105
 	if (self == NULL) {
109 106
 		PyErr_SetString(PyExc_RuntimeError, "self is NULL");
110
-		Py_INCREF(Py_None);
111
-		return Py_None;
107
+		return NULL;
112 108
 	}
113 109
 
114 110
 	if (self->msg == NULL) {
115 111
 		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
116
-		Py_INCREF(Py_None);
117
-		return Py_None;
112
+		return NULL;
118 113
 	}
119 114
 
120 115
 	if ((self->msg->first_line).type != SIP_REQUEST) {
121 116
 		PyErr_SetString(PyExc_RuntimeError, "Not a request message - set destination is not possible.\n");
122
-		Py_INCREF(Py_None);
123
-		return Py_None;
117
+		return NULL;
124 118
 	}
125 119
 
126 120
 	if(!PyArg_ParseTuple(args, "s:set_dst_uri", &ruri.s))
... ...
@@ -129,8 +123,8 @@ static PyObject *msg_set_dst_uri(msgobject *self, PyObject *args)
129 123
 	ruri.len = strlen(ruri.s);
130 124
 
131 125
 	if (set_dst_uri(self->msg, &ruri) < 0) {
132
-		LM_ERR("Error in set_dst_uri\n");
133 126
 		PyErr_SetString(PyExc_RuntimeError, "Error in set_dst_uri\n");
127
+		return NULL;
134 128
 	}
135 129
 	/* dst_uri changes, so it makes sense to re-use the current uri for
136 130
 	 * forking */
... ...
@@ -147,14 +141,12 @@ static PyObject *msg_getHeader(msgobject *self, PyObject *args)
147 141
 
148 142
 	if (self == NULL) {
149 143
 		PyErr_SetString(PyExc_RuntimeError, "self is NULL");
150
-		Py_INCREF(Py_None);
151
-		return Py_None;
144
+		return NULL;
152 145
 	}
153 146
 
154 147
 	if (self->msg == NULL) {
155 148
 		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
156
-		Py_INCREF(Py_None);
157
-		return Py_None;
149
+		return NULL;
158 150
 	}
159 151
 
160 152
 	if(!PyArg_ParseTuple(args, "s:getHeader", &hname.s))
... ...
@@ -191,22 +183,19 @@ PyObject *msg_call_function(msgobject *self, PyObject *args)
191 183
 
192 184
 	if (self == NULL) {
193 185
 		PyErr_SetString(PyExc_RuntimeError, "self is NULL");
194
-		Py_INCREF(Py_None);
195
-		return Py_None;
186
+		return NULL;
196 187
 	}
197 188
 
198 189
 	if (self->msg == NULL) {
199 190
 		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
200
-		Py_INCREF(Py_None);
201
-		return Py_None;
191
+		return NULL;
202 192
 	}
203 193
 
204 194
 	i = PySequence_Size(args);
205 195
 	if (i < 1 || i > 3) {
206 196
 		PyErr_SetString(PyExc_RuntimeError, "call_function() should " \
207 197
 				"have from 1 to 3 arguments");
208
-		Py_INCREF(Py_None);
209
-		return Py_None;
198
+		return NULL;
210 199
 	}
211 200
 
212 201
 	if(!PyArg_ParseTuple(args, "s|ss:call_function", &fname, &arg1, &arg2))
... ...
@@ -215,8 +204,7 @@ PyObject *msg_call_function(msgobject *self, PyObject *args)
215 204
 	fexport = find_export_record(fname, i - 1, 0);
216 205
 	if (fexport == NULL) {
217 206
 		PyErr_SetString(PyExc_RuntimeError, "no such function");
218
-		Py_INCREF(Py_None);
219
-		return Py_None;
207
+		return NULL;
220 208
 	}
221 209
 
222 210
 	act = mk_action(MODULE2_T, 4 /* number of (type, value) pairs */,
... ...
@@ -229,38 +217,34 @@ PyObject *msg_call_function(msgobject *self, PyObject *args)
229 217
 	if (act == NULL) {
230 218
 		PyErr_SetString(PyExc_RuntimeError,
231 219
 				"action structure could not be created");
232
-		Py_INCREF(Py_None);
233
-		return Py_None;
220
+		return NULL;
234 221
 	}
235 222
 
236 223
 	if (fexport->fixup != NULL) {
237 224
 		if (i >= 3) {
238 225
 			rval = fexport->fixup(&(act->val[3].u.data), 2);
239 226
 			if (rval < 0) {
240
-				PyErr_SetString(PyExc_RuntimeError, "Error in fixup (2)");
241
-				Py_INCREF(Py_None);
242 227
 				pkg_free(act);
243
-				return Py_None;
228
+				PyErr_SetString(PyExc_RuntimeError, "Error in fixup (2)");
229
+				return NULL;
244 230
 			}
245 231
 			act->val[3].type = MODFIXUP_ST;
246 232
 		}
247 233
 		if (i >= 2) {
248 234
 			rval = fexport->fixup(&(act->val[2].u.data), 1);
249 235
 			if (rval < 0) {
250
-				PyErr_SetString(PyExc_RuntimeError, "Error in fixup (1)");
251
-				Py_INCREF(Py_None);
252 236
 				pkg_free(act);
253
-				return Py_None;
237
+				PyErr_SetString(PyExc_RuntimeError, "Error in fixup (1)");
238
+				return NULL;
254 239
 			}
255 240
 			act->val[2].type = MODFIXUP_ST;
256 241
 		}
257 242
 		if (i == 1) {
258 243
 			rval = fexport->fixup(0, 0);
259 244
 			if (rval < 0) {
260
-				PyErr_SetString(PyExc_RuntimeError, "Error in fixup (0)");
261
-				Py_INCREF(Py_None);
262 245
 				pkg_free(act);
263
-				return Py_None;
246
+				PyErr_SetString(PyExc_RuntimeError, "Error in fixup (0)");
247
+				return NULL;
264 248
 			}
265 249
 		}
266 250
 	}
... ...
@@ -306,14 +290,12 @@ static PyObject *msg_getType(msgobject *self, PyObject *unused)
306 290
 
307 291
 	if (self == NULL) {
308 292
 		PyErr_SetString(PyExc_RuntimeError, "self is NULL");
309
-		Py_INCREF(Py_None);
310
-		return Py_None;
293
+		return NULL;
311 294
 	}
312 295
 
313 296
 	if (self->msg == NULL) {
314 297
 		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
315
-		Py_INCREF(Py_None);
316
-		return Py_None;
298
+		return NULL;
317 299
 	}
318 300
 
319 301
 	switch ((self->msg->first_line).type)
... ...
@@ -340,21 +322,19 @@ static PyObject *msg_getMethod(msgobject *self, PyObject *unused)
340 322
 
341 323
 	if (self == NULL) {
342 324
 		PyErr_SetString(PyExc_RuntimeError, "self is NULL");
343
-		Py_INCREF(Py_None);
344
-		return Py_None;
325
+		return NULL;
345 326
 	}
346 327
 
347 328
 	if (self->msg == NULL) {
348 329
 		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
349
-		Py_INCREF(Py_None);
350
-		return Py_None;
330
+		return NULL;
351 331
 	}
352 332
 
353 333
 	if ((self->msg->first_line).type != SIP_REQUEST) {
354 334
 		PyErr_SetString(PyExc_RuntimeError, "Not a request message - no method available.\n");
355
-		Py_INCREF(Py_None);
356
-		return Py_None;
335
+		return NULL;
357 336
 	}
337
+
358 338
 	rval = &((self->msg->first_line).u.request.method);
359 339
 	return PyUnicode_FromStringAndSize(rval->s, rval->len);
360 340
 }
... ...
@@ -365,20 +345,17 @@ static PyObject *msg_getStatus(msgobject *self, PyObject *unused)
365 345
 
366 346
 	if (self == NULL) {
367 347
 		PyErr_SetString(PyExc_RuntimeError, "self is NULL");
368
-		Py_INCREF(Py_None);
369
-		return Py_None;
348
+		return NULL;
370 349
 	}
371 350
 
372 351
 	if (self->msg == NULL) {
373 352
 		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
374
-		Py_INCREF(Py_None);
375
-		return Py_None;
353
+		return NULL;
376 354
 	}
377 355
 
378 356
 	if ((self->msg->first_line).type != SIP_REPLY) {
379 357
 		PyErr_SetString(PyExc_RuntimeError, "Not a non-reply message - no status available.\n");
380
-		Py_INCREF(Py_None);
381
-		return Py_None;
358
+		return NULL;
382 359
 	}
383 360
 
384 361
 	rval = &((self->msg->first_line).u.reply.status);
... ...
@@ -391,20 +368,17 @@ static PyObject *msg_getRURI(msgobject *self, PyObject *unused)
391 368
 
392 369
 	if (self == NULL) {
393 370
 		PyErr_SetString(PyExc_RuntimeError, "self is NULL");
394
-		Py_INCREF(Py_None);
395
-		return Py_None;
371
+		return NULL;
396 372
 	}
397 373
 
398 374
 	if (self->msg == NULL) {
399 375
 		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
400
-		Py_INCREF(Py_None);
401
-		return Py_None;
376
+		return NULL;
402 377
 	}
403 378
 
404 379
 	if ((self->msg->first_line).type != SIP_REQUEST) {
405 380
 		PyErr_SetString(PyExc_RuntimeError, "Not a request message - RURI is not available.\n");
406
-		Py_INCREF(Py_None);
407
-		return Py_None;
381
+		return NULL;
408 382
 	}
409 383
 
410 384
 	rval = &((self->msg->first_line).u.request.uri);
... ...
@@ -417,14 +391,12 @@ static PyObject *msg_get_src_address(msgobject *self, PyObject *unused)
417 391
 
418 392
 	if (self == NULL) {
419 393
 		PyErr_SetString(PyExc_RuntimeError, "self is NULL");
420
-		Py_INCREF(Py_None);
421
-		return Py_None;
394
+		return NULL;
422 395
 	}
423 396
 
424 397
 	if (self->msg == NULL) {
425 398
 		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
426
-		Py_INCREF(Py_None);
427
-		return Py_None;
399
+		return NULL;
428 400
 	}
429 401
 
430 402
 	src_ip = PyUnicode_FromString(ip_addr2a(&self->msg->rcv.src_ip));
... ...
@@ -457,14 +429,12 @@ static PyObject *msg_get_dst_address(msgobject *self, PyObject *unused)
457 429
 
458 430
 	if (self == NULL) {
459 431
 		PyErr_SetString(PyExc_RuntimeError, "self is NULL");
460
-		Py_INCREF(Py_None);
461
-		return Py_None;
432
+		return NULL;
462 433
 	}
463 434
 
464 435
 	if (self->msg == NULL) {
465 436
 		PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
466
-		Py_INCREF(Py_None);
467
-		return Py_None;
437
+		return NULL;
468 438
 	}
469 439
 
470 440
 	dst_ip = PyUnicode_FromString(ip_addr2a(&self->msg->rcv.dst_ip));