Update documentation

This commit is contained in:
Daniel Grunwald 2017-01-27 21:51:56 +01:00
parent f5130e51eb
commit 32e0d35603
2 changed files with 26 additions and 15 deletions

View file

@ -120,6 +120,7 @@ pub fn parse_args(
/// 5. `*name : ty`
/// 6. `**name`
/// 7. `**name : ty`
///
/// The types used must implement the `FromPyObject` trait.
/// If no type is specified, the parameter implicitly uses
/// `&PyObject` (format 1), `&PyTuple` (format 4) or `&PyDict` (format 6).
@ -132,6 +133,13 @@ pub fn parse_args(
/// them to the parameters. If the extraction is successful, `py_argparse!()` evaluates
/// the body expression and returns of that evaluation.
/// If extraction fails, `py_argparse!()` returns a failed `PyResult` without evaluating `body`.
///
/// The `py_argparse!()` macro special-cases reference types (when `ty` starts with a `&` token):
/// In this case, the macro uses the `RefFromPyObject` trait instead of the `FromPyObject` trait.
/// When using at least one reference parameter, the `body` block is placed within a closure,
/// so `return` statements might behave unexpectedly in this case. (this only affects direct use
/// of `py_argparse!`; `py_fn!` is unaffected as the body there is always in a separate function
/// from the generated argument-parsing code).
#[macro_export]
macro_rules! py_argparse {
($py:expr, $fname:expr, $args:expr, $kwargs:expr, $plist:tt $body:block) => {

View file

@ -141,6 +141,23 @@ pub struct PyErr {
pub type PyResult<T> = Result<T, PyErr>;
impl PyErr {
/// Creates a new PyErr of type `T`.
///
/// `value` can be:
/// * `NoArgs`: the exception instance will be created using python `T()`
/// * a tuple: the exception instance will be created using python `T(*tuple)`
/// * any other value: the exception instance will be created using python `T(value)`
///
/// Panics if `T` is not a python class derived from `BaseException`.
///
/// Example:
/// `return Err(PyErr::new::<exc::TypeError, _>(py, "Error message"));`
pub fn new<T, V>(py: Python, value: V) -> PyErr
where T: PythonObjectWithTypeObject, V: ToPyObject
{
PyErr::new_helper(py, py.get_type::<T>(), value.to_py_object(py).into_object())
}
/// Gets whether an error is present in the Python interpreter's global state.
#[inline]
pub fn occurred(_ : Python) -> bool {
@ -199,20 +216,6 @@ impl PyErr {
}
}
/// Creates a new PyErr of type `T`.
///
/// `value` can be:
/// * `NoArgs`: the exception instance will be created using python `T()`
/// * a tuple: the exception instance will be created using python `T(*tuple)`
/// * any other value: the exception instance will be created using python `T(value)`
///
/// Panics if `T` is not a python class derived from `BaseException`.
pub fn new<T, V>(py: Python, value: V) -> PyErr
where T: PythonObjectWithTypeObject, V: ToPyObject
{
PyErr::new_helper(py, py.get_type::<T>(), value.to_py_object(py).into_object())
}
fn new_helper(_py: Python, ty: PyType, value: PyObject) -> PyErr {
assert!(unsafe { ffi::PyExceptionClass_Check(ty.as_object().as_ptr()) } != 0);
PyErr {
@ -254,7 +257,7 @@ impl PyErr {
}
/// Construct a new error, with the usual lazy initialization of Python exceptions.
/// `exc` is the exception type; usually one of the standard exceptions like `PyExc::runtime_error()`.
/// `exc` is the exception type; usually one of the standard exceptions like `py.get_type::<exc::RuntimeError>()`.
/// `value` is the exception instance, or a tuple of arguments to pass to the exception constructor.
#[inline]
pub fn new_lazy_init(exc: PyType, value: Option<PyObject>) -> PyErr {