]> granicus.if.org Git - python/commitdiff
bpo-36669: add matmul support to weakref.proxy (GH-12932)
authorMark Dickinson <dickinsm@gmail.com>
Fri, 26 Apr 2019 06:56:15 +0000 (15:56 +0900)
committerGitHub <noreply@github.com>
Fri, 26 Apr 2019 06:56:15 +0000 (15:56 +0900)
Doc/library/weakref.rst
Doc/whatsnew/3.8.rst
Lib/test/test_weakref.py
Misc/NEWS.d/next/Library/2019-04-24-17-08-45.bpo-36669.X4g0fu.rst [new file with mode: 0644]
Objects/weakrefobject.c

index 7f3d267d74c2ec588702db7610d17441d3efc107..80a908bbd83b0acfa26880dadc540d1a01a4c3b9 100644 (file)
@@ -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 proxy objects to include the matrix
+      multiplication operators ``@`` and ``@=``.
+
 
 .. function:: getweakrefcount(object)
 
index 2270334a281b0874725988fbb3f8db4b18240ba2..4cb9c4f0f20e94a74062f15b062a1bf5807054bf 100644 (file)
@@ -450,6 +450,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
 ---
 
index 50a46f817f9ffe8e824bf3fc398086f9130c3403..6f15c03ac5292ff663647b52b35dab4b83d8039f 100644 (file)
@@ -285,6 +285,21 @@ class ReferencesTestCase(TestBase):
         p //= 5
         self.assertEqual(p, 21)
 
+    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)
+
     # 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/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 (file)
index 0000000..53bdefe
--- /dev/null
@@ -0,0 +1 @@
+Add missing matrix multiplication operator support to weakref.proxy.
index 9227aa688f47e1d138629154a3b31786a566b2e9..ff6d92254f7fe2ca47ed2a868b8a4efb6346c967 100644 (file)
@@ -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 = {