| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,6 +37,7 @@ struct ArgAttribute { | |||
| 37 | 37 | name: Option<String>, | |
| 38 | 38 | kind: ParameterKind, | |
| 39 | 39 | default: Option<DefaultValue>, | |
| 40 | + error_msg: Option<String>, | ||
| 40 | 41 | } | |
| 41 | 42 | ||
| 42 | 43 | impl ArgAttribute { | |
@@ -63,6 +64,7 @@ impl ArgAttribute { | |||
| 63 | 64 | name: None, | |
| 64 | 65 | kind, | |
| 65 | 66 | default: None, | |
| 67 | + error_msg: None, | ||
| 66 | 68 | }); | |
| 67 | 69 | return Ok(()); | |
| 68 | 70 | }; | |
@@ -94,6 +96,12 @@ impl ArgAttribute { | |||
| 94 | 96 | } | |
| 95 | 97 | let val = meta.value()?.parse::<syn::LitStr>()?; | |
| 96 | 98 | self.name = Some(val.value()) | |
| 99 | + } else if meta.path.is_ident("error_msg") { | ||
| 100 | + if self.error_msg.is_some() { | ||
| 101 | + return Err(meta.error("already have an error_msg")); | ||
| 102 | + } | ||
| 103 | + let val = meta.value()?.parse::<syn::LitStr>()?; | ||
| 104 | + self.error_msg = Some(val.value()) | ||
| 97 | 105 | } else { | |
| 98 | 106 | return Err(meta.error("Unrecognized pyarg attribute")); | |
| 99 | 107 | } | |
@@ -146,8 +154,15 @@ fn generate_field((i, field): (usize, &Field)) -> Result<TokenStream> { | |||
| 146 | 154 | .or(name_string) | |
| 147 | 155 | .ok_or_else(|| err_span!(field, "field in tuple struct must have name attribute"))?; | |
| 148 | 156 | ||
| 149 | - let middle = quote! { | ||
| 150 | - .map(|x| ::rustpython_vm::convert::TryFromObject::try_from_object(vm, x)).transpose()? | ||
| 157 | + let middle = if let Some(error_msg) = &attr.error_msg { | ||
| 158 | + quote! { | ||
| 159 | + .map(|x| ::rustpython_vm::convert::TryFromObject::try_from_object(vm, x) | ||
| 160 | + .map_err(|_| vm.new_type_error(#error_msg))).transpose()? | ||
| 161 | + } | ||
| 162 | + } else { | ||
| 163 | + quote! { | ||
| 164 | + .map(|x| ::rustpython_vm::convert::TryFromObject::try_from_object(vm, x)).transpose()? | ||
| 165 | + } | ||
| 151 | 166 | }; | |
| 152 | 167 | ||
| 153 | 168 | let ending = if let Some(default) = attr.default { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -855,13 +855,13 @@ pub struct PyFunctionNewArgs { | |||
| 855 | 855 | code: PyRef<PyCode>, | |
| 856 | 856 | #[pyarg(positional)] | |
| 857 | 857 | globals: PyDictRef, | |
| 858 | - #[pyarg(any, optional)] | ||
| 858 | + #[pyarg(any, optional, error_msg = "arg 3 (name) must be None or string")] | ||
| 859 | 859 | name: OptionalArg<PyStrRef>, | |
| 860 | - #[pyarg(any, optional)] | ||
| 860 | + #[pyarg(any, optional, error_msg = "arg 4 (defaults) must be None or tuple")] | ||
| 861 | 861 | argdefs: Option<PyTupleRef>, | |
| 862 | - #[pyarg(any, optional)] | ||
| 862 | + #[pyarg(any, optional, error_msg = "arg 5 (closure) must be None or tuple")] | ||
| 863 | 863 | closure: Option<PyTupleRef>, | |
| 864 | - #[pyarg(any, optional)] | ||
| 864 | + #[pyarg(any, optional, error_msg = "arg 6 (kwdefaults) must be None or dict")] | ||
| 865 | 865 | kwdefaults: Option<PyDictRef>, | |
| 866 | 866 | } | |
| 867 | 867 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,26 +59,16 @@ impl Constructor for PyInterpolation { | |||
| 59 | 59 | type Args = InterpolationArgs; | |
| 60 | 60 | ||
| 61 | 61 | fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> { | |
| 62 | - let conversion = match args.conversion { | ||
| 63 | - OptionalArg::Present(c) => { | ||
| 64 | - if vm.is_none(&c) { | ||
| 65 | - vm.ctx.none() | ||
| 66 | - } else { | ||
| 67 | - let s = c.downcast::<PyStr>().map_err(|_| { | ||
| 68 | - vm.new_type_error( | ||
| 69 | - "Interpolation() argument 'conversion' must be str or None", | ||
| 70 | - ) | ||
| 71 | - })?; | ||
| 72 | - let s_str = s.as_str(); | ||
| 73 | - if s_str.len() != 1 || !matches!(s_str.chars().next(), Some('s' | 'r' | 'a')) { | ||
| 74 | - return Err(vm.new_value_error( | ||
| 75 | - "Interpolation() argument 'conversion' must be one of 's', 'a' or 'r'", | ||
| 76 | - )); | ||
| 77 | - } | ||
| 78 | - s.into() | ||
| 79 | - } | ||
| 62 | + let conversion: PyObjectRef = if let Some(s) = args.conversion { | ||
| 63 | + let s_str = s.as_str(); | ||
| 64 | + if s_str.len() != 1 || !matches!(s_str.chars().next(), Some('s' | 'r' | 'a')) { | ||
| 65 | + return Err(vm.new_value_error( | ||
| 66 | + "Interpolation() argument 'conversion' must be one of 's', 'a' or 'r'", | ||
| 67 | + )); | ||
| 80 | 68 | } | |
| 81 | - OptionalArg::Missing => vm.ctx.none(), | ||
| 69 | + s.into() | ||
| 70 | + } else { | ||
| 71 | + vm.ctx.none() | ||
| 82 | 72 | }; | |
| 83 | 73 | ||
| 84 | 74 | let expression = args | |
@@ -103,8 +93,12 @@ pub struct InterpolationArgs { | |||
| 103 | 93 | value: PyObjectRef, | |
| 104 | 94 | #[pyarg(any, optional)] | |
| 105 | 95 | expression: OptionalArg<PyStrRef>, | |
| 106 | - #[pyarg(any, optional)] | ||
| 107 | - conversion: OptionalArg<PyObjectRef>, | ||
| 96 | + #[pyarg( | ||
| 97 | + any, | ||
| 98 | + optional, | ||
| 99 | + error_msg = "Interpolation() argument 'conversion' must be str or None" | ||
| 100 | + )] | ||
| 101 | + conversion: Option<PyStrRef>, | ||
| 108 | 102 | #[pyarg(any, optional)] | |
| 109 | 103 | format_spec: OptionalArg<PyStrRef>, | |
| 110 | 104 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -60,8 +60,8 @@ impl Constructor for PySuper { | |||
| 60 | 60 | ||
| 61 | 61 | #[derive(FromArgs)] | |
| 62 | 62 | pub struct InitArgs { | |
| 63 | - #[pyarg(positional, optional)] | ||
| 64 | - py_type: OptionalArg<PyObjectRef>, | ||
| 63 | + #[pyarg(positional, optional, error_msg = "super() argument 1 must be a type")] | ||
| 64 | + py_type: OptionalArg<PyTypeRef>, | ||
| 65 | 65 | #[pyarg(positional, optional)] | |
| 66 | 66 | py_obj: OptionalArg<PyObjectRef>, | |
| 67 | 67 | } | |
@@ -75,10 +75,7 @@ impl Initializer for PySuper { | |||
| 75 | 75 | vm: &VirtualMachine, | |
| 76 | 76 | ) -> PyResult<()> { | |
| 77 | 77 | // Get the type: | |
| 78 | - let (typ, obj) = if let OptionalArg::Present(ty_obj) = py_type { | ||
| 79 | - let ty = ty_obj | ||
| 80 | - .downcast::<PyType>() | ||
| 81 | - .map_err(|_| vm.new_type_error("super() argument 1 must be a type"))?; | ||
| 78 | + let (typ, obj) = if let OptionalArg::Present(ty) = py_type { | ||
| 82 | 79 | (ty, py_obj.unwrap_or_none(vm)) | |
| 83 | 80 | } else { | |
| 84 | 81 | let frame = vm | |
| Back | FazBrowse Home | New Git URL |
0 commit comments