From c02e310a5888c41a16d7050431d8f10dff401bd1 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Wed, 24 Apr 2019 17:06:06 +0900 Subject: [PATCH 1/5] Fix missing weakproxy support for matmul operators --- Lib/test/test_weakref.py | 12 ++++++++++++ Objects/weakrefobject.c | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index 50a46f817f9ffe..18c978e38c5f2f 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -285,6 +285,18 @@ def __ifloordiv__(self, other): p //= 5 self.assertEqual(p, 21) + def test_proxy_matmul(self): + class C: + def __matmul__(self, other): + return 1729 + def __imatmul__(self, other): + return 561 + o = C() + p = weakref.proxy(o) + self.assertEqual(p @ 5, 1729) + p @= 5 + self.assertEqual(p, 561) + # The PyWeakref_* C API is documented as allowing either NULL or # None as the value for the callback, where either means "no # callback". The "no callback" ref and proxy objects are supposed diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 9227aa688f47e1..ff6d92254f7fe2 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -525,6 +525,8 @@ WRAP_BINARY(proxy_iand, PyNumber_InPlaceAnd) WRAP_BINARY(proxy_ixor, PyNumber_InPlaceXor) WRAP_BINARY(proxy_ior, PyNumber_InPlaceOr) WRAP_UNARY(proxy_index, PyNumber_Index) +WRAP_BINARY(proxy_matmul, PyNumber_MatrixMultiply) +WRAP_BINARY(proxy_imatmul, PyNumber_InPlaceMatrixMultiply) static int proxy_bool(PyWeakReference *proxy) @@ -642,6 +644,8 @@ static PyNumberMethods proxy_as_number = { proxy_ifloor_div, /*nb_inplace_floor_divide*/ proxy_itrue_div, /*nb_inplace_true_divide*/ proxy_index, /*nb_index*/ + proxy_matmul, /*nb_matrix_multiply*/ + proxy_imatmul, /*nb_inplace_matrix_multiply*/ }; static PySequenceMethods proxy_as_sequence = { From ddec7bd154c55d1bbfc821b6f5d6bab60ac92278 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Wed, 24 Apr 2019 17:09:15 +0900 Subject: [PATCH 2/5] Add Misc/NEWS entry. --- .../NEWS.d/next/Library/2019-04-24-17-08-45.bpo-36669.X4g0fu.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2019-04-24-17-08-45.bpo-36669.X4g0fu.rst diff --git a/Misc/NEWS.d/next/Library/2019-04-24-17-08-45.bpo-36669.X4g0fu.rst b/Misc/NEWS.d/next/Library/2019-04-24-17-08-45.bpo-36669.X4g0fu.rst new file mode 100644 index 00000000000000..53bdefee12f2c5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-04-24-17-08-45.bpo-36669.X4g0fu.rst @@ -0,0 +1 @@ +Add missing matrix multiplication operator support to weakref.proxy. From 5c97368621f3fc88b61ce6b409864f8cee5ea9de Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Thu, 25 Apr 2019 18:30:17 +0900 Subject: [PATCH 3/5] Expand test to cover __rmatmul__ --- Lib/test/test_weakref.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index 18c978e38c5f2f..6f15c03ac5292f 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -289,11 +289,14 @@ def test_proxy_matmul(self): class C: def __matmul__(self, other): return 1729 + def __rmatmul__(self, other): + return -163 def __imatmul__(self, other): return 561 o = C() p = weakref.proxy(o) self.assertEqual(p @ 5, 1729) + self.assertEqual(5 @ p, -163) p @= 5 self.assertEqual(p, 561) From 07a2a2c189cae451cf11cede00393f63a4122988 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Thu, 25 Apr 2019 18:06:03 +0200 Subject: [PATCH 4/5] Add versionchanged and whatsnew documentation entries. --- Doc/library/weakref.rst | 4 ++++ Doc/whatsnew/3.8.rst | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst index 7f3d267d74c2ec..1b10b69e8cc49a 100644 --- a/Doc/library/weakref.rst +++ b/Doc/library/weakref.rst @@ -139,6 +139,10 @@ Extension types can easily be made to support weak references; see prevent their use as dictionary keys. *callback* is the same as the parameter of the same name to the :func:`ref` function. + .. versionchanged:: 3.8 + Extended the operator support on proxys object to include the matrix + multiplication operators ``@`` and ``@=``. + .. function:: getweakrefcount(object) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index bd7ad3f87cb504..c7f75ac791810b 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -434,6 +434,13 @@ venv activating virtual environments under PowerShell Core 6.1. (Contributed by Brett Cannon in :issue:`32718`.) +weakref +------- + +* The proxy objects returned by :func:`weakref.proxy` now support the matrix + multiplication operators ``@`` and ``@=`` in addition to the other + numeric operators. (Contributed by Mark Dickinson in :issue:`36669`.) + xml --- From 0adff054766def3c2d201a50d440136724cd2698 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Thu, 25 Apr 2019 18:11:03 +0200 Subject: [PATCH 5/5] Fix typo in versionchanged entry. --- Doc/library/weakref.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst index 1b10b69e8cc49a..80a908bbd83b0a 100644 --- a/Doc/library/weakref.rst +++ b/Doc/library/weakref.rst @@ -140,7 +140,7 @@ Extension types can easily be made to support weak references; see of the same name to the :func:`ref` function. .. versionchanged:: 3.8 - Extended the operator support on proxys object to include the matrix + Extended the operator support on proxy objects to include the matrix multiplication operators ``@`` and ``@=``.