MCPcopy Index your code
hub / github.com/RustPython/RustPython / ord

Function ord

crates/vm/src/stdlib/builtins.rs:931–952  ·  view source on GitHub ↗
(string: Either<ArgBytesLike, PyStrRef>, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

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 {

Callers 15

Proc0Function · 0.85
__calc_alt_digitsMethod · 0.85
escape_encodeFunction · 0.85
charmap_buildFunction · 0.85
unicode_internal_encodeFunction · 0.85
unicode_internal_decodeFunction · 0.85
PyUnicode_EncodeUTF32Function · 0.85
SPECIALFunction · 0.85
UB64Function · 0.85
_IS_BASE64Function · 0.85
_FROM_BASE64Function · 0.85
_DECODE_DIRECTFunction · 0.85

Calls 7

with_refMethod · 0.80
code_pointsMethod · 0.80
ErrClass · 0.50
lenMethod · 0.45
as_wtf8Method · 0.45
to_u32Method · 0.45
char_lenMethod · 0.45

Tested by 15

xmlcharnamereplaceMethod · 0.68
uninamereplaceMethod · 0.68
test_charmapencodeMethod · 0.68
handler1Method · 0.68
test_callbacksMethod · 0.68
test_bug828737Method · 0.68
test_string_literalsMethod · 0.68
test_objecttypesMethod · 0.68
test_raiseMemErrorMethod · 0.68
test_non_asciiMethod · 0.68