Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef315d0

Browse files
committed
EXPR_REGEX matchs "*obj"
1 parent 6663463 commit ef315d0

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

tests/test_upgrade_pythoncapi.py

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

113+
def test_expr_regex(self):
114+
# Test EXPR_REGEX
115+
self.check_replace("a->b->ob_type", "Py_TYPE(a->b)")
116+
self.check_replace("a.b->ob_type", "Py_TYPE(a.b)")
117+
self.check_replace("array[2]->ob_type", "Py_TYPE(array[2])")
118+
119+
# Don't match function calls
120+
self.check_dont_replace("func()->ob_type")
121+
113122
def test_pythoncapi_compat(self):
114123
# If pythoncapi_compat.h is included, avoid compatibility includes
115124
# and macros.
@@ -531,27 +540,33 @@ def test_py_is(self):
531540
""")
532541

533542
self.check_replace("""
534-
void test_expr(struct MyStruct *obj)
543+
void test_expr(struct MyStruct *obj, PyObject **obj2)
535544
{
536545
if (obj->attr1 == Py_None) {
537546
return 1;
538547
}
539548
if (obj->attr2.name == Py_None) {
540549
return 1;
541550
}
551+
if (*obj2 == Py_None) {
552+
return 1;
553+
}
542554
return 0;
543555
}
544556
""", """
545557
#include "pythoncapi_compat.h"
546558
547-
void test_expr(struct MyStruct *obj)
559+
void test_expr(struct MyStruct *obj, PyObject **obj2)
548560
{
549561
if (Py_IsNone(obj->attr1)) {
550562
return 1;
551563
}
552564
if (Py_IsNone(obj->attr2.name)) {
553565
return 1;
554566
}
567+
if (Py_IsNone(*obj2)) {
568+
return 1;
569+
}
555570
return 0;
556571
}
557572
""")

upgrade_pythoncapi.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
ID_REGEX = r'\b[a-zA-Z][a-zA-Z0-9_]*\b'
2222
# Match 'array[3]'
2323
SUBEXPR_REGEX = fr'{ID_REGEX}(?:\[[^]]+\])*'
24-
# Match a C expression like "frame", "frame.attr" or "obj->attr".
24+
# Match a C expression like "frame", "frame.attr", "obj->attr" or "*obj".
2525
# Don't match functions calls like "func()".
26-
EXPR_REGEX = fr"{SUBEXPR_REGEX}(?:(?:->|\.){SUBEXPR_REGEX})*"
26+
EXPR_REGEX = (fr"\*?" # "*" prefix
27+
fr"{SUBEXPR_REGEX}" # "var"
28+
fr"(?:(?:->|\.){SUBEXPR_REGEX})*") # "->attr" or ".attr"
2729

2830

2931
def get_member_regex_str(member):

0 commit comments

Comments
 (0)