From: Adorilson Bezerra Date: Sat, 12 Apr 2025 12:02:43 +0000 (+0100) Subject: gh-58211: Add tests for the `__self__` attribute of builtins functions (#113575) X-Git-Tag: v3.14.0b1~515 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=891465fc7a6cf096d5d58db70532e2f3809b1c24;p=thirdparty%2FPython%2Fcpython.git gh-58211: Add tests for the `__self__` attribute of builtins functions (#113575) --------- Co-authored-by: Victor Stinner Co-authored-by: Alex Waygood Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Sergey B Kirpichev Co-authored-by: Serhiy Storchaka --- diff --git a/Lib/test/test_funcattrs.py b/Lib/test/test_funcattrs.py index d919d62613ea..375f456dfde8 100644 --- a/Lib/test/test_funcattrs.py +++ b/Lib/test/test_funcattrs.py @@ -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()