repr() functions with their names

This commit is contained in:
Noah committed 2020-12-17 16:29:44 -06:00
1 parent a4aef93327
commit 5a56dd1a77
14 files changed
+63 -43

No files matched your search

+21 -10
View File
@@ -40,6 +40,12 @@ pub trait Constant: Sized {
fn map_constant<Bag: ConstantBag>(self, bag: &Bag) -> Bag::Constant {
bag.make_constant(self.into_data())
}
fn map_name<Bag: ConstantBag>(
name: Self::Name,
bag: &Bag,
) -> <Bag::Constant as Constant>::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<Bag: ConstantBag>(name: String, bag: &Bag) -> <Bag::Constant as Constant>::Name {
bag.make_name(name)
}
}
pub trait ConstantBag: Sized {
@@ -99,9 +108,9 @@ pub struct CodeObject<C: Constant = ConstantData> {
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<Box<[isize]>>,
pub constants: Box<[C]>,
#[serde(bound(
@@ -549,9 +558,9 @@ impl<C: Constant> CodeObject<C> {
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<C: Constant> CodeObject<C> {
names
.into_vec()
.into_iter()
.map(|x| bag.make_name_ref(x.as_ref()))
.map(|x| C::map_name(x, bag))
.collect::<Box<[_]>>()
};
CodeObject {
@@ -674,6 +683,8 @@ impl<C: Constant> CodeObject<C> {
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<C: Constant> CodeObject<C> {
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<C: Constant> CodeObject<C> {
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<C: Constant> CodeObject<C> {
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<C: Constant> fmt::Debug for CodeObject<C> {
write!(
f,
"<code object {} at ??? file {:?}, line {}>",
self.obj_name, self.source_path, self.first_line_number
self.obj_name.as_ref(),
self.source_path.as_ref(),
self.first_line_number
)
}
}
+1 -1
View File
@@ -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,
+1 -1
View File
@@ -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,
)?;
+1 -3
View File
@@ -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("\"")
+3 -3
View File
@@ -206,7 +206,7 @@ impl PyCodeRef {
fn repr(self) -> String {
let code = &self.code;
format!(
"<code object {} at 0x{:x} file {:?}, line {}>",
"<code object {} at {:#x} file {:?}, line {}>",
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()
}
+18
View File
@@ -40,6 +40,7 @@ pub struct PyFunction {
closure: Option<PyTupleTyped<PyCellRef>>,
defaults: Option<PyTupleRef>,
kw_only_defaults: Option<PyDictRef>,
name: PyMutex<PyStrRef>,
}
impl PyFunction {
@@ -50,6 +51,7 @@ impl PyFunction {
defaults: Option<PyTupleRef>,
kw_only_defaults: Option<PyDictRef>,
) -> 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<Self>) -> String {
format!("<function {} at {:#x}>", zelf.name.lock(), zelf.get_id())
}
#[cfg(feature = "jit")]
#[pymethod(magic)]
fn jit(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<()> {
+2 -8
View File
@@ -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]
+2 -2
View File
@@ -673,9 +673,9 @@ impl PyMemoryView {
#[pymethod(magic)]
fn repr(zelf: PyRef<Self>) -> String {
if zelf.released.load() {
format!("<released memory at 0x{:x}>", zelf.get_id())
format!("<released memory at {:#x}>", zelf.get_id())
} else {
format!("<memory at 0x{:x}>", zelf.get_id())
format!("<memory at {:#x}>", zelf.get_id())
}
}
+1 -1
View File
@@ -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)]
+3 -3
View File
@@ -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()
}
}
+2 -2
View File
@@ -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()
}
}
+2 -2
View File
@@ -240,13 +240,13 @@ fn write_traceback_entry<W: Write>(
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(())
}
+3 -3
View File
@@ -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 {
+3 -4
View File
@@ -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<PyCod
.get(name.borrow_value())
.map(|frozen| {
let mut frozen = frozen.code.clone();
frozen.source_path = format!("frozen {}", name);
frozen.source_path = PyStr::from(format!("frozen {}", name)).into_ref(vm);
PyCode::new(frozen)
})
.ok_or_else(|| vm.new_import_error(format!("No such frozen object named {}", name), name))