-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest_upgrade_pythoncapi.py
962 lines (855 loc) · 27.4 KB
/
test_upgrade_pythoncapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
#!/usr/bin/env python3
import io
import os
import sys
import tempfile
import textwrap
import unittest
# Get upgrade_pythoncapi.py of the parent directory
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
import upgrade_pythoncapi # noqa
def operations(disable=None):
if isinstance(disable, str):
disable = (disable,)
elif not disable:
disable = ()
operations = ["all"]
for op in upgrade_pythoncapi.EXCLUDE_FROM_ALL:
if op.NAME in disable:
continue
operations.append(op.NAME)
for name in disable:
operations.append(f'-{name}')
operations = ','.join(operations)
return operations
def patch(source, no_compat=False, disable=None):
args = ['script', 'mod.c', '-o', operations(disable=disable)]
if no_compat:
args.append('--no-compat')
patcher = upgrade_pythoncapi.Patcher(args)
return patcher.patch(source)
def reformat(source):
return textwrap.dedent(source).strip()
class Tests(unittest.TestCase):
maxDiff = 80 * 30
def _patch_file(self, source, tmp_dir=None):
# test Patcher.patcher()
filename = tempfile.mktemp(suffix='.c', dir=tmp_dir)
old_filename = filename + ".old"
try:
with open(filename, "w", encoding="utf-8", newline="") as fp:
fp.write(source)
old_stderr = sys.stderr
old_argv = list(sys.argv)
try:
# redirect stderr
sys.stderr = io.StringIO()
if tmp_dir is not None:
arg = tmp_dir
else:
arg = filename
sys.argv = ['script', arg]
try:
upgrade_pythoncapi.Patcher().main()
except SystemExit as exc:
self.assertEqual(exc.code, 0)
else:
self.fail("SystemExit not raised")
finally:
sys.stderr = old_stderr
sys.argv = old_argv
with open(filename, encoding="utf-8", newline="") as fp:
new_contents = fp.read()
with open(old_filename, encoding="utf-8", newline="") as fp:
old_contents = fp.read()
finally:
try:
os.unlink(filename)
except FileNotFoundError:
pass
try:
os.unlink(old_filename)
except FileNotFoundError:
pass
self.assertEqual(old_contents, source)
return new_contents
def test_patch_file(self):
source = """
PyTypeObject*
test_type(PyObject *obj, PyTypeObject *type)
{
Py_TYPE(obj) = type;
return Py_TYPE(obj);
}
"""
expected = """
#include "pythoncapi_compat.h"
PyTypeObject*
test_type(PyObject *obj, PyTypeObject *type)
{
Py_SET_TYPE(obj, type);
return Py_TYPE(obj);
}
"""
source = reformat(source)
expected = reformat(expected)
new_contents = self._patch_file(source)
self.assertEqual(new_contents, expected)
with tempfile.TemporaryDirectory() as tmp_dir:
new_contents = self._patch_file(source, tmp_dir)
self.assertEqual(new_contents, expected)
def test_patch_file_preserve_newlines(self):
source = """
Py_ssize_t get_size(PyVarObject *obj)\r\n\
\n\
{ return obj->ob_size; }\r\
"""
expected = """
Py_ssize_t get_size(PyVarObject *obj)\r\n\
\n\
{ return Py_SIZE(obj); }\r\
"""
source = reformat(source)
expected = reformat(expected)
new_contents = self._patch_file(source)
self.assertEqual(new_contents, expected)
def check_replace(self, source, expected, **kwargs):
source = reformat(source)
expected = reformat(expected)
self.assertEqual(patch(source, **kwargs), expected)
def check_dont_replace(self, source, disable=None):
source = reformat(source)
self.assertEqual(patch(source, disable=disable), source)
def test_expr_regex(self):
# Test EXPR_REGEX
self.check_replace("a->b->ob_type", "Py_TYPE(a->b)")
self.check_replace("a.b->ob_type", "Py_TYPE(a.b)")
self.check_replace("array[2]->ob_type", "Py_TYPE(array[2])")
# Don't match function calls
self.check_dont_replace("func()->ob_type")
def test_pythoncapi_compat(self):
# If pythoncapi_compat.h is included, avoid compatibility includes
# and macros.
#
# Otherise, Py_SET_TYPE() requires a macro and PyFrame_GetBack()
# requires 2 macros and an include.
HEADERS = (
'<pythoncapi_compat.h>',
'"pythoncapi_compat.h"',
)
for header in HEADERS:
# There is no empty line between the include and the function
# on purpose.
self.check_replace("""
#include %s
void test_set_type(PyObject *obj, PyTypeObject *type)
{
Py_TYPE(obj) = type;
}
PyFrameObject* frame_back_borrowed(PyFrameObject *frame)
{
return frame->f_back;
}
""" % header, """
#include %s
void test_set_type(PyObject *obj, PyTypeObject *type)
{
Py_SET_TYPE(obj, type);
}
PyFrameObject* frame_back_borrowed(PyFrameObject *frame)
{
return _PyFrame_GetBackBorrow(frame);
}
""" % header)
def test_py_type(self):
source = """
PyTypeObject* get_type(PyObject *obj)
{ return obj->ob_type; }
"""
expected = """
PyTypeObject* get_type(PyObject *obj)
{ return Py_TYPE(obj); }
"""
self.check_replace(source, expected)
def test_py_size(self):
source = """
Py_ssize_t get_size(PyVarObject *obj)
{ return obj->ob_size; }
"""
expected = """
Py_ssize_t get_size(PyVarObject *obj)
{ return Py_SIZE(obj); }
"""
self.check_replace(source, expected)
def test_py_refcnt(self):
source = """
Py_ssize_t get_refcnt(PyObject *obj)
{ return obj->ob_refcnt; }
"""
expected = """
Py_ssize_t get_refcnt(PyObject *obj)
{ return Py_REFCNT(obj); }
"""
self.check_replace(source, expected)
def test_py_set_type(self):
source = """
void test_type(PyObject *obj, PyTypeObject *type)
{
obj->ob_type = type;
Py_TYPE(obj) = type;
}
"""
expected = """
#include "pythoncapi_compat.h"
void test_type(PyObject *obj, PyTypeObject *type)
{
Py_SET_TYPE(obj, type);
Py_SET_TYPE(obj, type);
}
"""
self.check_replace(source, expected)
self.check_dont_replace("""
PyTypeObject* get_type(PyObject *obj, PyTypeObject *check_type)
{
assert(Py_TYPE(args) == check_type);
return Py_TYPE(obj);
}
""")
def test_py_set_size(self):
source = """\
void test_size(PyVarObject *obj)
{
obj->ob_size = 3;
Py_SIZE(obj) = 4;
}
"""
expected = """\
#include "pythoncapi_compat.h"
void test_size(PyVarObject *obj)
{
Py_SET_SIZE(obj, 3);
Py_SET_SIZE(obj, 4);
}
"""
self.check_replace(source, expected)
self.check_dont_replace("""
Py_ssize_t
get_size(PyObject *obj)
{
assert(Py_SIZE(args) == 1);
return Py_SIZE(obj);
}
""")
def test_py_set_refcnt(self):
source = """\
void set_refcnt(PyObject *obj)
{
obj->ob_refcnt = 1;
Py_REFCNT(obj) = 2;
}
"""
expected = """\
#include "pythoncapi_compat.h"
void set_refcnt(PyObject *obj)
{
Py_SET_REFCNT(obj, 1);
Py_SET_REFCNT(obj, 2);
}
"""
self.check_replace(source, expected)
self.check_dont_replace("""
Py_ssize_t
get_refcnt(PyObject *obj)
{
assert(Py_REFCNT(args) == 1);
return Py_REFCNT(obj);
}
""")
def test_pyobject_new(self):
source = """\
capsule = PyObject_NEW(PyCapsule, &PyCapsule_Type);
pattern = PyObject_NEW_VAR(PatternObject, Pattern_Type, n);
"""
expected = """\
capsule = PyObject_New(PyCapsule, &PyCapsule_Type);
pattern = PyObject_NewVar(PatternObject, Pattern_Type, n);
"""
self.check_replace(source, expected)
self.check_dont_replace("""
func = PyObject_NEW;
capsule2 = PyObject_NEW2(PyCapsule, &PyCapsule_Type);
func2 = PyObject_NEW_VAR;
pattern2 = PyObject_NEW_VAR2(PatternObject, Pattern_Type, n);
""")
def test_pymem_malloc(self):
source = """\
void *ptr = PyMem_MALLOC(10);
ptr = PyMem_REALLOC(ptr, 20);
PyMem_FREE(ptr);
PyMem_DEL(ptr);
PyMem_Del(ptr);
"""
expected = """\
void *ptr = PyMem_Malloc(10);
ptr = PyMem_Realloc(ptr, 20);
PyMem_Free(ptr);
PyMem_Free(ptr);
PyMem_Free(ptr);
"""
self.check_replace(source, expected)
def test_pyobject_malloc(self):
source = """\
void *ptr = PyObject_MALLOC(10);
ptr = PyObject_REALLOC(ptr, 20);
PyObject_FREE(ptr);
PyObject_DEL(ptr);
PyObject_Del(ptr);
"""
expected = """\
void *ptr = PyObject_Malloc(10);
ptr = PyObject_Realloc(ptr, 20);
PyObject_Free(ptr);
PyObject_Free(ptr);
PyObject_Free(ptr);
"""
self.check_replace(source, expected)
def test_pyframe_getback(self):
source = """\
PyFrameObject* frame_back_borrowed(PyFrameObject *frame)
{
return frame->f_back;
}
"""
expected = """\
#include "pythoncapi_compat.h"
PyFrameObject* frame_back_borrowed(PyFrameObject *frame)
{
return _PyFrame_GetBackBorrow(frame);
}
"""
self.check_replace(source, expected)
def test_pyframe_getcode(self):
self.check_replace("""\
PyCodeObject* frame_code_borrowed(PyFrameObject *frame)
{
return frame->f_code;
}
""", """\
#include "pythoncapi_compat.h"
PyCodeObject* frame_code_borrowed(PyFrameObject *frame)
{
return _PyFrame_GetCodeBorrow(frame);
}
""")
def test_get_member_regex(self):
# Use PyFrame_GetCode() to test get_member_regex()
self.check_dont_replace("""
void frame_set_code(PyFrameObject *frame, PyCodeObject *code)
{
frame->f_code = code;
}
void frame_clear_code(PyFrameObject *frame)
{
Py_CLEAR(frame->f_code);
}
""")
def test_pythreadstate_getinterpreter(self):
self.check_replace("""
PyInterpreterState* get_interp(PyThreadState *tstate)
{ return tstate->interp; }
""", """
#include "pythoncapi_compat.h"
PyInterpreterState* get_interp(PyThreadState *tstate)
{ return PyThreadState_GetInterpreter(tstate); }
""")
def test_pythreadstate_getframe(self):
self.check_replace("""
PyFrameObject* get_frame(PyThreadState *tstate)
{ return tstate->frame; }
""", """
#include "pythoncapi_compat.h"
PyFrameObject* get_frame(PyThreadState *tstate)
{ return _PyThreadState_GetFrameBorrow(tstate); }
""")
def test_py_newref_return(self):
self.check_replace("""
PyObject* new_ref(PyObject *obj) {
Py_INCREF(obj);
return obj;
}
PyObject* same_line(PyObject *obj) {
Py_INCREF(obj); return obj;
}
PyObject* new_xref(PyObject *obj) {
Py_XINCREF(obj);
return obj;
}
PyObject* cast(PyLongObject *obj) {
Py_XINCREF(obj);
return (PyObject *)obj;
}
""", """
#include "pythoncapi_compat.h"
PyObject* new_ref(PyObject *obj) {
return Py_NewRef(obj);
}
PyObject* same_line(PyObject *obj) {
return Py_NewRef(obj);
}
PyObject* new_xref(PyObject *obj) {
return Py_XNewRef(obj);
}
PyObject* cast(PyLongObject *obj) {
return Py_XNewRef(obj);
}
""")
def test_py_newref(self):
# INCREF, assign
self.check_replace("""
void set_attr(MyStruct *obj, PyObject *value, int test)
{
// 1
Py_INCREF(value);
obj->attr = value;
// 2
obj->attr = value;
Py_INCREF(value);
// 3
obj->attr = value;
Py_INCREF(obj->attr);
}
""", """
#include "pythoncapi_compat.h"
void set_attr(MyStruct *obj, PyObject *value, int test)
{
// 1
obj->attr = Py_NewRef(value);
// 2
obj->attr = Py_NewRef(value);
// 3
obj->attr = Py_NewRef(value);
}
""")
# Same line
self.check_replace("""
void set_attr(MyStruct *obj, PyObject *value, int test)
{
// same line 1
obj->attr = value; Py_INCREF(value);
// same line 2
if (test) { obj->attr = value; Py_INCREF(obj->attr); }
// same line 3
if (test) { Py_INCREF(value); obj->attr = value; }
}
""", """
#include "pythoncapi_compat.h"
void set_attr(MyStruct *obj, PyObject *value, int test)
{
// same line 1
obj->attr = Py_NewRef(value);
// same line 2
if (test) { obj->attr = Py_NewRef(value); }
// same line 3
if (test) { obj->attr = Py_NewRef(value); }
}
""")
# Cast
self.check_replace("""
void set_attr(MyStruct *obj, PyObject *value, int test)
{
// cast 1
Py_INCREF(value);
obj->attr = (PyObject*)value;
// cast 2
obj->attr = (PyObject*)value;
Py_INCREF(value);
// assign var, incref
PyCodeObject *code_obj = (PyCodeObject *)code;
Py_INCREF(code_obj);
// assign var, incref
PyCodeObject* code_obj = (PyCodeObject *)code;
Py_INCREF(code);
// assign var, xincref
PyCodeObject * code_obj = (PyCodeObject *)code;
Py_XINCREF(code_obj);
// incref, assign var
Py_INCREF(code);
PyCodeObject* code_obj = (PyCodeObject *)code;
// xincref, assign var
Py_XINCREF(code);
PyCodeObject *code_obj = (PyCodeObject *)code;
}
""", """
#include "pythoncapi_compat.h"
void set_attr(MyStruct *obj, PyObject *value, int test)
{
// cast 1
obj->attr = Py_NewRef(value);
// cast 2
obj->attr = Py_NewRef(value);
// assign var, incref
PyCodeObject *code_obj = (PyCodeObject *)Py_NewRef(code);
// assign var, incref
PyCodeObject* code_obj = (PyCodeObject *)Py_NewRef(code);
// assign var, xincref
PyCodeObject * code_obj = (PyCodeObject *)Py_XNewRef(code);
// incref, assign var
PyCodeObject* code_obj = (PyCodeObject *)Py_NewRef(code);
// xincref, assign var
PyCodeObject *code_obj = (PyCodeObject *)Py_XNewRef(code);
}
""")
# Py_XINCREF
self.check_replace("""
void set_xattr(MyStruct *obj, PyObject *value)
{
// 1
Py_XINCREF(value);
obj->attr = value;
// 2
obj->attr = value;
Py_XINCREF(value);
// 3
obj->attr = value;
Py_XINCREF(obj->attr);
}
""", """
#include "pythoncapi_compat.h"
void set_xattr(MyStruct *obj, PyObject *value)
{
// 1
obj->attr = Py_XNewRef(value);
// 2
obj->attr = Py_XNewRef(value);
// 3
obj->attr = Py_XNewRef(value);
}
""")
# the first Py_INCREF should be replaced before the second one,
# otherwise the first Py_INCREF is not replaced.
self.check_replace("""
void set(void)
{
PyObject *x, *y;
Py_INCREF(Py_None);
x = Py_None;
Py_INCREF(Py_None);
x = Py_None;
Py_DECREF(x);
Py_DECREF(y);
}
""", """
#include "pythoncapi_compat.h"
void set(void)
{
PyObject *x, *y;
x = Py_NewRef(Py_None);
x = Py_NewRef(Py_None);
Py_DECREF(x);
Py_DECREF(y);
}
""")
# Indentation matters for conditional code
self.check_dont_replace("""
void test1(int test)
{
PyObject *res;
if (test)
res = Py_True;
else
res = Py_False;
Py_INCREF(res);
Py_DECREF(res);
}
int test2(struct datetime* result, PyObject *tzinfo)
{
int res = 0;
if (test)
res = 1;
else
Py_INCREF(tzinfo);
result->tzinfo = tzinfo;
return res;
}
""")
def test_py_clear(self):
self.check_replace("""
void clear(int test)
{
PyObject *obj;
// two lines
Py_XDECREF(obj);
obj = NULL;
// inside if
if (test) { Py_XDECREF(obj); obj = NULL; }
}
""", """
void clear(int test)
{
PyObject *obj;
// two lines
Py_CLEAR(obj);
// inside if
if (test) { Py_CLEAR(obj); }
}
""")
# Don't replace Py_DECREF()
self.check_dont_replace("""
void dont_clear(void)
{
PyObject *obj;
Py_DECREF(obj);
obj = NULL;
}
""", disable="Py_SETREF")
def test_py_setref(self):
self.check_replace("""
void set(PyObject **obj, PyObject *t)
{
// DECREF
Py_DECREF(*obj);
*obj = t;
// XDECREF
Py_XDECREF(*obj);
*obj = t;
// DECREF, INCREF
Py_DECREF(*obj);
Py_INCREF(t);
*obj = t;
}
""", """
#include "pythoncapi_compat.h"
void set(PyObject **obj, PyObject *t)
{
// DECREF
Py_SETREF(*obj, t);
// XDECREF
Py_XSETREF(*obj, t);
// DECREF, INCREF
Py_SETREF(*obj, Py_NewRef(t));
}
""")
self.check_replace("""
void set(PyObject **obj, PyObject *value)
{
// 1
PyObject *old = *obj;
*obj = value;
Py_DECREF(old);
// 2
PyObject *old = *obj;
*obj = Py_XNewRef(value);
Py_DECREF(old);
// 3
PyObject *old = *obj;
*obj = value;
Py_XDECREF(old);
// 4
PyObject *old = *obj;
*obj = Py_NewRef(value);
Py_XDECREF(old);
}
""", """
#include "pythoncapi_compat.h"
void set(PyObject **obj, PyObject *value)
{
// 1
Py_SETREF(*obj, value);
// 2
Py_SETREF(*obj, Py_XNewRef(value));
// 3
Py_XSETREF(*obj, value);
// 4
Py_XSETREF(*obj, Py_NewRef(value));
}
""")
# INCREF, DECREF, assign
self.check_replace("""
void set(void)
{
// 1
Py_INCREF(value);
Py_DECREF(obj);
obj = value;
// 2
Py_INCREF(value);
Py_XDECREF(obj);
obj = value;
// 3
Py_XINCREF(value);
Py_DECREF(obj);
obj = value;
// 4
Py_XINCREF(value);
Py_XDECREF(obj);
obj = value;
}
""", """
#include "pythoncapi_compat.h"
void set(void)
{
// 1
Py_SETREF(obj, Py_NewRef(value));
// 2
Py_XSETREF(obj, Py_NewRef(value));
// 3
Py_SETREF(obj, Py_XNewRef(value));
// 4
Py_XSETREF(obj, Py_XNewRef(value));
}
""")
# old variable
self.check_replace("""
void set(PyObject **obj, PyObject *value)
{
// 1
PyObject *old_next = (PyObject*)self->tb_next;
self->tb_next = (PyTracebackObject *)Py_XNewRef(new_next);
Py_XDECREF(old_next);
// 2
old_next = (PyObject*)self->tb_next;
self->tb_next = (PyTracebackObject *)Py_XNewRef(new_next);
Py_XDECREF(old_next);
}
""", """
#include "pythoncapi_compat.h"
void set(PyObject **obj, PyObject *value)
{
// 1
Py_XSETREF(self->tb_next, (PyTracebackObject *)Py_XNewRef(new_next));
// 2
Py_XSETREF(self->tb_next, (PyTracebackObject *)Py_XNewRef(new_next));
}
""")
# Py_CLEAR
self.check_replace("""
void set(PyObject **obj, PyObject *value)
{
// 1
Py_CLEAR(self->tb_next);
self->tb_next = value;
// 2
Py_INCREF(value);
Py_CLEAR(self->tb_next);
self->tb_next = value;
// 3
Py_XINCREF(value);
Py_CLEAR(self->tb_next);
self->tb_next = value;
}
""", """
#include "pythoncapi_compat.h"
void set(PyObject **obj, PyObject *value)
{
// 1
Py_XSETREF(self->tb_next, value);
// 2
Py_XSETREF(self->tb_next, Py_NewRef(value));
// 3
Py_XSETREF(self->tb_next, Py_XNewRef(value));
}
""")
def test_py_is(self):
self.check_replace("""
void test_py_is(PyObject *x)
{
if (x == Py_None) {
return 1;
}
if (x == Py_True) {
return 2;
}
if (x == Py_False) {
return 3;
}
return 0;
}
void test_py_is_not(PyObject *x)
{
if (x != Py_None) {
return 1;
}
if (x != Py_True) {
return 2;
}
if (x != Py_False) {
return 3;
}
return 0;
}
""", """
#include "pythoncapi_compat.h"
void test_py_is(PyObject *x)
{
if (Py_IsNone(x)) {
return 1;
}
if (Py_IsTrue(x)) {
return 2;
}
if (Py_IsFalse(x)) {
return 3;
}
return 0;
}
void test_py_is_not(PyObject *x)
{
if (!Py_IsNone(x)) {
return 1;
}
if (!Py_IsTrue(x)) {
return 2;
}
if (!Py_IsFalse(x)) {
return 3;
}
return 0;
}
""")
self.check_replace("""
void test_expr(struct MyStruct *obj, PyObject **obj2)
{
if (obj->attr1 == Py_None) {
return 1;
}
if (obj->attr2.name == Py_None) {
return 1;
}
if (*obj2 == Py_None) {
return 1;
}
return 0;
}
""", """
#include "pythoncapi_compat.h"
void test_expr(struct MyStruct *obj, PyObject **obj2)
{
if (Py_IsNone(obj->attr1)) {
return 1;
}
if (Py_IsNone(obj->attr2.name)) {
return 1;
}
if (Py_IsNone(*obj2)) {
return 1;
}
return 0;
}
""")
def test_no_compat(self):
# Don't add "#include "pythoncapi_compat.h"
source = """
void test_type(PyObject *obj, PyTypeObject *type)
{
obj->ob_type = type;
}
"""
expected = """
void test_type(PyObject *obj, PyTypeObject *type)
{
Py_SET_TYPE(obj, type);
}
"""
self.check_replace(source, expected, no_compat=True)
if __name__ == "__main__":
unittest.main()