Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions benches/microbenchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use criterion::{
criterion_group, criterion_main, BatchSize, BenchmarkGroup, BenchmarkId, Criterion, Throughput,
};
use rustpython_compiler::Mode;
use rustpython_vm::ItemProtocol;
use rustpython_vm::PyResult;
use rustpython_vm::{InitParameter, Interpreter, PySettings};
use rustpython_vm::{utils::ascii, InitParameter, Interpreter, ItemProtocol, PyResult, PySettings};
use std::path::{Path, PathBuf};
use std::{ffi, fs, io};

Expand Down Expand Up @@ -133,7 +131,7 @@ fn bench_rustpy_code(group: &mut BenchmarkGroup<WallTime>, bench: &MicroBenchmar
scope
.locals
.set_item(
vm.ctx.new_ascii_literal(crate::utils::ascii!("ITERATIONS")),
vm.ctx.new_ascii_literal(ascii!("ITERATIONS")),
vm.ctx.new_int(idx),
vm,
)
Expand Down
2 changes: 1 addition & 1 deletion vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub use rustpython_derive::*;

// This is above everything else so that the defined macros are available everywhere
#[macro_use]
pub mod macros;
pub(crate) mod macros;

mod anystr;
pub mod builtins;
Expand Down
10 changes: 10 additions & 0 deletions vm/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,13 @@ cfg_if::cfg_if! {
}
}
}

macro_rules! ascii {
($x:literal) => {{
const _: () = {
["not ascii"][!rustpython_vm::utils::bytes_is_ascii($x) as usize];
};
unsafe { ascii::AsciiStr::from_ascii_unchecked($x.as_bytes()) }
}};
}
pub use ascii;
20 changes: 5 additions & 15 deletions vm/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::builtins::{pystr::PyStr, PyFloat};
use crate::exceptions::IntoPyException;
use crate::{
builtins::{pystr::PyStr, PyFloat},
exceptions::IntoPyException,
IntoPyObject, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
VirtualMachine,
};
use num_traits::ToPrimitive;

pub use crate::macros::ascii;

pub enum Either<A, B> {
A(A),
B(B),
Expand Down Expand Up @@ -127,8 +129,7 @@ impl ToCString for PyStr {
}
}

#[allow(dead_code)]
pub(crate) const fn bytes_is_ascii(x: &str) -> bool {
pub const fn bytes_is_ascii(x: &str) -> bool {
let x = x.as_bytes();
let mut i = 0;
while i < x.len() {
Expand All @@ -139,14 +140,3 @@ pub(crate) const fn bytes_is_ascii(x: &str) -> bool {
}
true
}

macro_rules! ascii {
($x:literal) => {{
const _: () = {
["not ascii"][!crate::utils::bytes_is_ascii($x) as usize];
};
unsafe { ascii::AsciiStr::from_ascii_unchecked($x.as_bytes()) }
}};
}

pub(crate) use ascii;