Skip to content

Commit 6663463

Browse files
committed
Py_Is operation uses EXPR_REGEX
1 parent 1ba32a4 commit 6663463

2 files changed

Lines changed: 29 additions & 11 deletions

File tree

tests/test_upgrade_pythoncapi.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,6 @@ def check_dont_replace(self, source):
110110
source = reformat(source)
111111
self.assertEqual(patch(source), source)
112112

113-
def test_expr_regex(self):
114-
self.check_replace("a->b->ob_type", "Py_TYPE(a->b)")
115-
self.check_replace("a.b->ob_type", "Py_TYPE(a.b)")
116-
self.check_replace("array[2]->ob_type", "Py_TYPE(array[2])")
117-
118-
# Don't match function calls
119-
self.check_dont_replace("func()->ob_type")
120-
121113
def test_pythoncapi_compat(self):
122114
# If pythoncapi_compat.h is included, avoid compatibility includes
123115
# and macros.
@@ -538,6 +530,32 @@ def test_py_is(self):
538530
}
539531
""")
540532

533+
self.check_replace("""
534+
void test_expr(struct MyStruct *obj)
535+
{
536+
if (obj->attr1 == Py_None) {
537+
return 1;
538+
}
539+
if (obj->attr2.name == Py_None) {
540+
return 1;
541+
}
542+
return 0;
543+
}
544+
""", """
545+
#include "pythoncapi_compat.h"
546+
547+
void test_expr(struct MyStruct *obj)
548+
{
549+
if (Py_IsNone(obj->attr1)) {
550+
return 1;
551+
}
552+
if (Py_IsNone(obj->attr2.name)) {
553+
return 1;
554+
}
555+
return 0;
556+
}
557+
""")
558+
541559

542560
def test_no_compat(self):
543561
# Don't add "#include "pythoncapi_compat.h"

upgrade_pythoncapi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,12 @@ def replace2(regs):
315315
return f'{x} = _Py_StealRef({y});'
316316

317317
REPLACE = []
318-
id_regex = r'(%s)' % ID_REGEX
318+
expr = r'(%s)' % EXPR_REGEX
319319
for name in ('None', 'True', 'False'):
320320
REPLACE.extend((
321-
(re.compile(fr'({ID_REGEX}) == Py_{name}\b'),
321+
(re.compile(fr'{expr} == Py_{name}\b'),
322322
fr'Py_Is{name}(\1)'),
323-
(re.compile(fr'({ID_REGEX}) != Py_{name}\b'),
323+
(re.compile(fr'{expr} != Py_{name}\b'),
324324
fr'!Py_Is{name}(\1)'),
325325
))
326326

0 commit comments

Comments
 (0)