Parses the delimiter from a Python object and returns its ASCII value. This function attempts to extract the 'delimiter' attribute from the given Python object and ensures that the attribute is a single-character string. If successful, it returns the ASCII value of the character. If the attribute is not a single-character string, an error is returned. # Arguments `vm` - A reference to the Virtu
(vm: &VirtualMachine, obj: &PyObject)
| 133 | /// * If the 'delimiter' attribute is not a single-character string, a type error is returned. |
| 134 | /// * If the 'obj' is not of string type and does not have a 'delimiter' attribute, a type error is returned. |
| 135 | fn parse_delimiter_from_obj(vm: &VirtualMachine, obj: &PyObject) -> PyResult<u8> { |
| 136 | if let Ok(attr) = obj.get_attr("delimiter", vm) { |
| 137 | parse_delimiter_from_obj(vm, &attr) |
| 138 | } else { |
| 139 | match_class!(match obj.to_owned() { |
| 140 | s @ PyStr => { |
| 141 | Ok(s.as_bytes().iter().copied().exactly_one().map_err(|_| { |
| 142 | vm.new_type_error(format!( |
| 143 | r#""delimiter" must be a unicode character, not a string of length {}"#, |
| 144 | s.len() |
| 145 | )) |
| 146 | })?) |
| 147 | } |
| 148 | attr => { |
| 149 | let msg = format!( |
| 150 | r#""delimiter" must be a unicode character, not {}"#, |
| 151 | attr.class() |
| 152 | ); |
| 153 | Err(vm.new_type_error(msg)) |
| 154 | } |
| 155 | }) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | fn parse_quotechar_from_obj(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Option<u8>> { |
| 160 | match_class!(match obj.get_attr("quotechar", vm)? { |
no test coverage detected