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]]: