mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
Add support for ascii formatting of f-strings.
This commit is contained in:
1 parent
2dab55864c
commit
291ffd2671
5 files changed
+30
-11
No files matched your search
@@ -39,9 +39,17 @@ v = Value()
|
||||
assert f'{v}' == 'foo'
|
||||
assert f'{v!r}' == 'bar'
|
||||
assert f'{v!s}' == 'baz'
|
||||
assert f'{v!a}' == 'bar'
|
||||
|
||||
# advanced expressions:
|
||||
|
||||
assert f'{True or True}' == 'True'
|
||||
assert f'{1 == 1}' == 'True'
|
||||
assert f'{"0" if True else "1"}' == '0'
|
||||
|
||||
# Test ascii representation of unicodes:
|
||||
v = "\u262e"
|
||||
assert f'>{v}' == '>\u262e'
|
||||
assert f'>{v!r}' == ">'\u262e'"
|
||||
assert f'>{v!s}' == '>\u262e'
|
||||
assert f'>{v!a}' == r">'\u262e'"
|
||||
+9
-2
@@ -60,8 +60,15 @@ fn builtin_any(iterable: PyIterable<bool>, vm: &VirtualMachine) -> PyResult<bool
|
||||
|
||||
fn builtin_ascii(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
|
||||
let repr = vm.to_repr(&obj)?;
|
||||
let ascii = to_ascii(&repr.value);
|
||||
Ok(ascii)
|
||||
}
|
||||
|
||||
/// Convert a string to ascii compatible, escaping unicodes into escape
|
||||
/// sequences.
|
||||
pub fn to_ascii(value: &str) -> String {
|
||||
let mut ascii = String::new();
|
||||
for c in repr.value.chars() {
|
||||
for c in value.chars() {
|
||||
if c.is_ascii() {
|
||||
ascii.push(c)
|
||||
} else {
|
||||
@@ -74,7 +81,7 @@ fn builtin_ascii(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
|
||||
ascii.push_str(&hex)
|
||||
}
|
||||
}
|
||||
Ok(ascii)
|
||||
ascii
|
||||
}
|
||||
|
||||
fn builtin_bin(x: PyIntRef, _vm: &VirtualMachine) -> String {
|
||||
|
||||
+4
-7
@@ -17,17 +17,14 @@ pub fn eval(vm: &VirtualMachine, source: &str, scope: Scope, source_path: &str)
|
||||
mod tests {
|
||||
use super::eval;
|
||||
use super::VirtualMachine;
|
||||
use crate::pyobject::IdProtocol;
|
||||
|
||||
#[test]
|
||||
fn test_print_42() {
|
||||
let source = String::from("print('Hello world')");
|
||||
let vm: VirtualMachine = Default::default();
|
||||
let vm = VirtualMachine::default();
|
||||
let vars = vm.new_scope_with_builtins();
|
||||
let _result = eval(&vm, &source, vars, "<unittest>");
|
||||
|
||||
// TODO: check result?
|
||||
//assert_eq!(
|
||||
// parse_ast,
|
||||
// );
|
||||
let result = eval(&vm, &source, vars, "<unittest>").expect("this should pass");
|
||||
assert!(result.is(&vm.ctx.none()));
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -666,7 +666,7 @@ impl Frame {
|
||||
let value = match conversion {
|
||||
Some(Str) => vm.to_str(&self.pop_value())?.into_object(),
|
||||
Some(Repr) => vm.to_repr(&self.pop_value())?.into_object(),
|
||||
Some(Ascii) => self.pop_value(), // TODO
|
||||
Some(Ascii) => vm.to_ascii(&self.pop_value())?,
|
||||
None => self.pop_value(),
|
||||
};
|
||||
|
||||
|
||||
+8
-1
@@ -11,7 +11,7 @@ use std::fmt;
|
||||
use std::rc::Rc;
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
|
||||
use crate::builtins;
|
||||
use crate::builtins::{self, to_ascii};
|
||||
use crate::bytecode;
|
||||
use crate::frame::{ExecutionResult, Frame, FrameRef};
|
||||
use crate::frozen;
|
||||
@@ -445,6 +445,13 @@ impl VirtualMachine {
|
||||
TryFromObject::try_from_object(self, repr)
|
||||
}
|
||||
|
||||
pub fn to_ascii(&self, obj: &PyObjectRef) -> PyResult {
|
||||
let repr = self.call_method(obj, "__repr__", vec![])?;
|
||||
let repr: PyStringRef = TryFromObject::try_from_object(self, repr)?;
|
||||
let ascii = to_ascii(&repr.value);
|
||||
Ok(self.new_str(ascii))
|
||||
}
|
||||
|
||||
pub fn import(&self, module: &str, from_list: &PyObjectRef, level: usize) -> PyResult {
|
||||
// if the import inputs seem weird, e.g a package import or something, rather than just
|
||||
// a straight `import ident`
|
||||
|
||||
Reference in new issue
Block a user