[3.13] gh-58211: Add tests for the __self__ attribute of builtins functions (GH-113575) (#132437)

gh-58211: Add tests for  the `__self__` attribute of builtins functions (GH-113575)

---------
(cherry picked from commit 891465fc7a)

Co-authored-by: Adorilson Bezerra <adorilson@gmail.com>
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>
This commit is contained in:
Miss Islington (bot) 2025-04-12 14:25:51 +02:00 committed by GitHub
parent 17b61394ee
commit 70c4bd63d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View 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()