Skip to content

Commit 57805f7

Browse files
authored
Merge pull request RustPython#2372 from RustPython/coolreader18/functions-have-names
Show functions' names
2 parents b01cca9 + 13ea0ef commit 57805f7

File tree

30 files changed

+173
-160
lines changed

30 files changed

+173
-160
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,

derive/src/pyclass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ where
326326
quote! {
327327
class.set_str_attr(
328328
#py_name,
329-
ctx.new_function_named(Self::#ident, #py_name.to_owned())
329+
ctx.make_funcdef(#py_name, Self::#ident)
330330
#doc
331331
.#build_func(ctx),
332332
);

derive/src/pymodule.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl ModuleItem for FunctionItem {
263263
);
264264
let module = args.module_name();
265265
let new_func = quote_spanned!(ident.span()=>
266-
vm.ctx.new_function_named(#ident, #py_name.to_owned())
266+
vm.ctx.make_funcdef(#py_name, #ident)
267267
#doc
268268
.into_function()
269269
.with_module(vm.ctx.new_str(#module.to_owned()))

examples/mini_repl.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ fn run(vm: &vm::VirtualMachine) -> vm::pyobject::PyResult<()> {
4141
let scope: vm::scope::Scope = vm.new_scope_with_builtins();
4242

4343
// typing `quit()` is too long, let's make `on(False)` work instead.
44-
scope.globals.set_item("on", vm.ctx.new_function(on), vm)?;
44+
scope
45+
.globals
46+
.set_item("on", vm.ctx.new_function("on", on), vm)?;
4547

4648
// let's include a fibonacci function, but let's be lazy and write it in Python
4749
add_python_function!(

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/builtinfunc.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ use crate::vm::VirtualMachine;
1313

1414
pub struct PyNativeFuncDef {
1515
pub func: PyNativeFunc,
16-
pub name: Option<PyStrRef>,
16+
pub name: PyStrRef,
1717
pub doc: Option<PyStrRef>,
1818
}
1919

20-
impl From<PyNativeFunc> for PyNativeFuncDef {
21-
fn from(func: PyNativeFunc) -> Self {
20+
impl PyNativeFuncDef {
21+
pub fn new(func: PyNativeFunc, name: PyStrRef) -> Self {
2222
Self {
2323
func,
24-
name: None,
24+
name,
2525
doc: None,
2626
}
2727
}
@@ -70,19 +70,10 @@ impl PyValue for PyBuiltinFunction {
7070

7171
impl fmt::Debug for PyBuiltinFunction {
7272
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73-
let name = match &self.value.name {
74-
Some(s) => s.borrow_value(),
75-
None => "<unknown name>",
76-
};
77-
write!(f, "builtin function {}", name)
73+
write!(f, "builtin function {}", self.value.name.borrow_value())
7874
}
7975
}
8076

81-
impl From<PyNativeFunc> for PyBuiltinFunction {
82-
fn from(value: PyNativeFunc) -> Self {
83-
PyNativeFuncDef::from(value).into()
84-
}
85-
}
8677
impl From<PyNativeFuncDef> for PyBuiltinFunction {
8778
fn from(value: PyNativeFuncDef) -> Self {
8879
Self {
@@ -124,7 +115,7 @@ impl PyBuiltinFunction {
124115
vm.unwrap_or_none(self.module.clone())
125116
}
126117
#[pyproperty(magic)]
127-
fn name(&self) -> Option<PyStrRef> {
118+
fn name(&self) -> PyStrRef {
128119
self.value.name.clone()
129120
}
130121
#[pyproperty(magic)]
@@ -184,7 +175,7 @@ impl Callable for PyBuiltinMethod {
184175
#[pyimpl(with(SlotDescriptor, Callable))]
185176
impl PyBuiltinMethod {
186177
#[pyproperty(magic)]
187-
fn name(&self) -> Option<PyStrRef> {
178+
fn name(&self) -> PyStrRef {
188179
self.value.name.clone()
189180
}
190181
#[pyproperty(magic)]

vm/src/builtins/bytearray.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub(crate) fn init(context: &PyContext) {
9393
PyByteArray::extend_class(context, &context.types.bytearray_type);
9494
let bytearray_type = &context.types.bytearray_type;
9595
extend_class!(context, bytearray_type, {
96-
"maketrans" => context.new_method(PyBytesInner::maketrans),
96+
"maketrans" => context.new_method("maketrans", PyBytesInner::maketrans),
9797
});
9898

9999
PyByteArrayIterator::extend_class(context, &context.types.bytearray_iterator_type);

vm/src/builtins/bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub(crate) fn init(context: &PyContext) {
9191
PyBytes::extend_class(context, &context.types.bytes_type);
9292
let bytes_type = &context.types.bytes_type;
9393
extend_class!(context, bytes_type, {
94-
"maketrans" => context.new_method(PyBytesInner::maketrans),
94+
"maketrans" => context.new_method("maketrans", PyBytesInner::maketrans),
9595
});
9696
PyBytesIterator::extend_class(context, &context.types.bytes_iterator_type);
9797
}

0 commit comments

Comments
 (0)