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

Function PlaySound

crates/vm/src/stdlib/winsound.rs:83–165  ·  view source on GitHub ↗
(args: PlaySoundArgs, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

81
82 #[pyfunction]
83 fn PlaySound(args: PlaySoundArgs, vm: &VirtualMachine) -> PyResult<()> {
84 let sound = args.sound;
85 let flags = args.flags as u32;
86
87 if vm.is_none(&sound) {
88 let ok = unsafe { super::win32::PlaySoundW(core::ptr::null(), 0, flags) };
89 if ok == 0 {
90 return Err(vm.new_runtime_error("Failed to play sound"));
91 }
92 return Ok(());
93 }
94
95 if flags & SND_MEMORY != 0 {
96 if flags & SND_ASYNC != 0 {
97 return Err(vm.new_runtime_error("Cannot play asynchronously from memory"));
98 }
99 let buffer = PyBuffer::try_from_borrowed_object(vm, &sound)?;
100 let buf = buffer
101 .as_contiguous()
102 .ok_or_else(|| vm.new_type_error("a bytes-like object is required, not 'str'"))?;
103 let ok = unsafe { super::win32::PlaySoundW(buf.as_ptr() as *const u16, 0, flags) };
104 if ok == 0 {
105 return Err(vm.new_runtime_error("Failed to play sound"));
106 }
107 return Ok(());
108 }
109
110 if sound.downcastable::<PyBytes>() {
111 let type_name = sound.class().name().to_string();
112 return Err(vm.new_type_error(format!(
113 "'sound' must be str, os.PathLike, or None, not {type_name}"
114 )));
115 }
116
117 // os.fspath(sound)
118 let path = match sound.downcast_ref::<PyStr>() {
119 Some(s) => s.as_wtf8().to_owned(),
120 None => {
121 let fspath = vm.get_method_or_type_error(
122 sound.clone(),
123 identifier!(vm, __fspath__),
124 || {
125 let type_name = sound.class().name().to_string();
126 format!("'sound' must be str, os.PathLike, or None, not {type_name}")
127 },
128 )?;
129
130 if vm.is_none(&fspath) {
131 return Err(vm.new_type_error(format!(
132 "'sound' must be str, os.PathLike, or None, not {}",
133 sound.class().name()
134 )));
135 }
136 let result = fspath.call((), vm)?;
137
138 if result.downcastable::<PyBytes>() {
139 return Err(vm.new_type_error("'sound' must resolve to str, not bytes"));
140 }

Callers

nothing calls this directly

Calls 15

ok_or_elseMethod · 0.80
to_stringMethod · 0.80
downcast_refMethod · 0.80
to_wide_with_nulMethod · 0.80
ErrClass · 0.50
is_noneMethod · 0.45
as_contiguousMethod · 0.45
as_ptrMethod · 0.45
nameMethod · 0.45
classMethod · 0.45
to_ownedMethod · 0.45

Tested by

no test coverage detected