@@ -268,6 +268,10 @@ def _get_positions(iterator: NodeIterator):
268268 "start" in x ["@pos" ].keys (), nodes ) ]
269269 return [ (int (n ["offset" ]), int (n ["line" ]), int (n ["col" ])) for n in start_positions ]
270270
271+ def decrefAndGC (self , obj ) -> None :
272+ del obj
273+ gc .collect ()
274+
271275 def testIteratorPreOrder (self ) -> None :
272276 root = self ._itTestTree ()
273277 it = iterator (root , TreeOrder .PRE_ORDER )
@@ -456,8 +460,7 @@ def testLoad(self) -> None:
456460 def testOrphanFilter (self ) -> None :
457461 ctx = self ._parse_fixture ()
458462 it = ctx .filter ("//uast:RuntimeImport" )
459- del ctx
460- gc .collect ()
463+ self .decrefAndGC (ctx )
461464 # We should be able to retrieve values from the iterator
462465 # after the context has been DECREFed but the iterator
463466 # still exists
@@ -467,8 +470,7 @@ def testOrphanFilter(self) -> None:
467470
468471 # Chaining calls has the same effect as splitting
469472 # the effect across different lines as above
470- del it
471- gc .collect ()
473+ self .decrefAndGC (it )
472474 it = self ._parse_fixture ().filter ("//uast:RuntimeImport" )
473475 next (it )
474476 obj = next (it ).get ()
@@ -478,8 +480,7 @@ def testOrphanFilter(self) -> None:
478480 def testOrphanIterator (self ) -> None :
479481 ctx = self ._parse_fixture ()
480482 it = ctx .iterate (TreeOrder .PRE_ORDER )
481- del ctx
482- gc .collect ()
483+ self .decrefAndGC (ctx )
483484 # We should be able to retrieve values from the iterator
484485 # after the context has been DECREFed but the iterator
485486 # still exists
@@ -488,8 +489,7 @@ def testOrphanIterator(self) -> None:
488489
489490 # Chaining calls has the same effect as splitting
490491 # the effect across different lines as above
491- del it
492- gc .collect ()
492+ self .decrefAndGC (it )
493493 it = self ._parse_fixture ().iterate (TreeOrder .POST_ORDER )
494494 obj = next (it )
495495 self .assertIsInstance (obj , Node )
@@ -499,29 +499,25 @@ def testLoadOrphanNode(self) -> None:
499499 it = ctx .iterate (TreeOrder .PRE_ORDER )
500500 # The underlying ctx should not be deallocated even if ctx goes
501501 # out of scope because the iterator is still alive
502- del ctx
503- gc .collect ()
502+ self .decrefAndGC (ctx )
504503 next (it ); next (it ); next (it );
505504 node = next (it )
506- del it
507- gc .collect ()
505+ self .decrefAndGC (it )
508506 # Context should not have been deallocated yet because we
509507 # want to iterate from the node onwards
510508 it2 = node .iterate (TreeOrder .PRE_ORDER )
511509 node_ext = node .node_ext
512510 # node could be deallocated here also, if we by, any chance,
513511 # we happen to be storing only the external nodes
514- del node
515- gc .collect ()
512+ self .decrefAndGC (node )
516513 obj = node_ext .load ()
517514 typ = obj ["@type" ]
518515 self .assertEqual ("uast:RuntimeImport" , typ )
519516
520517 def testFilterOrphanNode (self ) -> None :
521518 ctx = self ._parse_fixture ()
522519 root = ctx .root
523- del ctx
524- gc .collect ()
520+ self .decrefAndGC (ctx )
525521 # filter should work here over the tree even if we ctx has
526522 # been DECREFed by the interpreter (it has gone out of scope)
527523 it = root .filter ("//uast:RuntimeImport" )
0 commit comments