Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP [internal] Allow going from Rust-CPython pointers to PyO3 pyclass structs #12451

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/rust/engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/rust/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ members = [
"client",
"concrete_time",
"engine_pyo3",
"engine_pyo3_extension",
"fs",
"fs/brfs",
"fs/fs_util",
Expand Down Expand Up @@ -65,6 +66,7 @@ default-members = [
"async_value",
"concrete_time",
"engine_pyo3",
"engine_pyo3_extension",
"fs",
"fs/fs_util",
"fs/store",
Expand Down Expand Up @@ -126,6 +128,7 @@ num_enum = "0.4"
parking_lot = "0.11"
petgraph = "0.5"
process_execution = { path = "process_execution" }
pyo3 = "0.14.1"
rand = "0.8"
regex = "1"
reqwest = { version = "0.11", default_features = false, features = ["stream", "rustls-tls"] }
Expand Down
12 changes: 0 additions & 12 deletions src/rust/engine/engine_pyo3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@ name = "engine_pyo3"
authors = [ "Pants Build <[email protected]>" ]
publish = false

[lib]
crate-type = ["cdylib"]

[features]
# NB: To actually load this crate from python, the `extension-module` feature must be enabled. But
# unfortunately, enabling `extension-module` causes tests linked against `pyo3` to fail. We
# define a feature to enable that, but we do not enable it by default: someone building this module
# in order to extract `libengine_pyo3.so` should pass `cargo build .. --features=engine_pyo3/extension-module`.
# see https://github.com/PyO3/pyo3/issues/340
extension-module = ["pyo3/extension-module"]
default = []

[dependencies]
fs = { path = "../fs" }
hashing = { path = "../hashing" }
Expand Down
4 changes: 0 additions & 4 deletions src/rust/engine/engine_pyo3/src/externs/mod.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use fs::{GlobExpansionConjunction, PathGlobs, PreparedPathGlobs, StrictGlobMatch
use hashing::{Digest, Fingerprint};
use store::Snapshot;

pub(crate) fn register(m: &PyModule) -> PyResult<()> {
pub fn register(m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(match_path_globs, m)?)?;
m.add_function(wrap_pyfunction!(default_cache_path, m)?)?;
m.add_class::<PyDigest>()?;
Expand Down
6 changes: 5 additions & 1 deletion src/rust/engine/engine_pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@
#![allow(clippy::not_unsafe_ptr_arg_deref)]
#![type_length_limit = "43757804"]

mod externs;
pub mod fs;
pub mod nailgun;
pub mod scheduler;
pub mod testutil;
pub mod workunits;
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).

use super::PyExecutor;
use pyo3::create_exception;
use pyo3::exceptions::{PyBrokenPipeError, PyException, PyKeyboardInterrupt};
use pyo3::prelude::*;
use pyo3::types::PyDict;

use crate::scheduler::PyExecutor;
use task_executor::Executor;

pub(crate) fn register(py: Python, m: &PyModule) -> PyResult<()> {
pub fn register(py: Python, m: &PyModule) -> PyResult<()> {
m.add(
"PantsdConnectionException",
py.get_type::<PantsdConnectionException>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,14 @@
use pyo3::exceptions::PyException;
use pyo3::prelude::*;

mod fs;
mod nailgun;
mod testutil;
mod workunits;

#[pymodule]
fn native_engine_pyo3(py: Python, m: &PyModule) -> PyResult<()> {
self::fs::register(m)?;
self::nailgun::register(py, m)?;
self::testutil::register(m)?;
self::workunits::register(m)?;

pub fn register(m: &PyModule) -> PyResult<()> {
m.add_class::<PyExecutor>()?;

Ok(())
}

#[pyclass]
#[derive(Debug, Clone)]
struct PyExecutor(task_executor::Executor);
pub struct PyExecutor(pub task_executor::Executor);

#[pymethods]
impl PyExecutor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

use std::sync::Arc;

use super::PyExecutor;
use parking_lot::Mutex;
use pyo3::exceptions::PyAssertionError;
use pyo3::prelude::*;
use pyo3::types::PyType;

use crate::scheduler::PyExecutor;
use testutil_mock::{StubCAS, StubCASBuilder};

pub(crate) fn register(m: &PyModule) -> PyResult<()> {
pub fn register(m: &PyModule) -> PyResult<()> {
m.add_class::<PyStubCAS>()?;
m.add_class::<PyStubCASBuilder>()?;
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// Licensed under the Apache License, Version 2.0 (see LICENSE).

use pyo3::prelude::*;

use workunit_store::Metric;

pub(crate) fn register(m: &PyModule) -> PyResult<()> {
pub fn register(m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(all_counter_names, m)?)?;
Ok(())
}
Expand Down
22 changes: 22 additions & 0 deletions src/rust/engine/engine_pyo3_extension/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
version = "0.0.1"
edition = "2018"
name = "engine_pyo3_extension"
authors = [ "Pants Build <[email protected]>" ]
publish = false

[lib]
crate-type = ["cdylib"]

[features]
# NB: To actually load this crate from python, the `extension-module` feature must be enabled. But
# unfortunately, enabling `extension-module` causes tests linked against `pyo3` to fail. We
# define a feature to enable that, but we do not enable it by default: someone building this module
# in order to extract `libengine_pyo3.so` should pass `cargo build .. --features=engine_pyo3/extension-module`.
# see https://github.com/PyO3/pyo3/issues/340
extension-module = ["pyo3/extension-module"]
default = []

[dependencies]
engine_pyo3 = { "path" = "../engine_pyo3" }
pyo3 = "0.14.1"
34 changes: 34 additions & 0 deletions src/rust/engine/engine_pyo3_extension/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).

#![deny(warnings)]
// Enable all clippy lints except for many of the pedantic ones. It's a shame this needs to be copied and pasted across crates, but there doesn't appear to be a way to include inner attributes from a common source.
#![deny(
clippy::all,
clippy::default_trait_access,
clippy::expl_impl_clone_on_copy,
clippy::if_not_else,
clippy::needless_continue,
clippy::unseparated_literal_suffix,
// TODO: Falsely triggers for async/await:
// see https://github.com/rust-lang/rust-clippy/issues/5360
// clippy::used_underscore_binding
)]
// It is often more clear to show that nothing is being moved.
#![allow(clippy::match_ref_pats)]
// Subjective style.
#![allow(
clippy::len_without_is_empty,
clippy::redundant_field_names,
clippy::too_many_arguments
)]
// Default isn't as big a deal as people seem to think it is.
#![allow(clippy::new_without_default, clippy::new_ret_no_self)]
// Arc<Mutex> can be more clear than needing to grok Orderings:
#![allow(clippy::mutex_atomic)]

fn main() {
// NB: The native extension only works with the Python interpreter version it was built with
// (e.g. Python 3.7 vs 3.8).
println!("cargo:rerun-if-env-changed=PY");
}
40 changes: 40 additions & 0 deletions src/rust/engine/engine_pyo3_extension/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).

#![deny(warnings)]
// Enable all clippy lints except for many of the pedantic ones. It's a shame this needs to be copied and pasted across crates, but there doesn't appear to be a way to include inner attributes from a common source.
#![deny(
clippy::all,
clippy::default_trait_access,
clippy::expl_impl_clone_on_copy,
clippy::if_not_else,
clippy::needless_continue,
clippy::unseparated_literal_suffix,
// TODO: Falsely triggers for async/await:
// see https://github.com/rust-lang/rust-clippy/issues/5360
// clippy::used_underscore_binding
)]
// It is often more clear to show that nothing is being moved.
#![allow(clippy::match_ref_pats)]
// Subjective style.
#![allow(
clippy::len_without_is_empty,
clippy::redundant_field_names,
clippy::too_many_arguments
)]
// Default isn't as big a deal as people seem to think it is.
#![allow(clippy::new_without_default, clippy::new_ret_no_self)]
// Arc<Mutex> can be more clear than needing to grok Orderings:
#![allow(clippy::mutex_atomic)]

use pyo3::prelude::*;

#[pymodule]
fn native_engine_pyo3(py: Python, m: &PyModule) -> PyResult<()> {
engine_pyo3::fs::register(m)?;
engine_pyo3::nailgun::register(py, m)?;
engine_pyo3::scheduler::register(m)?;
engine_pyo3::testutil::register(m)?;
engine_pyo3::workunits::register(m)?;
Ok(())
}
8 changes: 8 additions & 0 deletions src/rust/engine/src/externs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use cpython::{
PyObject, PyResult as CPyResult, PyTuple, PyType, Python, PythonObject, ToPyObject,
};
use lazy_static::lazy_static;
use pyo3::FromPyPointer;

use logging::PythonLogLevel;

Expand Down Expand Up @@ -420,3 +421,10 @@ pub enum GeneratorResponse {
Get(Get),
GetMulti(Vec<Get>),
}

pub(crate) fn from_rust_cpython_to_pyo3<'p>(
value: &PyObject,
py: pyo3::Python<'p>,
) -> &'p pyo3::PyAny {
unsafe { pyo3::PyAny::from_owned_ptr(py, value.as_ptr() as *mut pyo3::ffi::PyObject) }
}