[Python-checkins] bpo-36922: use Py_TPFLAGS_METHOD_DESCRIPTOR in lookup_maybe_method() (GH-13865)

Miss Islington (bot) webhook-mailer at python.org
Mon Jun 17 08:12:56 EDT 2019


https://github.com/python/cpython/commit/988fff5d0e7fccecbf776c08ec56695820b3b4a8
commit: 988fff5d0e7fccecbf776c08ec56695820b3b4a8
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-06-17T05:12:42-07:00
summary:

bpo-36922: use Py_TPFLAGS_METHOD_DESCRIPTOR in lookup_maybe_method() (GH-13865)

(cherry picked from commit 2e9954d3472a23919b96323fcd5bb6c1d6927155)

Co-authored-by: Jeroen Demeyer <J.Demeyer at UGent.be>

files:
A Misc/NEWS.d/next/Core and Builtins/2019-06-06-13-59-52.bpo-36922.EMZ3TF.rst
M Lib/test/test_descr.py
M Objects/typeobject.c

diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index 6b018ccc56fa..9998a3cc77a2 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -4647,9 +4647,11 @@ class Foo:
     def test_mixing_slot_wrappers(self):
         class X(dict):
             __setattr__ = dict.__setitem__
+            __neg__ = dict.copy
         x = X()
         x.y = 42
         self.assertEqual(x["y"], 42)
+        self.assertEqual(x, -x)
 
     def test_slot_shadows_class_variable(self):
         with self.assertRaises(ValueError) as cm:
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-06-06-13-59-52.bpo-36922.EMZ3TF.rst b/Misc/NEWS.d/next/Core and Builtins/2019-06-06-13-59-52.bpo-36922.EMZ3TF.rst
new file mode 100644
index 000000000000..c2a2ad4e8e73
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-06-06-13-59-52.bpo-36922.EMZ3TF.rst	
@@ -0,0 +1 @@
+Slot functions optimize any callable with ``Py_TPFLAGS_METHOD_DESCRIPTOR`` instead of only instances of ``function``.
\ No newline at end of file
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 006df8d1f090..01e95aea61af 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -1412,7 +1412,7 @@ lookup_maybe_method(PyObject *self, _Py_Identifier *attrid, int *unbound)
         return NULL;
     }
 
-    if (PyFunction_Check(res)) {
+    if (PyType_HasFeature(Py_TYPE(res), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
         /* Avoid temporary PyMethodObject */
         *unbound = 1;
         Py_INCREF(res);



More information about the Python-checkins mailing list