@@ -154,11 +154,14 @@ def onPopScope(self, cm: CDIMemo) -> bool:
154154 self .onStatusMemo (cm )
155155 return True
156156
157- def getBranch (self ) -> str :
157+ def getParentBranch (self , cm : CDIMemo ) -> str :
158158 """Get the Treeview branch iid of the tag currently being parsed"""
159- if not len (self ._tag_stack ):
160- return ""
161- branch = self ._tag_stack [- 1 ].getBranch ()
159+ # if not len(self._tag_stack):
160+ # return ""
161+ # branch = self._tag_stack[-1].getBranch()
162+ # NOTE: ^ _tag_stack is unreliable due to race condition
163+ # (append/pop may occur before or after this call)!
164+ branch = cm .getBranch ()
162165 return branch if (branch is not None ) else ""
163166
164167 def _onPopScope (self , cm : CDIMemo ):
@@ -183,7 +186,7 @@ def _onPopScope(self, cm: CDIMemo):
183186 assert nameLower is not None # only None for done/fail events
184187 cm .content
185188 if nameLower == "name" :
186- parentIID = self .getBranch ( )
189+ parentIID = self .getParentBranch ( cm )
187190 assert parentIID is not None , "name must be in a branch"
188191 if parentIID :
189192 assert cm .content is not None
@@ -301,13 +304,15 @@ def _onPushScope(self, cm: CDIMemo):
301304 else :
302305 raise NotImplementedError (tagLower )
303306 new_branch = self ._treeview .insert (
304- self .getBranch ( ),
307+ self .getParentBranch ( cm ),
305308 index ,
306309 iid = self ._current_iid ,
307310 text = content ,
308311 )
309- self ._tag_stack [- 1 ].iid = new_branch
310312 # values=(), image=None
313+ # self._tag_stack[-1].iid = new_branch
314+ # NOTE: ^ _tag_stack is unreliable due to race condition!
315+ cm .iid = new_branch
311316 self ._current_iid += 1 # TODO: associate with SubElement
312317 elif tagLower == "acdi" :
313318 pass # handled by superclass (sets self.acdi)
@@ -320,13 +325,15 @@ def _onPushScope(self, cm: CDIMemo):
320325 content = ""
321326 break
322327 new_branch = self ._treeview .insert (
323- self .getBranch ( ),
328+ self .getParentBranch ( cm ),
324329 index ,
325330 iid = self ._current_iid ,
326331 text = content ,
327332 )
328- self ._tag_stack [- 1 ].iid = new_branch
329333 # values=(), image=None
334+ # self._tag_stack[-1].iid = new_branch
335+ # NOTE: ^ _tag_stack is unreliable due to race condition!
336+ cm .iid = new_branch
330337 self ._current_iid += 1 # TODO: associate with SubElement
331338 # and/or set values keyword argument to create association(s)
332339
0 commit comments