]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-58211: Add tests for the `__self__` attribute of builtins functions (#113575)
authorAdorilson Bezerra <adorilson@gmail.com>
Sat, 12 Apr 2025 12:02:43 +0000 (13:02 +0100)
committerGitHub <noreply@github.com>
Sat, 12 Apr 2025 12:02:43 +0000 (12:02 +0000)
---------

Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/test_funcattrs.py

index d919d62613ea7cee69bcf590857580525ee7fae9..375f456dfde8346c6a28e74e1881e82813e2dd27 100644 (file)
@@ -3,6 +3,7 @@ import types
 import typing
 import unittest
 import warnings
+from test import support
 
 
 def global_function():
@@ -478,6 +479,33 @@ class BuiltinFunctionPropertiesTest(unittest.TestCase):
         self.assertEqual([1, 2, 3].append.__qualname__, 'list.append')
         self.assertEqual({'foo': 'bar'}.pop.__qualname__, 'dict.pop')
 
+    @support.cpython_only
+    def test_builtin__self__(self):
+        # See https://github.com/python/cpython/issues/58211.
+        import builtins
+        import time
+
+        # builtin function:
+        self.assertIs(len.__self__, builtins)
+        self.assertIs(time.sleep.__self__, time)
+
+        # builtin classmethod:
+        self.assertIs(dict.fromkeys.__self__, dict)
+        self.assertIs(float.__getformat__.__self__, float)
+
+        # builtin staticmethod:
+        self.assertIsNone(str.maketrans.__self__)
+        self.assertIsNone(bytes.maketrans.__self__)
+
+        # builtin bound instance method:
+        l = [1, 2, 3]
+        self.assertIs(l.append.__self__, l)
+
+        d = {'foo': 'bar'}
+        self.assertEqual(d.pop.__self__, d)
+
+        self.assertIsNone(None.__repr__.__self__)
+
 
 if __name__ == "__main__":
     unittest.main()