Merge pull request #1685 from youknowone/slot-name

pyslot guess slot name from function name
This commit is contained in:
Jeong YunWon
2020-01-15 11:51:16 +09:00
committed by GitHub
32 changed files with 64 additions and 54 deletions

View File

@@ -207,15 +207,25 @@ impl Class {
)?;
attr_idxs.push(i);
} else if name == "pyslot" {
let pyslot_err = "#[pyslot] must be of the form #[pyslot(slotname)]";
let pyslot_err = "#[pyslot] must be of the form #[pyslot] or #[pyslot(slotname)]";
let nesteds =
meta_to_vec(meta).map_err(|meta| err_span!(meta, "{}", pyslot_err))?;
if nesteds.len() != 1 {
if nesteds.len() > 1 {
return Err(Diagnostic::spanned_error(&quote!(#(#nesteds)*), pyslot_err));
}
let slot_ident = match nesteds.into_iter().next().unwrap() {
NestedMeta::Meta(Meta::Word(ident)) => ident,
bad => bail_span!(bad, "{}", pyslot_err),
let slot_ident = if nesteds.is_empty() {
let ident_str = sig.ident.to_string();
if ident_str.starts_with("tp_") {
let slot_name = &ident_str[3..];
proc_macro2::Ident::new(slot_name, sig.ident.span())
} else {
sig.ident.clone()
}
} else {
match nesteds.into_iter().next().unwrap() {
NestedMeta::Meta(Meta::Word(ident)) => ident,
bad => bail_span!(bad, "{}", pyslot_err),
}
};
self.add_item(
ClassItem::Slot {

View File

@@ -55,7 +55,7 @@ impl PyBaseException {
}
}
#[pyslot(new)]
#[pyslot]
fn tp_new(cls: PyClassRef, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
PyBaseException::new(args.args, vm).into_ref_with_type(vm, cls)
}

View File

@@ -89,7 +89,7 @@ pub(crate) fn init(context: &PyContext) {
#[pyimpl]
impl PyByteArray {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
options: ByteInnerNewOptions,

View File

@@ -91,7 +91,7 @@ pub(crate) fn init(context: &PyContext) {
#[pyimpl]
impl PyBytes {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
options: ByteInnerNewOptions,

View File

@@ -55,7 +55,7 @@ impl PyBuiltinDescriptor for PyClassMethod {
#[pyimpl]
impl PyClassMethod {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
callable: PyObjectRef,

View File

@@ -213,7 +213,7 @@ impl PyComplex {
!Complex64::is_zero(&self.value)
}
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
real: OptionalArg<IntoPyFloat>,

View File

@@ -27,7 +27,7 @@ impl PyValue for PyEnumerate {
#[pyimpl]
impl PyEnumerate {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
iterable: PyObjectRef,

View File

@@ -25,7 +25,7 @@ impl PyValue for PyFilter {
#[pyimpl]
impl PyFilter {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
function: PyObjectRef,

View File

@@ -176,7 +176,7 @@ fn int_eq(value: f64, other: &BigInt) -> bool {
#[pyimpl]
#[allow(clippy::trivially_copy_pass_by_ref)]
impl PyFloat {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
arg: OptionalArg<PyObjectRef>,

View File

@@ -14,7 +14,7 @@ pub fn init(context: &PyContext) {
#[pyimpl]
impl FrameRef {
#[pyslot(new)]
#[pyslot]
fn tp_new(_cls: FrameRef, vm: &VirtualMachine) -> PyResult<Self> {
Err(vm.new_type_error("Cannot directly create frame object".to_string()))
}

View File

@@ -213,7 +213,7 @@ fn inner_truediv(i1: &BigInt, i2: &BigInt, vm: &VirtualMachine) -> PyResult {
#[pyimpl]
impl PyInt {
#[pyslot(new)]
#[pyslot]
fn tp_new(cls: PyClassRef, options: IntOptions, vm: &VirtualMachine) -> PyResult<PyIntRef> {
PyInt::new(options.get_int_value(vm)?).into_ref_with_type(vm, cls)
}

View File

@@ -762,8 +762,8 @@ impl PyList {
Ok(())
}
#[pyslot(new)]
fn list_new(
#[pyslot]
fn tp_new(
cls: PyClassRef,
iterable: OptionalArg<PyObjectRef>,
vm: &VirtualMachine,

View File

@@ -24,7 +24,7 @@ impl PyValue for PyMap {
#[pyimpl]
impl PyMap {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
function: PyObjectRef,

View File

@@ -36,7 +36,7 @@ impl PyMappingProxy {
}
}
#[pyslot(new)]
#[pyslot]
fn tp_new(cls: PyClassRef, mapping: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
PyMappingProxy {
mapping: MappingProxyInner::Dict(mapping),

View File

@@ -20,7 +20,7 @@ impl PyMemoryView {
try_as_byte(&self.obj_ref)
}
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
bytes_object: PyObjectRef,

View File

@@ -19,7 +19,7 @@ impl PyValue for PyNamespace {
#[pyimpl]
impl PyNamespace {
#[pyslot(new)]
#[pyslot]
fn tp_new(cls: PyClassRef, kwargs: KwArgs, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
let zelf = PyNamespace.into_ref_with_type(vm, cls)?;
for (name, value) in kwargs.into_iter() {

View File

@@ -37,7 +37,7 @@ impl<T: IntoPyObject> IntoPyObject for Option<T> {
#[pyimpl]
impl PyNone {
#[pyslot(new)]
#[pyslot]
fn tp_new(_: PyClassRef, vm: &VirtualMachine) -> PyNoneRef {
vm.ctx.none.clone()
}

View File

@@ -131,7 +131,7 @@ impl PyBuiltinDescriptor for PyProperty {
#[pyimpl]
impl PyProperty {
#[pyslot(new)]
#[pyslot]
fn tp_new(cls: PyClassRef, args: PropertyArgs, vm: &VirtualMachine) -> PyResult<PyPropertyRef> {
PyProperty {
getter: args.fget,

View File

@@ -384,7 +384,7 @@ impl PyRange {
pyhash::hash_iter(elements.iter(), vm)
}
#[pyslot(new)]
#[pyslot]
fn tp_new(args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
let range = if args.args.len() <= 2 {
let (cls, stop) = args.bind(vm)?;

View File

@@ -330,7 +330,7 @@ macro_rules! try_set_cmp {
#[pyimpl]
impl PySet {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
iterable: OptionalArg<PyIterable>,
@@ -586,7 +586,7 @@ impl PySet {
#[pyimpl]
impl PyFrozenSet {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
iterable: OptionalArg<PyIterable>,

View File

@@ -88,7 +88,7 @@ impl PySlice {
}
}
#[pyslot(new)]
#[pyslot]
fn tp_new(cls: PyClassRef, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult<PySliceRef> {
let slice: PySlice = match args.args.len() {
0 => {

View File

@@ -30,7 +30,7 @@ impl PyBuiltinDescriptor for PyStaticMethod {
#[pyimpl]
impl PyStaticMethodRef {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
callable: PyObjectRef,

View File

@@ -179,7 +179,7 @@ struct StrArgs {
#[pyimpl]
impl PyString {
#[pyslot(new)]
#[pyslot]
fn tp_new(cls: PyClassRef, args: StrArgs, vm: &VirtualMachine) -> PyResult<PyStringRef> {
let string: PyStringRef = match args.object {
OptionalArg::Present(input) => {

View File

@@ -76,7 +76,7 @@ impl PySuper {
}
}
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
py_type: OptionalArg<PyClassRef>,

View File

@@ -227,8 +227,8 @@ impl PyTuple {
Ok(false)
}
#[pyslot(new)]
fn tuple_new(
#[pyslot]
fn tp_new(
cls: PyClassRef,
iterable: OptionalArg<PyObjectRef>,
vm: &VirtualMachine,

View File

@@ -21,7 +21,7 @@ pub type PyWeakProxyRef = PyRef<PyWeakProxy>;
#[pyimpl]
impl PyWeakProxy {
// TODO: callbacks
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
referent: PyObjectRef,

View File

@@ -20,7 +20,7 @@ impl PyValue for PyZip {
#[pyimpl]
impl PyZip {
#[pyslot(new)]
#[pyslot]
fn tp_new(cls: PyClassRef, iterables: Args, vm: &VirtualMachine) -> PyResult<PyZipRef> {
let iterators = iterables
.into_iter()

View File

@@ -190,7 +190,7 @@ impl PyValue for PyArray {
#[pyimpl]
impl PyArray {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
spec: PyStringRef,

View File

@@ -39,7 +39,7 @@ impl PyDeque {
#[pyimpl]
impl PyDeque {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
iter: OptionalArg<PyIterable>,

View File

@@ -41,7 +41,7 @@ impl PyHasher {
}
}
#[pyslot(new)]
#[pyslot]
fn tp_new(_cls: PyClassRef, _args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
Ok(PyHasher::new("md5", HashWrapper::md5())
.into_ref(vm)

View File

@@ -31,7 +31,7 @@ impl PyValue for PyItertoolsChain {
#[pyimpl]
impl PyItertoolsChain {
#[pyslot(new)]
#[pyslot]
fn tp_new(cls: PyClassRef, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
PyItertoolsChain {
iterables: args.args,
@@ -103,7 +103,7 @@ impl PyValue for PyItertoolsCompress {
#[pyimpl]
impl PyItertoolsCompress {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
data: PyObjectRef,
@@ -154,7 +154,7 @@ impl PyValue for PyItertoolsCount {
#[pyimpl]
impl PyItertoolsCount {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
start: OptionalArg<PyIntRef>,
@@ -207,7 +207,7 @@ impl PyValue for PyItertoolsCycle {
#[pyimpl]
impl PyItertoolsCycle {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
iterable: PyObjectRef,
@@ -272,7 +272,7 @@ impl PyValue for PyItertoolsRepeat {
#[pyimpl]
impl PyItertoolsRepeat {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
object: PyObjectRef,
@@ -332,7 +332,7 @@ impl PyValue for PyItertoolsStarmap {
#[pyimpl]
impl PyItertoolsStarmap {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
function: PyObjectRef,
@@ -374,7 +374,7 @@ impl PyValue for PyItertoolsTakewhile {
#[pyimpl]
impl PyItertoolsTakewhile {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
predicate: PyObjectRef,
@@ -433,7 +433,7 @@ impl PyValue for PyItertoolsDropwhile {
#[pyimpl]
impl PyItertoolsDropwhile {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
predicate: PyCallable,
@@ -502,7 +502,7 @@ fn pyobject_to_opt_usize(obj: PyObjectRef, vm: &VirtualMachine) -> Option<usize>
#[pyimpl]
impl PyItertoolsIslice {
#[pyslot(new)]
#[pyslot]
fn tp_new(cls: PyClassRef, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
let (iter, start, stop, step) = match args.args.len() {
0 | 1 => {
@@ -616,7 +616,7 @@ impl PyValue for PyItertoolsFilterFalse {
#[pyimpl]
impl PyItertoolsFilterFalse {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
predicate: PyObjectRef,
@@ -673,7 +673,7 @@ impl PyValue for PyItertoolsAccumulate {
#[pyimpl]
impl PyItertoolsAccumulate {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
iterable: PyObjectRef,
@@ -838,7 +838,7 @@ struct ProductArgs {
#[pyimpl]
impl PyItertoolsProduct {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
iterables: Args<PyObjectRef>,
@@ -949,7 +949,7 @@ impl PyValue for PyItertoolsCombinations {
#[pyimpl]
impl PyItertoolsCombinations {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
iterable: PyObjectRef,
@@ -1048,7 +1048,7 @@ impl PyValue for PyItertoolsCombinationsWithReplacement {
#[pyimpl]
impl PyItertoolsCombinationsWithReplacement {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
iterable: PyObjectRef,
@@ -1144,7 +1144,7 @@ impl PyValue for PyItertoolsPermutations {
#[pyimpl]
impl PyItertoolsPermutations {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
iterable: PyObjectRef,
@@ -1277,7 +1277,7 @@ struct ZiplongestArgs {
#[pyimpl]
impl PyItertoolsZiplongest {
#[pyslot(new)]
#[pyslot]
fn tp_new(
cls: PyClassRef,
iterables: Args,

View File

@@ -90,7 +90,7 @@ impl PySocket {
self.sock.borrow()
}
#[pyslot(new)]
#[pyslot]
fn tp_new(cls: PyClassRef, _args: PyFuncArgs, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
PySocket {
kind: Cell::default(),