8000 gh-99300: Use Py_NewRef() in Modules/_datetimemodule.c (#99465) · python/cpython@f15a0fc · GitHub
[go: up one dir, main page]

Skip to content

Commit f15a0fc

Browse files
authored
gh-99300: Use Py_NewRef() in Modules/_datetimemodule.c (#99465)
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in Modules/_datetimemodule.c and Modules/_zoneinfo.c
1 parent 7e4dec0 commit f15a0fc

File tree

2 files changed

+49
-98
lines changed

2 files changed

+49
-98
lines changed

Modules/_datetimemodule.c

Lines changed: 34 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ divide_nearest(PyObject *m, PyObject *n)
189189
temp = _PyLong_DivmodNear(m, n);
190190
if (temp == NULL)
191191
return NULL;
192-
result = PyTuple_GET_ITEM(temp, 0);
193-
Py_INCREF(result);
192+
result = Py_NewRef(PyTuple_GET_ITEM(temp, 0));
194193
Py_DECREF(temp);
195194

196195
return result;
@@ -1005,8 +1004,7 @@ new_datetime_ex2(int year, int month, int day, int hour, int minute,
10051004
DATE_SET_SECOND(self, second);
10061005
DATE_SET_MICROSECOND(self, usecond);
10071006
if (aware) {
1008-
Py_INCREF(tzinfo);
1009-
self->tzinfo = tzinfo;
1007+
self->tzinfo = Py_NewRef(tzinfo);
10101008
}
10111009
DATE_SET_FOLD(self, fold);
10121010
}
@@ -1083,8 +1081,7 @@ new_time_ex2(int hour, int minute, int second, int usecond,
10831081
TIME_SET_SECOND(self, second);
10841082
TIME_SET_MICROSECOND(self, usecond);
10851083
if (aware) {
1086-
Py_INCREF(tzinfo);
1087-
self->tzinfo = tzinfo;
1084+
self->tzinfo = Py_NewRef(tzinfo);
10881085
}
10891086
TIME_SET_FOLD(self, fold);
10901087
}
@@ -1165,10 +1162,8 @@ create_timezone(PyObject *offset, PyObject *name)
11651162
if (self == NULL) {
11661163
return NULL;
11671164
}
1168-
Py_INCREF(offset);
1169-
self->offset = offset;
1170-
Py_XINCREF(name);
1171-
self->name = name;
1165+
self->offset = Py_NewRef(offset);
1166+
self->name = Py_XNewRef(name);
11721167
return (PyObject *)self;
11731168
}
11741169

@@ -1182,8 +1177,7 @@ new_timezone(PyObject *offset, PyObject *name)
11821177
assert(name == NULL || PyUnicode_Check(name));
11831178

11841179
if (name == NULL && delta_bool((PyDateTime_Delta *)offset) == 0) {
1185-
Py_INCREF(PyDateTime_TimeZone_UTC);
1186-
return PyDateTime_TimeZone_UTC;
1180+
return Py_NewRef(PyDateTime_TimeZone_UTC);
11871181
}
11881182
if ((GET_TD_DAYS(offset) == -1 &&
11891183
GET_TD_SECONDS(offset) == 0 &&
@@ -1397,8 +1391,7 @@ tzinfo_from_isoformat_results(int rv, int tzoffset, int tz_useconds)
13971391
if (rv == 1) {
13981392
// Create a timezone from offset in seconds (0 returns UTC)
13991393
if (tzoffset == 0) {
1400-
Py_INCREF(PyDateTime_TimeZone_UTC);
1401-
return PyDateTime_TimeZone_UTC;
1394+
return Py_NewRef(PyDateTime_TimeZone_UTC);
14021395
}
14031396

14041397
PyObject *delta = new_delta(0, tzoffset, tz_useconds, 1);
@@ -1409,8 +1402,7 @@ tzinfo_from_isoformat_results(int rv, int tzoffset, int tz_useconds)
14091402
Py_DECREF(delta);
14101403
}
14111404
else {
1412-
tzinfo = Py_None;
1413-
Py_INCREF(Py_None);
1405+
tzinfo = Py_NewRef(Py_None);
14141406
}
14151407

14161408
return tzinfo;
@@ -1943,8 +1935,7 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
19431935
goto BadDivmod;
19441936
}
19451937

1946-
num = PyTuple_GET_ITEM(tuple, 0); /* leftover seconds */
1947-
Py_INCREF(num);
1938+
num = Py_NewRef(PyTuple_GET_ITEM(tuple, 0)); /* leftover seconds */
19481939
Py_DECREF(tuple);
19491940

19501941
tuple = checked_divmod(num, seconds_per_day);
@@ -1962,8 +1953,7 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
19621953
goto BadDivmod;
19631954
}
19641955

1965-
num = PyTuple_GET_ITEM(tuple, 0); /* leftover days */
1966-
Py_INCREF(num);
1956+
num = Py_NewRef(PyTuple_GET_ITEM(tuple, 0)); /* leftover days */
19671957
d = _PyLong_AsInt(num);
19681958
if (d == -1 && PyErr_Occurred()) {
19691959
goto Done;
@@ -3346,8 +3336,7 @@ iso_calendar_date_year(PyDateTime_IsoCalendarDate *self, void *unused)
33463336
if (year == NULL) {
33473337
return NULL;
33483338
}
3349-
Py_INCREF(year);
3350-
return year;
3339+
return Py_NewRef(year);
33513340
}
33523341

33533342
static PyObject *
@@ -3357,8 +3346,7 @@ iso_calendar_date_week(PyDateTime_IsoCalendarDate *self, void *unused)
33573346
if (week == NULL) {
33583347
return NULL;
33593348
}
3360-
Py_INCREF(week);
3361-
return week;
3349+
return Py_NewRef(week);
33623350
}
33633351

33643352
static PyObject *
@@ -3368,8 +3356,7 @@ iso_calendar_date_weekday(PyDateTime_IsoCalendarDate *self, void *unused)
33683356
if (weekday == NULL) {
33693357
return NULL;
33703358
}
3371-
Py_INCREF(weekday);
3372-
return weekday;
3359+
return Py_NewRef(weekday);
33733360
}
33743361

33753362
static PyGetSetDef iso_calendar_date_getset[] = {
@@ -3980,8 +3967,7 @@ timezone_str(PyDateTime_TimeZone *self)
39803967
char sign;
39813968

39823969
if (self->name != NULL) {
3983-
Py_INCREF(self->name);
3984-
return self->name;
3970+
return Py_NewRef(self->name);
39853971
}
39863972
if ((PyObject *)self == PyDateTime_TimeZone_UTC ||
39873973
(GET_TD_DAYS(self->offset) == 0 &&
@@ -3997,8 +3983,7 @@ timezone_str(PyDateTime_TimeZone *self)
39973983
}
39983984
else {
39993985
sign = '+';
4000-
offset = self->offset;
4001-
Py_INCREF(offset);
3986+
offset = Py_NewRef(self->offset);
40023987
}
40033988
/* Offset is not negative here. */
40043989
microseconds = GET_TD_MICROSECONDS(offset);
@@ -4033,8 +4018,7 @@ timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt)
40334018
if (_timezone_check_argument(dt, "utcoffset") == -1)
40344019
return NULL;
40354020

4036-
Py_INCREF(self->offset);
4037-
return self->offset;
4021+
return Py_NewRef(self->offset);
40384022
}
40394023

40404024
static PyObject *
@@ -4171,8 +4155,7 @@ static PyObject *
41714155
time_tzinfo(PyDateTime_Time *self, void *unused)
41724156
{
41734157
PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None;
4174-
Py_INCREF(result);
4175-
return result;
4158+
return Py_NewRef(result);
41764159
}
41774160

41784161
static PyObject *
@@ -4217,8 +4200,7 @@ time_from_pickle(PyTypeObject *type, PyObject *state, PyObject *tzinfo)
42174200
me->hashcode = -1;
42184201
me->hastzinfo = aware;
42194202
if (aware) {
4220-
Py_INCREF(tzinfo);
4221-
me->tzinfo = tzinfo;
4203+
me->tzinfo = Py_NewRef(tzinfo);
42224204
}
42234205
if (pdata[0] & (1 << 7)) {
42244206
me->data[0] -= 128;
@@ -4514,12 +4496,10 @@ time_richcompare(PyObject *self, PyObject *other, int op)
45144496
result = diff_to_bool(diff, op);
45154497
}
45164498
else if (op == Py_EQ) {
4517-
result = Py_False;
4518-
Py_INCREF(result);
4499+
result = Py_NewRef(Py_False);
45194500
}
45204501
else if (op == Py_NE) {
4521-
result = Py_True;
4522-
Py_INCREF(result);
4502+
result = Py_NewRef(Py_True);
45234503
}
45244504
else {
45254505
PyErr_SetString(PyExc_TypeError,
@@ -4548,8 +4528,7 @@ time_hash(PyDateTime_Time *self)
45484528
return -1;
45494529
}
45504530
else {
4551-
self0 = (PyObject *)self;
4552-
Py_INCREF(self0);
4531+
self0 = Py_NewRef(self);
45534532
}
45544533
offset = time_utcoffset(self0, NULL);
45554534
Py_DECREF(self0);
@@ -4846,8 +4825,7 @@ static PyObject *
48464825
datetime_tzinfo(PyDateTime_DateTime *self, void *unused)
48474826
{
48484827
PyObject *result = HASTZINFO(self) ? self->tzinfo : Py_None;
4849-
Py_INCREF(result);
4850-
return result;
4828+
return Py_NewRef(result);
48514829
}
48524830

48534831
static PyObject *
@@ -4894,8 +4872,7 @@ datetime_from_pickle(PyTypeObject *type, PyObject *state, PyObject *tzinfo)
48944872
me->hashcode = -1;
48954873
me->hastzinfo = aware;
48964874
if (aware) {
4897-
Py_INCREF(tzinfo);
4898-
me->tzinfo = tzinfo;
4875+
me->tzinfo = Py_NewRef(tzinfo);
48994876
}
49004877
if (pdata[2] & (1 << 7)) {
49014878
me->data[2] -= 128;
@@ -5307,8 +5284,7 @@ _sanitize_isoformat_str(PyObject *dtstr)
53075284
}
53085285

53095286
if (surrogate_separator == 0) {
5310-
Py_INCREF(dtstr);
5311-
return dtstr;
5287+
return Py_NewRef(dtstr);
53125288
}
53135289

53145290
PyObject *str_out = _PyUnicode_Copy(dtstr);
@@ -5622,9 +5598,8 @@ datetime_subtract(PyObject *left, PyObject *right)
56225598
int delta_d, delta_s, delta_us;
56235599

56245600
if (GET_DT_TZINFO(left) == GET_DT_TZINFO(right)) {
5625-
offset2 = offset1 = Py_None;
5626-
Py_INCREF(offset1);
5627-
Py_INCREF(offset2);
5601+
offset1 = Py_NewRef(Py_None);
5602+
offset2 = Py_NewRef(Py_None);
56285603
}
56295604
else {
56305605
offset1 = datetime_utcoffset(left, NULL);
@@ -5969,12 +5944,10 @@ datetime_richcompare(PyObject *self, PyObject *other, int op)
59695944
result = diff_to_bool(diff, op);
59705945
}
59715946
else if (op == Py_EQ) {
5972-
result = Py_False;
5973-
Py_INCREF(result);
5947+
result = Py_NewRef(Py_False);
59745948
}
59755949
else if (op == Py_NE) {
5976-
result = Py_True;
5977-
Py_INCREF(result);
5950+
result = Py_NewRef(Py_True);
59785951
}
59795952
else {
59805953
PyErr_SetString(PyExc_TypeError,
@@ -6006,8 +5979,7 @@ datetime_hash(PyDateTime_DateTime *self)
60065979
return -1;
60075980
}
60085981
else {
6009-
self0 = (PyObject *)self;
6010-
Py_INCREF(self0);
5982+
self0 = Py_NewRef(self);
60115983
}
60125984
offset = datetime_utcoffset(self0, NULL);
60135985
Py_DECREF(self0);
@@ -6224,15 +6196,13 @@ datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
62246196
if (self_tzinfo == NULL)
62256197
return NULL;
62266198
} else {
6227-
self_tzinfo = self->tzinfo;
6228-
Py_INCREF(self_tzinfo);
6199+
self_tzinfo = Py_NewRef(self->tzinfo);
62296200
}
62306201

62316202
/* Conversion to self's own time zone is a NOP. */
62326203
if (self_tzinfo == tzinfo) {
62336204
Py_DECREF(self_tzinfo);
6234-
Py_INCREF(self);
6235-
return self;
6205+
return (PyDateTime_DateTime*)Py_NewRef(self);
62366206
}
62376207

62386208
/* Convert self to UTC. */
@@ -6278,8 +6248,7 @@ datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
62786248
else {
62796249
/* Result is already aware - just replace tzinfo. */
62806250
temp = result->tzinfo;
6281-
result->tzinfo = PyDateTime_TimeZone_UTC;
6282-
Py_INCREF(result->tzinfo);
6251+
result->tzinfo = Py_NewRef(PyDateTime_TimeZone_UTC);
62836252
Py_DECREF(temp);
62846253
}
62856254

@@ -6449,8 +6418,7 @@ datetime_utctimetuple(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored))
64496418

64506419
tzinfo = GET_DT_TZINFO(self);
64516420
if (tzinfo == Py_None) {
6452-
utcself = self;
6453-
Py_INCREF(utcself);
6421+
utcself = (PyDateTime_DateTime*)Py_NewRef(self);
64546422
}
64556423
else {
64566424
PyObject *offset;
@@ -6459,8 +6427,7 @@ datetime_utctimetuple(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored))
64596427
return NULL;
64606428
if (offset == Py_None) {
64616429
Py_DECREF(offset);
6462-
utcself = self;
6463-
Py_INCREF(utcself);
6430+
utcself = (PyDateTime_DateTime*)Py_NewRef(self);
64646431
}
64656432
else {
64666433
utcself = (PyDateTime_DateTime *)add_datetime_timedelta(self,

0 commit comments

Comments
 (0)
0