From 5a56dd1a77e8a4488be99ab8cee444fe63e314e4 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Thu, 17 Dec 2020 12:42:44 -0600 Subject: [PATCH] repr() functions with their names --- bytecode/src/bytecode.rs | 31 +++++++++++++++++++++---------- common/src/float_ops.rs | 2 +- jit/src/lib.rs | 2 +- parser/src/token.rs | 4 +--- vm/src/builtins/code.rs | 6 +++--- vm/src/builtins/function.rs | 18 ++++++++++++++++++ vm/src/builtins/make_module.rs | 10 ++-------- vm/src/builtins/memory.rs | 4 ++-- vm/src/builtins/object.rs | 2 +- vm/src/builtins/traceback.rs | 6 +++--- vm/src/coroutine.rs | 4 ++-- vm/src/exceptions.rs | 4 ++-- vm/src/import.rs | 6 +++--- vm/src/stdlib/imp.rs | 7 +++---- 14 files changed, 63 insertions(+), 43 deletions(-) diff --git a/bytecode/src/bytecode.rs b/bytecode/src/bytecode.rs index 8051fa73d..845067d58 100644 --- a/bytecode/src/bytecode.rs +++ b/bytecode/src/bytecode.rs @@ -40,6 +40,12 @@ pub trait Constant: Sized { fn map_constant(self, bag: &Bag) -> Bag::Constant { bag.make_constant(self.into_data()) } + fn map_name( + name: Self::Name, + bag: &Bag, + ) -> ::Name { + bag.make_name_ref(name.as_ref()) + } } impl Constant for ConstantData { type Name = String; @@ -63,6 +69,9 @@ impl Constant for ConstantData { fn into_data(self) -> ConstantData { self } + fn map_name(name: String, bag: &Bag) -> ::Name { + bag.make_name(name) + } } pub trait ConstantBag: Sized { @@ -99,9 +108,9 @@ pub struct CodeObject { pub posonlyarg_count: usize, // Number of positional-only arguments pub arg_count: usize, pub kwonlyarg_count: usize, - pub source_path: String, + pub source_path: C::Name, pub first_line_number: usize, - pub obj_name: String, // Name of the object that created this code object + pub obj_name: C::Name, // Name of the object that created this code object pub cell2arg: Option>, pub constants: Box<[C]>, #[serde(bound( @@ -549,9 +558,9 @@ impl CodeObject { posonlyarg_count: usize, arg_count: usize, kwonlyarg_count: usize, - source_path: String, + source_path: C::Name, first_line_number: usize, - obj_name: String, + obj_name: C::Name, ) -> Self { CodeObject { instructions: Box::new([]), @@ -660,7 +669,7 @@ impl CodeObject { names .into_vec() .into_iter() - .map(|x| bag.make_name_ref(x.as_ref())) + .map(|x| C::map_name(x, bag)) .collect::>() }; CodeObject { @@ -674,6 +683,8 @@ impl CodeObject { varnames: map_names(self.varnames), cellvars: map_names(self.cellvars), freevars: map_names(self.freevars), + source_path: C::map_name(self.source_path, bag), + obj_name: C::map_name(self.obj_name, bag), instructions: self.instructions, locations: self.locations, @@ -681,9 +692,7 @@ impl CodeObject { posonlyarg_count: self.posonlyarg_count, arg_count: self.arg_count, kwonlyarg_count: self.kwonlyarg_count, - source_path: self.source_path, first_line_number: self.first_line_number, - obj_name: self.obj_name, cell2arg: self.cell2arg, } } @@ -705,6 +714,8 @@ impl CodeObject { varnames: map_names(&self.varnames), cellvars: map_names(&self.cellvars), freevars: map_names(&self.freevars), + source_path: bag.make_name_ref(self.source_path.as_ref()), + obj_name: bag.make_name_ref(self.obj_name.as_ref()), instructions: self.instructions.clone(), locations: self.locations.clone(), @@ -712,9 +723,7 @@ impl CodeObject { posonlyarg_count: self.posonlyarg_count, arg_count: self.arg_count, kwonlyarg_count: self.kwonlyarg_count, - source_path: self.source_path.clone(), first_line_number: self.first_line_number, - obj_name: self.obj_name.clone(), cell2arg: self.cell2arg.clone(), } } @@ -955,7 +964,9 @@ impl fmt::Debug for CodeObject { write!( f, "", - self.obj_name, self.source_path, self.first_line_number + self.obj_name.as_ref(), + self.source_path.as_ref(), + self.first_line_number ) } } diff --git a/common/src/float_ops.rs b/common/src/float_ops.rs index 582150873..f93bc59e4 100644 --- a/common/src/float_ops.rs +++ b/common/src/float_ops.rs @@ -252,7 +252,7 @@ pub fn to_hex(value: f64) -> String { const BITS: i16 = 52; const FRACT_MASK: u64 = 0xf_ffff_ffff_ffff; format!( - "{}0x{:x}.{:013x}p{:+}", + "{}{:#x}.{:013x}p{:+}", sign_fmt, mantissa >> BITS, mantissa & FRACT_MASK, diff --git a/jit/src/lib.rs b/jit/src/lib.rs index dbb93936d..9d8db3428 100644 --- a/jit/src/lib.rs +++ b/jit/src/lib.rs @@ -77,7 +77,7 @@ impl Jit { builder.finalize(); let id = self.module.declare_function( - &format!("jit_{}", bytecode.obj_name), + &format!("jit_{}", bytecode.obj_name.as_ref()), Linkage::Export, &self.ctx.func.signature, )?; diff --git a/parser/src/token.rs b/parser/src/token.rs index bb11c6bac..316c34e48 100644 --- a/parser/src/token.rs +++ b/parser/src/token.rs @@ -124,13 +124,11 @@ impl fmt::Display for Tok { write!(f, "b\"")?; for i in value { match i { - 0..=8 => write!(f, "\\x0{}", i)?, 9 => f.write_str("\\t")?, 10 => f.write_str("\\n")?, - 11 => write!(f, "\\x0{:x}", i)?, 13 => f.write_str("\\r")?, 32..=126 => f.write_char(*i as char)?, - _ => write!(f, "\\x{:x}", i)?, + _ => write!(f, "\\x{:02x}", i)?, } } f.write_str("\"") diff --git a/vm/src/builtins/code.rs b/vm/src/builtins/code.rs index 7d4d10e48..130dd9b69 100644 --- a/vm/src/builtins/code.rs +++ b/vm/src/builtins/code.rs @@ -206,7 +206,7 @@ impl PyCodeRef { fn repr(self) -> String { let code = &self.code; format!( - "", + "", code.obj_name, self.get_id(), code.source_path, @@ -225,7 +225,7 @@ impl PyCodeRef { } #[pyproperty] - fn co_filename(self) -> String { + fn co_filename(self) -> PyStrRef { self.code.source_path.clone() } @@ -246,7 +246,7 @@ impl PyCodeRef { } #[pyproperty] - fn co_name(self) -> String { + fn co_name(self) -> PyStrRef { self.code.obj_name.clone() } diff --git a/vm/src/builtins/function.rs b/vm/src/builtins/function.rs index 8aa8cdcdc..5cdb4e656 100644 --- a/vm/src/builtins/function.rs +++ b/vm/src/builtins/function.rs @@ -40,6 +40,7 @@ pub struct PyFunction { closure: Option>, defaults: Option, kw_only_defaults: Option, + name: PyMutex, } impl PyFunction { @@ -50,6 +51,7 @@ impl PyFunction { defaults: Option, kw_only_defaults: Option, ) -> Self { + let name = PyMutex::new(code.obj_name.clone()); PyFunction { code, #[cfg(feature = "jit")] @@ -58,6 +60,7 @@ impl PyFunction { closure, defaults, kw_only_defaults, + name, } } @@ -321,6 +324,21 @@ impl PyFunction { self.globals.clone() } + #[pyproperty(magic)] + fn name(&self) -> PyStrRef { + self.name.lock().clone() + } + + #[pyproperty(magic, setter)] + fn set_name(&self, name: PyStrRef) { + *self.name.lock() = name; + } + + #[pymethod(magic)] + fn repr(zelf: PyRef) -> String { + format!("", zelf.name.lock(), zelf.get_id()) + } + #[cfg(feature = "jit")] #[pymethod(magic)] fn jit(zelf: PyRef, vm: &VirtualMachine) -> PyResult<()> { diff --git a/vm/src/builtins/make_module.rs b/vm/src/builtins/make_module.rs index 2e6647f8d..6c7f4a9a6 100644 --- a/vm/src/builtins/make_module.rs +++ b/vm/src/builtins/make_module.rs @@ -341,15 +341,9 @@ mod decl { // builtin_help #[pyfunction] - fn hex(number: PyIntRef, vm: &VirtualMachine) -> PyResult { + fn hex(number: PyIntRef) -> String { let n = number.borrow_value(); - let s = if n.is_negative() { - format!("-0x{:x}", -n) - } else { - format!("0x{:x}", n) - }; - - Ok(vm.ctx.new_str(s)) + format!("{:#x}", n) } #[pyfunction] diff --git a/vm/src/builtins/memory.rs b/vm/src/builtins/memory.rs index 35497bc8e..b0983c71d 100644 --- a/vm/src/builtins/memory.rs +++ b/vm/src/builtins/memory.rs @@ -673,9 +673,9 @@ impl PyMemoryView { #[pymethod(magic)] fn repr(zelf: PyRef) -> String { if zelf.released.load() { - format!("", zelf.get_id()) + format!("", zelf.get_id()) } else { - format!("", zelf.get_id()) + format!("", zelf.get_id()) } } diff --git a/vm/src/builtins/object.rs b/vm/src/builtins/object.rs index f5c886b88..51f410b80 100644 --- a/vm/src/builtins/object.rs +++ b/vm/src/builtins/object.rs @@ -168,7 +168,7 @@ impl PyBaseObject { #[pymethod(magic)] fn repr(zelf: PyObjectRef) -> String { - format!("<{} object at 0x{:x}>", zelf.class().name, zelf.get_id()) + format!("<{} object at {:#x}>", zelf.class().name, zelf.get_id()) } #[pyclassmethod(magic)] diff --git a/vm/src/builtins/traceback.rs b/vm/src/builtins/traceback.rs index 26f5455bc..10cb0aefb 100644 --- a/vm/src/builtins/traceback.rs +++ b/vm/src/builtins/traceback.rs @@ -1,6 +1,6 @@ use crate::builtins::pytype::PyTypeRef; use crate::frame::FrameRef; -use crate::pyobject::{PyClassImpl, PyContext, PyRef, PyValue}; +use crate::pyobject::{BorrowValue, PyClassImpl, PyContext, PyRef, PyValue}; use crate::vm::VirtualMachine; #[pyclass(module = false, name = "traceback")] @@ -67,9 +67,9 @@ impl serde::Serialize for PyTraceback { use serde::ser::SerializeStruct; let mut struc = s.serialize_struct("PyTraceback", 3)?; - struc.serialize_field("name", &self.frame.code.obj_name)?; + struc.serialize_field("name", self.frame.code.obj_name.borrow_value())?; struc.serialize_field("lineno", &self.lineno)?; - struc.serialize_field("filename", &self.frame.code.source_path)?; + struc.serialize_field("filename", self.frame.code.source_path.borrow_value())?; struc.end() } } diff --git a/vm/src/coroutine.rs b/vm/src/coroutine.rs index dc119f07c..e951d2846 100644 --- a/vm/src/coroutine.rs +++ b/vm/src/coroutine.rs @@ -1,4 +1,4 @@ -use crate::builtins::pytype::PyTypeRef; +use crate::builtins::{PyStrRef, PyTypeRef}; use crate::exceptions::{self, PyBaseExceptionRef}; use crate::frame::{ExecutionResult, FrameRef}; use crate::pyobject::{PyObjectRef, PyResult, TypeProtocol}; @@ -165,7 +165,7 @@ impl Coro { pub fn frame(&self) -> FrameRef { self.frame.clone() } - pub fn name(&self) -> String { + pub fn name(&self) -> PyStrRef { self.frame.code.obj_name.clone() } } diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 8ac1bbd71..9e4b87a8f 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -240,13 +240,13 @@ fn write_traceback_entry( output: &mut W, tb_entry: &PyTracebackRef, ) -> Result<(), W::Error> { - let filename = tb_entry.frame.code.source_path.to_owned(); + let filename = tb_entry.frame.code.source_path.borrow_value(); writeln!( output, r##" File "{}", line {}, in {}"##, filename, tb_entry.lineno, tb_entry.frame.code.obj_name )?; - print_source_line(output, &filename, tb_entry.lineno)?; + print_source_line(output, filename, tb_entry.lineno)?; Ok(()) } diff --git a/vm/src/import.rs b/vm/src/import.rs index b4d238ce2..c2a6d0d55 100644 --- a/vm/src/import.rs +++ b/vm/src/import.rs @@ -9,7 +9,7 @@ use crate::builtins::{code, list}; #[cfg(feature = "rustpython-compiler")] use crate::compile; use crate::exceptions::PyBaseExceptionRef; -use crate::pyobject::{ItemProtocol, PyResult, PyValue, TryFromObject, TypeProtocol}; +use crate::pyobject::{BorrowValue, ItemProtocol, PyResult, PyValue, TryFromObject, TypeProtocol}; use crate::scope::Scope; use crate::version::get_git_revision; use crate::vm::{InitParameter, VirtualMachine}; @@ -139,12 +139,12 @@ fn remove_importlib_frames_inner( return (None, false); }; - let file_name = &traceback.frame.code.source_path; + let file_name = traceback.frame.code.source_path.borrow_value(); let (inner_tb, mut now_in_importlib) = remove_importlib_frames_inner(vm, traceback.next.clone(), always_trim); if file_name == "_frozen_importlib" || file_name == "_frozen_importlib_external" { - if traceback.frame.code.obj_name == "_call_with_frames_removed" { + if traceback.frame.code.obj_name.borrow_value() == "_call_with_frames_removed" { now_in_importlib = true; } if always_trim || now_in_importlib { diff --git a/vm/src/stdlib/imp.rs b/vm/src/stdlib/imp.rs index af9378136..61fda699d 100644 --- a/vm/src/stdlib/imp.rs +++ b/vm/src/stdlib/imp.rs @@ -1,10 +1,9 @@ use crate::builtins::bytes::PyBytesRef; use crate::builtins::code::PyCode; use crate::builtins::module::PyModuleRef; -use crate::builtins::pystr; -use crate::builtins::pystr::PyStrRef; +use crate::builtins::pystr::{self, PyStr, PyStrRef}; use crate::import; -use crate::pyobject::{BorrowValue, ItemProtocol, PyObjectRef, PyResult}; +use crate::pyobject::{BorrowValue, ItemProtocol, PyObjectRef, PyResult, PyValue}; use crate::vm::VirtualMachine; #[cfg(feature = "threading")] @@ -82,7 +81,7 @@ fn _imp_get_frozen_object(name: PyStrRef, vm: &VirtualMachine) -> PyResult