annotate rust/hg-pyo3/src/copy_tracing.rs @ 53040:cdd7bf612c7b stable tip

bundle-spec: properly format boolean parameter (issue6960) This was breaking automatic clone bundle generation. This changeset fixes it and add a test to catch it in the future.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Tue, 11 Mar 2025 02:29:42 +0100
parents 6332e5f857f6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52987
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
1 // copy_tracing.rs
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
2 //
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
3 // Copyright 2025 Mercurial developers
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
4 //
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
5 // This software may be used and distributed according to the terms of the
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
6 // GNU General Public License version 2 or any later version.
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
7
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
8 //! Bindings for the `hg::copy_tracing` module provided by the
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
9 //! `hg-core` package.
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
10 //!
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
11 //! From Python, this will be seen as `mercurial.pyo3_rustext.copy_tracing`
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
12
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
13 use hg::copy_tracing::ChangedFiles;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
14 use hg::copy_tracing::CombineChangesetCopies;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
15 use hg::Revision;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
16 use pyo3::types::PyBytes;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
17 use pyo3::types::PyDict;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
18 use pyo3::types::PyList;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
19 use pyo3::types::PyTuple;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
20
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
21 use crate::revision::PyRevision;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
22 use crate::utils::new_submodule;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
23 use crate::utils::PyBytesDeref;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
24
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
25 use pyo3::prelude::*;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
26
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
27 /// Combines copies information contained into revision `revs` to build a copy
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
28 /// map.
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
29 ///
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
30 /// See mercurial/copies.py for details
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
31 #[pyfunction]
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
32 #[pyo3(name = "combine_changeset_copies")]
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
33 pub fn combine_changeset_copies_wrapper(
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
34 revs: Bound<'_, PyList>,
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
35 children_count: Bound<'_, PyDict>,
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
36 target_rev: PyRevision,
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
37 rev_info: Bound<'_, PyAny>,
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
38 multi_thread: bool,
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
39 ) -> PyResult<PyObject> {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
40 let py = revs.py();
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
41 let target_rev = Revision(target_rev.0);
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
42 let children_count = children_count
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
43 .iter()
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
44 .map(|(k, v)| {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
45 Ok((Revision(k.extract::<PyRevision>()?.0), v.extract()?))
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
46 })
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
47 .collect::<PyResult<_>>()?;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
48
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
49 /// (Revision number, parent 1, parent 2, copy data for this revision)
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
50 type RevInfo<Bytes> = (Revision, Revision, Revision, Option<Bytes>);
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
51
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
52 let revs_info =
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
53 revs.iter().map(|rev_py| -> PyResult<RevInfo<Py<PyBytes>>> {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
54 let rev = Revision(rev_py.extract::<PyRevision>()?.0);
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
55 let ret = rev_info.call1((rev_py,))?;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
56 let tuple: &Bound<'_, PyTuple> = ret.downcast()?;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
57 let p1 = Revision(tuple.get_item(0)?.extract::<PyRevision>()?.0);
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
58 let p2 = Revision(tuple.get_item(1)?.extract::<PyRevision>()?.0);
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
59 let opt_bytes = tuple.get_item(2)?.extract()?;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
60 Ok((rev, p1, p2, opt_bytes))
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
61 });
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
62
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
63 let path_copies;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
64 if !multi_thread {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
65 let mut combine_changeset_copies =
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
66 CombineChangesetCopies::new(children_count);
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
67
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
68 for rev_info in revs_info {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
69 let (rev, p1, p2, opt_bytes) = rev_info?;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
70 let files = match &opt_bytes {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
71 Some(bytes) => ChangedFiles::new(bytes.as_bytes(py)),
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
72 // Python None was extracted to Option::None,
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
73 // meaning there was no copy data.
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
74 None => ChangedFiles::new_empty(),
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
75 };
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
76
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
77 combine_changeset_copies.add_revision(rev, p1, p2, files)
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
78 }
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
79 path_copies = combine_changeset_copies.finish(target_rev)
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
80 } else {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
81 // Use a bounded channel to provide back-pressure:
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
82 // if the child thread is slower to process revisions than this thread
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
83 // is to gather data for them, an unbounded channel would keep
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
84 // growing and eat memory.
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
85 //
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
86 // TODO: tweak the bound?
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
87 let (rev_info_sender, rev_info_receiver) =
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
88 crossbeam_channel::bounded::<RevInfo<PyBytesDeref>>(1000);
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
89
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
90 // This channel (going the other way around) however is unbounded.
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
91 // If they were both bounded, there might potentially be deadlocks
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
92 // where both channels are full and both threads are waiting on each
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
93 // other.
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
94 let (pybytes_sender, pybytes_receiver) =
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
95 crossbeam_channel::unbounded();
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
96
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
97 // Start a thread that does CPU-heavy processing in parallel with the
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
98 // loop below.
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
99 //
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
100 // If the parent thread panics, `rev_info_sender` will be dropped and
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
101 // “disconnected”. `rev_info_receiver` will be notified of this and
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
102 // exit its own loop.
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
103 let thread = std::thread::spawn(move || {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
104 let mut combine_changeset_copies =
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
105 CombineChangesetCopies::new(children_count);
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
106 for (rev, p1, p2, opt_bytes) in rev_info_receiver {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
107 let files = match &opt_bytes {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
108 Some(raw) => ChangedFiles::new(raw.as_ref()),
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
109 // Python None was extracted to Option::None,
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
110 // meaning there was no copy data.
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
111 None => ChangedFiles::new_empty(),
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
112 };
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
113 combine_changeset_copies.add_revision(rev, p1, p2, files);
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
114
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
115 // Send `PyBytes` back to the parent thread so the parent
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
116 // thread can drop it. Otherwise the GIL would be implicitly
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
117 // acquired here through `impl Drop for PyBytes`.
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
118 if let Some(bytes) = opt_bytes {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
119 if pybytes_sender.send(bytes.unwrap()).is_err() {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
120 // The channel is disconnected, meaning the parent
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
121 // thread panicked or returned
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
122 // early through
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
123 // `?` to propagate a Python exception.
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
124 break;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
125 }
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
126 }
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
127 }
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
128
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
129 combine_changeset_copies.finish(target_rev)
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
130 });
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
131
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
132 for rev_info in revs_info {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
133 let (rev, p1, p2, opt_bytes) = rev_info?;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
134 let opt_bytes = opt_bytes.map(|b| PyBytesDeref::new(py, b));
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
135
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
136 // We’d prefer to avoid the child thread calling into Python code,
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
137 // but this avoids a potential deadlock on the GIL if it does:
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
138 py.allow_threads(|| {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
139 rev_info_sender.send((rev, p1, p2, opt_bytes)).expect(
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
140 "combine_changeset_copies: channel is disconnected",
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
141 );
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
142 });
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
143
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
144 // Drop anything in the channel, without blocking
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
145 pybytes_receiver.try_iter().for_each(drop);
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
146 }
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
147 // We’d prefer to avoid the child thread calling into Python code,
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
148 // but this avoids a potential deadlock on the GIL if it does:
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
149 path_copies = py.allow_threads(|| {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
150 // Disconnect the channel to signal the child thread to stop:
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
151 // the `for … in rev_info_receiver` loop will end.
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
152 drop(rev_info_sender);
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
153
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
154 // Wait for the child thread to stop, and propagate any panic.
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
155 thread.join().unwrap_or_else(|panic_payload| {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
156 std::panic::resume_unwind(panic_payload)
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
157 })
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
158 });
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
159
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
160 // Drop anything left in the channel
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
161 drop(pybytes_receiver)
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
162 };
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
163
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
164 let out = PyDict::new(py);
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
165 for (dest, source) in path_copies.into_iter() {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
166 out.set_item(
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
167 PyBytes::new(py, &dest.into_vec()),
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
168 PyBytes::new(py, &source.into_vec()),
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
169 )?;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
170 }
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
171 Ok(out.into_any().unbind())
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
172 }
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
173
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
174 pub fn init_module<'py>(
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
175 py: Python<'py>,
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
176 package: &str,
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
177 ) -> PyResult<Bound<'py, PyModule>> {
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
178 let m = new_submodule(py, package, "copy_tracing")?;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
179 m.add_function(wrap_pyfunction!(combine_changeset_copies_wrapper, &m)?)?;
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
180 Ok(m)
6332e5f857f6 pyo3: transliterate the `copy_tracing` module from `hg-cpython`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
181 }