Skip to content

Commit

Permalink
Add Python package module for Python+Chapel interop (#26156)
Browse files Browse the repository at this point in the history
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
jabraham17 authored Nov 21, 2024
2 parents ac4e93c + 9400f6b commit 53bee29
Show file tree
Hide file tree
Showing 69 changed files with 2,671 additions and 3 deletions.
7 changes: 7 additions & 0 deletions doc/rst/meta/modules/packages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ File Formats and I/O
YAML <packages/YAML>
Zarr <packages/Zarr>

Interoperability
----------------
.. toctree::
:maxdepth: 1

Python <packages/Python>

Math / Numerical Computing
--------------------------
.. toctree::
Expand Down
1 change: 1 addition & 0 deletions modules/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ PACKAGES_TO_DOCUMENT = \
packages/ParallelIO.chpl \
packages/PeekPoke.chpl \
packages/PrecisionSerializer.chpl \
packages/Python.chpl \
packages/RangeChunk.chpl \
packages/RecordParser.chpl \
packages/ReplicatedVar.chpl \
Expand Down
Loading

0 comments on commit 53bee29

Please sign in to comment.