@@ -459,8 +459,7 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
459
459
state -> start = (void * ) ((char * ) ptr + start * state -> charsize );
460
460
state -> end = (void * ) ((char * ) ptr + end * state -> charsize );
461
461
462
- Py_INCREF (string );
463
- state -> string = string ;
462
+ state -> string = Py_NewRef (string );
464
463
state -> pos = start ;
465
464
state -> endpos = end ;
466
465
@@ -499,8 +498,7 @@ getslice(int isbytes, const void *ptr,
499
498
if (isbytes ) {
500
499
if (PyBytes_CheckExact (string ) &&
501
500
start == 0 && end == PyBytes_GET_SIZE (string )) {
502
- Py_INCREF (string );
503
- return string ;
501
+ return Py_NewRef (string );
504
502
}
505
503
return PyBytes_FromStringAndSize (
506
504
(const char * )ptr + start , end - start );
@@ -1089,8 +1087,7 @@ pattern_subx(_sremodulestate* module_state,
1089
1087
1090
1088
if (PyCallable_Check (ptemplate )) {
1091
1089
/* sub/subn takes either a function or a template */
1092
- filter = ptemplate ;
1093
- Py_INCREF (filter );
1090
+ filter = Py_NewRef (ptemplate );
1094
1091
filter_type = CALLABLE ;
1095
1092
} else {
1096
1093
/* if not callable, check if it's a literal string */
@@ -1109,8 +1106,7 @@ pattern_subx(_sremodulestate* module_state,
1109
1106
if (view .buf )
1110
1107
PyBuffer_Release (& view );
1111
1108
if (literal ) {
1112
- filter = ptemplate ;
1113
- Py_INCREF (filter );
1109
+ filter = Py_NewRef (ptemplate );
1114
1110
filter_type = LITERAL ;
1115
1111
} else {
1116
1112
/* not a literal; hand it over to the template compiler */
@@ -1120,8 +1116,8 @@ pattern_subx(_sremodulestate* module_state,
1120
1116
1121
1117
assert (Py_TYPE (filter ) == module_state -> Template_Type );
1122
1118
if (Py_SIZE (filter ) == 0 ) {
1123
- Py_INCREF ((( TemplateObject * ) filter ) -> literal );
1124
- Py_SETREF ( filter , (( TemplateObject * )filter )-> literal );
1119
+ Py_SETREF ( filter ,
1120
+ Py_NewRef ((( TemplateObject * )filter )-> literal ) );
1125
1121
filter_type = LITERAL ;
1126
1122
}
1127
1123
else {
@@ -1195,8 +1191,7 @@ pattern_subx(_sremodulestate* module_state,
1195
1191
goto error ;
1196
1192
} else {
1197
1193
/* filter is literal string */
1198
- item = filter ;
1199
- Py_INCREF (item );
1194
+ item = Py_NewRef (filter );
1200
1195
}
1201
1196
1202
1197
/* add to list */
@@ -1317,8 +1312,7 @@ static PyObject *
1317
1312
_sre_SRE_Pattern___copy___impl (PatternObject * self )
1318
1313
/*[clinic end generated code: output=85dedc2db1bd8694 input=a730a59d863bc9f5]*/
1319
1314
{
1320
- Py_INCREF (self );
1321
- return (PyObject * )self ;
1315
+ return Py_NewRef (self );
1322
1316
}
1323
1317
1324
1318
/*[clinic input]
@@ -1333,8 +1327,7 @@ static PyObject *
1333
1327
_sre_SRE_Pattern___deepcopy__ (PatternObject * self , PyObject * memo )
1334
1328
/*[clinic end generated code: output=2ad25679c1f1204a input=a465b1602f997bed]*/
1335
1329
{
1336
- Py_INCREF (self );
1337
- return (PyObject * )self ;
1330
+ return Py_NewRef (self );
1338
1331
}
1339
1332
1340
1333
static PyObject *
@@ -1500,19 +1493,16 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
1500
1493
PyBuffer_Release (& view );
1501
1494
}
1502
1495
1503
- Py_INCREF (pattern );
1504
- self -> pattern = pattern ;
1496
+ self -> pattern = Py_NewRef (pattern );
1505
1497
1506
1498
self -> flags = flags ;
1507
1499
1508
1500
self -> groups = groups ;
1509
1501
1510
1502
if (PyDict_GET_SIZE (groupindex ) > 0 ) {
1511
- Py_INCREF (groupindex );
1512
- self -> groupindex = groupindex ;
1503
+ self -> groupindex = Py_NewRef (groupindex );
1513
1504
if (PyTuple_GET_SIZE (indexgroup ) > 0 ) {
1514
- Py_INCREF (indexgroup );
1515
- self -> indexgroup = indexgroup ;
1505
+ self -> indexgroup = Py_NewRef (indexgroup );
1516
1506
}
1517
1507
}
1518
1508
@@ -1555,8 +1545,7 @@ _sre_template_impl(PyObject *module, PyObject *pattern, PyObject *template)
1555
1545
if (!self )
1556
1546
return NULL ;
1557
1547
self -> chunks = 1 + 2 * n ;
1558
- self -> literal = PyList_GET_ITEM (template , 0 );
1559
- Py_INCREF (self -> literal );
1548
+ self -> literal = Py_NewRef (PyList_GET_ITEM (template , 0 ));
1560
1549
for (Py_ssize_t i = 0 ; i < n ; i ++ ) {
1561
1550
Py_ssize_t index = PyLong_AsSsize_t (PyList_GET_ITEM (template , 2 * i + 1 ));
1562
1551
if (index == -1 && PyErr_Occurred ()) {
@@ -1576,8 +1565,7 @@ _sre_template_impl(PyObject *module, PyObject *pattern, PyObject *template)
1576
1565
literal = NULL ;
1577
1566
self -> chunks -- ;
1578
1567
}
1579
- Py_XINCREF (literal );
1580
- self -> items [i ].literal = literal ;
1568
+ self -> items [i ].literal = Py_XNewRef (literal );
1581
1569
}
1582
1570
return (PyObject * ) self ;
1583
1571
@@ -2128,8 +2116,7 @@ match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def)
2128
2116
2129
2117
if (self -> string == Py_None || self -> mark [index ] < 0 ) {
2130
2118
/* return default value if the string or group is undefined */
2131
- Py_INCREF (def );
2132
- return def ;
2119
+ return Py_NewRef (def );
2133
2120
}
2134
2121
2135
2122
ptr = getstring (self -> string , & length , & isbytes , & charsize , & view );
@@ -2448,8 +2435,7 @@ match_regs(MatchObject* self)
2448
2435
PyTuple_SET_ITEM (regs , index , item );
2449
2436
}
2450
2437
2451
- Py_INCREF (regs );
2452
- self -> regs = regs ;
2438
+ self -> regs = Py_NewRef (regs );
2453
2439
2454
2440
return regs ;
2455
2441
}
@@ -2463,8 +2449,7 @@ static PyObject *
2463
2449
_sre_SRE_Match___copy___impl (MatchObject * self )
2464
2450
/*[clinic end generated code: output=a779c5fc8b5b4eb4 input=3bb4d30b6baddb5b]*/
2465
2451
{
2466
- Py_INCREF (self );
2467
- return (PyObject * )self ;
2452
+ return Py_NewRef (self );
2468
2453
}
2469
2454
2470
2455
/*[clinic input]
@@ -2479,8 +2464,7 @@ static PyObject *
2479
2464
_sre_SRE_Match___deepcopy__ (MatchObject * self , PyObject * memo )
2480
2465
/*[clinic end generated code: output=ba7cb46d655e4ee2 input=779d12a31c2c325e]*/
2481
2466
{
2482
- Py_INCREF (self );
2483
- return (PyObject * )self ;
2467
+ return Py_NewRef (self );
2484
2468
}
2485
2469
2486
2470
PyDoc_STRVAR (match_doc ,
@@ -2509,8 +2493,7 @@ match_lastgroup_get(MatchObject *self, void *Py_UNUSED(ignored))
2509
2493
{
2510
2494
PyObject * result = PyTuple_GET_ITEM (self -> pattern -> indexgroup ,
2511
2495
self -> lastindex );
2512
- Py_INCREF (result );
2513
- return result ;
2496
+ return Py_NewRef (result );
2514
2497
}
2515
2498
Py_RETURN_NONE ;
2516
2499
}
@@ -2519,8 +2502,7 @@ static PyObject *
2519
2502
match_regs_get (MatchObject * self , void * Py_UNUSED (ignored ))
2520
2503
{
2521
2504
if (self -> regs ) {
2522
- Py_INCREF (self -> regs );
2523
- return self -> regs ;
2505
+ return Py_NewRef (self -> regs );
2524
2506
} else
2525
2507
return match_regs (self );
2526
2508
}
@@ -2564,11 +2546,9 @@ pattern_new_match(_sremodulestate* module_state,
2564
2546
if (!match )
2565
2547
return NULL ;
2566
2548
2567
- Py_INCREF (pattern );
2568
- match -> pattern = pattern ;
2549
+ match -> pattern = (PatternObject * )Py_NewRef (pattern );
2569
2550
2570
- Py_INCREF (state -> string );
2571
- match -> string = state -> string ;
2551
+ match -> string = Py_NewRef (state -> string );
2572
2552
2573
2553
match -> regs = NULL ;
2574
2554
match -> groups = pattern -> groups + 1 ;
@@ -2788,8 +2768,7 @@ pattern_scanner(_sremodulestate *module_state,
2788
2768
return NULL ;
2789
2769
}
2790
2770
2791
- Py_INCREF (self );
2792
- scanner -> pattern = (PyObject * ) self ;
2771
+ scanner -> pattern = Py_NewRef (self );
2793
2772
2794
2773
PyObject_GC_Track (scanner );
2795
2774
return (PyObject * ) scanner ;
@@ -2834,8 +2813,7 @@ static PyObject *
2834
2813
expand_template (TemplateObject * self , MatchObject * match )
2835
2814
{
2836
2815
if (Py_SIZE (self ) == 0 ) {
2837
- Py_INCREF (self -> literal );
2838
- return self -> literal ;
2816
+ return Py_NewRef (self -> literal );
2839
2817
}
2840
2818
2841
2819
PyObject * result = NULL ;
@@ -2855,8 +2833,7 @@ expand_template(TemplateObject *self, MatchObject *match)
2855
2833
out = & PyList_GET_ITEM (list , 0 );
2856
2834
}
2857
2835
2858
- Py_INCREF (self -> literal );
2859
- out [count ++ ] = self -> literal ;
2836
+ out [count ++ ] = Py_NewRef (self -> literal );
2860
2837
for (Py_ssize_t i = 0 ; i < Py_SIZE (self ); i ++ ) {
2861
2838
Py_ssize_t index = self -> items [i ].index ;
2862
2839
if (index >= match -> groups ) {
@@ -2868,15 +2845,13 @@ expand_template(TemplateObject *self, MatchObject *match)
2868
2845
goto cleanup ;
2869
2846
}
2870
2847
if (item != Py_None ) {
2871
- Py_INCREF (item );
2872
- out [count ++ ] = item ;
2848
+ out [count ++ ] = Py_NewRef (item );
2873
2849
}
2874
2850
Py_DECREF (item );
2875
2851
2876
2852
PyObject * literal = self -> items [i ].literal ;
2877
2853
if (literal != NULL ) {
2878
- Py_INCREF (literal );
2879
- out [count ++ ] = literal ;
2854
+ out [count ++ ] = Py_NewRef (literal );
2880
2855
}
2881
2856
}
2882
2857
0 commit comments