Skip to content

Commit e3346b8

Browse files
committed
Fix autopatching
1 parent d40b6c5 commit e3346b8

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_autopatch_capi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,19 @@ def test_replace_field_access():
9090
'(PyObject*)function_call(a, b, c(0))->ob_type->ob_base;',
9191
'(PyObject*)Py_TYPE(function_call(a, b, c(0)))->ob_base;',
9292
)
93+
check_autopatched(
94+
'''
95+
#if SOME_MACRO
96+
obj->ob_type->tp_free(self);
97+
#else
98+
obj->ob_type->tp_free(self);
99+
#endif
100+
''',
101+
'''
102+
#if SOME_MACRO
103+
Py_TYPE(obj)->tp_free(self);
104+
#else
105+
Py_TYPE(obj)->tp_free(self);
106+
#endif
107+
''',
108+
)

graalpython/lib-graalpython/modules/autopatch_capi.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,31 @@ def consume_identifier_backwards(idx):
9999
idx -= 1
100100
return idx
101101

102-
first = True
102+
allowed_tokens = {'()', '[]', '.', 'id'}
103103
while idx >= 0:
104104
c = contents[idx]
105-
if c == ')' and first:
105+
if '()' in allowed_tokens and c == ')':
106106
idx = consume_pairwise_backwards(idx, '(', ')')
107-
elif c == ']':
107+
allowed_tokens = {'[]', 'id'}
108+
elif '[]' in allowed_tokens and c == ']':
108109
idx = consume_pairwise_backwards(idx, '[', ']')
109-
elif c.isidentifier() or c.isdigit():
110+
allowed_tokens = {'[]', 'id'}
111+
elif 'id' in allowed_tokens and c.isidentifier() or c.isdigit():
110112
id_start = consume_identifier_backwards(idx)
111113
if contents[id_start + 1: idx + 1] == 'return':
112114
idx += 1
113115
break
114116
idx = id_start
115-
elif c == '.':
117+
allowed_tokens = {'.'}
118+
elif '.' in allowed_tokens and c == '.':
116119
idx -= 1
117-
elif c == '>' and idx > 1 and contents[idx - 1] == '-':
120+
allowed_tokens = {'[]', 'id'}
121+
elif '.' in allowed_tokens and c == '>' and idx > 1 and contents[idx - 1] == '-':
118122
idx -= 2
123+
allowed_tokens = {'[]', 'id'}
119124
else:
120125
idx += 1
121126
break
122-
first = False
123127
idx = consume_whitespace_backwards(idx)
124128

125129
receiver_start = consume_whitespace_forward(idx)

0 commit comments

Comments
 (0)