Skip to content

Commit 7abb6c0

Browse files
authoredApr 26, 2019
bpo-36669: add matmul support to weakref.proxy (pythonGH-12932)
1 parent 3cde440 commit 7abb6c0

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed
 

‎Doc/library/weakref.rst

+4
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ Extension types can easily be made to support weak references; see
139139
prevent their use as dictionary keys. *callback* is the same as the parameter
140140
of the same name to the :func:`ref` function.
141141

142+
.. versionchanged:: 3.8
143+
Extended the operator support on proxy objects to include the matrix
144+
multiplication operators ``@`` and ``@=``.
145+
142146

143147
.. function:: getweakrefcount(object)
144148

‎Doc/whatsnew/3.8.rst

+7
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,13 @@ venv
450450
activating virtual environments under PowerShell Core 6.1.
451451
(Contributed by Brett Cannon in :issue:`32718`.)
452452

453+
weakref
454+
-------
455+
456+
* The proxy objects returned by :func:`weakref.proxy` now support the matrix
457+
multiplication operators ``@`` and ``@=`` in addition to the other
458+
numeric operators. (Contributed by Mark Dickinson in :issue:`36669`.)
459+
453460
xml
454461
---
455462

‎Lib/test/test_weakref.py

+15
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,21 @@ def __ifloordiv__(self, other):
285285
p //= 5
286286
self.assertEqual(p, 21)
287287

288+
def test_proxy_matmul(self):
289+
class C:
290+
def __matmul__(self, other):
291+
return 1729
292+
def __rmatmul__(self, other):
293+
return -163
294+
def __imatmul__(self, other):
295+
return 561
296+
o = C()
297+
p = weakref.proxy(o)
298+
self.assertEqual(p @ 5, 1729)
299+
self.assertEqual(5 @ p, -163)
300+
p @= 5
301+
self.assertEqual(p, 561)
302+
288303
# The PyWeakref_* C API is documented as allowing either NULL or
289304
# None as the value for the callback, where either means "no
290305
# callback". The "no callback" ref and proxy objects are supposed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add missing matrix multiplication operator support to weakref.proxy.

‎Objects/weakrefobject.c

+4
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ WRAP_BINARY(proxy_iand, PyNumber_InPlaceAnd)
525525
WRAP_BINARY(proxy_ixor, PyNumber_InPlaceXor)
526526
WRAP_BINARY(proxy_ior, PyNumber_InPlaceOr)
527527
WRAP_UNARY(proxy_index, PyNumber_Index)
528+
WRAP_BINARY(proxy_matmul, PyNumber_MatrixMultiply)
529+
WRAP_BINARY(proxy_imatmul, PyNumber_InPlaceMatrixMultiply)
528530

529531
static int
530532
proxy_bool(PyWeakReference *proxy)
@@ -642,6 +644,8 @@ static PyNumberMethods proxy_as_number = {
642644
proxy_ifloor_div, /*nb_inplace_floor_divide*/
643645
proxy_itrue_div, /*nb_inplace_true_divide*/
644646
proxy_index, /*nb_index*/
647+
proxy_matmul, /*nb_matrix_multiply*/
648+
proxy_imatmul, /*nb_inplace_matrix_multiply*/
645649
};
646650

647651
static PySequenceMethods proxy_as_sequence = {

0 commit comments

Comments
 (0)
Please sign in to comment.