Skip to content

Commit 576f132

Browse files
Issue #20440: Cleaning up the code by using Py_SETREF.
1 parent dcf76c9 commit 576f132

File tree

14 files changed

+39
-120
lines changed

14 files changed

+39
-120
lines changed

Modules/_datetimemodule.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,10 +1057,8 @@ format_utcoffset(char *buf, size_t buflen, const char *sep,
10571057
}
10581058
/* Offset is normalized, so it is negative if days < 0 */
10591059
if (GET_TD_DAYS(offset) < 0) {
1060-
PyObject *temp = offset;
10611060
sign = '-';
1062-
offset = delta_negative((PyDateTime_Delta *)offset);
1063-
Py_DECREF(temp);
1061+
Py_SETREF(offset, delta_negative((PyDateTime_Delta *)offset));
10641062
if (offset == NULL)
10651063
return -1;
10661064
}
@@ -3047,10 +3045,8 @@ tzinfo_fromutc(PyDateTime_TZInfo *self, PyObject *dt)
30473045
if (dst == Py_None)
30483046
goto Inconsistent;
30493047
if (delta_bool((PyDateTime_Delta *)dst) != 0) {
3050-
PyObject *temp = result;
3051-
result = add_datetime_timedelta((PyDateTime_DateTime *)result,
3052-
(PyDateTime_Delta *)dst, 1);
3053-
Py_DECREF(temp);
3048+
Py_SETREF(result, add_datetime_timedelta((PyDateTime_DateTime *)result,
3049+
(PyDateTime_Delta *)dst, 1));
30543050
if (result == NULL)
30553051
goto Fail;
30563052
}
@@ -4157,10 +4153,7 @@ datetime_datetime_now_impl(PyTypeObject *type, PyObject *tz)
41574153
tz);
41584154
if (self != NULL && tz != Py_None) {
41594155
/* Convert UTC to tzinfo's zone. */
4160-
PyObject *temp = self;
4161-
4162-
self = _PyObject_CallMethodId(tz, &PyId_fromutc, "O", self);
4163-
Py_DECREF(temp);
4156+
self = _PyObject_CallMethodId(tz, &PyId_fromutc, "N", self);
41644157
}
41654158
return self;
41664159
}
@@ -4195,10 +4188,7 @@ datetime_fromtimestamp(PyObject *cls, PyObject *args, PyObject *kw)
41954188
tzinfo);
41964189
if (self != NULL && tzinfo != Py_None) {
41974190
/* Convert UTC to tzinfo's zone. */
4198-
PyObject *temp = self;
4199-
4200-
self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "O", self);
4201-
Py_DECREF(temp);
4191+
self = _PyObject_CallMethodId(tzinfo, &PyId_fromutc, "N", self);
42024192
}
42034193
return self;
42044194
}
@@ -4421,9 +4411,7 @@ datetime_subtract(PyObject *left, PyObject *right)
44214411
return NULL;
44224412

44234413
if (offdiff != NULL) {
4424-
PyObject *temp = result;
4425-
result = delta_subtract(result, offdiff);
4426-
Py_DECREF(temp);
4414+
Py_SETREF(result, delta_subtract(result, offdiff));
44274415
Py_DECREF(offdiff);
44284416
}
44294417
}

Modules/_elementtree.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,8 @@ element_init(PyObject *self, PyObject *args, PyObject *kwds)
396396
Py_XDECREF(attrib);
397397

398398
/* Replace the objects already pointed to by tag, text and tail. */
399-
tmp = self_elem->tag;
400399
Py_INCREF(tag);
401-
self_elem->tag = tag;
402-
Py_DECREF(tmp);
400+
Py_SETREF(self_elem->tag, tag);
403401

404402
tmp = self_elem->text;
405403
Py_INCREF(Py_None);

Modules/_lsprof.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,6 @@ profiler_dealloc(ProfilerObject *op)
762762
static int
763763
profiler_init(ProfilerObject *pObj, PyObject *args, PyObject *kw)
764764
{
765-
PyObject *o;
766765
PyObject *timer = NULL;
767766
double timeunit = 0.0;
768767
int subcalls = 1;
@@ -777,11 +776,9 @@ profiler_init(ProfilerObject *pObj, PyObject *args, PyObject *kw)
777776

778777
if (setSubcalls(pObj, subcalls) < 0 || setBuiltins(pObj, builtins) < 0)
779778
return -1;
780-
o = pObj->externalTimer;
781-
pObj->externalTimer = timer;
782-
Py_XINCREF(timer);
783-
Py_XDECREF(o);
784779
pObj->externalTimerUnit = timeunit;
780+
Py_XINCREF(timer);
781+
Py_SETREF(pObj->externalTimer, timer);
785782
return 0;
786783
}
787784

Modules/_pickle.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4494,8 +4494,6 @@ Pickler_get_persid(PicklerObject *self)
44944494
static int
44954495
Pickler_set_persid(PicklerObject *self, PyObject *value)
44964496
{
4497-
PyObject *tmp;
4498-
44994497
if (value == NULL) {
45004498
PyErr_SetString(PyExc_TypeError,
45014499
"attribute deletion is not supported");
@@ -4507,10 +4505,8 @@ Pickler_set_persid(PicklerObject *self, PyObject *value)
45074505
return -1;
45084506
}
45094507

4510-
tmp = self->pers_func;
45114508
Py_INCREF(value);
4512-
self->pers_func = value;
4513-
Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4509+
Py_SETREF(self->pers_func, value);
45144510

45154511
return 0;
45164512
}
@@ -6946,8 +6942,6 @@ Unpickler_get_persload(UnpicklerObject *self)
69466942
static int
69476943
Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
69486944
{
6949-
PyObject *tmp;
6950-
69516945
if (value == NULL) {
69526946
PyErr_SetString(PyExc_TypeError,
69536947
"attribute deletion is not supported");
@@ -6960,10 +6954,8 @@ Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
69606954
return -1;
69616955
}
69626956

6963-
tmp = self->pers_func;
69646957
Py_INCREF(value);
6965-
self->pers_func = value;
6966-
Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6958+
Py_SETREF(self->pers_func, value);
69676959

69686960
return 0;
69696961
}

Modules/readline.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,8 @@ set_hook(const char *funcname, PyObject **hook_var, PyObject *args)
321321
Py_CLEAR(*hook_var);
322322
}
323323
else if (PyCallable_Check(function)) {
324-
PyObject *tmp = *hook_var;
325324
Py_INCREF(function);
326-
*hook_var = function;
327-
Py_XDECREF(tmp);
325+
Py_SETREF(*hook_var, function);
328326
}
329327
else {
330328
PyErr_Format(PyExc_TypeError,

Objects/exceptions.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,11 @@ BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5959
static int
6060
BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
6161
{
62-
PyObject *tmp;
63-
6462
if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
6563
return -1;
6664

67-
tmp = self->args;
68-
self->args = args;
69-
Py_INCREF(self->args);
70-
Py_XDECREF(tmp);
65+
Py_INCREF(args);
66+
Py_SETREF(self->args, args);
7167

7268
return 0;
7369
}
@@ -328,11 +324,10 @@ PyException_GetCause(PyObject *self) {
328324

329325
/* Steals a reference to cause */
330326
void
331-
PyException_SetCause(PyObject *self, PyObject *cause) {
332-
PyObject *old_cause = ((PyBaseExceptionObject *)self)->cause;
333-
((PyBaseExceptionObject *)self)->cause = cause;
327+
PyException_SetCause(PyObject *self, PyObject *cause)
328+
{
334329
((PyBaseExceptionObject *)self)->suppress_context = 1;
335-
Py_XDECREF(old_cause);
330+
Py_SETREF(((PyBaseExceptionObject *)self)->cause, cause);
336331
}
337332

338333
PyObject *
@@ -344,10 +339,9 @@ PyException_GetContext(PyObject *self) {
344339

345340
/* Steals a reference to context */
346341
void
347-
PyException_SetContext(PyObject *self, PyObject *context) {
348-
PyObject *old_context = ((PyBaseExceptionObject *)self)->context;
349-
((PyBaseExceptionObject *)self)->context = context;
350-
Py_XDECREF(old_context);
342+
PyException_SetContext(PyObject *self, PyObject *context)
343+
{
344+
Py_SETREF(((PyBaseExceptionObject *)self)->context, context);
351345
}
352346

353347

Objects/frameobject.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,11 @@ frame_gettrace(PyFrameObject *f, void *closure)
349349
static int
350350
frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
351351
{
352-
PyObject* old_value;
353-
354352
/* We rely on f_lineno being accurate when f_trace is set. */
355353
f->f_lineno = PyFrame_GetLineNumber(f);
356354

357-
old_value = f->f_trace;
358355
Py_XINCREF(v);
359-
f->f_trace = v;
360-
Py_XDECREF(old_value);
356+
Py_SETREF(f->f_trace, v);
361357

362358
return 0;
363359
}

Objects/funcobject.c

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ func_get_code(PyFunctionObject *op)
249249
static int
250250
func_set_code(PyFunctionObject *op, PyObject *value)
251251
{
252-
PyObject *tmp;
253252
Py_ssize_t nfree, nclosure;
254253

255254
/* Not legal to del f.func_code or to set it to anything
@@ -270,10 +269,8 @@ func_set_code(PyFunctionObject *op, PyObject *value)
270269
nclosure, nfree);
271270
return -1;
272271
}
273-
tmp = op->func_code;
274272
Py_INCREF(value);
275-
op->func_code = value;
276-
Py_DECREF(tmp);
273+
Py_SETREF(op->func_code, value);
277274
return 0;
278275
}
279276

@@ -287,19 +284,15 @@ func_get_name(PyFunctionObject *op)
287284
static int
288285
func_set_name(PyFunctionObject *op, PyObject *value)
289286
{
290-
PyObject *tmp;
291-
292287
/* Not legal to del f.func_name or to set it to anything
293288
* other than a string object. */
294289
if (value == NULL || !PyUnicode_Check(value)) {
295290
PyErr_SetString(PyExc_TypeError,
296291
"__name__ must be set to a string object");
297292
return -1;
298293
}
299-
tmp = op->func_name;
300294
Py_INCREF(value);
301-
op->func_name = value;
302-
Py_DECREF(tmp);
295+
Py_SETREF(op->func_name, value);
303296
return 0;
304297
}
305298

@@ -313,19 +306,15 @@ func_get_qualname(PyFunctionObject *op)
313306
static int
314307
func_set_qualname(PyFunctionObject *op, PyObject *value)
315308
{
316-
PyObject *tmp;
317-
318309
/* Not legal to del f.__qualname__ or to set it to anything
319310
* other than a string object. */
320311
if (value == NULL || !PyUnicode_Check(value)) {
321312
PyErr_SetString(PyExc_TypeError,
322313
"__qualname__ must be set to a string object");
323314
return -1;
324315
}
325-
tmp = op->func_qualname;
326316
Py_INCREF(value);
327-
op->func_qualname = value;
328-
Py_DECREF(tmp);
317+
Py_SETREF(op->func_qualname, value);
329318
return 0;
330319
}
331320

@@ -343,8 +332,6 @@ func_get_defaults(PyFunctionObject *op)
343332
static int
344333
func_set_defaults(PyFunctionObject *op, PyObject *value)
345334
{
346-
PyObject *tmp;
347-
348335
/* Legal to del f.func_defaults.
349336
* Can only set func_defaults to NULL or a tuple. */
350337
if (value == Py_None)
@@ -354,10 +341,8 @@ func_set_defaults(PyFunctionObject *op, PyObject *value)
354341
"__defaults__ must be set to a tuple object");
355342
return -1;
356343
}
357-
tmp = op->func_defaults;
358344
Py_XINCREF(value);
359-
op->func_defaults = value;
360-
Py_XDECREF(tmp);
345+
Py_SETREF(op->func_defaults, value);
361346
return 0;
362347
}
363348

@@ -375,8 +360,6 @@ func_get_kwdefaults(PyFunctionObject *op)
375360
static int
376361
func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
377362
{
378-
PyObject *tmp;
379-
380363
if (value == Py_None)
381364
value = NULL;
382365
/* Legal to del f.func_kwdefaults.
@@ -386,10 +369,8 @@ func_set_kwdefaults(PyFunctionObject *op, PyObject *value)
386369
"__kwdefaults__ must be set to a dict object");
387370
return -1;
388371
}
389-
tmp = op->func_kwdefaults;
390372
Py_XINCREF(value);
391-
op->func_kwdefaults = value;
392-
Py_XDECREF(tmp);
373+
Py_SETREF(op->func_kwdefaults, value);
393374
return 0;
394375
}
395376

@@ -408,8 +389,6 @@ func_get_annotations(PyFunctionObject *op)
408389
static int
409390
func_set_annotations(PyFunctionObject *op, PyObject *value)
410391
{
411-
PyObject *tmp;
412-
413392
if (value == Py_None)
414393
value = NULL;
415394
/* Legal to del f.func_annotations.
@@ -420,10 +399,8 @@ func_set_annotations(PyFunctionObject *op, PyObject *value)
420399
"__annotations__ must be set to a dict object");
421400
return -1;
422401
}
423-
tmp = op->func_annotations;
424402
Py_XINCREF(value);
425-
op->func_annotations = value;
426-
Py_XDECREF(tmp);
403+
Py_SETREF(op->func_annotations, value);
427404
return 0;
428405
}
429406

Objects/genobject.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -510,19 +510,15 @@ gen_get_name(PyGenObject *op)
510510
static int
511511
gen_set_name(PyGenObject *op, PyObject *value)
512512
{
513-
PyObject *tmp;
514-
515513
/* Not legal to del gen.gi_name or to set it to anything
516514
* other than a string object. */
517515
if (value == NULL || !PyUnicode_Check(value)) {
518516
PyErr_SetString(PyExc_TypeError,
519517
"__name__ must be set to a string object");
520518
return -1;
521519
}
522-
tmp = op->gi_name;
523520
Py_INCREF(value);
524-
op->gi_name = value;
525-
Py_DECREF(tmp);
521+
Py_SETREF(op->gi_name, value);
526522
return 0;
527523
}
528524

@@ -536,19 +532,15 @@ gen_get_qualname(PyGenObject *op)
536532
static int
537533
gen_set_qualname(PyGenObject *op, PyObject *value)
538534
{
539-
PyObject *tmp;
540-
541535
/* Not legal to del gen.__qualname__ or to set it to anything
542536
* other than a string object. */
543537
if (value == NULL || !PyUnicode_Check(value)) {
544538
PyErr_SetString(PyExc_TypeError,
545539
"__qualname__ must be set to a string object");
546540
return -1;
547541
}
548-
tmp = op->gi_qualname;
549542
Py_INCREF(value);
550-
op->gi_qualname = value;
551-
Py_DECREF(tmp);
543+
Py_SETREF(op->gi_qualname, value);
552544
return 0;
553545
}
554546

0 commit comments

Comments
 (0)