Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Python
package module for Python+Chapel interop (#26156)
Adds a new package module, `Python`, which supports calling Python code from Chapel. For example, the following Chapel progam use the python interface to `BeautifulSoup` to parse HTML ```chapel import Python; use URL; use List; proc main() { const url = 'https://chapel-lang.org/docs/main/language/spec/interoperability.html'; var htmlReader = openUrlReader(url); var html = htmlReader.readAll(string); var interp = new Python.Interpreter(); var mod = new Python.Module(interp, "bs4"); var cls = new Python.Class(mod, "BeautifulSoup"); var soup = cls(html, 'html.parser'); var res: list(owned Python.ClassObject?); res = soup.callMethod(res.type, "find_all", "h3"); for c in res { writeln(c!.getAttr(string, "text")); } } ``` As an another example, this simple program compiles an runs a Python lambda from Chapel code ```chapel import Python; config const n = 10; config const func = "lambda x,: x + 1 if x % 2 != 0 else x"; proc apply(interp: borrowed, type t, arr, l) { var lambdaFunc = new Python.Function(interp, l); var res: [arr.domain] t; for i in arr.domain { res(i) = lambdaFunc(t, arr(i)); } return res; } proc main() { var interp = new Python.Interpreter(); var data: [1..#n] int = 1..#n; writeln(" data: ", data); var res = apply(interp, int, data, func); writeln("res: ", res); } ``` Future work: - Convert classes to a hierarchy under `Value` so that more implementation can be shared and the Chapel interface can better imitate python - Add special support for common python types as sub-classes of value, to avoid round tripping values through chapel in many cases - Support common operators like add. These can be called today as dunder methods (`.call("__add__", ...)`), but it would be nice to support native operators like `+` - Add the ability to compile arbitrary chapel strings, beyond just lambdas - Setup python to use Chapel stdout/stderr - Add custom adapters for Chapel arrays to allow python functions to operate on Chapel arrays, without copying - Support python context managers as Chapel context managers - Fully support Python sets, Python dictionaries, Chapel sets, Chapel maps, Chapel associative arrays, and Chapel associative domains - Make sure all python objects are properly reference counted - Resolve TODO with LLVM IR verification: #26235 [Reviewed by @DanilaFe]
- Loading branch information