Skip to content

Commit cdd3848

Browse files
committed
allow AsciiStr/AsciiString -> PyStr conversion
1 parent afe9b9c commit cdd3848

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

stdlib/src/ssl.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,19 +1113,13 @@ fn cert_to_py(vm: &VirtualMachine, cert: &X509Ref, binary: bool) -> PyResult {
11131113
.iter()
11141114
.filter_map(|gen_name| {
11151115
if let Some(email) = gen_name.email() {
1116-
Some(
1117-
vm.new_tuple((vm.ctx.new_ascii_literal(ascii!("email")), email))
1118-
.into(),
1119-
)
1116+
Some(vm.new_tuple((ascii!("email"), email)).into())
11201117
} else if let Some(dnsname) = gen_name.dnsname() {
1121-
Some(
1122-
vm.new_tuple((vm.ctx.new_ascii_literal(ascii!("DNS")), dnsname))
1123-
.into(),
1124-
)
1118+
Some(vm.new_tuple((ascii!("DNS"), dnsname)).into())
11251119
} else if let Some(ip) = gen_name.ipaddress() {
11261120
Some(
11271121
vm.new_tuple((
1128-
vm.ctx.new_ascii_literal(ascii!("IP Address")),
1122+
ascii!("IP Address"),
11291123
String::from_utf8_lossy(ip).into_owned(),
11301124
))
11311125
.into(),

vm/src/builtins/pystr.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::{
1818
IdProtocol, ItemProtocol, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef,
1919
PyRef, PyResult, PyValue, TryIntoRef, TypeProtocol, VirtualMachine,
2020
};
21+
use ascii::{AsciiStr, AsciiString};
2122
use bstr::ByteSlice;
2223
use itertools::Itertools;
2324
use num_traits::ToPrimitive;
@@ -117,15 +118,21 @@ where
117118
}
118119
}
119120

121+
impl From<AsciiString> for PyStr {
122+
fn from(s: AsciiString) -> Self {
123+
unsafe { Self::new_ascii_unchecked(s.into()) }
124+
}
125+
}
126+
120127
impl From<String> for PyStr {
121-
fn from(s: String) -> PyStr {
128+
fn from(s: String) -> Self {
122129
s.into_boxed_str().into()
123130
}
124131
}
125132

126133
impl From<Box<str>> for PyStr {
127134
#[inline]
128-
fn from(value: Box<str>) -> PyStr {
135+
fn from(value: Box<str>) -> Self {
129136
// doing the check is ~10x faster for ascii, and is actually only 2% slower worst case for
130137
// non-ascii; see https://github.com/RustPython/RustPython/pull/2586#issuecomment-844611532
131138
let is_ascii = value.is_ascii();
@@ -153,6 +160,13 @@ impl fmt::Display for PyStr {
153160
}
154161
}
155162

163+
impl TryIntoRef<PyStr> for AsciiString {
164+
#[inline]
165+
fn try_into_ref(self, vm: &VirtualMachine) -> PyResult<PyRef<PyStr>> {
166+
Ok(unsafe { PyStr::new_ascii_unchecked(self.into()) }.into_ref(vm))
167+
}
168+
}
169+
156170
impl TryIntoRef<PyStr> for String {
157171
#[inline]
158172
fn try_into_ref(self, vm: &VirtualMachine) -> PyResult<PyRef<PyStr>> {
@@ -1318,6 +1332,12 @@ impl IntoPyObject for &String {
13181332
}
13191333
}
13201334

1335+
impl IntoPyObject for &AsciiStr {
1336+
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
1337+
vm.ctx.new_ascii_literal(self)
1338+
}
1339+
}
1340+
13211341
type SplitArgs<'a> = anystr::SplitArgs<'a, PyStrRef>;
13221342

13231343
#[derive(FromArgs)]

vm/src/stdlib/sys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ fn sys_exc_info(vm: &VirtualMachine) -> (PyObjectRef, PyObjectRef, PyObjectRef)
242242

243243
fn sys_git_info(vm: &VirtualMachine) -> PyTupleRef {
244244
vm.new_tuple((
245-
vm.ctx.new_ascii_literal(ascii!("RustPython")),
245+
ascii!("RustPython"),
246246
version::get_git_identifier(),
247247
version::get_git_revision(),
248248
))

0 commit comments

Comments
 (0)