Skip to content

bpo-36669: Add matmul support for weakref.proxy #12932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/weakref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---

Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_weakref.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,21 @@ def __ifloordiv__(self, other):
p //= 5
self.assertEqual(p, 21)

def test_proxy_matmul(self):
class C:
def __matmul__(self, other):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a test for __rmatmul__ as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good plan. Done!

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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add missing matrix multiplication operator support to weakref.proxy.
4 changes: 4 additions & 0 deletions Objects/weakrefobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = {
Expand Down