(
encoding: PyObjectRef,
stacklevel: OptionalArg<i32>,
vm: &VirtualMachine,
)
| 5226 | |
| 5227 | #[pyfunction] |
| 5228 | fn text_encoding( |
| 5229 | encoding: PyObjectRef, |
| 5230 | stacklevel: OptionalArg<i32>, |
| 5231 | vm: &VirtualMachine, |
| 5232 | ) -> PyResult<PyStrRef> { |
| 5233 | if vm.is_none(&encoding) { |
| 5234 | let encoding = if vm.state.config.settings.utf8_mode > 0 { |
| 5235 | "utf-8" |
| 5236 | } else { |
| 5237 | "locale" |
| 5238 | }; |
| 5239 | if vm.state.config.settings.warn_default_encoding { |
| 5240 | let mut stacklevel = stacklevel.unwrap_or(2); |
| 5241 | if stacklevel > 1 |
| 5242 | && let Some(frame) = vm.current_frame() |
| 5243 | && let Some(stdlib_dir) = vm.state.config.paths.stdlib_dir.as_deref() |
| 5244 | { |
| 5245 | let path = frame.code.source_path().as_str(); |
| 5246 | if !path.starts_with(stdlib_dir) { |
| 5247 | stacklevel = stacklevel.saturating_sub(1); |
| 5248 | } |
| 5249 | } |
| 5250 | let stacklevel = usize::try_from(stacklevel).unwrap_or(0); |
| 5251 | crate::stdlib::_warnings::warn( |
| 5252 | vm.ctx.exceptions.encoding_warning, |
| 5253 | "'encoding' argument not specified".to_owned(), |
| 5254 | stacklevel, |
| 5255 | vm, |
| 5256 | )?; |
| 5257 | } |
| 5258 | return Ok(vm.ctx.new_str(encoding)); |
| 5259 | } |
| 5260 | encoding.try_into_value(vm) |
| 5261 | } |
| 5262 | |
| 5263 | #[cfg(test)] |
| 5264 | mod tests { |
no test coverage detected