Skip to content

Commit 5a56dd1

Browse files
committed
repr() functions with their names
1 parent a4aef93 commit 5a56dd1

File tree

14 files changed

+63
-43
lines changed

14 files changed

+63
-43
lines changed

bytecode/src/bytecode.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ pub trait Constant: Sized {
4040
fn map_constant<Bag: ConstantBag>(self, bag: &Bag) -> Bag::Constant {
4141
bag.make_constant(self.into_data())
4242
}
43+
fn map_name<Bag: ConstantBag>(
44+
name: Self::Name,
45+
bag: &Bag,
46+
) -> <Bag::Constant as Constant>::Name {
47+
bag.make_name_ref(name.as_ref())
48+
}
4349
}
4450
impl Constant for ConstantData {
4551
type Name = String;
@@ -63,6 +69,9 @@ impl Constant for ConstantData {
6369
fn into_data(self) -> ConstantData {
6470
self
6571
}
72+
fn map_name<Bag: ConstantBag>(name: String, bag: &Bag) -> <Bag::Constant as Constant>::Name {
73+
bag.make_name(name)
74+
}
6675
}
6776

6877
pub trait ConstantBag: Sized {
@@ -99,9 +108,9 @@ pub struct CodeObject<C: Constant = ConstantData> {
99108
pub posonlyarg_count: usize, // Number of positional-only arguments
100109
pub arg_count: usize,
101110
pub kwonlyarg_count: usize,
102-
pub source_path: String,
111+
pub source_path: C::Name,
103112
pub first_line_number: usize,
104-
pub obj_name: String, // Name of the object that created this code object
113+
pub obj_name: C::Name, // Name of the object that created this code object
105114
pub cell2arg: Option<Box<[isize]>>,
106115
pub constants: Box<[C]>,
107116
#[serde(bound(
@@ -549,9 +558,9 @@ impl<C: Constant> CodeObject<C> {
549558
posonlyarg_count: usize,
550559
arg_count: usize,
551560
kwonlyarg_count: usize,
552-
source_path: String,
561+
source_path: C::Name,
553562
first_line_number: usize,
554-
obj_name: String,
563+
obj_name: C::Name,
555564
) -> Self {
556565
CodeObject {
557566
instructions: Box::new([]),
@@ -660,7 +669,7 @@ impl<C: Constant> CodeObject<C> {
660669
names
661670
.into_vec()
662671
.into_iter()
663-
.map(|x| bag.make_name_ref(x.as_ref()))
672+
.map(|x| C::map_name(x, bag))
664673
.collect::<Box<[_]>>()
665674
};
666675
CodeObject {
@@ -674,16 +683,16 @@ impl<C: Constant> CodeObject<C> {
674683
varnames: map_names(self.varnames),
675684
cellvars: map_names(self.cellvars),
676685
freevars: map_names(self.freevars),
686+
source_path: C::map_name(self.source_path, bag),
687+
obj_name: C::map_name(self.obj_name, bag),
677688

678689
instructions: self.instructions,
679690
locations: self.locations,
680691
flags: self.flags,
681692
posonlyarg_count: self.posonlyarg_count,
682693
arg_count: self.arg_count,
683694
kwonlyarg_count: self.kwonlyarg_count,
684-
source_path: self.source_path,
685695
first_line_number: self.first_line_number,
686-
obj_name: self.obj_name,
687696
cell2arg: self.cell2arg,
688697
}
689698
}
@@ -705,16 +714,16 @@ impl<C: Constant> CodeObject<C> {
705714
varnames: map_names(&self.varnames),
706715
cellvars: map_names(&self.cellvars),
707716
freevars: map_names(&self.freevars),
717+
source_path: bag.make_name_ref(self.source_path.as_ref()),
718+
obj_name: bag.make_name_ref(self.obj_name.as_ref()),
708719

709720
instructions: self.instructions.clone(),
710721
locations: self.locations.clone(),
711722
flags: self.flags,
712723
posonlyarg_count: self.posonlyarg_count,
713724
arg_count: self.arg_count,
714725
kwonlyarg_count: self.kwonlyarg_count,
715-
source_path: self.source_path.clone(),
716726
first_line_number: self.first_line_number,
717-
obj_name: self.obj_name.clone(),
718727
cell2arg: self.cell2arg.clone(),
719728
}
720729
}
@@ -955,7 +964,9 @@ impl<C: Constant> fmt::Debug for CodeObject<C> {
955964
write!(
956965
f,
957966
"<code object {} at ??? file {:?}, line {}>",
958-
self.obj_name, self.source_path, self.first_line_number
967+
self.obj_name.as_ref(),
968+
self.source_path.as_ref(),
969+
self.first_line_number
959970
)
960971
}
961972
}

common/src/float_ops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ pub fn to_hex(value: f64) -> String {
252252
const BITS: i16 = 52;
253253
const FRACT_MASK: u64 = 0xf_ffff_ffff_ffff;
254254
format!(
255-
"{}0x{:x}.{:013x}p{:+}",
255+
"{}{:#x}.{:013x}p{:+}",
256256
sign_fmt,
257257
mantissa >> BITS,
258258
mantissa & FRACT_MASK,

jit/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl Jit {
7777
builder.finalize();
7878

7979
let id = self.module.declare_function(
80-
&format!("jit_{}", bytecode.obj_name),
80+
&format!("jit_{}", bytecode.obj_name.as_ref()),
8181
Linkage::Export,
8282
&self.ctx.func.signature,
8383
)?;

parser/src/token.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,11 @@ impl fmt::Display for Tok {
124124
write!(f, "b\"")?;
125125
for i in value {
126126
match i {
127-
0..=8 => write!(f, "\\x0{}", i)?,
128127
9 => f.write_str("\\t")?,
129128
10 => f.write_str("\\n")?,
130-
11 => write!(f, "\\x0{:x}", i)?,
131129
13 => f.write_str("\\r")?,
132130
32..=126 => f.write_char(*i as char)?,
133-
_ => write!(f, "\\x{:x}", i)?,
131+
_ => write!(f, "\\x{:02x}", i)?,
134132
}
135133
}
136134
f.write_str("\"")

vm/src/builtins/code.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl PyCodeRef {
206206
fn repr(self) -> String {
207207
let code = &self.code;
208208
format!(
209-
"<code object {} at 0x{:x} file {:?}, line {}>",
209+
"<code object {} at {:#x} file {:?}, line {}>",
210210
code.obj_name,
211211
self.get_id(),
212212
code.source_path,
@@ -225,7 +225,7 @@ impl PyCodeRef {
225225
}
226226

227227
#[pyproperty]
228-
fn co_filename(self) -> String {
228+
fn co_filename(self) -> PyStrRef {
229229
self.code.source_path.clone()
230230
}
231231

@@ -246,7 +246,7 @@ impl PyCodeRef {
246246
}
247247

248248
#[pyproperty]
249-
fn co_name(self) -> String {
249+
fn co_name(self) -> PyStrRef {
250250
self.code.obj_name.clone()
251251
}
252252

vm/src/builtins/function.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub struct PyFunction {
4040
closure: Option<PyTupleTyped<PyCellRef>>,
4141
defaults: Option<PyTupleRef>,
4242
kw_only_defaults: Option<PyDictRef>,
43+
name: PyMutex<PyStrRef>,
4344
}
4445

4546
impl PyFunction {
@@ -50,6 +51,7 @@ impl PyFunction {
5051
defaults: Option<PyTupleRef>,
5152
kw_only_defaults: Option<PyDictRef>,
5253
) -> Self {
54+
let name = PyMutex::new(code.obj_name.clone());
5355
PyFunction {
5456
code,
5557
#[cfg(feature = "jit")]
@@ -58,6 +60,7 @@ impl PyFunction {
5860
closure,
5961
defaults,
6062
kw_only_defaults,
63+
name,
6164
}
6265
}
6366

@@ -321,6 +324,21 @@ impl PyFunction {
321324
self.globals.clone()
322325
}
323326

327+
#[pyproperty(magic)]
328+
fn name(&self) -> PyStrRef {
329+
self.name.lock().clone()
330+
}
331+
332+
#[pyproperty(magic, setter)]
333+
fn set_name(&self, name: PyStrRef) {
334+
*self.name.lock() = name;
335+
}
336+
337+
#[pymethod(magic)]
338+
fn repr(zelf: PyRef<Self>) -> String {
339+
format!("<function {} at {:#x}>", zelf.name.lock(), zelf.get_id())
340+
}
341+
324342
#[cfg(feature = "jit")]
325343
#[pymethod(magic)]
326344
fn jit(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<()> {

vm/src/builtins/make_module.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,9 @@ mod decl {
341341
// builtin_help
342342

343343
#[pyfunction]
344-
fn hex(number: PyIntRef, vm: &VirtualMachine) -> PyResult {
344+
fn hex(number: PyIntRef) -> String {
345345
let n = number.borrow_value();
346-
let s = if n.is_negative() {
347-
format!("-0x{:x}", -n)
348-
} else {
349-
format!("0x{:x}", n)
350-
};
351-
352-
Ok(vm.ctx.new_str(s))
346+
format!("{:#x}", n)
353347
}
354348

355349
#[pyfunction]

vm/src/builtins/memory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,9 @@ impl PyMemoryView {
673673
#[pymethod(magic)]
674674
fn repr(zelf: PyRef<Self>) -> String {
675675
if zelf.released.load() {
676-
format!("<released memory at 0x{:x}>", zelf.get_id())
676+
format!("<released memory at {:#x}>", zelf.get_id())
677677
} else {
678-
format!("<memory at 0x{:x}>", zelf.get_id())
678+
format!("<memory at {:#x}>", zelf.get_id())
679679
}
680680
}
681681

vm/src/builtins/object.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl PyBaseObject {
168168

169169
#[pymethod(magic)]
170170
fn repr(zelf: PyObjectRef) -> String {
171-
format!("<{} object at 0x{:x}>", zelf.class().name, zelf.get_id())
171+
format!("<{} object at {:#x}>", zelf.class().name, zelf.get_id())
172172
}
173173

174174
#[pyclassmethod(magic)]

vm/src/builtins/traceback.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::builtins::pytype::PyTypeRef;
22
use crate::frame::FrameRef;
3-
use crate::pyobject::{PyClassImpl, PyContext, PyRef, PyValue};
3+
use crate::pyobject::{BorrowValue, PyClassImpl, PyContext, PyRef, PyValue};
44
use crate::vm::VirtualMachine;
55

66
#[pyclass(module = false, name = "traceback")]
@@ -67,9 +67,9 @@ impl serde::Serialize for PyTraceback {
6767
use serde::ser::SerializeStruct;
6868

6969
let mut struc = s.serialize_struct("PyTraceback", 3)?;
70-
struc.serialize_field("name", &self.frame.code.obj_name)?;
70+
struc.serialize_field("name", self.frame.code.obj_name.borrow_value())?;
7171
struc.serialize_field("lineno", &self.lineno)?;
72-
struc.serialize_field("filename", &self.frame.code.source_path)?;
72+
struc.serialize_field("filename", self.frame.code.source_path.borrow_value())?;
7373
struc.end()
7474
}
7575
}

0 commit comments

Comments
 (0)