queue -> stack (#45002)

This commit is contained in:
Harmen Stoppels 2024-07-02 16:41:29 +02:00 committed by GitHub
parent a6e6093922
commit 5b4edb9499
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 8 deletions

View File

@ -128,9 +128,9 @@ def startfile_prefix(prefix: str, compatible_with: str = sys.executable) -> Opti
except Exception: except Exception:
accept = lambda path: True accept = lambda path: True
queue = [(0, prefix)] stack = [(0, prefix)]
while queue: while stack:
depth, path = queue.pop() depth, path = stack.pop()
try: try:
iterator = os.scandir(path) iterator = os.scandir(path)
except OSError: except OSError:
@ -140,7 +140,7 @@ def startfile_prefix(prefix: str, compatible_with: str = sys.executable) -> Opti
try: try:
if entry.is_dir(follow_symlinks=True): if entry.is_dir(follow_symlinks=True):
if depth < 2: if depth < 2:
queue.append((depth + 1, entry.path)) stack.append((depth + 1, entry.path))
elif entry.name == "crt1.o" and accept(entry.path): elif entry.name == "crt1.o" and accept(entry.path):
return path return path
except Exception: except Exception:

View File

@ -497,10 +497,10 @@ def anchorify(data: Union[dict, list], identifier: Callable[[Any], str] = repr)
"""Replace identical dict/list branches in tree with references to earlier instances. The YAML """Replace identical dict/list branches in tree with references to earlier instances. The YAML
serializer generate anchors for them, resulting in small yaml files.""" serializer generate anchors for them, resulting in small yaml files."""
anchors: Dict[str, Union[dict, list]] = {} anchors: Dict[str, Union[dict, list]] = {}
queue: List[Union[dict, list]] = [data] stack: List[Union[dict, list]] = [data]
while queue: while stack:
item = queue.pop() item = stack.pop()
for key, value in item.items() if isinstance(item, dict) else enumerate(item): for key, value in item.items() if isinstance(item, dict) else enumerate(item):
if not isinstance(value, (dict, list)): if not isinstance(value, (dict, list)):
@ -511,7 +511,7 @@ def anchorify(data: Union[dict, list], identifier: Callable[[Any], str] = repr)
if anchor is None: if anchor is None:
anchors[id] = value anchors[id] = value
queue.append(value) stack.append(value)
else: else:
item[key] = anchor # replace with reference item[key] = anchor # replace with reference