Skip to content

Commit cc0d822

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 9c52981 commit cc0d822

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

csp/impl/wiring/base_parser.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,8 @@ def _get_source_from_interpreter_function(raw_func):
9999
current_interpreter_history = readline.get_current_history_length()
100100

101101
try:
102-
search_pattern = re.compile(r"^(\s*def\s)")
102+
search_pattern = re.compile(r"^(\s*def\s*" + raw_func.__name__ + r"\s*\()")
103103
decorator_pattern = re.compile(r"^(\s*@)")
104-
func_name = raw_func.__name__
105104
code_object = raw_func.__func__ if inspect.ismethod(raw_func) else raw_func.__code__
106105
except Exception:
107106
raise OSError("Could not get source code for interpreter-defined function.")
@@ -118,21 +117,23 @@ def _get_source_from_interpreter_function(raw_func):
118117

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

138139

0 commit comments

Comments
 (0)