Skip to content

Commit d31d0c6

Browse files
authored
Merge pull request RustPython#1685 from youknowone/slot-name
pyslot guess slot name from function name
2 parents 3ade61a + 8c7e01b commit d31d0c6

32 files changed

Lines changed: 64 additions & 54 deletions

derive/src/pyclass.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,25 @@ impl Class {
207207
)?;
208208
attr_idxs.push(i);
209209
} else if name == "pyslot" {
210-
let pyslot_err = "#[pyslot] must be of the form #[pyslot(slotname)]";
210+
let pyslot_err = "#[pyslot] must be of the form #[pyslot] or #[pyslot(slotname)]";
211211
let nesteds =
212212
meta_to_vec(meta).map_err(|meta| err_span!(meta, "{}", pyslot_err))?;
213-
if nesteds.len() != 1 {
213+
if nesteds.len() > 1 {
214214
return Err(Diagnostic::spanned_error(&quote!(#(#nesteds)*), pyslot_err));
215215
}
216-
let slot_ident = match nesteds.into_iter().next().unwrap() {
217-
NestedMeta::Meta(Meta::Word(ident)) => ident,
218-
bad => bail_span!(bad, "{}", pyslot_err),
216+
let slot_ident = if nesteds.is_empty() {
217+
let ident_str = sig.ident.to_string();
218+
if ident_str.starts_with("tp_") {
219+
let slot_name = &ident_str[3..];
220+
proc_macro2::Ident::new(slot_name, sig.ident.span())
221+
} else {
222+
sig.ident.clone()
223+
}
224+
} else {
225+
match nesteds.into_iter().next().unwrap() {
226+
NestedMeta::Meta(Meta::Word(ident)) => ident,
227+
bad => bail_span!(bad, "{}", pyslot_err),
228+
}
219229
};
220230
self.add_item(
221231
ClassItem::Slot {

vm/src/exceptions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl PyBaseException {
5555
}
5656
}
5757

58-
#[pyslot(new)]
58+
#[pyslot]
5959
fn tp_new(cls: PyClassRef, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
6060
PyBaseException::new(args.args, vm).into_ref_with_type(vm, cls)
6161
}

vm/src/obj/objbytearray.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub(crate) fn init(context: &PyContext) {
8989

9090
#[pyimpl]
9191
impl PyByteArray {
92-
#[pyslot(new)]
92+
#[pyslot]
9393
fn tp_new(
9494
cls: PyClassRef,
9595
options: ByteInnerNewOptions,

vm/src/obj/objbytes.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

9292
#[pyimpl]
9393
impl PyBytes {
94-
#[pyslot(new)]
94+
#[pyslot]
9595
fn tp_new(
9696
cls: PyClassRef,
9797
options: ByteInnerNewOptions,

vm/src/obj/objclassmethod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl PyBuiltinDescriptor for PyClassMethod {
5555

5656
#[pyimpl]
5757
impl PyClassMethod {
58-
#[pyslot(new)]
58+
#[pyslot]
5959
fn tp_new(
6060
cls: PyClassRef,
6161
callable: PyObjectRef,

vm/src/obj/objcomplex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl PyComplex {
213213
!Complex64::is_zero(&self.value)
214214
}
215215

216-
#[pyslot(new)]
216+
#[pyslot]
217217
fn tp_new(
218218
cls: PyClassRef,
219219
real: OptionalArg<IntoPyFloat>,

vm/src/obj/objenumerate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl PyValue for PyEnumerate {
2727

2828
#[pyimpl]
2929
impl PyEnumerate {
30-
#[pyslot(new)]
30+
#[pyslot]
3131
fn tp_new(
3232
cls: PyClassRef,
3333
iterable: PyObjectRef,

vm/src/obj/objfilter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl PyValue for PyFilter {
2525

2626
#[pyimpl]
2727
impl PyFilter {
28-
#[pyslot(new)]
28+
#[pyslot]
2929
fn tp_new(
3030
cls: PyClassRef,
3131
function: PyObjectRef,

vm/src/obj/objfloat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ fn int_eq(value: f64, other: &BigInt) -> bool {
176176
#[pyimpl]
177177
#[allow(clippy::trivially_copy_pass_by_ref)]
178178
impl PyFloat {
179-
#[pyslot(new)]
179+
#[pyslot]
180180
fn tp_new(
181181
cls: PyClassRef,
182182
arg: OptionalArg<PyObjectRef>,

vm/src/obj/objframe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn init(context: &PyContext) {
1414

1515
#[pyimpl]
1616
impl FrameRef {
17-
#[pyslot(new)]
17+
#[pyslot]
1818
fn tp_new(_cls: FrameRef, vm: &VirtualMachine) -> PyResult<Self> {
1919
Err(vm.new_type_error("Cannot directly create frame object".to_string()))
2020
}

0 commit comments

Comments
 (0)