| 2236 | } |
| 2237 | |
| 2238 | pub fn fsencode<'a>(&self, s: &'a Py<PyStr>) -> PyResult<Cow<'a, OsStr>> { |
| 2239 | if cfg!(windows) || s.is_utf8() { |
| 2240 | // XXX: this is sketchy on windows; it's not guaranteed that the |
| 2241 | // OsStr encoding will always be compatible with WTF-8. |
| 2242 | let s = unsafe { OsStr::from_encoded_bytes_unchecked(s.as_bytes()) }; |
| 2243 | return Ok(Cow::Borrowed(s)); |
| 2244 | } |
| 2245 | let errors = self.fs_encode_errors().to_owned(); |
| 2246 | let bytes = self |
| 2247 | .state |
| 2248 | .codec_registry |
| 2249 | .encode_text(s.to_owned(), "utf-8", Some(errors), self)? |
| 2250 | .to_vec(); |
| 2251 | // XXX: this is sketchy on windows; it's not guaranteed that the |
| 2252 | // OsStr encoding will always be compatible with WTF-8. |
| 2253 | let s = unsafe { OsString::from_encoded_bytes_unchecked(bytes) }; |
| 2254 | Ok(Cow::Owned(s)) |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | impl AsRef<Context> for VirtualMachine { |