Skip to content

Commit

Permalink
Revert "[internal] Switch from Rust-Cpython to PyO3 (pantsbuild#13526)"
Browse files Browse the repository at this point in the history
This reverts commit 021c3c0.
  • Loading branch information
Tom Dyas committed Nov 15, 2021
1 parent e48bc6d commit afc4fd6
Show file tree
Hide file tree
Showing 19 changed files with 2,048 additions and 1,457 deletions.
1 change: 1 addition & 0 deletions src/python/pants/bin/daemon_pants_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def __call__(
command: str,
args: Tuple[str, ...],
env: Dict[str, str],
working_directory: bytes,
cancellation_latch: PySessionCancellationLatch,
stdin_fileno: int,
stdout_fileno: int,
Expand Down
83 changes: 51 additions & 32 deletions src/python/pants/engine/fs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
Workspace,
)
from pants.engine.goal import Goal, GoalSubsystem
from pants.engine.internals.native_engine_pyo3 import PyDigest as DigestPyO3
from pants.engine.internals.native_engine_pyo3 import PySnapshot as SnapshotPyO3
from pants.engine.internals.scheduler import ExecutionError
from pants.engine.rules import Get, goal_rule, rule
from pants.testutil.rule_runner import QueryRule, RuleRunner
Expand Down Expand Up @@ -1091,50 +1093,60 @@ def is_changed_snapshot() -> bool:
# -----------------------------------------------------------------------------------------------


def test_digest_properties() -> None:
digest = Digest("a" * 64, 1000)
@pytest.mark.parametrize("cls", [Digest, DigestPyO3])
def test_digest_properties(cls) -> None:
digest = cls("a" * 64, 1000)
assert digest.fingerprint == "a" * 64
assert digest.serialized_bytes_length == 1000


def test_digest_repr() -> None:
assert str(Digest("a" * 64, 1)) == f"Digest({repr('a' * 64)}, 1)"
@pytest.mark.parametrize("cls", [Digest, DigestPyO3])
def test_digest_repr(cls) -> None:
assert str(cls("a" * 64, 1)) == f"Digest({repr('a' * 64)}, 1)"


def test_digest_hash() -> None:
assert hash(Digest("a" * 64, 1)) == -6148914691236517206
assert hash(Digest("b" * 64, 1)) == -4919131752989213765
@pytest.mark.parametrize("cls", [Digest, DigestPyO3])
def test_digest_hash(cls) -> None:
assert hash(cls("a" * 64, 1)) == -6148914691236517206
assert hash(cls("b" * 64, 1)) == -4919131752989213765
# Note that the size bytes is not considered in the hash.
assert hash(Digest("a" * 64, 1000)) == -6148914691236517206
assert hash(cls("a" * 64, 1000)) == -6148914691236517206


def test_digest_equality() -> None:
digest = Digest("a" * 64, 1)
assert digest == Digest("a" * 64, 1)
assert digest != Digest("a" * 64, 1000)
assert digest != Digest("0" * 64, 1)
@pytest.mark.parametrize("cls", [Digest, DigestPyO3])
def test_digest_equality(cls) -> None:
digest = cls("a" * 64, 1)
assert digest == cls("a" * 64, 1)
assert digest != cls("a" * 64, 1000)
assert digest != cls("0" * 64, 1)
with pytest.raises(TypeError):
digest < digest # type: ignore[operator]
digest < digest


def test_snapshot_properties() -> None:
digest = Digest("a" * 64, 1000)
snapshot = Snapshot._create_for_testing(digest, ["f.ext", "dir/f.ext"], ["dir"])
@pytest.mark.parametrize(
"snapshot_cls,digest_cls", [(Snapshot, Digest), (SnapshotPyO3, DigestPyO3)]
)
def test_snapshot_properties(snapshot_cls, digest_cls) -> None:
digest = digest_cls("a" * 64, 1000)
snapshot = snapshot_cls._create_for_testing(digest, ["f.ext", "dir/f.ext"], ["dir"])
assert snapshot.digest == digest
assert snapshot.files == ("f.ext", "dir/f.ext")
assert snapshot.dirs == ("dir",)


def test_snapshot_hash() -> None:
@pytest.mark.parametrize(
"snapshot_cls,digest_cls", [(Snapshot, Digest), (SnapshotPyO3, DigestPyO3)]
)
def test_snapshot_hash(snapshot_cls, digest_cls) -> None:
def assert_hash(
expected: int,
*,
digest_char: str = "a",
files: Optional[List[str]] = None,
dirs: Optional[List[str]] = None,
) -> None:
digest = Digest(digest_char * 64, 1000)
snapshot = Snapshot._create_for_testing(
digest = digest_cls(digest_char * 64, 1000)
snapshot = snapshot_cls._create_for_testing(
digest, files or ["f.ext", "dir/f.ext"], dirs or ["dir"]
)
assert hash(snapshot) == expected
Expand All @@ -1147,21 +1159,28 @@ def assert_hash(
assert_hash(-4919131752989213765, digest_char="b")


def test_snapshot_equality() -> None:
@pytest.mark.parametrize(
"snapshot_cls,digest_cls", [(Snapshot, Digest), (SnapshotPyO3, DigestPyO3)]
)
def test_snapshot_equality(snapshot_cls, digest_cls) -> None:
# Only the digest is used for equality.
snapshot = Snapshot._create_for_testing(Digest("a" * 64, 1000), ["f.ext", "dir/f.ext"], ["dir"])
assert snapshot == Snapshot._create_for_testing(
Digest("a" * 64, 1000), ["f.ext", "dir/f.ext"], ["dir"]
snapshot = snapshot_cls._create_for_testing(
digest_cls("a" * 64, 1000), ["f.ext", "dir/f.ext"], ["dir"]
)
assert snapshot == snapshot_cls._create_for_testing(
digest_cls("a" * 64, 1000), ["f.ext", "dir/f.ext"], ["dir"]
)
assert snapshot == snapshot_cls._create_for_testing(
digest_cls("a" * 64, 1000), ["f.ext", "dir/f.ext"], ["foo"]
)
assert snapshot == Snapshot._create_for_testing(
Digest("a" * 64, 1000), ["f.ext", "dir/f.ext"], ["foo"]
assert snapshot == snapshot_cls._create_for_testing(
digest_cls("a" * 64, 1000), ["f.ext"], ["dir"]
)
assert snapshot == Snapshot._create_for_testing(Digest("a" * 64, 1000), ["f.ext"], ["dir"])
assert snapshot != Snapshot._create_for_testing(
Digest("a" * 64, 0), ["f.ext", "dir/f.ext"], ["dir"]
assert snapshot != snapshot_cls._create_for_testing(
digest_cls("a" * 64, 0), ["f.ext", "dir/f.ext"], ["dir"]
)
assert snapshot != Snapshot._create_for_testing(
Digest("b" * 64, 1000), ["f.ext", "dir/f.ext"], ["dir"]
assert snapshot != snapshot_cls._create_for_testing(
digest_cls("b" * 64, 1000), ["f.ext", "dir/f.ext"], ["dir"]
)
with pytest.raises(TypeError):
snapshot < snapshot # type: ignore[operator]
snapshot < snapshot
9 changes: 2 additions & 7 deletions src/python/pants/engine/internals/native_engine.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class RawFdRunner(Protocol):
command: str,
args: tuple[str, ...],
env: dict[str, str],
working_directory: bytes,
cancellation_latch: PySessionCancellationLatch,
stdin_fileno: int,
stdout_fileno: int,
Expand Down Expand Up @@ -148,9 +149,6 @@ class PyDigest:
def fingerprint(self) -> str: ...
@property
def serialized_bytes_length(self) -> int: ...
def __eq__(self, other: PyDigest | Any) -> bool: ...
def __hash__(self) -> int: ...
def __repr__(self) -> str: ...

class PySnapshot:
def __init__(self) -> None: ...
Expand All @@ -164,9 +162,6 @@ class PySnapshot:
def dirs(self) -> tuple[str, ...]: ...
@property
def files(self) -> tuple[str, ...]: ...
def __eq__(self, other: PySnapshot | Any) -> bool: ...
def __hash__(self) -> int: ...
def __repr__(self) -> str: ...

class PyExecutionRequest:
def __init__(
Expand All @@ -189,7 +184,7 @@ class PyGeneratorResponseGetMulti:
def __init__(self, gets: tuple[PyGeneratorResponseGet, ...]) -> None: ...

class PyNailgunServer:
def port(self) -> int: ...
pass

class PyRemotingOptions:
def __init__(self, **kwargs: Any) -> None: ...
Expand Down
26 changes: 26 additions & 0 deletions src/python/pants/engine/internals/native_engine_pyo3.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from __future__ import annotations

from typing import Any, Sequence

from pants.engine.fs import PathGlobs

# TODO: black and flake8 disagree about the content of this file:
Expand All @@ -26,6 +28,30 @@ def default_cache_path() -> str: ...
# cast to `tuple()` when not necessary.
def match_path_globs(path_globs: PathGlobs, paths: tuple[str, ...]) -> str: ...

class PyDigest:
def __init__(self, fingerprint: str, serialized_bytes_length: int) -> None: ...
@property
def fingerprint(self) -> str: ...
@property
def serialized_bytes_length(self) -> int: ...
def __eq__(self, other: PyDigest | Any) -> bool: ...
def __hash__(self) -> int: ...

class PySnapshot:
def __init__(self) -> None: ...
@classmethod
def _create_for_testing(
cls, digest: PyDigest, files: Sequence[str], dirs: Sequence[str]
) -> PySnapshot: ...
@property
def digest(self) -> PyDigest: ...
@property
def dirs(self) -> tuple[str, ...]: ...
@property
def files(self) -> tuple[str, ...]: ...
def __eq__(self, other: PySnapshot | Any) -> bool: ...
def __hash__(self) -> int: ...

# ------------------------------------------------------------------------------
# Workunits
# ------------------------------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions src/python/pants/engine/internals/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,12 @@ def execute(

states = [
Throw(
raw_root.result,
python_traceback=raw_root.python_traceback,
engine_traceback=raw_root.engine_traceback,
raw_root.result(),
python_traceback=raw_root.python_traceback(),
engine_traceback=raw_root.engine_traceback(),
)
if raw_root.is_throw
else Return(raw_root.result)
if raw_root.is_throw()
else Return(raw_root.result())
for raw_root in raw_roots
]

Expand Down
58 changes: 41 additions & 17 deletions src/rust/engine/Cargo.lock

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

Loading

0 comments on commit afc4fd6

Please sign in to comment.