@@ -189,8 +189,7 @@ divide_nearest(PyObject *m, PyObject *n)
189
189
temp = _PyLong_DivmodNear (m , n );
190
190
if (temp == NULL )
191
191
return NULL ;
192
- result = PyTuple_GET_ITEM (temp , 0 );
193
- Py_INCREF (result );
192
+ result = Py_NewRef (PyTuple_GET_ITEM (temp , 0 ));
194
193
Py_DECREF (temp );
195
194
196
195
return result ;
@@ -1005,8 +1004,7 @@ new_datetime_ex2(int year, int month, int day, int hour, int minute,
1005
1004
DATE_SET_SECOND (self , second );
1006
1005
DATE_SET_MICROSECOND (self , usecond );
1007
1006
if (aware ) {
1008
- Py_INCREF (tzinfo );
1009
- self -> tzinfo = tzinfo ;
1007
+ self -> tzinfo = Py_NewRef (tzinfo );
1010
1008
}
1011
1009
DATE_SET_FOLD (self , fold );
1012
1010
}
@@ -1083,8 +1081,7 @@ new_time_ex2(int hour, int minute, int second, int usecond,
1083
1081
TIME_SET_SECOND (self , second );
1084
1082
TIME_SET_MICROSECOND (self , usecond );
1085
1083
if (aware ) {
1086
- Py_INCREF (tzinfo );
1087
- self -> tzinfo = tzinfo ;
1084
+ self -> tzinfo = Py_NewRef (tzinfo );
1088
1085
}
1089
1086
TIME_SET_FOLD (self , fold );
1090
1087
}
@@ -1165,10 +1162,8 @@ create_timezone(PyObject *offset, PyObject *name)
1165
1162
if (self == NULL ) {
1166
1163
return NULL ;
1167
1164
}
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 );
1172
1167
return (PyObject * )self ;
1173
1168
}
1174
1169
@@ -1182,8 +1177,7 @@ new_timezone(PyObject *offset, PyObject *name)
1182
1177
assert (name == NULL || PyUnicode_Check (name ));
1183
1178
1184
1179
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 );
1187
1181
}
1188
1182
if ((GET_TD_DAYS (offset ) == -1 &&
1189
1183
GET_TD_SECONDS (offset ) == 0 &&
@@ -1397,8 +1391,7 @@ tzinfo_from_isoformat_results(int rv, int tzoffset, int tz_useconds)
1397
1391
if (rv == 1 ) {
1398
1392
// Create a timezone from offset in seconds (0 returns UTC)
1399
1393
if (tzoffset == 0 ) {
1400
- Py_INCREF (PyDateTime_TimeZone_UTC );
1401
- return PyDateTime_TimeZone_UTC ;
1394
+ return Py_NewRef (PyDateTime_TimeZone_UTC );
1402
1395
}
1403
1396
1404
1397
PyObject * delta = new_delta (0 , tzoffset , tz_useconds , 1 );
@@ -1409,8 +1402,7 @@ tzinfo_from_isoformat_results(int rv, int tzoffset, int tz_useconds)
1409
1402
Py_DECREF (delta );
1410
1403
}
1411
1404
else {
1412
- tzinfo = Py_None ;
1413
- Py_INCREF (Py_None );
1405
+ tzinfo = Py_NewRef (Py_None );
1414
1406
}
1415
1407
1416
1408
return tzinfo ;
@@ -1943,8 +1935,7 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
1943
1935
goto BadDivmod ;
1944
1936
}
1945
1937
1946
- num = PyTuple_GET_ITEM (tuple , 0 ); /* leftover seconds */
1947
- Py_INCREF (num );
1938
+ num = Py_NewRef (PyTuple_GET_ITEM (tuple , 0 )); /* leftover seconds */
1948
1939
Py_DECREF (tuple );
1949
1940
1950
1941
tuple = checked_divmod (num , seconds_per_day );
@@ -1962,8 +1953,7 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
1962
1953
goto BadDivmod ;
1963
1954
}
1964
1955
1965
- num = PyTuple_GET_ITEM (tuple , 0 ); /* leftover days */
1966
- Py_INCREF (num );
1956
+ num = Py_NewRef (PyTuple_GET_ITEM (tuple , 0 )); /* leftover days */
1967
1957
d = _PyLong_AsInt (num );
1968
1958
if (d == -1 && PyErr_Occurred ()) {
1969
1959
goto Done ;
@@ -3346,8 +3336,7 @@ iso_calendar_date_year(PyDateTime_IsoCalendarDate *self, void *unused)
3346
3336
if (year == NULL ) {
3347
3337
return NULL ;
3348
3338
}
3349
- Py_INCREF (year );
3350
- return year ;
3339
+ return Py_NewRef (year );
3351
3340
}
3352
3341
3353
3342
static PyObject *
@@ -3357,8 +3346,7 @@ iso_calendar_date_week(PyDateTime_IsoCalendarDate *self, void *unused)
3357
3346
if (week == NULL ) {
3358
3347
return NULL ;
3359
3348
}
3360
- Py_INCREF (week );
3361
- return week ;
3349
+ return Py_NewRef (week );
3362
3350
}
3363
3351
3364
3352
static PyObject *
@@ -3368,8 +3356,7 @@ iso_calendar_date_weekday(PyDateTime_IsoCalendarDate *self, void *unused)
3368
3356
if (weekday == NULL ) {
3369
3357
return NULL ;
3370
3358
}
3371
- Py_INCREF (weekday );
3372
- return weekday ;
3359
+ return Py_NewRef (weekday );
3373
3360
}
3374
3361
3375
3362
static PyGetSetDef iso_calendar_date_getset [] = {
@@ -3980,8 +3967,7 @@ timezone_str(PyDateTime_TimeZone *self)
3980
3967
char sign ;
3981
3968
3982
3969
if (self -> name != NULL ) {
3983
- Py_INCREF (self -> name );
3984
- return self -> name ;
3970
+ return Py_NewRef (self -> name );
3985
3971
}
3986
3972
if ((PyObject * )self == PyDateTime_TimeZone_UTC ||
3987
3973
(GET_TD_DAYS (self -> offset ) == 0 &&
@@ -3997,8 +3983,7 @@ timezone_str(PyDateTime_TimeZone *self)
3997
3983
}
3998
3984
else {
3999
3985
sign = '+' ;
4000
- offset = self -> offset ;
4001
- Py_INCREF (offset );
3986
+ offset = Py_NewRef (self -> offset );
4002
3987
}
4003
3988
/* Offset is not negative here. */
4004
3989
microseconds = GET_TD_MICROSECONDS (offset );
@@ -4033,8 +4018,7 @@ timezone_utcoffset(PyDateTime_TimeZone *self, PyObject *dt)
4033
4018
if (_timezone_check_argument (dt , "utcoffset" ) == -1 )
4034
4019
return NULL ;
4035
4020
4036
- Py_INCREF (self -> offset );
4037
- return self -> offset ;
4021
+ return Py_NewRef (self -> offset );
4038
4022
}
4039
4023
4040
4024
static PyObject *
@@ -4171,8 +4155,7 @@ static PyObject *
4171
4155
time_tzinfo (PyDateTime_Time * self , void * unused )
4172
4156
{
4173
4157
PyObject * result = HASTZINFO (self ) ? self -> tzinfo : Py_None ;
4174
- Py_INCREF (result );
4175
- return result ;
4158
+ return Py_NewRef (result );
4176
4159
}
4177
4160
4178
4161
static PyObject *
@@ -4217,8 +4200,7 @@ time_from_pickle(PyTypeObject *type, PyObject *state, PyObject *tzinfo)
4217
4200
me -> hashcode = -1 ;
4218
4201
me -> hastzinfo = aware ;
4219
4202
if (aware ) {
4220
- Py_INCREF (tzinfo );
4221
- me -> tzinfo = tzinfo ;
4203
+ me -> tzinfo = Py_NewRef (tzinfo );
4222
4204
}
4223
4205
if (pdata [0 ] & (1 << 7 )) {
4224
4206
me -> data [0 ] -= 128 ;
@@ -4514,12 +4496,10 @@ time_richcompare(PyObject *self, PyObject *other, int op)
4514
4496
result = diff_to_bool (diff , op );
4515
4497
}
4516
4498
else if (op == Py_EQ ) {
4517
- result = Py_False ;
4518
- Py_INCREF (result );
4499
+ result = Py_NewRef (Py_False );
4519
4500
}
4520
4501
else if (op == Py_NE ) {
4521
- result = Py_True ;
4522
- Py_INCREF (result );
4502
+ result = Py_NewRef (Py_True );
4523
4503
}
4524
4504
else {
4525
4505
PyErr_SetString (PyExc_TypeError ,
@@ -4548,8 +4528,7 @@ time_hash(PyDateTime_Time *self)
4548
4528
return -1 ;
4549
4529
}
4550
4530
else {
4551
- self0 = (PyObject * )self ;
4552
- Py_INCREF (self0 );
4531
+ self0 = Py_NewRef (self );
4553
4532
}
4554
4533
offset = time_utcoffset (self0 , NULL );
4555
4534
Py_DECREF (self0 );
@@ -4846,8 +4825,7 @@ static PyObject *
4846
4825
datetime_tzinfo (PyDateTime_DateTime * self , void * unused )
4847
4826
{
4848
4827
PyObject * result = HASTZINFO (self ) ? self -> tzinfo : Py_None ;
4849
- Py_INCREF (result );
4850
- return result ;
4828
+ return Py_NewRef (result );
4851
4829
}
4852
4830
4853
4831
static PyObject *
@@ -4894,8 +4872,7 @@ datetime_from_pickle(PyTypeObject *type, PyObject *state, PyObject *tzinfo)
4894
4872
me -> hashcode = -1 ;
4895
4873
me -> hastzinfo = aware ;
4896
4874
if (aware ) {
4897
- Py_INCREF (tzinfo );
4898
- me -> tzinfo = tzinfo ;
4875
+ me -> tzinfo = Py_NewRef (tzinfo );
4899
4876
}
4900
4877
if (pdata [2 ] & (1 << 7 )) {
4901
4878
me -> data [2 ] -= 128 ;
@@ -5307,8 +5284,7 @@ _sanitize_isoformat_str(PyObject *dtstr)
5307
5284
}
5308
5285
5309
5286
if (surrogate_separator == 0 ) {
5310
- Py_INCREF (dtstr );
5311
- return dtstr ;
5287
+ return Py_NewRef (dtstr );
5312
5288
}
5313
5289
5314
5290
PyObject * str_out = _PyUnicode_Copy (dtstr );
@@ -5622,9 +5598,8 @@ datetime_subtract(PyObject *left, PyObject *right)
5622
5598
int delta_d , delta_s , delta_us ;
5623
5599
5624
5600
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 );
5628
5603
}
5629
5604
else {
5630
5605
offset1 = datetime_utcoffset (left , NULL );
@@ -5969,12 +5944,10 @@ datetime_richcompare(PyObject *self, PyObject *other, int op)
5969
5944
result = diff_to_bool (diff , op );
5970
5945
}
5971
5946
else if (op == Py_EQ ) {
5972
- result = Py_False ;
5973
- Py_INCREF (result );
5947
+ result = Py_NewRef (Py_False );
5974
5948
}
5975
5949
else if (op == Py_NE ) {
5976
- result = Py_True ;
5977
- Py_INCREF (result );
5950
+ result = Py_NewRef (Py_True );
5978
5951
}
5979
5952
else {
5980
5953
PyErr_SetString (PyExc_TypeError ,
@@ -6006,8 +5979,7 @@ datetime_hash(PyDateTime_DateTime *self)
6006
5979
return -1 ;
6007
5980
}
6008
5981
else {
6009
- self0 = (PyObject * )self ;
6010
- Py_INCREF (self0 );
5982
+ self0 = Py_NewRef (self );
6011
5983
}
6012
5984
offset = datetime_utcoffset (self0 , NULL );
6013
5985
Py_DECREF (self0 );
@@ -6224,15 +6196,13 @@ datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
6224
6196
if (self_tzinfo == NULL )
6225
6197
return NULL ;
6226
6198
} else {
6227
- self_tzinfo = self -> tzinfo ;
6228
- Py_INCREF (self_tzinfo );
6199
+ self_tzinfo = Py_NewRef (self -> tzinfo );
6229
6200
}
6230
6201
6231
6202
/* Conversion to self's own time zone is a NOP. */
6232
6203
if (self_tzinfo == tzinfo ) {
6233
6204
Py_DECREF (self_tzinfo );
6234
- Py_INCREF (self );
6235
- return self ;
6205
+ return (PyDateTime_DateTime * )Py_NewRef (self );
6236
6206
}
6237
6207
6238
6208
/* Convert self to UTC. */
@@ -6278,8 +6248,7 @@ datetime_astimezone(PyDateTime_DateTime *self, PyObject *args, PyObject *kw)
6278
6248
else {
6279
6249
/* Result is already aware - just replace tzinfo. */
6280
6250
temp = result -> tzinfo ;
6281
- result -> tzinfo = PyDateTime_TimeZone_UTC ;
6282
- Py_INCREF (result -> tzinfo );
6251
+ result -> tzinfo = Py_NewRef (PyDateTime_TimeZone_UTC );
6283
6252
Py_DECREF (temp );
6284
6253
}
6285
6254
@@ -6449,8 +6418,7 @@ datetime_utctimetuple(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored))
6449
6418
6450
6419
tzinfo = GET_DT_TZINFO (self );
6451
6420
if (tzinfo == Py_None ) {
6452
- utcself = self ;
6453
- Py_INCREF (utcself );
6421
+ utcself = (PyDateTime_DateTime * )Py_NewRef (self );
6454
6422
}
6455
6423
else {
6456
6424
PyObject * offset ;
@@ -6459,8 +6427,7 @@ datetime_utctimetuple(PyDateTime_DateTime *self, PyObject *Py_UNUSED(ignored))
6459
6427
return NULL ;
6460
6428
if (offset == Py_None ) {
6461
6429
Py_DECREF (offset );
6462
- utcself = self ;
6463
- Py_INCREF (utcself );
6430
+ utcself = (PyDateTime_DateTime * )Py_NewRef (self );
6464
6431
}
6465
6432
else {
6466
6433
utcself = (PyDateTime_DateTime * )add_datetime_timedelta (self ,
0 commit comments