author | Pierre-Yves David <pierre-yves.david@octobus.net> |
Fri, 02 Dec 2022 18:19:59 +0100 | |
changeset 49757 | 5f71fff8dc74 |
parent 49631 | c7fb9b74e753 |
child 52046 | 28a0eb21ff04 |
permissions | -rw-r--r-- |
44505
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
1 |
use cpython::exc::ValueError; |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
2 |
use cpython::{PyBytes, PyDict, PyErr, PyObject, PyResult, PyTuple, Python}; |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
3 |
use hg::revlog::Node; |
43251
970978975574
rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
4 |
|
970978975574
rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
5 |
#[allow(unused)] |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
6 |
pub fn print_python_trace(py: Python) -> PyResult<PyObject> { |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
7 |
eprintln!("==============================="); |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
8 |
eprintln!("Printing Python stack from Rust"); |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
9 |
eprintln!("==============================="); |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
10 |
let traceback = py.import("traceback")?; |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
11 |
let sys = py.import("sys")?; |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
12 |
let kwargs = PyDict::new(py); |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
13 |
kwargs.set_item(py, "file", sys.get(py, "stderr")?)?; |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
14 |
traceback.call(py, "print_stack", PyTuple::new(py, &[]), Some(&kwargs)) |
970978975574
rust-utils: introduce a debug util to print the python stack trace
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
15 |
} |
44505
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
16 |
|
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
17 |
// Necessary evil for the time being, could maybe be moved to |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
18 |
// a TryFrom in Node itself |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
19 |
const NODE_BYTES_LENGTH: usize = 20; |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
20 |
type NodeData = [u8; NODE_BYTES_LENGTH]; |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
21 |
|
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
22 |
/// Copy incoming Python bytes given as `PyObject` into `Node`, |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
23 |
/// doing the necessary checks |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
24 |
pub fn node_from_py_object<'a>( |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
25 |
py: Python, |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
26 |
bytes: &'a PyObject, |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
27 |
) -> PyResult<Node> { |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
28 |
let as_py_bytes: &'a PyBytes = bytes.extract(py)?; |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
29 |
node_from_py_bytes(py, as_py_bytes) |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
30 |
} |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
31 |
|
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
32 |
/// Clone incoming Python bytes given as `PyBytes` as a `Node`, |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
33 |
/// doing the necessary checks. |
44973
26114bd6ec60
rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents:
44505
diff
changeset
|
34 |
pub fn node_from_py_bytes(py: Python, bytes: &PyBytes) -> PyResult<Node> { |
44505
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
35 |
<NodeData>::try_from(bytes.data(py)) |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
36 |
.map_err(|_| { |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
37 |
PyErr::new::<ValueError, _>( |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
38 |
py, |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
39 |
format!("{}-byte hash required", NODE_BYTES_LENGTH), |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
40 |
) |
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
41 |
}) |
44973
26114bd6ec60
rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents:
44505
diff
changeset
|
42 |
.map(Into::into) |
44505
d738b7a18438
rust-nodemap: add utils to create `Node`s from Python objects
Georges Racinet <georges.racinet@octobus.net>
parents:
43251
diff
changeset
|
43 |
} |