Skip to content

Commit 3198753

Browse files
committed
Tweak regex to avoid second lookup and be more durable to colliding names
Signed-off-by: Tim Paine <[email protected]>
1 parent d1ab44a commit 3198753

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

csp/impl/wiring/base_parser.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ def _get_source_from_interpreter_function(raw_func):
101101
current_interpreter_history = readline.get_current_history_length()
102102

103103
try:
104-
search_pattern = re.compile(r"^(\s*def\s)")
104+
search_pattern = re.compile(r"^(\s*def\s*" + raw_func.__name__ + r"\s*\()")
105105
decorator_pattern = re.compile(r"^(\s*@)")
106-
func_name = raw_func.__name__
107106
code_object = raw_func.__func__ if inspect.ismethod(raw_func) else raw_func.__code__
108107
except Exception:
109108
raise OSError("Could not get source code for interpreter-defined function.")
@@ -120,21 +119,23 @@ def _get_source_from_interpreter_function(raw_func):
120119

121120
# if its a def name_of_function(...
122121
if search_pattern.match(line):
123-
# go through to get decorators
124-
if func_name in line:
125-
# reassemble function
126-
for i in range(starting_index_of_function, current_interpreter_history + 1):
127-
reassembled_function += f"{readline.get_history_item(i)}\n"
128-
129-
for line_number_with_decorator in range(starting_index_of_function - 1, -1, -1):
130-
if decorator_pattern.match(readline.get_history_item(line_number_with_decorator)):
131-
reassembled_function = (
132-
f"{readline.get_history_item(line_number_with_decorator)}\n" + reassembled_function
133-
)
134-
else:
135-
break
136-
break
122+
# reassemble function
123+
for i in range(starting_index_of_function, current_interpreter_history + 1):
124+
reassembled_function += f"{readline.get_history_item(i)}\n"
125+
126+
for line_number_with_decorator in range(starting_index_of_function - 1, -1, -1):
127+
if decorator_pattern.match(readline.get_history_item(line_number_with_decorator)):
128+
reassembled_function = (
129+
f"{readline.get_history_item(line_number_with_decorator)}\n" + reassembled_function
130+
)
131+
else:
132+
break
133+
break
137134
starting_index_of_function -= 1
135+
136+
if reassembled_function == "":
137+
raise OSError("Could not find function definition for interpreter-defined function.")
138+
138139
return reassembled_function
139140

140141

0 commit comments

Comments
 (0)