# HG changeset patch # User Simon Sapin # Date 1632136596 -7200 # Node ID 627cd8f33db0a60090b80d5470addc7e1a4e3f7e # Parent 1194394510ba35243703992b08d62dd0e24fca33 rust: Remove support for passing a dict to the Rust pathutil.dirs() That is only used by the Python dirstatemap, which not used when Rust is enabled. This allows removing the private `extract_dirstate` function which creates `DirstateEntry` values. This in turn will make easier upcoming changes to `DirstateEntry`. Differential Revision: https://phab.mercurial-scm.org/D11460 diff -r 1194394510ba -r 627cd8f33db0 rust/hg-cpython/src/dirstate.rs --- a/rust/hg-cpython/src/dirstate.rs Mon Sep 20 12:52:32 2021 +0200 +++ b/rust/hg-cpython/src/dirstate.rs Mon Sep 20 13:16:36 2021 +0200 @@ -21,13 +21,11 @@ exceptions, }; use cpython::{ - exc, PyBytes, PyDict, PyErr, PyList, PyModule, PyObject, PyResult, - PySequence, Python, + PyBytes, PyDict, PyErr, PyList, PyModule, PyObject, PyResult, Python, }; use hg::dirstate_tree::on_disk::V2_FORMAT_MARKER; -use hg::{utils::hg_path::HgPathBuf, DirstateEntry, EntryState, StateMap}; +use hg::DirstateEntry; use libc::{c_char, c_int}; -use std::convert::TryFrom; // C code uses a custom `dirstate_tuple` type, checks in multiple instances // for this type, and raises a Python `Exception` if the check does not pass. @@ -78,34 +76,6 @@ maybe_obj.ok_or_else(|| PyErr::fetch(py)) } -pub fn extract_dirstate(py: Python, dmap: &PyDict) -> Result { - dmap.items(py) - .iter() - .map(|(filename, stats)| { - let stats = stats.extract::(py)?; - let state = stats.get_item(py, 0)?.extract::(py)?; - let state = - EntryState::try_from(state.data(py)[0]).map_err(|e| { - PyErr::new::(py, e.to_string()) - })?; - let mode = stats.get_item(py, 1)?.extract(py)?; - let size = stats.get_item(py, 2)?.extract(py)?; - let mtime = stats.get_item(py, 3)?.extract(py)?; - let filename = filename.extract::(py)?; - let filename = filename.data(py); - Ok(( - HgPathBuf::from(filename.to_owned()), - DirstateEntry { - state, - mode, - size, - mtime, - }, - )) - }) - .collect() -} - /// Create the module, with `__package__` given from parent pub fn init_module(py: Python, package: &str) -> PyResult { let dotted_name = &format!("{}.dirstate", package); diff -r 1194394510ba -r 627cd8f33db0 rust/hg-cpython/src/dirstate/dirs_multiset.rs --- a/rust/hg-cpython/src/dirstate/dirs_multiset.rs Mon Sep 20 12:52:32 2021 +0200 +++ b/rust/hg-cpython/src/dirstate/dirs_multiset.rs Mon Sep 20 13:16:36 2021 +0200 @@ -11,14 +11,13 @@ use std::cell::RefCell; use cpython::{ - exc, ObjectProtocol, PyBool, PyBytes, PyClone, PyDict, PyErr, PyObject, - PyResult, Python, UnsafePyLeaked, + exc, ObjectProtocol, PyBytes, PyClone, PyDict, PyErr, PyObject, PyResult, + Python, UnsafePyLeaked, }; -use crate::dirstate::extract_dirstate; use hg::{ utils::hg_path::{HgPath, HgPathBuf}, - DirsMultiset, DirsMultisetIter, DirstateError, DirstateMapError, + DirsMultiset, DirsMultisetIter, DirstateMapError, }; py_class!(pub class Dirs |py| { @@ -29,20 +28,11 @@ def __new__( _cls, map: PyObject, - only_tracked: Option = None ) -> PyResult { - let only_tracked_b = if let Some(only_tracked) = only_tracked { - only_tracked.extract::(py)?.is_true() - } else { - false - }; - let inner = if let Ok(map) = map.cast_as::(py) { - let dirstate = extract_dirstate(py, &map)?; - let dirstate = dirstate.iter().map(|(k, v)| Ok((k, *v))); - DirsMultiset::from_dirstate(dirstate, only_tracked_b) - .map_err(|e: DirstateError| { - PyErr::new::(py, e.to_string()) - })? + let inner = if map.cast_as::(py).is_ok() { + let err = "pathutil.dirs() with a dict should only be used by the Python dirstatemap \ + and should not be used when Rust is enabled"; + return Err(PyErr::new::(py, err.to_string())) } else { let map: Result, PyErr> = map .iter(py)? diff -r 1194394510ba -r 627cd8f33db0 tests/test-dirs.py --- a/tests/test-dirs.py Mon Sep 20 12:52:32 2021 +0200 +++ b/tests/test-dirs.py Mon Sep 20 13:16:36 2021 +0200 @@ -13,13 +13,13 @@ (b'a/a/a', [b'a', b'a/a', b'']), (b'alpha/beta/gamma', [b'', b'alpha', b'alpha/beta']), ]: - d = pathutil.dirs({}) + d = pathutil.dirs([]) d.addpath(case) self.assertEqual(sorted(d), sorted(want)) def testinvalid(self): with self.assertRaises(ValueError): - d = pathutil.dirs({}) + d = pathutil.dirs([]) d.addpath(b'a//b')