Skip to content

Add @stdlib/python module for Python interoperability#405

Open
nbeerbower wants to merge 10 commits into
mainfrom
claude/python-interop-stdlib-zvOH7
Open

Add @stdlib/python module for Python interoperability#405
nbeerbower wants to merge 10 commits into
mainfrom
claude/python-interop-stdlib-zvOH7

Conversation

@nbeerbower

Copy link
Copy Markdown
Collaborator

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();

claude added 10 commits January 18, 2026 21:35
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants