From e5bf112a1c1c7fe45d27c3101a3469d28226f568 Mon Sep 17 00:00:00 2001 From: ChJR Date: Sat, 11 Jun 2022 15:53:54 +0900 Subject: [PATCH 01/13] Implement __func__ in @staticmethod --- vm/src/builtins/staticmethod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/vm/src/builtins/staticmethod.rs b/vm/src/builtins/staticmethod.rs index 88f8317d8f..f72859a982 100644 --- a/vm/src/builtins/staticmethod.rs +++ b/vm/src/builtins/staticmethod.rs @@ -68,6 +68,11 @@ impl PyStaticMethod { #[pyimpl(with(Callable, GetDescriptor, Constructor), flags(BASETYPE, HAS_DICT))] impl PyStaticMethod { + #[pyproperty(magic)] + fn func(&self) -> PyObjectRef { + self.callable.clone() + } + #[pyproperty(magic)] fn isabstractmethod(&self, vm: &VirtualMachine) -> PyObjectRef { match vm.get_attribute_opt(self.callable.clone(), "__isabstractmethod__") { From c3d14171efab66d8200ddae9faca17f6573caa98 Mon Sep 17 00:00:00 2001 From: ChJR Date: Mon, 13 Jun 2022 00:53:32 +0900 Subject: [PATCH 02/13] Add descriptors to @classmethod and @staticmethod --- vm/src/builtins/classmethod.rs | 30 ++++++++++++++++++++++++++++++ vm/src/builtins/staticmethod.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/vm/src/builtins/classmethod.rs b/vm/src/builtins/classmethod.rs index 882e412eb5..ebc3219191 100644 --- a/vm/src/builtins/classmethod.rs +++ b/vm/src/builtins/classmethod.rs @@ -100,6 +100,36 @@ impl PyClassMethod { self.callable.lock().clone() } + #[pyproperty(magic)] + fn wrapped(&self) -> PyObjectRef { + self.callable.clone() + } + + #[pyproperty(magic)] + fn module(&self, vm: &VirtualMachine) -> PyResult { + self.callable.get_attr("__module__", vm) + } + + #[pyproperty(magic)] + fn qualname(&self, vm: &VirtualMachine) -> PyResult { + self.callable.get_attr("__qualname__", vm) + } + + #[pyproperty(magic)] + fn name(&self, vm: &VirtualMachine) -> PyResult { + self.callable.get_attr("__name__", vm) + } + + #[pyproperty(magic)] + fn annotations(&self, vm: &VirtualMachine) -> PyResult { + self.callable.get_attr("__annotations__", vm) + } + + #[pyproperty(magic)] + fn doc(&self, vm: &VirtualMachine) -> PyResult { + self.callable.get_attr("__doc__", vm) + } + #[pyproperty(magic)] fn isabstractmethod(&self, vm: &VirtualMachine) -> PyObjectRef { match vm.get_attribute_opt(self.callable.lock().clone(), "__isabstractmethod__") { diff --git a/vm/src/builtins/staticmethod.rs b/vm/src/builtins/staticmethod.rs index f72859a982..ece6f918b5 100644 --- a/vm/src/builtins/staticmethod.rs +++ b/vm/src/builtins/staticmethod.rs @@ -73,6 +73,36 @@ impl PyStaticMethod { self.callable.clone() } + #[pyproperty(magic)] + fn wrapped(&self) -> PyObjectRef { + self.callable.clone() + } + + #[pyproperty(magic)] + fn module(&self, vm: &VirtualMachine) -> PyResult { + self.callable.get_attr("__module__", vm) + } + + #[pyproperty(magic)] + fn qualname(&self, vm: &VirtualMachine) -> PyResult { + self.callable.get_attr("__qualname__", vm) + } + + #[pyproperty(magic)] + fn name(&self, vm: &VirtualMachine) -> PyResult { + self.callable.get_attr("__name__", vm) + } + + #[pyproperty(magic)] + fn annotations(&self, vm: &VirtualMachine) -> PyResult { + self.callable.get_attr("__annotations__", vm) + } + + #[pyproperty(magic)] + fn doc(&self, vm: &VirtualMachine) -> PyResult { + self.callable.get_attr("__doc__", vm) + } + #[pyproperty(magic)] fn isabstractmethod(&self, vm: &VirtualMachine) -> PyObjectRef { match vm.get_attribute_opt(self.callable.clone(), "__isabstractmethod__") { From 4c970561572afe42ffc10e1674cee834deb7d0f3 Mon Sep 17 00:00:00 2001 From: ChJR Date: Mon, 13 Jun 2022 01:15:53 +0900 Subject: [PATCH 03/13] Fix __doc__ attribute --- vm/src/builtins/classmethod.rs | 29 +++++++++++++++++------------ vm/src/builtins/staticmethod.rs | 19 ++++++++++++------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/vm/src/builtins/classmethod.rs b/vm/src/builtins/classmethod.rs index ebc3219191..60d33710c7 100644 --- a/vm/src/builtins/classmethod.rs +++ b/vm/src/builtins/classmethod.rs @@ -64,11 +64,21 @@ impl Constructor for PyClassMethod { type Args = PyObjectRef; fn py_new(cls: PyTypeRef, callable: Self::Args, vm: &VirtualMachine) -> PyResult { - PyClassMethod { + let _callable = callable.clone(); + let result: PyResult = PyClassMethod { callable: PyMutex::new(callable), } .into_ref_with_type(vm, cls) - .map(Into::into) + .map(Into::into); + + let doc: PyResult = _callable.get_attr("__doc__", vm); + let doc = vm.unwrap_pyresult(doc); + let obj = vm.unwrap_pyresult(result.clone()); + + match obj.set_attr("__doc__", doc, vm) { + Err(e) => Err(e), + Ok(_) => result, + } } } @@ -102,32 +112,27 @@ impl PyClassMethod { #[pyproperty(magic)] fn wrapped(&self) -> PyObjectRef { - self.callable.clone() + self.callable.lock().clone() } #[pyproperty(magic)] fn module(&self, vm: &VirtualMachine) -> PyResult { - self.callable.get_attr("__module__", vm) + self.callable.lock().get_attr("__module__", vm) } #[pyproperty(magic)] fn qualname(&self, vm: &VirtualMachine) -> PyResult { - self.callable.get_attr("__qualname__", vm) + self.callable.lock().get_attr("__qualname__", vm) } #[pyproperty(magic)] fn name(&self, vm: &VirtualMachine) -> PyResult { - self.callable.get_attr("__name__", vm) + self.callable.lock().get_attr("__name__", vm) } #[pyproperty(magic)] fn annotations(&self, vm: &VirtualMachine) -> PyResult { - self.callable.get_attr("__annotations__", vm) - } - - #[pyproperty(magic)] - fn doc(&self, vm: &VirtualMachine) -> PyResult { - self.callable.get_attr("__doc__", vm) + self.callable.lock().get_attr("__annotations__", vm) } #[pyproperty(magic)] diff --git a/vm/src/builtins/staticmethod.rs b/vm/src/builtins/staticmethod.rs index ece6f918b5..52d796487c 100644 --- a/vm/src/builtins/staticmethod.rs +++ b/vm/src/builtins/staticmethod.rs @@ -41,9 +41,19 @@ impl Constructor for PyStaticMethod { type Args = PyObjectRef; fn py_new(cls: PyTypeRef, callable: Self::Args, vm: &VirtualMachine) -> PyResult { - PyStaticMethod { callable } + let _callable = callable.clone(); + let result: PyResult = PyStaticMethod { callable } .into_ref_with_type(vm, cls) - .map(Into::into) + .map(Into::into); + + let doc: PyResult = _callable.get_attr("__doc__", vm); + let doc = vm.unwrap_pyresult(doc); + let obj = vm.unwrap_pyresult(result.clone()); + + match obj.set_attr("__doc__", doc, vm) { + Err(e) => Err(e), + Ok(_) => result, + } } } @@ -98,11 +108,6 @@ impl PyStaticMethod { self.callable.get_attr("__annotations__", vm) } - #[pyproperty(magic)] - fn doc(&self, vm: &VirtualMachine) -> PyResult { - self.callable.get_attr("__doc__", vm) - } - #[pyproperty(magic)] fn isabstractmethod(&self, vm: &VirtualMachine) -> PyObjectRef { match vm.get_attribute_opt(self.callable.clone(), "__isabstractmethod__") { From ced6b682bf0f27243cc0eff4f5ca5634f2c71125 Mon Sep 17 00:00:00 2001 From: ChJR Date: Mon, 13 Jun 2022 01:19:23 +0900 Subject: [PATCH 04/13] Fix string representations of @classmethod and @staticmethod --- vm/src/builtins/classmethod.rs | 22 +++++++++++++++++++++- vm/src/builtins/staticmethod.rs | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/vm/src/builtins/classmethod.rs b/vm/src/builtins/classmethod.rs index 60d33710c7..00e67d52e3 100644 --- a/vm/src/builtins/classmethod.rs +++ b/vm/src/builtins/classmethod.rs @@ -1,4 +1,4 @@ -use super::{PyBoundMethod, PyType, PyTypeRef}; +use super::{PyBoundMethod, PyStr, PyType, PyTypeRef}; use crate::{ class::PyClassImpl, common::lock::PyMutex, @@ -135,6 +135,26 @@ impl PyClassMethod { self.callable.lock().get_attr("__annotations__", vm) } + #[pymethod(magic)] + fn repr(&self, vm: &VirtualMachine) -> Option { + let callable = self.callable.lock().repr(vm).unwrap(); + let class = Self::class(vm); + + match ( + class + .qualname(vm) + .downcast_ref::() + .map(|n| n.as_str()), + class.module(vm).downcast_ref::().map(|m| m.as_str()), + ) { + (None, _) => None, + (Some(qualname), Some(module)) if module != "builtins" => { + Some(format!("<{}.{}({})>", module, qualname, callable)) + } + _ => Some(format!("<{}({})>", class.slot_name(), callable)), + } + } + #[pyproperty(magic)] fn isabstractmethod(&self, vm: &VirtualMachine) -> PyObjectRef { match vm.get_attribute_opt(self.callable.lock().clone(), "__isabstractmethod__") { diff --git a/vm/src/builtins/staticmethod.rs b/vm/src/builtins/staticmethod.rs index 52d796487c..1946011568 100644 --- a/vm/src/builtins/staticmethod.rs +++ b/vm/src/builtins/staticmethod.rs @@ -108,6 +108,26 @@ impl PyStaticMethod { self.callable.get_attr("__annotations__", vm) } + #[pymethod(magic)] + fn repr(&self, vm: &VirtualMachine) -> Option { + let callable = self.callable.repr(vm).unwrap(); + let class = Self::class(vm); + + match ( + class + .qualname(vm) + .downcast_ref::() + .map(|n| n.as_str()), + class.module(vm).downcast_ref::().map(|m| m.as_str()), + ) { + (None, _) => None, + (Some(qualname), Some(module)) if module != "builtins" => { + Some(format!("<{}.{}({})>", module, qualname, callable)) + } + _ => Some(format!("<{}({})>", class.slot_name(), callable)), + } + } + #[pyproperty(magic)] fn isabstractmethod(&self, vm: &VirtualMachine) -> PyObjectRef { match vm.get_attribute_opt(self.callable.clone(), "__isabstractmethod__") { From d6e71ad18a8a2b960caa050bda45fedbcc423fd3 Mon Sep 17 00:00:00 2001 From: ChJR Date: Mon, 13 Jun 2022 07:11:41 +0900 Subject: [PATCH 05/13] Fix __get__ in @classmethod --- vm/src/builtins/classmethod.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/vm/src/builtins/classmethod.rs b/vm/src/builtins/classmethod.rs index 00e67d52e3..03c00d995f 100644 --- a/vm/src/builtins/classmethod.rs +++ b/vm/src/builtins/classmethod.rs @@ -53,10 +53,13 @@ impl GetDescriptor for PyClassMethod { cls: Option, vm: &VirtualMachine, ) -> PyResult { - let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?; - let cls = cls.unwrap_or_else(|| obj.class().clone().into()); - let callable = zelf.callable.lock().clone(); - Ok(PyBoundMethod::new_ref(cls, callable, &vm.ctx).into()) + let (zelf, _obj) = Self::_unwrap(zelf, obj.clone(), vm)?; + let cls = cls.unwrap_or_else(|| _obj.class().clone().into()); + let _descr_get: PyResult = zelf.callable.lock().get_attr("__get__", vm); + match _descr_get { + Err(_) => Ok(PyBoundMethod::new_ref(cls, zelf.callable.lock().clone(), &vm.ctx).into()), + Ok(_descr_get) => vm.invoke(&_descr_get, (cls.clone(), cls)), + } } } From b6df0ce6e8365afb75f009e799707d99d7eecab2 Mon Sep 17 00:00:00 2001 From: ChJR Date: Mon, 13 Jun 2022 07:12:47 +0900 Subject: [PATCH 06/13] Enable decorator tests --- Lib/test/test_decorators.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Lib/test/test_decorators.py b/Lib/test/test_decorators.py index abd0f81f4f..8324d0be55 100644 --- a/Lib/test/test_decorators.py +++ b/Lib/test/test_decorators.py @@ -94,16 +94,12 @@ def func(x): self.assertEqual(repr(wrapper), format_str.format(func)) return wrapper - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_staticmethod(self): wrapper = self.check_wrapper_attrs(staticmethod, '') # bpo-43682: Static methods are callable since Python 3.10 self.assertEqual(wrapper(1), 1) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_classmethod(self): wrapper = self.check_wrapper_attrs(classmethod, '') @@ -297,8 +293,6 @@ def bar(): return 42 self.assertEqual(bar(), 42) self.assertEqual(actions, expected_actions) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_wrapped_descriptor_inside_classmethod(self): class BoundWrapper: def __init__(self, wrapped): @@ -337,8 +331,6 @@ def outer(cls): self.assertEqual(Class().inner(), 'spam') self.assertEqual(Class().outer(), 'eggs') - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_wrapped_classmethod_inside_classmethod(self): class MyClassMethod1: def __init__(self, func): From 728582fe1bda046b04d7698b432a050971b76ebb Mon Sep 17 00:00:00 2001 From: ChJR Date: Mon, 13 Jun 2022 07:59:45 +0900 Subject: [PATCH 07/13] Fix codes with Clippy --- vm/src/builtins/classmethod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/builtins/classmethod.rs b/vm/src/builtins/classmethod.rs index 03c00d995f..45b49d67f2 100644 --- a/vm/src/builtins/classmethod.rs +++ b/vm/src/builtins/classmethod.rs @@ -53,7 +53,7 @@ impl GetDescriptor for PyClassMethod { cls: Option, vm: &VirtualMachine, ) -> PyResult { - let (zelf, _obj) = Self::_unwrap(zelf, obj.clone(), vm)?; + let (zelf, _obj) = Self::_unwrap(zelf, obj, vm)?; let cls = cls.unwrap_or_else(|| _obj.class().clone().into()); let _descr_get: PyResult = zelf.callable.lock().get_attr("__get__", vm); match _descr_get { From f2e62cf50ca6ffed79d413ed1c356303ee1c58ad Mon Sep 17 00:00:00 2001 From: ChJR Date: Mon, 13 Jun 2022 21:58:18 +0900 Subject: [PATCH 08/13] Fix other affected tests --- Lib/test/test_reprlib.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_reprlib.py b/Lib/test/test_reprlib.py index bc0d4d1f7a..611fb9d1e4 100644 --- a/Lib/test/test_reprlib.py +++ b/Lib/test/test_reprlib.py @@ -198,8 +198,6 @@ def inner(): r'int object at 0x[0-9A-Fa-f]+>') self.assertRegex(r(x), r'') - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_descriptors(self): eq = self.assertEqual # method descriptors From 9dd999c7efd6defa11006eff95f4b5a8e4d67cc1 Mon Sep 17 00:00:00 2001 From: ChJR Date: Sun, 10 Jul 2022 15:28:11 +0900 Subject: [PATCH 09/13] Improve code quality --- vm/src/builtins/classmethod.rs | 11 ++++------- vm/src/builtins/staticmethod.rs | 17 ++++++++--------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/vm/src/builtins/classmethod.rs b/vm/src/builtins/classmethod.rs index 45b49d67f2..405b0d09b4 100644 --- a/vm/src/builtins/classmethod.rs +++ b/vm/src/builtins/classmethod.rs @@ -67,21 +67,18 @@ impl Constructor for PyClassMethod { type Args = PyObjectRef; fn py_new(cls: PyTypeRef, callable: Self::Args, vm: &VirtualMachine) -> PyResult { - let _callable = callable.clone(); let result: PyResult = PyClassMethod { - callable: PyMutex::new(callable), + callable: PyMutex::new(callable.clone()), } .into_ref_with_type(vm, cls) .map(Into::into); - let doc: PyResult = _callable.get_attr("__doc__", vm); + let doc: PyResult = callable.get_attr("__doc__", vm); let doc = vm.unwrap_pyresult(doc); let obj = vm.unwrap_pyresult(result.clone()); - match obj.set_attr("__doc__", doc, vm) { - Err(e) => Err(e), - Ok(_) => result, - } + obj.set_attr("__doc__", doc, vm)?; + result } } diff --git a/vm/src/builtins/staticmethod.rs b/vm/src/builtins/staticmethod.rs index 1946011568..10244884a5 100644 --- a/vm/src/builtins/staticmethod.rs +++ b/vm/src/builtins/staticmethod.rs @@ -41,19 +41,18 @@ impl Constructor for PyStaticMethod { type Args = PyObjectRef; fn py_new(cls: PyTypeRef, callable: Self::Args, vm: &VirtualMachine) -> PyResult { - let _callable = callable.clone(); - let result: PyResult = PyStaticMethod { callable } - .into_ref_with_type(vm, cls) - .map(Into::into); + let result: PyResult = PyStaticMethod { + callable: callable.clone(), + } + .into_ref_with_type(vm, cls) + .map(Into::into); - let doc: PyResult = _callable.get_attr("__doc__", vm); + let doc: PyResult = callable.get_attr("__doc__", vm); let doc = vm.unwrap_pyresult(doc); let obj = vm.unwrap_pyresult(result.clone()); - match obj.set_attr("__doc__", doc, vm) { - Err(e) => Err(e), - Ok(_) => result, - } + obj.set_attr("__doc__", doc, vm)?; + result } } From 9d9f9d364b20da2c67d7dbca302c18bb1652b301 Mon Sep 17 00:00:00 2001 From: ChJR Date: Fri, 15 Jul 2022 22:40:59 +0900 Subject: [PATCH 10/13] Change the variable name better format --- vm/src/builtins/classmethod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vm/src/builtins/classmethod.rs b/vm/src/builtins/classmethod.rs index 405b0d09b4..c4d99fe6ae 100644 --- a/vm/src/builtins/classmethod.rs +++ b/vm/src/builtins/classmethod.rs @@ -55,10 +55,10 @@ impl GetDescriptor for PyClassMethod { ) -> PyResult { let (zelf, _obj) = Self::_unwrap(zelf, obj, vm)?; let cls = cls.unwrap_or_else(|| _obj.class().clone().into()); - let _descr_get: PyResult = zelf.callable.lock().get_attr("__get__", vm); - match _descr_get { + let call_descr_get: PyResult = zelf.callable.lock().get_attr("__get__", vm); + match call_descr_get { Err(_) => Ok(PyBoundMethod::new_ref(cls, zelf.callable.lock().clone(), &vm.ctx).into()), - Ok(_descr_get) => vm.invoke(&_descr_get, (cls.clone(), cls)), + Ok(call_descr_get) => vm.invoke(&call_descr_get, (cls.clone(), cls)), } } } From 4bf56447125acd0f522ee2e9ad14f614fa3520e7 Mon Sep 17 00:00:00 2001 From: ChJR Date: Sat, 16 Jul 2022 17:09:27 +0900 Subject: [PATCH 11/13] Improve code quality --- vm/src/builtins/classmethod.rs | 20 +++++++++++--------- vm/src/builtins/staticmethod.rs | 19 +++++++++---------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/vm/src/builtins/classmethod.rs b/vm/src/builtins/classmethod.rs index c4d99fe6ae..d321947d2a 100644 --- a/vm/src/builtins/classmethod.rs +++ b/vm/src/builtins/classmethod.rs @@ -67,18 +67,20 @@ impl Constructor for PyClassMethod { type Args = PyObjectRef; fn py_new(cls: PyTypeRef, callable: Self::Args, vm: &VirtualMachine) -> PyResult { - let result: PyResult = PyClassMethod { - callable: PyMutex::new(callable.clone()), + let doc = callable.get_attr("__doc__", vm); + + let result = PyClassMethod { + callable: PyMutex::new(callable), } - .into_ref_with_type(vm, cls) - .map(Into::into); + .into_ref_with_type(vm, cls)?; + let obj = PyObjectRef::from(result); - let doc: PyResult = callable.get_attr("__doc__", vm); - let doc = vm.unwrap_pyresult(doc); - let obj = vm.unwrap_pyresult(result.clone()); + match doc { + Err(_) => None, + Ok(doc) => Some(obj.set_attr("__doc__", doc, vm)), + }; - obj.set_attr("__doc__", doc, vm)?; - result + Ok(obj) } } diff --git a/vm/src/builtins/staticmethod.rs b/vm/src/builtins/staticmethod.rs index 10244884a5..502b2abf7c 100644 --- a/vm/src/builtins/staticmethod.rs +++ b/vm/src/builtins/staticmethod.rs @@ -41,18 +41,17 @@ impl Constructor for PyStaticMethod { type Args = PyObjectRef; fn py_new(cls: PyTypeRef, callable: Self::Args, vm: &VirtualMachine) -> PyResult { - let result: PyResult = PyStaticMethod { - callable: callable.clone(), - } - .into_ref_with_type(vm, cls) - .map(Into::into); + let doc = callable.get_attr("__doc__", vm); + + let result = PyStaticMethod { callable }.into_ref_with_type(vm, cls)?; + let obj = PyObjectRef::from(result); - let doc: PyResult = callable.get_attr("__doc__", vm); - let doc = vm.unwrap_pyresult(doc); - let obj = vm.unwrap_pyresult(result.clone()); + match doc { + Err(_) => None, + Ok(doc) => Some(obj.set_attr("__doc__", doc, vm)), + }; - obj.set_attr("__doc__", doc, vm)?; - result + Ok(obj) } } From fccb4d6b95c7111d6a743b0b00ee11af89a137e3 Mon Sep 17 00:00:00 2001 From: ChJR Date: Sat, 16 Jul 2022 21:16:16 +0900 Subject: [PATCH 12/13] Simplify __doc__ initializing --- vm/src/builtins/classmethod.rs | 7 +++---- vm/src/builtins/staticmethod.rs | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/vm/src/builtins/classmethod.rs b/vm/src/builtins/classmethod.rs index d321947d2a..4bb4a2af9a 100644 --- a/vm/src/builtins/classmethod.rs +++ b/vm/src/builtins/classmethod.rs @@ -75,10 +75,9 @@ impl Constructor for PyClassMethod { .into_ref_with_type(vm, cls)?; let obj = PyObjectRef::from(result); - match doc { - Err(_) => None, - Ok(doc) => Some(obj.set_attr("__doc__", doc, vm)), - }; + if let Ok(doc) = doc { + obj.set_attr("__doc__", doc, vm)?; + } Ok(obj) } diff --git a/vm/src/builtins/staticmethod.rs b/vm/src/builtins/staticmethod.rs index 502b2abf7c..5198bc04a8 100644 --- a/vm/src/builtins/staticmethod.rs +++ b/vm/src/builtins/staticmethod.rs @@ -46,10 +46,9 @@ impl Constructor for PyStaticMethod { let result = PyStaticMethod { callable }.into_ref_with_type(vm, cls)?; let obj = PyObjectRef::from(result); - match doc { - Err(_) => None, - Ok(doc) => Some(obj.set_attr("__doc__", doc, vm)), - }; + if let Ok(doc) = doc { + obj.set_attr("__doc__", doc, vm)?; + } Ok(obj) } From 8ef21ee628bd4f28ee16b5d12f63986b44b04f58 Mon Sep 17 00:00:00 2001 From: ChJR Date: Sat, 16 Jul 2022 21:28:01 +0900 Subject: [PATCH 13/13] Apply PEP 614 Check 'Named Expressions Need Not Be Parenthesized' Section --- Lib/test/test_decorators.py | 13 ++++++------- parser/src/python.lalrpop | 2 +- parser/src/python.rs | 18 +++++++++--------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Lib/test/test_decorators.py b/Lib/test/test_decorators.py index 8324d0be55..57a741ffd2 100644 --- a/Lib/test/test_decorators.py +++ b/Lib/test/test_decorators.py @@ -199,13 +199,12 @@ def unimp(func): code = compile(codestr, "test", "exec") self.assertRaises(exc, eval, code, context) - # TODO: RUSTPYTHON; := operator is invalid syntax - # def test_expressions(self): - # for expr in ( - # "(x,)", "(x, y)", "x := y", "(x := y)", "x @y", "(x @ y)", "x[0]", - # "w[x].y.z", "w + x - (y + z)", "x(y)()(z)", "[w, x, y][z]", "x.y", - # ): - # compile(f"@{expr}\ndef f(): pass", "test", "exec") + def test_expressions(self): + for expr in ( + "(x,)", "(x, y)", "x := y", "(x := y)", "x @y", "(x @ y)", "x[0]", + "w[x].y.z", "w + x - (y + z)", "x(y)()(z)", "[w, x, y][z]", "x.y", + ): + compile(f"@{expr}\ndef f(): pass", "test", "exec") def test_double(self): class C(object): diff --git a/parser/src/python.lalrpop b/parser/src/python.lalrpop index 0d2ca33164..af6532badc 100644 --- a/parser/src/python.lalrpop +++ b/parser/src/python.lalrpop @@ -634,7 +634,7 @@ ClassDef: ast::Stmt = { // Decorators: Decorator: ast::Expr = { - "@" "\n" => { + "@" "\n" => { p }, }; diff --git a/parser/src/python.rs b/parser/src/python.rs index d264222f2f..4957fa4653 100644 --- a/parser/src/python.rs +++ b/parser/src/python.rs @@ -1,5 +1,5 @@ -// auto-generated: "lalrpop 0.19.7" -// sha3: 39aa91107495c6c1fc36b3a29432d9734e32e59d677b96962503bc0f6716 +// auto-generated: "lalrpop 0.19.8" +// sha3: 86d5818e500762c6e3eb3bc4602ed02373d87800aad460d22ece9749fd8fe424 use crate::ast; use crate::fstring::parse_located_fstring; use crate::function::{ArgumentList, parse_args, parse_params}; @@ -968,7 +968,7 @@ mod __parse__Top { // State 416 0, 0, 0, 0, 0, 0, 0, -852, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // State 417 - 0, -218, -218, 0, -218, 0, -218, -218, -218, -218, 0, 0, -218, 0, -218, -218, 0, 0, -218, 0, -218, -218, 0, 0, -218, 103, 0, -218, -218, 0, -218, 0, -218, -218, -218, -218, 0, -218, 0, 0, 0, 0, -218, -218, -218, 0, -218, 0, 0, -218, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -218, 0, 0, 0, -218, 0, -218, -218, 0, 0, -218, -218, 0, 0, 0, 0, 0, 0, 0, 0, -218, 0, -218, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + -218, -218, -218, 0, -218, 0, -218, -218, -218, -218, 0, 0, -218, 0, -218, -218, 0, 0, -218, 0, -218, -218, 0, 0, -218, 103, 0, -218, -218, 0, -218, 0, -218, -218, -218, -218, 0, -218, 0, 0, 0, 0, -218, -218, -218, 0, -218, 0, 0, -218, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -218, 0, 0, 0, -218, 0, -218, -218, 0, 0, -218, -218, 0, 0, 0, 0, 0, 0, 0, 0, -218, 0, -218, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // State 418 0, 0, 0, 0, 0, 0, 0, -850, 0, 0, 0, 0, 0, 0, -850, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -850, 0, 0, 0, 0, 0, -850, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -850, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -850, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // State 419 @@ -984,7 +984,7 @@ mod __parse__Top { // State 424 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 499, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // State 425 - 0, 0, 0, 0, 0, 0, 0, -432, 0, 0, 0, 0, 0, 0, -432, 0, 0, 0, 0, 0, 0, 0, 0, 0, -432, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -432, 0, 0, 0, 0, 0, -432, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -432, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -432, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + -432, 0, 0, 0, 0, 0, 0, -432, 0, 0, 0, 0, 0, 0, -432, 0, 0, 0, 0, 0, 0, 0, 0, 0, -432, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -432, 0, 0, 0, 0, 0, -432, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -432, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -432, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // State 426 -220, -220, -220, -220, -220, -220, -220, -220, -220, -220, -220, -220, -220, -220, -220, -220, -220, 0, -220, 0, -220, -220, -220, -220, -220, 0, -220, -220, -220, -220, -220, -220, -220, -220, -220, -220, -220, -220, -220, 0, 0, 0, -220, -220, -220, -220, -220, -220, 0, -220, 0, 0, 0, 0, 0, 0, 0, -220, 0, 0, -220, -220, -220, 0, -220, 0, -220, -220, 0, 0, -220, -220, 0, 0, 0, 0, 0, 0, 0, 0, -220, -220, -220, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // State 427 @@ -1234,7 +1234,7 @@ mod __parse__Top { // State 549 -110, 0, 0, -110, 0, -110, 0, -110, 0, 0, -110, -110, 0, -110, -110, 0, -110, 0, 0, 0, 0, 0, -110, -110, -110, 0, -110, 0, 0, -110, 0, -110, 0, 0, 0, 0, -110, 0, -110, 0, 0, 0, 0, 0, 0, -110, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -110, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // State 550 - 0, 0, 0, 0, 0, 0, 0, -431, 0, 0, 0, 0, 0, 0, -431, 0, 0, 0, 0, 0, 0, 0, 0, 0, -431, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -431, 0, 0, 0, 0, 0, -431, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -431, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -431, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + -431, 0, 0, 0, 0, 0, 0, -431, 0, 0, 0, 0, 0, 0, -431, 0, 0, 0, 0, 0, 0, 0, 0, 0, -431, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -431, 0, 0, 0, 0, 0, -431, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -431, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -431, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // State 551 -225, -225, -225, -225, -225, -225, -225, -225, -225, -225, -225, -225, -225, -225, -225, -225, -225, 0, -225, 0, -225, -225, -225, -225, -225, 0, -225, -225, -225, -225, -225, -225, -225, -225, -225, -225, -225, -225, -225, 0, 0, 0, -225, -225, -225, -225, -225, -225, 0, -225, 0, 0, 0, 0, 0, 0, 0, -225, 0, 0, -225, -225, -225, 0, -225, 0, -225, -225, 0, 0, -225, -225, 0, 0, 0, 0, 0, 0, 0, 0, -225, -225, -225, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // State 552 @@ -3836,7 +3836,7 @@ mod __parse__Top { 169 => 202, 201 => 228, 230 => 261, - 13 | 15 | 19 | 32 | 37 | 105..=106 | 114 | 147..=148 | 155 | 203 | 231 => 417, + 13 | 15 | 19 | 24 | 32 | 37 | 105..=106 | 114 | 147..=148 | 155 | 203 | 231 => 417, 17 | 60..=61 | 107 | 111 | 149..=150 | 152..=153 | 183 | 186..=188 | 211..=216 | 242..=247 | 249 | 265..=266 | 268 | 270..=272 | 287..=292 | 304..=307 | 316 => 428, 26 => 457, 44 | 92 | 122 | 157 => 478, @@ -3944,6 +3944,7 @@ mod __parse__Top { 171 => 53, 172 => match state { 44 | 92 | 122 | 157 => 93, + 24 => 455, 32 => 468, 37 => 471, 203 => 673, @@ -4080,8 +4081,7 @@ mod __parse__Top { 97 => 143, 142 => 181, 1 | 36 | 39 | 54 | 71..=72 | 101 | 126 | 159 => 343, - 15 | 32 | 37 | 44 | 92 | 105..=106 | 114 | 122 | 147..=148 | 155 | 157 | 203 | 231 => 425, - 24 => 455, + 15 | 24 | 32 | 37 | 44 | 92 | 105..=106 | 114 | 122 | 147..=148 | 155 | 157 | 203 | 231 => 425, 25 => 456, 35 => 469, 38 | 77 | 138 | 178 => 472, @@ -16852,7 +16852,7 @@ mod __parse__Top { _: core::marker::PhantomData<()>, ) -> (usize, usize) { - // Decorator = "@", Test, "\n" => ActionFn(793); + // Decorator = "@", NamedExpressionTest, "\n" => ActionFn(793); assert!(__symbols.len() >= 3); let __sym2 = __pop_Variant0(__symbols); let __sym1 = __pop_Variant52(__symbols);