GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing rustpython.github.io — function.rs.html -- source 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 use std::collections::HashMap; use std::mem; use std::ops::RangeInclusive; use indexmap::IndexMap; use result_like::impl_option_like; use crate::exceptions::PyBaseExceptionRef; use crate::obj::objtuple::PyTuple; use crate::obj::objtype::{isinstance, PyClassRef}; use crate::pyobject::{ IntoPyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol, }; use crate::vm::VirtualMachine; use self::OptionalArg::*; /// The `PyFuncArgs` struct is one of the most used structs then creating /// a rust function that can be called from python. It holds both positional /// arguments, as well as keyword arguments passed to the function. #[derive(Debug, Default, Clone)] pub struct PyFuncArgs { pub args: Vec<PyObjectRef>, // sorted map, according to https://www.python.org/dev/peps/pep-0468/ pub kwargs: IndexMap<String, PyObjectRef>, } /// Conversion from vector of python objects to function arguments. impl From<Vec<PyObjectRef>> for PyFuncArgs { fn from(args: Vec<PyObjectRef>) -> Self { PyFuncArgs { args, kwargs: IndexMap::new(), } } } impl From<PyObjectRef> for PyFuncArgs { fn from(arg: PyObjectRef) -> Self { PyFuncArgs { args: vec![arg], kwargs: IndexMap::new(), } } } impl From<(&Args, &KwArgs)> for PyFuncArgs { fn from(arg: (&Args, &KwArgs)) -> Self { let Args(args) = arg.0; let KwArgs(kwargs) = arg.1; PyFuncArgs { args: args.clone(), kwargs: kwargs.iter().map(|(k, v)| (k.clone(), v.clone())).collect(), } } } impl FromArgs for PyFuncArgs { fn from_args(_vm: &VirtualMachine, args: &mut PyFuncArgs) -> Result<Self, ArgumentError> { Ok(mem::replace(args, Default::default())) } } impl PyFuncArgs { pub fn new(mut args: Vec<PyObjectRef>, kwarg_names: Vec<String>) -> PyFuncArgs { // last `kwarg_names.len()` elements of args in order of appearance in the call signature let kwarg_values = args.drain((args.len() - kwarg_names.len())..); let mut kwargs = IndexMap::new(); for (name, value) in kwarg_names.iter().zip(kwarg_values) { kwargs.insert(name.clone(), value); } PyFuncArgs { args, kwargs } } pub fn insert(&self, item: PyObjectRef) -> PyFuncArgs { let mut args = PyFuncArgs { args: self.args.clone(), kwargs: self.kwargs.clone(), }; args.args.insert(0, item); args } pub fn shift(&mut self) -> PyObjectRef { self.args.remove(0) } pub fn get_kwarg(&self, key: &str, default: PyObjectRef) -> PyObjectRef { self.kwargs .get(key) .cloned() .unwrap_or_else(|| default.clone()) } pub fn get_optional_kwarg(&self, key: &str) -> Option<PyObjectRef> { self.kwargs.get(key).cloned() } pub fn get_optional_kwarg_with_type( &self, key: &str, ty: PyClassRef, vm: &VirtualMachine, ) -> PyResult<Option<PyObjectRef>> { match self.get_optional_kwarg(key) { Some(kwarg) => { if isinstance(&kwarg, &ty) { Ok(Some(kwarg)) } else { let expected_ty_name = vm.to_pystr(&ty)?; let actual_ty_name = vm.to_pystr(&kwarg.class())?; Err(vm.new_type_error(format!( "argument of type {} is required for named parameter `{}` (got: {})", expected_ty_name, key, actual_ty_name ))) } } None => Ok(None), } } pub fn take_positional(&mut self) -> Option<PyObjectRef> { if self.args.is_empty() { None } else { Some(self.args.remove(0)) } } pub fn take_positional_keyword(&mut self, name: &str) -> Option<PyObjectRef> { self.take_positional().or_else(|| self.take_keyword(name)) } pub fn take_keyword(&mut self, name: &str) -> Option<PyObjectRef> { self.kwargs.swap_remove(name) } pub fn remaining_keywords<'a>( &'a mut self, ) -> impl Iterator<Item = (String, PyObjectRef)> + 'a { self.kwargs.drain(..) } /// Binds these arguments to their respective values. /// /// If there is an insufficient number of arguments, there are leftover /// arguments after performing the binding, or if an argument is not of /// the expected type, a TypeError is raised. /// /// If the given `FromArgs` includes any conversions, exceptions raised /// during the conversion will halt the binding and return the error. pub fn bind<T: FromArgs>(mut self, vm: &VirtualMachine) -> PyResult<T> { let given_args = self.args.len(); let bound = match T::from_args(vm, &mut self) { Ok(args) => args, Err(ArgumentError::TooFewArgs) => { return Err(vm.new_type_error(format!( "Expected at least {} arguments ({} given)", T::arity().start(), given_args, ))); } Err(ArgumentError::TooManyArgs) => { return Err(vm.new_type_error(format!( "Expected at most {} arguments ({} given)", T::arity().end(), given_args, ))); } Err(ArgumentError::InvalidKeywordArgument(name)) => { return Err(vm.new_type_error(format!("{} is an invalid keyword argument", name))); } Err(ArgumentError::RequiredKeywordArgument(name)) => { return Err(vm.new_type_error(format!("Required keyqord only argument {}", name))); } Err(ArgumentError::Exception(ex)) => { return Err(ex); } }; if !self.args.is_empty() { Err(vm.new_type_error(format!( "Expected at most {} arguments ({} given)", T::arity().end(), given_args, ))) } else if !self.kwargs.is_empty() { Err(vm.new_type_error(format!( "Unexpected keyword argument {}", self.kwargs.keys().next().unwrap() ))) } else { Ok(bound) } } } /// An error encountered while binding arguments to the parameters of a Python /// function call. pub enum ArgumentError { /// The call provided fewer positional arguments than the function requires. TooFewArgs, /// The call provided more positional arguments than the function accepts. TooManyArgs, /// The function doesn't accept a keyword argument with the given name. InvalidKeywordArgument(String), /// The function require a keyword argument with the given name, but one wasn't provided RequiredKeywordArgument(String), /// An exception was raised while binding arguments to the function /// parameters. Exception(PyBaseExceptionRef), } impl From<PyBaseExceptionRef> for ArgumentError { fn from(ex: PyBaseExceptionRef) -> Self { ArgumentError::Exception(ex) } } /// Implemented by any type that can be accepted as a parameter to a built-in /// function. /// pub trait FromArgs: Sized { /// The range of positional arguments permitted by the function signature. /// /// Returns an empty range if not applicable. fn arity() -> RangeInclusive<usize> { 0..=0 } /// Extracts this item from the next argument(s). fn from_args(vm: &VirtualMachine, args: &mut PyFuncArgs) -> Result<Self, ArgumentError>; } /// A map of keyword arguments to their values. /// /// A built-in function with a `KwArgs` parameter is analagous to a Python /// function with `**kwargs`. All remaining keyword arguments are extracted /// (and hence the function will permit an arbitrary number of them). /// /// `KwArgs` optionally accepts a generic type parameter to allow type checks /// or conversions of each argument. /// /// Note: /// /// KwArgs is only for functions that accept arbitrary keyword arguments. For /// functions that accept only *specific* named arguments, a rust struct with /// an appropriate FromArgs implementation must be created. pub struct KwArgs<T = PyObjectRef>(HashMap<String, T>); impl<T> KwArgs<T> { pub fn pop_kwarg(&mut self, name: &str) -> Option<T> { self.0.remove(name) } } impl<T> FromArgs for KwArgs<T> where T: TryFromObject, { fn from_args(vm: &VirtualMachine, args: &mut PyFuncArgs) -> Result<Self, ArgumentError> { let mut kwargs = HashMap::new(); for (name, value) in args.remaining_keywords() { kwargs.insert(name, T::try_from_object(vm, value)?); } Ok(KwArgs(kwargs)) } } impl<T> IntoIterator for KwArgs<T> { type Item = (String, T); type IntoIter = std::collections::hash_map::IntoIter<String, T>; fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } } /// A list of positional argument values. /// /// A built-in function with a `Args` parameter is analogous to a Python /// function with `*args`. All remaining positional arguments are extracted /// (and hence the function will permit an arbitrary number of them). /// /// `Args` optionally accepts a generic type parameter to allow type checks /// or conversions of each argument. #[derive(Clone)] pub struct Args<T = PyObjectRef>(Vec<T>); impl<T> Args<T> { pub fn into_vec(self) -> Vec<T> { self.0 } } impl<T: PyValue> Args<PyRef<T>> { pub fn into_tuple(self, vm: &VirtualMachine) -> PyObjectRef { vm.ctx .new_tuple(self.0.into_iter().map(PyRef::into_object).collect()) } } impl<T> FromArgs for Args<T> where T: TryFromObject, { fn from_args(vm: &VirtualMachine, args: &mut PyFuncArgs) -> Result<Self, ArgumentError> { let mut varargs = Vec::new(); while let Some(value) = args.take_positional() { varargs.push(T::try_from_object(vm, value)?); } Ok(Args(varargs)) } } impl<T> IntoIterator for Args<T> { type Item = T; type IntoIter = std::vec::IntoIter<T>; fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } } impl<T> FromArgs for T where T: TryFromObject, { fn arity() -> RangeInclusive<usize> { 1..=1 } fn from_args(vm: &VirtualMachine, args: &mut PyFuncArgs) -> Result<Self, ArgumentError> { if let Some(value) = args.take_positional() { Ok(T::try_from_object(vm, value)?) } else { Err(ArgumentError::TooFewArgs) } } } /// An argument that may or may not be provided by the caller. /// /// This style of argument is not possible in pure Python. #[derive(Debug, is_macro::Is)] pub enum OptionalArg<T = PyObjectRef> { Present(T), Missing, } impl_option_like!(OptionalArg, Present, Missing); pub type OptionalOption<T> = OptionalArg<Option<T>>; impl<T> OptionalOption<T> { #[inline] pub fn flat_option(self) -> Option<T> { match self { Present(Some(value)) => Some(value), _ => None, } } } impl<T> FromArgs for OptionalArg<T> where T: TryFromObject, { fn arity() -> RangeInclusive<usize> { 0..=1 } fn from_args(vm: &VirtualMachine, args: &mut PyFuncArgs) -> Result<Self, ArgumentError> { if let Some(value) = args.take_positional() { Ok(Present(T::try_from_object(vm, value)?)) } else { Ok(Missing) } } } // For functions that accept no arguments. Implemented explicitly instead of via // macro below to avoid unused warnings. impl FromArgs for () { fn from_args(_vm: &VirtualMachine, _args: &mut PyFuncArgs) -> Result<Self, ArgumentError> { Ok(()) } } // A tuple of types that each implement `FromArgs` represents a sequence of // arguments that can be bound and passed to a built-in function. // // Technically, a tuple can contain tuples, which can contain tuples, and so on, // so this actually represents a tree of values to be bound from arguments, but // in practice this is only used for the top-level parameters. macro_rules! tuple_from_py_func_args { ($($T:ident),+) => { impl<$($T),+> FromArgs for ($($T,)+) where $($T: FromArgs),+ { fn arity() -> RangeInclusive<usize> { let mut min = 0; let mut max = 0; $( let (start, end) = $T::arity().into_inner(); min += start; max += end; )+ min..=max } fn from_args(vm: &VirtualMachine, args: &mut PyFuncArgs) -> Result<Self, ArgumentError> { Ok(($($T::from_args(vm, args)?,)+)) } } }; } // Implement `FromArgs` for up to 5-tuples, allowing built-in functions to bind // up to 5 top-level parameters (note that `Args`, `KwArgs`, nested tuples, etc. // count as 1, so this should actually be more than enough). tuple_from_py_func_args!(A); tuple_from_py_func_args!(A, B); tuple_from_py_func_args!(A, B, C); tuple_from_py_func_args!(A, B, C, D); tuple_from_py_func_args!(A, B, C, D, E); tuple_from_py_func_args!(A, B, C, D, E, F); /// A built-in Python function. pub type PyNativeFunc = Box<dyn Fn(&VirtualMachine, PyFuncArgs) -> PyResult + 'static>; /// Implemented by types that are or can generate built-in functions. /// /// For example, any function that: /// /// - Accepts a sequence of types that implement `FromArgs`, followed by a /// `&VirtualMachine` /// - Returns some type that implements `IntoPyObject` /// /// will generate a `PyNativeFunc` that performs the appropriate type and arity /// checking, any requested conversions, and then if successful call the function /// with the bound values. /// /// A bare `PyNativeFunc` also implements this trait, allowing the above to be /// done manually, for rare situations that don't fit into this model. pub trait IntoPyNativeFunc<T, R, VM> { fn into_func(self) -> PyNativeFunc; } impl<F> IntoPyNativeFunc<PyFuncArgs, PyResult, VirtualMachine> for F where F: Fn(&VirtualMachine, PyFuncArgs) -> PyResult + 'static, { fn into_func(self) -> PyNativeFunc { Box::new(self) } } pub struct OwnedParam<T>(std::marker::PhantomData<T>); pub struct RefParam<T>(std::marker::PhantomData<T>); // This is the "magic" that allows rust functions of varying signatures to // generate native python functions. // // Note that this could be done without a macro - it is simply to avoid repetition. macro_rules! into_py_native_func_tuple { ($(($n:tt, $T:ident)),*) => { impl<F, $($T,)* R> IntoPyNativeFunc<($(OwnedParam<$T>,)*), R, VirtualMachine> for F where F: Fn($($T,)* &VirtualMachine) -> R + 'static, $($T: FromArgs,)* R: IntoPyObject, { fn into_func(self) -> PyNativeFunc { Box::new(move |vm, args| { let ($($n,)*) = args.bind::<($($T,)*)>(vm)?; (self)($($n,)* vm).into_pyobject(vm) }) } } impl<F, S, $($T,)* R> IntoPyNativeFunc<(RefParam<S>, $(OwnedParam<$T>,)*), R, VirtualMachine> for F where F: Fn(&S, $($T,)* &VirtualMachine) -> R + 'static, S: PyValue, $($T: FromArgs,)* R: IntoPyObject, { fn into_func(self) -> PyNativeFunc { Box::new(move |vm, args| { let (zelf, $($n,)*) = args.bind::<(PyRef<S>, $($T,)*)>(vm)?; (self)(&zelf, $($n,)* vm).into_pyobject(vm) }) } } impl<F, $($T,)* R> IntoPyNativeFunc<($(OwnedParam<$T>,)*), R, ()> for F where F: Fn($($T,)*) -> R + 'static, $($T: FromArgs,)* R: IntoPyObject, { fn into_func(self) -> PyNativeFunc { IntoPyNativeFunc::into_func(move |$($n,)* _vm: &VirtualMachine| (self)($($n,)*)) } } impl<F, S, $($T,)* R> IntoPyNativeFunc<(RefParam<S>, $(OwnedParam<$T>,)*), R, ()> for F where F: Fn(&S, $($T,)*) -> R + 'static, S: PyValue, $($T: FromArgs,)* R: IntoPyObject, { fn into_func(self) -> PyNativeFunc { IntoPyNativeFunc::into_func(move |zelf: &S, $($n,)* _vm: &VirtualMachine| (self)(zelf, $($n,)*)) } } }; } into_py_native_func_tuple!(); into_py_native_func_tuple!((a, A)); into_py_native_func_tuple!((a, A), (b, B)); into_py_native_func_tuple!((a, A), (b, B), (c, C)); into_py_native_func_tuple!((a, A), (b, B), (c, C), (d, D)); into_py_native_func_tuple!((a, A), (b, B), (c, C), (d, D), (e, E)); /// Tests that the predicate is True on a single value, or if the value is a tuple a tuple, then /// test that any of the values contained within the tuples satisfies the predicate. Type parameter /// T specifies the type that is expected, if the input value is not of that type or a tuple of /// values of that type, then a TypeError is raised. pub fn single_or_tuple_any<T: PyValue, F: Fn(PyRef<T>) -> PyResult<bool>>( obj: PyObjectRef, predicate: F, message: fn(&PyObjectRef) -> String, vm: &VirtualMachine, ) -> PyResult<bool> { // TODO: figure out some way to have recursive calls without... this use std::marker::PhantomData; struct Checker<'vm, T: PyValue, F: Fn(PyRef<T>) -> PyResult<bool>> { predicate: F, message: fn(&PyObjectRef) -> String, vm: &'vm VirtualMachine, t: PhantomData<T>, } impl<T: PyValue, F: Fn(PyRef<T>) -> PyResult<bool>> Checker<'_, T, F> { fn check(&self, obj: PyObjectRef) -> PyResult<bool> { match_class!(match obj { obj @ T => (self.predicate)(obj), tuple @ PyTuple => { for obj in tuple.as_slice().iter() { if self.check(obj.clone())? { return Ok(true); } } Ok(false) } obj => Err(self.vm.new_type_error((self.message)(&obj))), }) } } let checker = Checker { predicate, message, vm, t: PhantomData, }; checker.check(obj) } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |