From 779d88af3678a069c2870a138764b6bb995e64c0 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 20 Jun 2024 09:11:47 -0700 Subject: [PATCH] Add a helper function to set up standard module paths This builds on files_with_contexts to get contexts with stdlib. Signed-off-by: Danila Fedorin --- tools/chapel-py/src/chapel/__init__.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tools/chapel-py/src/chapel/__init__.py b/tools/chapel-py/src/chapel/__init__.py index 0ce19ffec5ce..e665225768fe 100644 --- a/tools/chapel-py/src/chapel/__init__.py +++ b/tools/chapel-py/src/chapel/__init__.py @@ -330,13 +330,17 @@ def each_matching(node, pattern, iterator=preorder): yield (child, variables) -def files_with_contexts(files): +def files_with_contexts( + files, setup: Optional[typing.Callable[[Context], None]] = None +): """ Some files might have the same name, which Dyno really doesn't like. Stratify files into "buckets"; within each bucket, all filenames are unique. Between each bucket, re-create the Dyno context to avoid giving it conflicting files. + For each newly-created context, call the setup function with it. + Yields files from the argument, as well as the context created for them. """ @@ -354,10 +358,29 @@ def files_with_contexts(files): ctx = Context() to_yield = buckets[bucket] + if setup: + setup(ctx) + for filename in to_yield: yield (filename, ctx) +def files_with_stdlib_contexts( + files, setup: Optional[typing.Callable[[Context], None]] = None +): + """ + Like files_with_contexts, but also includes the standard library in the + context. + """ + + def setup_with_stdlib(ctx): + ctx.set_module_paths([], []) + if setup: + setup(ctx) + + yield from files_with_contexts(files, setup_with_stdlib) + + def range_to_tokens( rng: Location, lines: List[str] ) -> List[typing.Tuple[int, int, int]]: