Debian Codegraph @

subprojects/pythoncapi-compat/tests/test_upgrade_pythoncapi.py

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