Page MenuHomePhabricator

rust-cpython: started cpython crate bindings

Authored by gracinet on Dec 15 2018, 6:41 AM.



This changeset introduces the hg-cpython crate,
that compiles as a shared library holding a whole
Python package (mercurial.rustext), with only the empty
'ancestor' submodule for now.

Such bindings will be easier and safer to develop and maintain
that those of hg-direct-ffi.
They don't involve C code, only unsafe Rust that's mostly isolated
within the cpython crate.

The long-term goal would be to import the provided modules, such
as rustext.ancestor with mercurial.policy.importmod, same as
we already do with cext modules.

Diff Detail

rHG Mercurial
Automatic diff as part of commit; lint not applicable.
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

gracinet created this revision.Dec 15 2018, 6:41 AM
yuja added a subscriber: yuja.Dec 15 2018, 10:22 PM

I've queued the first 5 patches, thanks. Please send a follow up to fix nits
and minor issues.

+py_module_initializer!(rustext, initrustext, PyInit_rustext, |py, m| {
+ m.add(
+ py,
+ "doc",
+ "Mercurial core concepts - Rust implementation",
+ )?;
+ let dotted_name: String = m.get(py, "name")?.extract(py)?;
+ m.add(py, "package", "mercurial")?;

IIUC, package should be set by the importer, not by the module. And if
set, I think it should be "mercurial.rustext" since this is a package module.

+impl GraphError {
+ pub fn pynew(py: Python, inner: hg::GraphError) -> PyErr {
+ match inner {
+ hg::GraphError::ParentOutOfRange(r) => {
+ GraphError::new(py, ("ParentOutOfRange", r))
+ }
+ }
+ }

Maybe this can be a trait impl or helper function that maps hg::GraphError
to PyErr::new::<exc::ValueType, _>() ?

+ let sys = PyModule::import(py, "sys")?;
+ let sys_modules: PyDict = sys.get(py, "modules")?.extract(py)?;
+ sys_modules.set_item(py, dotted_name, &m)?;

We'll probably want to move this to the caller to avoid dups. The init
function can have the same signature as the lambda of


Perhaps this is an old name.

This revision was automatically updated to reflect the committed changes.