PythonScript
PythonScript copied to clipboard
Crash when using editor.research(regex, lambda m: matches_list.append(m))
Using either 32-bit or 64-bit PythonScript v1.5.4, I get a crash when running the following code:
overall_match_number = 0
def match_found(m):
global overall_match_number
num_groups = len(m.groups())
for g in xrange(num_groups + 1):
print(overall_match_number, num_groups, g, m.start(g), m.end(g), m.span(g), m.group(g), m.groups(), m.lastindex, m.expand(r'hello \1 there'), m.lastindex)
overall_match_number += 1
# X7a X99b X23 zzzzz X39c
def main():
matches_list = []
editor.research('X([0-9]+)([abc]?)', lambda m: matches_list.append(m))
for m in matches_list: match_found(m)
main()
If I change main() to be:
def main():
editor.research('X([0-9]+)([abc]?)', match_found)
Then the code runs as intended and produces proper output:
0 2 0 3831 3834 (3831, 3834) X7a ('7', 'a') 2 hello 7 there 2
0 2 1 3832 3833 (3832, 3833) 7 ('7', 'a') 2 hello 7 there 2
0 2 2 3833 3834 (3833, 3834) a ('7', 'a') 2 hello 7 there 2
1 2 0 3835 3839 (3835, 3839) X99b ('99', 'b') 2 hello 99 there 2
1 2 1 3836 3838 (3836, 3838) 99 ('99', 'b') 2 hello 99 there 2
1 2 2 3838 3839 (3838, 3839) b ('99', 'b') 2 hello 99 there 2
2 2 0 3840 3843 (3840, 3843) X23 ('23', '') 2 hello 23 there 2
2 2 1 3841 3843 (3841, 3843) 23 ('23', '') 2 hello 23 there 2
2 2 2 3843 3843 (3843, 3843) ('23', '') 2 hello 23 there 2
3 2 0 3850 3854 (3850, 3854) X39c ('39', 'c') 2 hello 39 there 2
3 2 1 3851 3853 (3851, 3853) 39 ('39', 'c') 2 hello 39 there 2
3 2 2 3853 3854 (3853, 3854) c ('39', 'c') 2 hello 39 there 2
so I think it has something to do with the "lambda"ing??
Anyway, the crash is a hard one, and gives me this box:

The following has been tested with 32 and 64bit versions of PS 1.5.4 and PS 3.0.6
import re
test = '#1 #2 #3 #4 #5'
matches_list = []
def match_found(match):
matches_list.append(match)
print(','.join(m.group() for m in matches_list))
def main():
global matches_list
matches_list = list()
print('python regex')
for m in re.finditer('#\d', test):
match_found(m)
matches_list = list()
print('regex search with normal callback function')
editor.research('#\d', match_found)
matches_list = list()
print('regex search with lambda function and stored group result')
editor.research('#\d', lambda m: matches_list.append(m.group()))
print(','.join(m for m in matches_list))
# # CRASH
# print('regex search with lambda function and stored match object')
# matches_list = list()
# editor.research('#\d', lambda m: matches_list.append(m))
# print(','.join(m.group() for m in matches_list))
main()
The ouput is always
python regex
#1
#1,#2
#1,#2,#3
#1,#2,#3,#4
#1,#2,#3,#4,#5
regex search with normal callback function
#1
#2,#2
#3,#3,#3
#4,#4,#4,#4
#5,#5,#5,#5,#5
regex search with lambda function and stored group result
#1,#2,#3,#4,#5
The interesting part is the regex search with normal callback function.
As we can see, the match objects are corrupted, and I assume that this is also the reason for the crash of the last, commented test.
Once the editor.research is finished, there are no valid match objects.
Unfortunately, this is all I can contribute with my lack of C++ knowledge.