@@ -146,12 +146,19 @@ def find_upstream_visits(session_id: int, db: SQLModelSession, max_depth: int =
146146 def _recursive_search (
147147 dirpath : str | Path ,
148148 search_string : str ,
149- partial_match = True ,
149+ partial_match : bool = True ,
150150 max_depth : int = 1 ,
151+ result : dict [str , Path ] = {},
151152 ):
153+ # Start a new dictionary object if none were provided
154+ # This if-block prevents in-place memory modification on subsequent loops
155+ if not result :
156+ result = {}
152157 # Stop recursing for this route once max depth hits 0
153158 if max_depth == 0 :
154- return
159+ return result
160+
161+ # Walk through the directories
155162 for entry in os .scandir (dirpath ):
156163 if entry .is_dir ():
157164 # Update dictionary with match and stop recursing for this route
@@ -160,16 +167,17 @@ def _recursive_search(
160167 if partial_match
161168 else search_string == entry .name
162169 ):
163- current_upstream_visits [entry .name ] = Path (entry .path )
170+ result [entry .name ] = Path (entry .path )
164171 else :
165172 # Continue searching down this route until max depth is reached
166- _recursive_search (
173+ result = _recursive_search (
167174 dirpath = entry .path ,
168175 search_string = search_string ,
169176 partial_match = partial_match ,
170177 max_depth = max_depth - 1 ,
178+ result = result ,
171179 )
172- continue
180+ return result
173181
174182 murfey_session = db .exec (
175183 select (MurfeySession ).where (MurfeySession .id == session_id )
@@ -186,14 +194,12 @@ def _recursive_search(
186194 upstream_data_dir ,
187195 ) in machine_config .upstream_data_directories .items ():
188196 # Recursively look for matching visit names under current directory
189- current_upstream_visits : dict [str , Path ] = {}
190- _recursive_search (
197+ upstream_visits [upstream_instrument ] = _recursive_search (
191198 dirpath = upstream_data_dir ,
192199 search_string = f"{ visit_name .split ('-' )[0 ]} -" ,
193200 partial_match = True ,
194201 max_depth = max_depth ,
195202 )
196- upstream_visits [upstream_instrument ] = current_upstream_visits
197203 return upstream_visits
198204
199205
0 commit comments