Add @stdlib/python module for Python interoperability#405
Open
nbeerbower wants to merge 10 commits into
Open
Conversation
Introduces a new standard library module enabling Hemlock programs to use
Python libraries like Hugging Face Transformers, NumPy, PyTorch, etc.
Features:
- Initialize/finalize Python interpreter (py_init, py_finalize)
- Import Python modules (py_import, py_from_import, py_add_path)
- Call Python functions with args/kwargs (py_call, py_call_method, py_call_kw)
- Automatic type conversion (to_python, from_python)
- Object attribute access (py_get, py_set, py_has)
- Collection operations (py_getitem, py_setitem, py_len)
- Collection builders (py_list, py_dict, py_tuple, py_set_create)
- Type checking (py_type, py_is_int, py_is_string, etc.)
- Error handling (py_error_occurred, py_check_error, py_error_fetch)
- GIL management for async code (py_gil_acquire, py_with_gil)
- NumPy integration (numpy_from_buffer, numpy_to_buffer, numpy_shape)
- Convenience functions (py_eval, py_exec, py_repr)
Type conversion table:
- null <-> None
- bool <-> bool
- i8-i64, u8-u64 <-> int
- f32, f64 <-> float
- string <-> str
- array <-> list
- object <-> dict
- buffer <-> bytes
Example usage:
import { py_init, py_import, py_call, from_python } from "@stdlib/python";
py_init();
let math = py_import("math");
let result = py_call(py_get(math, "sqrt"), [16]);
print(from_python(result)); // 4.0
py_finalize();
Python interop tests require libpython3.11.so which isn't available in CI. Move tests to tests/manual/ where they can be run locally when Python is installed.
- Remove `: ptr` type annotations from function params and local variables (interpreter has a bug with ptr type annotations when FFI is imported) - Replace Py*_Check macro calls with internal type checking functions (PyLong_Check etc. are C macros, not real functions) - Use .keys() method instead of non-existent __object_keys function - Fix py_eval to provide globals dict with __builtins__ - Use optional chaining for PyObj type detection in to_python Both test_python_basic.hml and test_python_types.hml now pass.
Manual tests require external dependencies (like Python) that aren't available in CI. Add -not -path "*/manual/*" to find command.
- Add python3-dev to CI dependencies for Python interop - Create dedicated python-stdlib test job in CI workflow - Move Python tests from manual/ to stdlib_python/ directory - Tests now run in CI with proper Python environment
Use deadsnakes PPA to ensure Python 3.11 is available in CI, matching the version expected by @stdlib/python module.
Switch from Python 3.11 to 3.10 which is the default on ubuntu-latest (Ubuntu 22.04). This allows the Python stdlib tests to run without needing additional PPAs or specific Python version installs.
- Install libpython3.10-dev explicitly for Python 3.10 library - Run ldconfig to refresh library cache - Add verification step to confirm library is available
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduces a new standard library module enabling Hemlock programs to use
Python libraries like Hugging Face Transformers, NumPy, PyTorch, etc.
Features:
Type conversion table:
Example usage:
import { py_init, py_import, py_call, from_python } from "@stdlib/python";
py_init();
let math = py_import("math");
let result = py_call(py_get(math, "sqrt"), [16]);
print(from_python(result)); // 4.0
py_finalize();