pyfail: module to use FAILPOINTS in Python

Summary:
This allows us to insert FAILPOINTS in Python so we can use sleep, return error
etc.

Differential Revision: D30495224

fbshipit-source-id: aef56d03bc32eefb69573cfa586aa63a301edffc
This commit is contained in:
Jun Wu 2021-08-27 11:28:42 -07:00 committed by Facebook GitHub Bot
parent c25134d21a
commit 3562880804
4 changed files with 48 additions and 0 deletions

View File

@ -32,6 +32,7 @@ pydirs = { path = "modules/pydirs" }
pyeagerepo = { path = "modules/pyeagerepo" }
pyedenapi = { path = "modules/pyedenapi" }
pyerror = { path = "modules/pyerror" }
pyfail = { path = "modules/pyfail" }
pyfs = { path = "modules/pyfs" }
pyhgmetrics = { path = "modules/pyhgmetrics" }
pyhgtime = { path = "modules/pyhgtime" }

View File

@ -0,0 +1,14 @@
[package]
name = "pyfail"
version = "0.1.0"
edition = "2018"
[dependencies]
cpython_ext = { path = "../../../../lib/cpython-ext", default-features = false }
cpython = { version = "0.5", default-features = false }
fail = "0.4"
[features]
default = []
python2 = ["cpython/python27-sys", "cpython_ext/python2"]
python3 = ["cpython/python3-sys", "cpython_ext/python3"]

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
use cpython::*;
use cpython_ext::PyNone;
use std::io;
pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> {
let name = [package, "fail"].join(".");
let m = PyModule::new(py, &name)?;
m.add(py, "failpoint", py_fn!(py, failpoint(name: &str)))?;
Ok(m)
}
fn failpoint(py: Python, name: &str) -> PyResult<PyNone> {
if let Some(e) = fail::eval(name, |_| fail_error(name)) {
Err(cpython_ext::error::translate_io_error(py, &e).into())
} else {
Ok(PyNone)
}
}
fn fail_error(name: &str) -> io::Error {
io::Error::new(
io::ErrorKind::Other,
format!("failpoint '{}' set by FAILPOINTS", name),
)
}

View File

@ -30,6 +30,7 @@ pub(crate) fn populate_module(py: Python<'_>, module: &PyModule) -> PyResult<PyN
m.add(py, "eagerepo", pyeagerepo::init_module(py, &name)?)?;
m.add(py, "edenapi", pyedenapi::init_module(py, &name)?)?;
m.add(py, "error", pyerror::init_module(py, &name)?)?;
m.add(py, "fail", pyfail::init_module(py, &name)?)?;
m.add(py, "fs", pyfs::init_module(py, &name)?)?;
m.add(py, "hgmetrics", pyhgmetrics::init_module(py, &name)?)?;
m.add(py, "hgtime", pyhgtime::init_module(py, &name)?)?;