pyo3/pyo3-derive-backend/src/py_method.rs

683 lines
22 KiB
Rust
Raw Normal View History

2017-05-16 05:24:06 +00:00
// Copyright (c) 2017-present PyO3 Project and Contributors
use syn;
2017-05-18 07:05:49 +00:00
use quote::{Tokens, ToTokens};
2017-05-20 06:14:59 +00:00
use method::{FnArg, FnSpec, FnType};
2017-05-18 07:05:49 +00:00
2017-06-13 04:08:59 +00:00
use utils;
2017-05-16 05:24:06 +00:00
pub fn gen_py_method<'a>(cls: &Box<syn::Ty>, name: &syn::Ident,
2017-05-20 06:14:59 +00:00
sig: &mut syn::MethodSig, meth_attrs: &mut Vec<syn::Attribute>) -> Tokens
2017-05-16 07:07:36 +00:00
{
2017-05-16 05:24:06 +00:00
check_generic(name, sig);
2017-06-18 15:00:27 +00:00
let doc = utils::get_doc(&meth_attrs, true);
2017-05-20 06:14:59 +00:00
let spec = FnSpec::parse(name, sig, meth_attrs);
2017-05-16 05:24:06 +00:00
2017-05-20 06:14:59 +00:00
match spec.tp {
2017-05-16 18:58:18 +00:00
FnType::Fn =>
2017-06-13 04:08:59 +00:00
impl_py_method_def(name, doc, &spec, &impl_wrap(cls, name, &spec, true)),
2017-05-20 06:14:59 +00:00
FnType::FnNew =>
2017-08-08 07:27:33 +00:00
impl_py_method_def_new(name, doc, &impl_wrap_new(cls, name, &spec)),
2017-07-27 23:14:54 +00:00
FnType::FnInit =>
impl_py_method_def_init(name, doc, &impl_wrap_init(cls, name, &spec)),
2017-05-20 06:14:59 +00:00
FnType::FnCall =>
2017-06-13 04:08:59 +00:00
impl_py_method_def_call(name, doc, &impl_wrap(cls, name, &spec, false)),
2017-06-08 18:29:40 +00:00
FnType::FnClass =>
2017-06-13 04:08:59 +00:00
impl_py_method_def_class(name, doc, &impl_wrap_class(cls, name, &spec)),
2017-06-08 18:29:40 +00:00
FnType::FnStatic =>
2017-06-13 04:08:59 +00:00
impl_py_method_def_static(name, doc, &impl_wrap_static(cls, name, &spec)),
2017-05-20 06:14:59 +00:00
FnType::Getter(ref getter) =>
2017-08-05 02:53:23 +00:00
impl_py_getter_def(name, doc, getter, &impl_wrap_getter(cls, name)),
2017-05-20 06:14:59 +00:00
FnType::Setter(ref setter) =>
2017-06-13 04:08:59 +00:00
impl_py_setter_def(name, doc, setter, &impl_wrap_setter(cls, name, &spec)),
2017-05-18 07:05:49 +00:00
}
}
2017-05-16 05:24:06 +00:00
2017-05-16 18:58:18 +00:00
fn check_generic(name: &syn::Ident, sig: &syn::MethodSig) {
2017-05-16 05:24:06 +00:00
if !sig.generics.ty_params.is_empty() {
panic!("python method can not be generic: {:?}", name);
}
}
2017-05-20 06:14:59 +00:00
/// Generate function wrapper (PyCFunction, PyCFunctionWithKeywords)
pub fn impl_wrap(cls: &Box<syn::Ty>, name: &syn::Ident, spec: &FnSpec, noargs: bool) -> Tokens {
2017-05-20 06:14:59 +00:00
let cb = impl_call(cls, name, &spec);
2017-05-26 23:51:33 +00:00
let output = &spec.output;
2017-05-20 06:14:59 +00:00
if spec.args.is_empty() && noargs {
quote! {
2017-07-23 17:07:23 +00:00
unsafe extern "C" fn __wrap(
_slf: *mut _pyo3::ffi::PyObject) -> *mut _pyo3::ffi::PyObject
{
2017-07-20 21:21:57 +00:00
const _LOCATION: &'static str = concat!(
stringify!(#cls), ".", stringify!(#name), "()");
2017-07-20 21:21:57 +00:00
let _pool = _pyo3::GILPool::new();
2017-07-23 17:07:23 +00:00
let _py = _pyo3::Python::assume_gil_acquired();
let _slf = _py.mut_from_borrowed_ptr::<#cls>(_slf);
2017-07-20 21:21:57 +00:00
2017-07-23 17:07:23 +00:00
let _result: #output = {
2017-07-20 21:21:57 +00:00
#cb
};
_pyo3::callback::cb_convert(
2017-07-23 17:07:23 +00:00
_pyo3::callback::PyObjectCallbackConverter, _py, _result)
}
}
} else {
let body = impl_arg_params(&spec, cb);
2017-05-20 06:14:59 +00:00
quote! {
2017-07-23 17:07:23 +00:00
unsafe extern "C" fn __wrap(
_slf: *mut _pyo3::ffi::PyObject,
_args: *mut _pyo3::ffi::PyObject,
_kwargs: *mut _pyo3::ffi::PyObject) -> *mut _pyo3::ffi::PyObject
{
2017-07-20 21:21:57 +00:00
use pyo3::ToPyPointer;
const _LOCATION: &'static str = concat!(
stringify!(#cls), ".", stringify!(#name), "()");
2017-07-20 21:21:57 +00:00
let _pool = _pyo3::GILPool::new();
2017-07-23 17:07:23 +00:00
let _py = _pyo3::Python::assume_gil_acquired();
let _slf = _py.mut_from_borrowed_ptr::<#cls>(_slf);
let _args = _py.from_borrowed_ptr::<_pyo3::PyTuple>(_args);
2017-07-23 17:07:23 +00:00
let _kwargs = _pyo3::argparse::get_kwargs(_py, _kwargs);
2017-07-20 21:21:57 +00:00
2017-07-23 17:07:23 +00:00
let _result: #output = {
2017-07-20 21:21:57 +00:00
#body
};
_pyo3::callback::cb_convert(
2017-07-23 17:07:23 +00:00
_pyo3::callback::PyObjectCallbackConverter, _py, _result)
}
2017-05-20 06:14:59 +00:00
}
2017-05-16 05:24:06 +00:00
}
}
2017-05-25 05:43:07 +00:00
/// Generate function wrapper for protocol method (PyCFunction, PyCFunctionWithKeywords)
pub fn impl_proto_wrap(cls: &Box<syn::Ty>, name: &syn::Ident, spec: &FnSpec) -> Tokens {
let cb = impl_call(cls, name, &spec);
let body = impl_arg_params(&spec, cb);
quote! {
#[allow(unused_mut)]
2017-07-23 17:07:23 +00:00
unsafe extern "C" fn __wrap(
_slf: *mut _pyo3::ffi::PyObject,
_args: *mut _pyo3::ffi::PyObject,
_kwargs: *mut _pyo3::ffi::PyObject) -> *mut _pyo3::ffi::PyObject
2017-05-25 05:43:07 +00:00
{
2017-07-20 21:21:57 +00:00
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
let _pool = _pyo3::GILPool::new();
2017-07-23 17:07:23 +00:00
let _py = _pyo3::Python::assume_gil_acquired();
let _slf = _py.mut_from_borrowed_ptr::<#cls>(_slf);
let _args = _py.from_borrowed_ptr::<_pyo3::PyTuple>(_args);
2017-07-23 17:07:23 +00:00
let _kwargs = _pyo3::argparse::get_kwargs(_py, _kwargs);
2017-07-20 21:21:57 +00:00
2017-07-23 17:07:23 +00:00
let _result = {
2017-07-20 21:21:57 +00:00
#body
};
_pyo3::callback::cb_convert(
2017-07-23 17:07:23 +00:00
_pyo3::callback::PyObjectCallbackConverter, _py, _result)
2017-05-25 05:43:07 +00:00
}
}
}
2017-05-20 06:14:59 +00:00
2017-06-08 18:29:40 +00:00
/// Generate class method wrapper (PyCFunction, PyCFunctionWithKeywords)
2017-08-08 07:27:33 +00:00
pub fn impl_wrap_new(cls: &Box<syn::Ty>, name: &syn::Ident, spec: &FnSpec) -> Tokens {
2017-07-23 17:07:23 +00:00
let names: Vec<syn::Ident> = spec.args.iter().enumerate().map(
|item| if item.1.py {syn::Ident::from("_py")} else {
syn::Ident::from(format!("arg{}", item.0))}).collect();
2017-06-08 18:29:40 +00:00
let cb = quote! {{
#cls::#name(&_obj, #(#names),*)
2017-06-08 18:29:40 +00:00
}};
2017-05-20 06:14:59 +00:00
let body = impl_arg_params(spec, cb);
let output = &spec.output;
2017-05-16 05:24:06 +00:00
quote! {
#[allow(unused_mut)]
2017-07-23 17:07:23 +00:00
unsafe extern "C" fn __wrap(
_cls: *mut _pyo3::ffi::PyTypeObject,
_args: *mut _pyo3::ffi::PyObject,
_kwargs: *mut _pyo3::ffi::PyObject) -> *mut _pyo3::ffi::PyObject
2017-05-16 05:24:06 +00:00
{
2017-08-08 07:58:12 +00:00
use std::ptr;
2017-08-08 07:27:33 +00:00
use pyo3::typeob::PyTypeInfo;
2017-07-20 21:21:57 +00:00
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
let _pool = _pyo3::GILPool::new();
2017-07-23 17:07:23 +00:00
let _py = _pyo3::Python::assume_gil_acquired();
2017-08-08 07:27:33 +00:00
match _pyo3::typeob::PyRawObject::new(_py, #cls::type_object(), _cls) {
Ok(_obj) => {
let _args = _py.from_borrowed_ptr::<_pyo3::PyTuple>(_args);
let _kwargs = _pyo3::argparse::get_kwargs(_py, _kwargs);
let _result: #output = {
#body
};
match _result {
Ok(_) => _obj.into_ptr(),
Err(e) => {
2017-08-08 07:58:12 +00:00
e.restore(_py);
ptr::null_mut()
2017-08-08 07:27:33 +00:00
}
}
}
Err(e) => {
2017-08-08 07:58:12 +00:00
e.restore(_py);
ptr::null_mut()
2017-08-08 07:27:33 +00:00
}
}
2017-05-16 05:24:06 +00:00
}
}
}
2017-07-27 23:14:54 +00:00
/// Generate function wrapper for ffi::initproc
fn impl_wrap_init(cls: &Box<syn::Ty>, name: &syn::Ident, spec: &FnSpec) -> Tokens {
let cb = impl_call(cls, name, &spec);
let body = impl_arg_params(&spec, cb);
quote! {
#[allow(unused_mut)]
unsafe extern "C" fn __wrap(
_slf: *mut _pyo3::ffi::PyObject,
_args: *mut _pyo3::ffi::PyObject,
_kwargs: *mut _pyo3::ffi::PyObject) -> _pyo3::c_int
{
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
let _pool = _pyo3::GILPool::new();
let _py = _pyo3::Python::assume_gil_acquired();
let _slf = _py.mut_from_borrowed_ptr::<#cls>(_slf);
let _args = _py.from_borrowed_ptr::<_pyo3::PyTuple>(_args);
2017-07-27 23:14:54 +00:00
let _kwargs = _pyo3::argparse::get_kwargs(_py, _kwargs);
let _result: PyResult<()> = {
#body
};
match _result {
Ok(_) => 0,
Err(e) => {
e.restore(_py);
-1
}
}
}
}
}
2017-06-08 18:29:40 +00:00
/// Generate class method wrapper (PyCFunction, PyCFunctionWithKeywords)
pub fn impl_wrap_class(cls: &Box<syn::Ty>, name: &syn::Ident, spec: &FnSpec) -> Tokens {
2017-07-23 17:07:23 +00:00
let names: Vec<syn::Ident> = spec.args.iter().enumerate().map(
|item| if item.1.py {syn::Ident::from("_py")} else {
syn::Ident::from(format!("arg{}", item.0))}).collect();
2017-06-08 18:29:40 +00:00
let cb = quote! {{
2017-07-23 17:07:23 +00:00
#cls::#name(&_cls, #(#names),*)
2017-06-08 18:29:40 +00:00
}};
let body = impl_arg_params(spec, cb);
let output = &spec.output;
quote! {
#[allow(unused_mut)]
2017-07-23 17:07:23 +00:00
unsafe extern "C" fn __wrap(
_cls: *mut _pyo3::ffi::PyObject,
_args: *mut _pyo3::ffi::PyObject,
_kwargs: *mut _pyo3::ffi::PyObject) -> *mut _pyo3::ffi::PyObject
2017-06-08 18:29:40 +00:00
{
2017-07-20 21:21:57 +00:00
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
let _pool = _pyo3::GILPool::new();
2017-07-23 17:07:23 +00:00
let _py = _pyo3::Python::assume_gil_acquired();
let _cls = _pyo3::PyType::from_type_ptr(_py, _cls as *mut _pyo3::ffi::PyTypeObject);
let _args = _py.from_borrowed_ptr::<_pyo3::PyTuple>(_args);
2017-07-23 17:07:23 +00:00
let _kwargs = _pyo3::argparse::get_kwargs(_py, _kwargs);
2017-07-20 21:21:57 +00:00
2017-07-23 17:07:23 +00:00
let _result: #output = {
2017-07-20 21:21:57 +00:00
#body
};
_pyo3::callback::cb_convert(
2017-07-23 17:07:23 +00:00
_pyo3::callback::PyObjectCallbackConverter, _py, _result)
2017-06-08 18:29:40 +00:00
}
}
}
/// Generate static method wrapper (PyCFunction, PyCFunctionWithKeywords)
pub fn impl_wrap_static(cls: &Box<syn::Ty>, name: &syn::Ident, spec: &FnSpec) -> Tokens {
2017-07-23 17:07:23 +00:00
let names: Vec<syn::Ident> = spec.args.iter().enumerate().map(
|item| if item.1.py {syn::Ident::from("_py")} else {
syn::Ident::from(format!("arg{}", item.0))}).collect();
2017-06-08 18:29:40 +00:00
let cb = quote! {{
#cls::#name(#(#names),*)
2017-06-08 18:29:40 +00:00
}};
let body = impl_arg_params(spec, cb);
let output = &spec.output;
quote! {
#[allow(unused_mut)]
2017-07-23 17:07:23 +00:00
unsafe extern "C" fn __wrap(
_slf: *mut _pyo3::ffi::PyObject,
_args: *mut _pyo3::ffi::PyObject,
_kwargs: *mut _pyo3::ffi::PyObject) -> *mut _pyo3::ffi::PyObject
2017-06-08 18:29:40 +00:00
{
2017-07-20 21:21:57 +00:00
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
let _pool = _pyo3::GILPool::new();
2017-07-23 17:07:23 +00:00
let _py = _pyo3::Python::assume_gil_acquired();
let _args = _py.from_borrowed_ptr::<_pyo3::PyTuple>(_args);
2017-07-23 17:07:23 +00:00
let _kwargs = _pyo3::argparse::get_kwargs(_py, _kwargs);
2017-06-08 18:29:40 +00:00
2017-07-23 17:07:23 +00:00
let _result: #output = {
2017-07-20 21:21:57 +00:00
#body
};
_pyo3::callback::cb_convert(
2017-07-23 17:07:23 +00:00
_pyo3::callback::PyObjectCallbackConverter, _py, _result)
2017-06-08 18:29:40 +00:00
}
}
}
2017-05-16 18:58:18 +00:00
/// Generate functiona wrapper (PyCFunction, PyCFunctionWithKeywords)
2017-08-05 02:53:23 +00:00
pub(crate) fn impl_wrap_getter(cls: &Box<syn::Ty>, name: &syn::Ident) -> Tokens {
2017-05-16 18:58:18 +00:00
quote! {
2017-07-23 17:07:23 +00:00
unsafe extern "C" fn __wrap(
_slf: *mut _pyo3::ffi::PyObject, _: *mut _pyo3::c_void) -> *mut _pyo3::ffi::PyObject
2017-05-16 18:58:18 +00:00
{
2017-07-20 21:21:57 +00:00
use std;
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
let _pool = _pyo3::GILPool::new();
2017-07-23 17:07:23 +00:00
let _py = _pyo3::Python::assume_gil_acquired();
let _slf = _py.mut_from_borrowed_ptr::<#cls>(_slf);
2017-07-20 21:21:57 +00:00
2017-07-23 17:07:23 +00:00
match _slf.#name() {
2017-07-20 21:21:57 +00:00
Ok(val) => {
2017-07-23 17:07:23 +00:00
val.into_object(_py).into_ptr()
2017-07-20 21:21:57 +00:00
}
Err(e) => {
2017-07-23 17:07:23 +00:00
e.restore(_py);
2017-07-20 21:21:57 +00:00
std::ptr::null_mut()
}
}
2017-05-16 18:58:18 +00:00
}
}
}
/// Generate functiona wrapper (PyCFunction, PyCFunctionWithKeywords)
2017-08-05 02:53:23 +00:00
pub(crate) fn impl_wrap_setter(cls: &Box<syn::Ty>, name: &syn::Ident, spec: &FnSpec) -> Tokens {
if spec.args.len() < 1 {
println!("Not enough arguments for setter {}::{}", quote!{#cls}, name);
}
2017-05-20 06:14:59 +00:00
let val_ty = spec.args[0].ty;
2017-05-16 18:58:18 +00:00
quote! {
#[allow(unused_mut)]
2017-07-23 17:07:23 +00:00
unsafe extern "C" fn __wrap(
_slf: *mut _pyo3::ffi::PyObject,
_value: *mut _pyo3::ffi::PyObject, _: *mut _pyo3::c_void) -> _pyo3::c_int
2017-05-16 18:58:18 +00:00
{
2017-07-20 21:21:57 +00:00
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()");
let _pool = _pyo3::GILPool::new();
2017-07-23 17:07:23 +00:00
let _py = _pyo3::Python::assume_gil_acquired();
let _slf = _py.mut_from_borrowed_ptr::<#cls>(_slf);
let _value = _py.from_borrowed_ptr(_value);
2017-07-20 21:21:57 +00:00
2017-07-23 17:07:23 +00:00
let _result = match <#val_ty as _pyo3::FromPyObject>::extract(_value) {
Ok(_val) => _slf.#name(_val),
2017-07-20 21:21:57 +00:00
Err(e) => Err(e)
};
2017-07-23 17:07:23 +00:00
match _result {
2017-07-20 21:21:57 +00:00
Ok(_) => 0,
Err(e) => {
2017-07-23 17:07:23 +00:00
e.restore(_py);
2017-07-20 21:21:57 +00:00
-1
2017-05-25 05:43:07 +00:00
}
2017-07-20 21:21:57 +00:00
}
2017-05-16 18:58:18 +00:00
}
}
}
fn impl_call(_cls: &Box<syn::Ty>, fname: &syn::Ident, spec: &FnSpec) -> Tokens {
2017-07-23 17:07:23 +00:00
let names: Vec<syn::Ident> = spec.args.iter().enumerate().map(
|item| if item.1.py {
syn::Ident::from("_py")
} else {
syn::Ident::from(format!("arg{}", item.0))
}
).collect();
2017-05-25 05:43:07 +00:00
quote! {{
2017-07-23 17:07:23 +00:00
_slf.#fname(#(#names),*)
2017-05-25 05:43:07 +00:00
}}
2017-05-16 05:24:06 +00:00
}
pub fn impl_arg_params(spec: &FnSpec, body: Tokens) -> Tokens {
let args: Vec<FnArg> = spec.args.iter()
.filter(|item| !item.py).map(|item| item.clone()).collect();
if args.is_empty() {
2017-05-25 05:43:07 +00:00
return body
}
2017-06-18 15:00:27 +00:00
2017-05-16 05:24:06 +00:00
let mut params = Vec::new();
2017-05-20 06:14:59 +00:00
for arg in spec.args.iter() {
if arg.py {
continue
}
2017-05-20 06:14:59 +00:00
if ! (spec.is_args(&arg.name) || spec.is_kwargs(&arg.name)) {
2017-05-18 07:05:49 +00:00
let name = arg.name.as_ref();
2017-06-14 05:37:26 +00:00
let kwonly = if spec.is_kw_only(&arg.name) {
syn::Ident::from("true")
} else {
syn::Ident::from("false")
};
2017-05-18 07:05:49 +00:00
let opt = if let Some(_) = arg.optional {
syn::Ident::from("true")
2017-06-14 05:37:26 +00:00
} else if let Some(_) = spec.default_value(&arg.name) {
syn::Ident::from("true")
2017-05-18 07:05:49 +00:00
} else {
2017-06-14 05:37:26 +00:00
syn::Ident::from("false")
2017-05-18 07:05:49 +00:00
};
2017-06-14 05:37:26 +00:00
2017-05-18 07:05:49 +00:00
params.push(
quote! {
2017-06-14 05:37:26 +00:00
_pyo3::argparse::ParamDescription{
name: #name, is_optional: #opt, kw_only: #kwonly}
2017-05-18 07:05:49 +00:00
}
);
}
2017-05-16 05:24:06 +00:00
}
let placeholders: Vec<syn::Ident> = params.iter().map(
|_| syn::Ident::from("None")).collect();
// generate extrat args
2017-07-23 17:07:23 +00:00
let len = spec.args.len();
2017-05-20 06:14:59 +00:00
let mut rargs = spec.args.clone();
rargs.reverse();
2017-05-16 05:24:06 +00:00
let mut body = body;
2017-07-23 17:07:23 +00:00
for (idx, arg) in rargs.iter().enumerate() {
body = impl_arg_param(&arg, &spec, &body, len-idx-1);
2017-05-16 05:24:06 +00:00
}
2017-05-18 07:05:49 +00:00
let accept_args = syn::Ident::from(
2017-05-20 06:14:59 +00:00
if spec.accept_args() { "true" } else { "false" });
2017-05-18 07:05:49 +00:00
let accept_kwargs = syn::Ident::from(
2017-05-20 06:14:59 +00:00
if spec.accept_kwargs() { "true" } else { "false" });
2017-05-18 07:05:49 +00:00
2017-05-16 05:24:06 +00:00
// create array of arguments, and then parse
quote! {
2017-07-23 17:07:23 +00:00
const _PARAMS: &'static [_pyo3::argparse::ParamDescription<'static>] = &[
2017-05-16 05:24:06 +00:00
#(#params),*
];
2017-07-23 17:07:23 +00:00
let mut _output = [#(#placeholders),*];
match _pyo3::argparse::parse_args(Some(_LOCATION), _PARAMS, &_args,
2017-07-23 17:07:23 +00:00
_kwargs, #accept_args, #accept_kwargs, &mut _output)
2017-06-07 02:26:59 +00:00
{
2017-05-16 05:24:06 +00:00
Ok(_) => {
2017-07-23 17:07:23 +00:00
let mut _iter = _output.iter();
2017-05-16 05:24:06 +00:00
#body
},
Err(err) => Err(err)
}
}
}
2017-07-23 17:07:23 +00:00
fn impl_arg_param(arg: &FnArg, spec: &FnSpec, body: &Tokens, idx: usize) -> Tokens {
if arg.py {
return body.clone()
}
2017-05-16 05:24:06 +00:00
let ty = arg.ty;
let name = arg.name;
2017-07-23 17:07:23 +00:00
let arg_name = syn::Ident::from(format!("arg{}", idx));
2017-05-16 05:24:06 +00:00
// First unwrap() asserts the iterated sequence is long enough (which should be guaranteed);
// second unwrap() asserts the parameter was not missing (which fn
// parse_args already checked for).
2017-05-20 06:14:59 +00:00
if spec.is_args(&name) {
2017-05-16 05:24:06 +00:00
quote! {
2017-07-23 17:07:23 +00:00
match <#ty as _pyo3::FromPyObject>::extract(_args.as_ref())
2017-05-16 05:24:06 +00:00
{
2017-07-23 17:07:23 +00:00
Ok(#arg_name) => {
2017-05-16 05:24:06 +00:00
#body
}
Err(e) => Err(e)
}
}
}
2017-05-20 06:14:59 +00:00
else if spec.is_kwargs(&name) {
2017-06-24 22:28:53 +00:00
quote! {{
2017-07-23 17:07:23 +00:00
let #arg_name = _kwargs;
2017-05-18 07:05:49 +00:00
#body
2017-06-24 22:28:53 +00:00
}}
2017-05-18 07:05:49 +00:00
}
else {
2017-05-25 05:43:07 +00:00
if let Some(_) = arg.optional {
2017-05-18 07:05:49 +00:00
// default value
let mut default = Tokens::new();
2017-05-20 06:14:59 +00:00
if let Some(d) = spec.default_value(name) {
2017-05-18 18:15:06 +00:00
let dt = quote!{ Some(#d) };
dt.to_tokens(&mut default);
2017-05-18 07:05:49 +00:00
} else {
syn::Ident::from("None").to_tokens(&mut default);
}
2017-06-14 05:37:26 +00:00
quote! {
match
2017-05-25 05:43:07 +00:00
match _iter.next().unwrap().as_ref() {
Some(_obj) => {
if _obj.is_none() {
Ok(#default)
2017-05-25 05:43:07 +00:00
} else {
match _obj.extract() {
Ok(_obj) => Ok(Some(_obj)),
2017-05-25 05:43:07 +00:00
Err(e) => Err(e)
}
2017-05-19 04:35:08 +00:00
}
2017-05-25 05:43:07 +00:00
},
None => Ok(#default) }
{
Ok(#arg_name) => #body,
Err(e) => Err(e)
2017-05-18 07:05:49 +00:00
}
}
2017-05-20 06:14:59 +00:00
} else if let Some(default) = spec.default_value(name) {
quote! {
match match _iter.next().unwrap().as_ref() {
Some(_obj) => {
if _obj.is_none() {
Ok(#default)
} else {
match _obj.extract() {
Ok(_obj) => Ok(_obj),
Err(e) => Err(e),
2017-05-19 04:35:08 +00:00
}
2017-05-18 07:05:49 +00:00
}
},
None => Ok(#default)
} {
Ok(#arg_name) => #body,
Err(e) => Err(e)
2017-05-18 07:05:49 +00:00
}
}
}
else {
quote! {
match _iter.next().unwrap().as_ref().unwrap().extract() {
Ok(#arg_name) => {
#body
2017-05-18 07:05:49 +00:00
}
Err(e) => Err(e)
2017-05-18 07:05:49 +00:00
}
}
}
}
}
2017-06-13 04:08:59 +00:00
pub fn impl_py_method_def(name: &syn::Ident, doc: syn::Lit, spec: &FnSpec, wrapper: &Tokens)
-> Tokens
{
if spec.args.is_empty() {
quote! {
_pyo3::class::PyMethodDefType::Method({
#wrapper
_pyo3::class::PyMethodDef {
ml_name: stringify!(#name),
2017-07-23 17:07:23 +00:00
ml_meth: _pyo3::class::PyMethodType::PyNoArgsFunction(__wrap),
ml_flags: _pyo3::ffi::METH_NOARGS,
2017-06-13 04:08:59 +00:00
ml_doc: #doc,
}
})
}
} else {
quote! {
_pyo3::class::PyMethodDefType::Method({
#wrapper
_pyo3::class::PyMethodDef {
ml_name: stringify!(#name),
2017-07-23 17:07:23 +00:00
ml_meth: _pyo3::class::PyMethodType::PyCFunctionWithKeywords(__wrap),
ml_flags: _pyo3::ffi::METH_VARARGS | _pyo3::ffi::METH_KEYWORDS,
2017-06-13 04:08:59 +00:00
ml_doc: #doc,
}
})
}
2017-05-18 07:05:49 +00:00
}
}
2017-06-13 04:08:59 +00:00
pub fn impl_py_method_def_new(name: &syn::Ident, doc: syn::Lit, wrapper: &Tokens) -> Tokens
{
2017-05-20 06:14:59 +00:00
quote! {
_pyo3::class::PyMethodDefType::New({
#wrapper
2017-05-18 07:05:49 +00:00
2017-05-20 06:14:59 +00:00
_pyo3::class::PyMethodDef {
ml_name: stringify!(#name),
2017-07-23 17:07:23 +00:00
ml_meth: _pyo3::class::PyMethodType::PyNewFunc(__wrap),
2017-05-20 06:14:59 +00:00
ml_flags: _pyo3::ffi::METH_VARARGS | _pyo3::ffi::METH_KEYWORDS,
2017-06-13 04:08:59 +00:00
ml_doc: #doc,
2017-05-20 06:14:59 +00:00
}
})
2017-05-18 07:05:49 +00:00
}
2017-05-16 05:24:06 +00:00
}
2017-07-27 23:14:54 +00:00
pub fn impl_py_method_def_init(name: &syn::Ident, doc: syn::Lit, wrapper: &Tokens) -> Tokens
{
quote! {
_pyo3::class::PyMethodDefType::Init({
#wrapper
_pyo3::class::PyMethodDef {
ml_name: stringify!(#name),
ml_meth: _pyo3::class::PyMethodType::PyInitFunc(__wrap),
ml_flags: _pyo3::ffi::METH_VARARGS | _pyo3::ffi::METH_KEYWORDS,
ml_doc: #doc,
}
})
}
}
2017-06-13 04:08:59 +00:00
pub fn impl_py_method_def_class(name: &syn::Ident, doc: syn::Lit, wrapper: &Tokens) -> Tokens
{
2017-06-08 18:29:40 +00:00
quote! {
_pyo3::class::PyMethodDefType::Class({
#wrapper
_pyo3::class::PyMethodDef {
ml_name: stringify!(#name),
2017-07-23 17:07:23 +00:00
ml_meth: _pyo3::class::PyMethodType::PyCFunctionWithKeywords(__wrap),
2017-06-08 18:29:40 +00:00
ml_flags: _pyo3::ffi::METH_VARARGS | _pyo3::ffi::METH_KEYWORDS |
_pyo3::ffi::METH_CLASS,
2017-06-13 04:08:59 +00:00
ml_doc: #doc,
2017-06-08 18:29:40 +00:00
}
})
}
}
2017-06-13 04:08:59 +00:00
pub fn impl_py_method_def_static(name: &syn::Ident, doc: syn::Lit, wrapper: &Tokens) -> Tokens
{
2017-06-08 18:29:40 +00:00
quote! {
_pyo3::class::PyMethodDefType::Static({
#wrapper
_pyo3::class::PyMethodDef {
ml_name: stringify!(#name),
2017-07-23 17:07:23 +00:00
ml_meth: _pyo3::class::PyMethodType::PyCFunctionWithKeywords(__wrap),
2017-06-08 18:29:40 +00:00
ml_flags: _pyo3::ffi::METH_VARARGS | _pyo3::ffi::METH_KEYWORDS | _pyo3::ffi::METH_STATIC,
2017-06-13 04:08:59 +00:00
ml_doc: #doc,
2017-06-08 18:29:40 +00:00
}
})
}
}
2017-06-13 04:08:59 +00:00
pub fn impl_py_method_def_call(name: &syn::Ident, doc: syn::Lit, wrapper: &Tokens) -> Tokens
{
2017-05-16 18:58:18 +00:00
quote! {
2017-05-20 06:14:59 +00:00
_pyo3::class::PyMethodDefType::Call({
2017-05-16 18:58:18 +00:00
#wrapper
2017-05-18 18:15:06 +00:00
_pyo3::class::PyMethodDef {
2017-05-16 18:58:18 +00:00
ml_name: stringify!(#name),
2017-07-23 17:07:23 +00:00
ml_meth: _pyo3::class::PyMethodType::PyCFunctionWithKeywords(__wrap),
2017-05-18 18:15:06 +00:00
ml_flags: _pyo3::ffi::METH_VARARGS | _pyo3::ffi::METH_KEYWORDS,
2017-06-13 04:08:59 +00:00
ml_doc: #doc,
2017-05-16 18:58:18 +00:00
}
})
}
}
2017-08-05 02:53:23 +00:00
pub(crate) fn impl_py_setter_def(name: &syn::Ident, doc: syn::Lit, setter: &Option<String>, wrapper: &Tokens)
-> Tokens
2017-06-13 04:08:59 +00:00
{
2017-05-20 06:14:59 +00:00
let n = if let &Some(ref name) = setter {
2017-05-16 18:58:18 +00:00
name.to_string()
} else {
let n = String::from(name.as_ref());
if n.starts_with("set_") {
n[4..].to_string()
} else {
n
}
};
quote! {
2017-05-18 18:15:06 +00:00
_pyo3::class::PyMethodDefType::Setter({
2017-05-16 18:58:18 +00:00
#wrapper
2017-05-18 18:15:06 +00:00
_pyo3::class::PySetterDef {
2017-05-16 18:58:18 +00:00
name: #n,
2017-07-23 17:07:23 +00:00
meth: __wrap,
2017-06-13 04:08:59 +00:00
doc: #doc,
2017-05-16 18:58:18 +00:00
}
})
}
}
2017-08-05 02:53:23 +00:00
pub(crate) fn impl_py_getter_def(name: &syn::Ident, doc: syn::Lit, getter: &Option<String>, wrapper: &Tokens)
-> Tokens
2017-06-13 04:08:59 +00:00
{
2017-05-20 06:14:59 +00:00
let n = if let &Some(ref name) = getter {
2017-05-16 18:58:18 +00:00
name.to_string()
} else {
let n = String::from(name.as_ref());
if n.starts_with("get_") {
n[4..].to_string()
} else {
n
2017-05-16 05:24:06 +00:00
}
2017-05-16 18:58:18 +00:00
};
quote! {
2017-05-18 18:15:06 +00:00
_pyo3::class::PyMethodDefType::Getter({
2017-05-16 18:58:18 +00:00
#wrapper
2017-05-18 18:15:06 +00:00
_pyo3::class::PyGetterDef {
2017-05-16 18:58:18 +00:00
name: #n,
2017-07-23 17:07:23 +00:00
meth: __wrap,
2017-06-13 04:08:59 +00:00
doc: #doc,
2017-05-16 18:58:18 +00:00
}
})
}
2017-05-16 05:24:06 +00:00
}