From cdd38488f8540b5df1f055ee260b88e538670e7a Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 11 Oct 2021 00:18:07 +0900 Subject: [PATCH] allow AsciiStr/AsciiString -> PyStr conversion --- stdlib/src/ssl.rs | 12 +++--------- vm/src/builtins/pystr.rs | 24 ++++++++++++++++++++++-- vm/src/stdlib/sys.rs | 2 +- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/stdlib/src/ssl.rs b/stdlib/src/ssl.rs index 5490df9f0d..b519cf41e0 100644 --- a/stdlib/src/ssl.rs +++ b/stdlib/src/ssl.rs @@ -1113,19 +1113,13 @@ fn cert_to_py(vm: &VirtualMachine, cert: &X509Ref, binary: bool) -> PyResult { .iter() .filter_map(|gen_name| { if let Some(email) = gen_name.email() { - Some( - vm.new_tuple((vm.ctx.new_ascii_literal(ascii!("email")), email)) - .into(), - ) + Some(vm.new_tuple((ascii!("email"), email)).into()) } else if let Some(dnsname) = gen_name.dnsname() { - Some( - vm.new_tuple((vm.ctx.new_ascii_literal(ascii!("DNS")), dnsname)) - .into(), - ) + Some(vm.new_tuple((ascii!("DNS"), dnsname)).into()) } else if let Some(ip) = gen_name.ipaddress() { Some( vm.new_tuple(( - vm.ctx.new_ascii_literal(ascii!("IP Address")), + ascii!("IP Address"), String::from_utf8_lossy(ip).into_owned(), )) .into(), diff --git a/vm/src/builtins/pystr.rs b/vm/src/builtins/pystr.rs index 61948dcfa1..86531465cc 100644 --- a/vm/src/builtins/pystr.rs +++ b/vm/src/builtins/pystr.rs @@ -18,6 +18,7 @@ use crate::{ IdProtocol, ItemProtocol, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryIntoRef, TypeProtocol, VirtualMachine, }; +use ascii::{AsciiStr, AsciiString}; use bstr::ByteSlice; use itertools::Itertools; use num_traits::ToPrimitive; @@ -117,15 +118,21 @@ where } } +impl From for PyStr { + fn from(s: AsciiString) -> Self { + unsafe { Self::new_ascii_unchecked(s.into()) } + } +} + impl From for PyStr { - fn from(s: String) -> PyStr { + fn from(s: String) -> Self { s.into_boxed_str().into() } } impl From> for PyStr { #[inline] - fn from(value: Box) -> PyStr { + fn from(value: Box) -> Self { // doing the check is ~10x faster for ascii, and is actually only 2% slower worst case for // non-ascii; see https://github.com/RustPython/RustPython/pull/2586#issuecomment-844611532 let is_ascii = value.is_ascii(); @@ -153,6 +160,13 @@ impl fmt::Display for PyStr { } } +impl TryIntoRef for AsciiString { + #[inline] + fn try_into_ref(self, vm: &VirtualMachine) -> PyResult> { + Ok(unsafe { PyStr::new_ascii_unchecked(self.into()) }.into_ref(vm)) + } +} + impl TryIntoRef for String { #[inline] fn try_into_ref(self, vm: &VirtualMachine) -> PyResult> { @@ -1318,6 +1332,12 @@ impl IntoPyObject for &String { } } +impl IntoPyObject for &AsciiStr { + fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { + vm.ctx.new_ascii_literal(self) + } +} + type SplitArgs<'a> = anystr::SplitArgs<'a, PyStrRef>; #[derive(FromArgs)] diff --git a/vm/src/stdlib/sys.rs b/vm/src/stdlib/sys.rs index 199250312e..eccce6b057 100644 --- a/vm/src/stdlib/sys.rs +++ b/vm/src/stdlib/sys.rs @@ -242,7 +242,7 @@ fn sys_exc_info(vm: &VirtualMachine) -> (PyObjectRef, PyObjectRef, PyObjectRef) fn sys_git_info(vm: &VirtualMachine) -> PyTupleRef { vm.new_tuple(( - vm.ctx.new_ascii_literal(ascii!("RustPython")), + ascii!("RustPython"), version::get_git_identifier(), version::get_git_revision(), ))