From 63d156097ce217bb1b154ff16158c413b16c1185 Mon Sep 17 00:00:00 2001 From: jfh Date: Wed, 16 Jun 2021 12:07:48 +0300 Subject: [PATCH] Add new and bool for NotImplemented. New simply returns the singleton. While `bool(NotImplemented)` already returned `True`, it is added since it will need to raise a warning in the future. --- Lib/test/test_builtin.py | 2 -- vm/src/builtins/singletons.rs | 13 +++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 9b50f0020..324fc4b3e 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -1561,8 +1561,6 @@ class BuiltinTest(unittest.TestCase): self.assertRaises(ValueError, x.translate, b"1", 1) self.assertRaises(TypeError, x.translate, b"1"*256, 1) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_construct_singletons(self): for const in None, Ellipsis, NotImplemented: tp = type(const) diff --git a/vm/src/builtins/singletons.rs b/vm/src/builtins/singletons.rs index a77f520e6..d43a822cb 100644 --- a/vm/src/builtins/singletons.rs +++ b/vm/src/builtins/singletons.rs @@ -61,6 +61,19 @@ impl PyValue for PyNotImplemented { #[pyimpl] impl PyNotImplemented { + #[pyslot] + fn tp_new(_: PyTypeRef, vm: &VirtualMachine) -> PyRef { + vm.ctx.not_implemented.clone() + } + + // TODO: As per https://bugs.python.org/issue35712, using NotImplemented + // in boolean contexts will need to raise a DeprecationWarning in 3.9 + // and, eventually, a TypeError. + #[pymethod(magic)] + fn bool(&self) -> bool { + true + } + #[pymethod(magic)] fn repr(&self) -> String { "NotImplemented".to_owned()