44
44
45
45
46
46
def simple_replace (contents , match , replacement , assignment ):
47
- "replaces the indexes in 'contents' described by the tuple 'group' with replacement"
48
-
47
+ "replaces the indexes in 'contents' described by the tuple 'group' with replacement, which may have backrefs "
48
+
49
49
start , end = match .span (1 )
50
- return contents [:start ] + replacement + contents [end :]
50
+ return contents [:start ] + match . expand ( replacement ) + contents [end :]
51
51
52
52
53
53
def replace_pre (contents , match , replacement , assignment ):
54
54
"replaces a pre-increment/decrement"
55
-
55
+
56
56
start , end = match .span (1 )
57
57
replacement = replacement .replace ('%receiver' , contents [start : end ])
58
58
start , end = match .span ()
@@ -61,24 +61,24 @@ def replace_pre(contents, match, replacement, assignment):
61
61
62
62
def replace_field_access (contents , match , replacement , assignment ):
63
63
"replaces a field access, scanning backwards to determine the receiver"
64
-
64
+
65
65
start , end = match .span (1 )
66
66
level = 0
67
-
67
+
68
68
def consume_whitespace_backwards (idx ):
69
69
while idx > 0 and contents [idx ].isspace ():
70
70
idx -= 1
71
71
return idx
72
-
72
+
73
73
def consume_whitespace_forward (idx ):
74
74
while idx < len (contents ) and contents [idx ].isspace ():
75
75
idx += 1
76
76
return idx
77
-
77
+
78
78
start = consume_whitespace_backwards (start - 1 )
79
79
if start < 1 or contents [start - 1 : start + 1 ] != '->' :
80
80
return contents
81
-
81
+
82
82
# scan backwards to capture the whole receiver
83
83
idx = consume_whitespace_backwards (start - 2 )
84
84
empty = True
@@ -110,12 +110,12 @@ def consume_whitespace_forward(idx):
110
110
else :
111
111
idx += 1
112
112
break
113
-
113
+
114
114
receiver_start = consume_whitespace_forward (idx )
115
115
receiver_string = contents [receiver_start : start - 1 ]
116
-
116
+
117
117
if assignment is not None :
118
-
118
+
119
119
# scan forwards to see if there's an assignment
120
120
idx = consume_whitespace_forward (end )
121
121
if contents [idx ] == '=' and contents [idx + 1 ] != '=' :
@@ -139,11 +139,11 @@ def consume_whitespace_forward(idx):
139
139
idx += 2
140
140
else :
141
141
idx += 1
142
-
142
+
143
143
value_string = contents [value_start :idx ]
144
-
144
+
145
145
return contents [:receiver_start ] + assignment .replace ('%receiver' , receiver_string ).replace ("%value" , value_string ) + contents [idx :]
146
-
146
+
147
147
return contents [:receiver_start ] + replacement .replace ('%receiver' , receiver_string ) + contents [end :]
148
148
149
149
@@ -157,13 +157,16 @@ def consume_whitespace_forward(idx):
157
157
r'\W(ob_item)\W' : (replace_field_access , 'PySequence_Fast_ITEMS((PyObject*)%receiver)' ),
158
158
r'^\s*()(std::)?free\((const_cast<char \*>)?\(?\w+->m_ml->ml_doc\)?\);' : (simple_replace , '//' ),
159
159
r'\W(m_ml\s*->\s*ml_doc)\W' : (replace_field_access , 'PyObject_GetDoc((PyObject*)(%receiver))' , 'PyObject_SetDoc((PyObject*)(%receiver), %value)' ),
160
+ r'\W(m_ml)\W' : (replace_field_access , '_PyCFunction_GetMethodDef((PyObject*)(%receiver))' ),
161
+ r'\W(m_module)\W' : (replace_field_access , '_PyCFunction_GetModule((PyObject*)(%receiver))' ),
162
+ r'(&PyTuple_GET_ITEM\(([\(\w](?:\w|->|\.|\(|\))*), 0\))' : (simple_replace , r'PySequence_Fast_ITEMS(\2)' ),
160
163
# already defined by GraalPy:
161
164
r'^\s*()#\s*define\s+Py_SET_TYPE\W' : (simple_replace , '//' ),
162
165
r'^\s*()#\s*define\s+Py_SET_SIZE\W' : (simple_replace , '//' ),
163
166
r'^\s*()#\s*define\s+Py_SET_REFCNT\W' : (simple_replace , '//' ),
164
167
}
165
168
166
-
169
+
167
170
def auto_patch (path , dryrun ):
168
171
"reads the given file, applies all replacements, and writes back the result if there were changes"
169
172
@@ -172,7 +175,7 @@ def auto_patch(path, dryrun):
172
175
contents = f .read ()
173
176
except UnicodeDecodeError :
174
177
return # may happen for binary files
175
-
178
+
176
179
original = contents
177
180
import re
178
181
for pattern , (* ops ,) in auto_replacements .items ():
0 commit comments