##// END OF EJS Templates
rust-cpython: use PyList.insert() instead of .insert_item()...
Yuya Nishihara -
r44701:06df075b default
parent child Browse files
Show More
@@ -1,129 +1,129 b''
1 1 // status.rs
2 2 //
3 3 // Copyright 2019, Raphaël Gomès <rgomes@octobus.net>
4 4 //
5 5 // This software may be used and distributed according to the terms of the
6 6 // GNU General Public License version 2 or any later version.
7 7
8 8 //! Bindings for the `hg::status` module provided by the
9 9 //! `hg-core` crate. From Python, this will be seen as
10 10 //! `rustext.dirstate.status`.
11 11
12 12 use crate::dirstate::DirstateMap;
13 13 use cpython::exc::ValueError;
14 14 use cpython::{
15 15 ObjectProtocol, PyBytes, PyErr, PyList, PyObject, PyResult, PyTuple,
16 16 Python, PythonObject, ToPyObject,
17 17 };
18 18 use hg::utils::hg_path::HgPathBuf;
19 19 use hg::{
20 20 matchers::{AlwaysMatcher, FileMatcher},
21 21 status,
22 22 utils::{files::get_path_from_bytes, hg_path::HgPath},
23 23 StatusResult,
24 24 };
25 25 use std::borrow::Borrow;
26 26
27 27 /// This will be useless once trait impls for collection are added to `PyBytes`
28 28 /// upstream.
29 29 fn collect_pybytes_list<P: AsRef<HgPath>>(
30 30 py: Python,
31 31 collection: &[P],
32 32 ) -> PyList {
33 33 let list = PyList::new(py, &[]);
34 34
35 35 for (i, path) in collection.iter().enumerate() {
36 list.insert_item(
36 list.insert(
37 37 py,
38 38 i,
39 39 PyBytes::new(py, path.as_ref().as_bytes()).into_object(),
40 40 )
41 41 }
42 42
43 43 list
44 44 }
45 45
46 46 pub fn status_wrapper(
47 47 py: Python,
48 48 dmap: DirstateMap,
49 49 matcher: PyObject,
50 50 root_dir: PyObject,
51 51 list_clean: bool,
52 52 last_normal_time: i64,
53 53 check_exec: bool,
54 54 ) -> PyResult<(PyList, PyList, PyList, PyList, PyList, PyList, PyList)> {
55 55 let bytes = root_dir.extract::<PyBytes>(py)?;
56 56 let root_dir = get_path_from_bytes(bytes.data(py));
57 57
58 58 let dmap: DirstateMap = dmap.to_py_object(py);
59 59 let dmap = dmap.get_inner(py);
60 60
61 61 match matcher.get_type(py).name(py).borrow() {
62 62 "alwaysmatcher" => {
63 63 let matcher = AlwaysMatcher;
64 64 let (lookup, status_res) = status(
65 65 &dmap,
66 66 &matcher,
67 67 &root_dir,
68 68 list_clean,
69 69 last_normal_time,
70 70 check_exec,
71 71 )
72 72 .map_err(|e| PyErr::new::<ValueError, _>(py, e.to_string()))?;
73 73 build_response(lookup, status_res, py)
74 74 }
75 75 "exactmatcher" => {
76 76 let files = matcher.call_method(
77 77 py,
78 78 "files",
79 79 PyTuple::new(py, &[]),
80 80 None,
81 81 )?;
82 82 let files: PyList = files.cast_into(py)?;
83 83 let files: PyResult<Vec<HgPathBuf>> = files
84 84 .iter(py)
85 85 .map(|f| {
86 86 Ok(HgPathBuf::from_bytes(
87 87 f.extract::<PyBytes>(py)?.data(py),
88 88 ))
89 89 })
90 90 .collect();
91 91
92 92 let files = files?;
93 93 let matcher = FileMatcher::new(&files)
94 94 .map_err(|e| PyErr::new::<ValueError, _>(py, e.to_string()))?;
95 95 let (lookup, status_res) = status(
96 96 &dmap,
97 97 &matcher,
98 98 &root_dir,
99 99 list_clean,
100 100 last_normal_time,
101 101 check_exec,
102 102 )
103 103 .map_err(|e| PyErr::new::<ValueError, _>(py, e.to_string()))?;
104 104 build_response(lookup, status_res, py)
105 105 }
106 106 e => {
107 107 return Err(PyErr::new::<ValueError, _>(
108 108 py,
109 109 format!("Unsupported matcher {}", e),
110 110 ));
111 111 }
112 112 }
113 113 }
114 114
115 115 fn build_response(
116 116 lookup: Vec<&HgPath>,
117 117 status_res: StatusResult,
118 118 py: Python,
119 119 ) -> PyResult<(PyList, PyList, PyList, PyList, PyList, PyList, PyList)> {
120 120 let modified = collect_pybytes_list(py, status_res.modified.as_ref());
121 121 let added = collect_pybytes_list(py, status_res.added.as_ref());
122 122 let removed = collect_pybytes_list(py, status_res.removed.as_ref());
123 123 let deleted = collect_pybytes_list(py, status_res.deleted.as_ref());
124 124 let clean = collect_pybytes_list(py, status_res.clean.as_ref());
125 125 let lookup = collect_pybytes_list(py, lookup.as_ref());
126 126 let unknown = PyList::new(py, &[]);
127 127
128 128 Ok((lookup, modified, added, removed, deleted, unknown, clean))
129 129 }
General Comments 0
You need to be logged in to leave comments. Login now