| 929 | |
| 930 | #[pyfunction] |
| 931 | fn ord(string: Either<ArgBytesLike, PyStrRef>, vm: &VirtualMachine) -> PyResult<u32> { |
| 932 | match string { |
| 933 | Either::A(bytes) => bytes.with_ref(|bytes| { |
| 934 | let bytes_len = bytes.len(); |
| 935 | if bytes_len != 1 { |
| 936 | return Err(vm.new_type_error(format!( |
| 937 | "ord() expected a character, but string of length {bytes_len} found" |
| 938 | ))); |
| 939 | } |
| 940 | Ok(u32::from(bytes[0])) |
| 941 | }), |
| 942 | Either::B(string) => match string.as_wtf8().code_points().exactly_one() { |
| 943 | Ok(character) => Ok(character.to_u32()), |
| 944 | Err(_) => { |
| 945 | let string_len = string.char_len(); |
| 946 | Err(vm.new_type_error(format!( |
| 947 | "ord() expected a character, but string of length {string_len} found" |
| 948 | ))) |
| 949 | } |
| 950 | }, |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | #[derive(FromArgs)] |
| 955 | struct PowArgs { |