From 87cf6848a5ee334f0888ad4a037d40e034be134f Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Thu, 12 Feb 2026 18:01:09 +0200 Subject: [PATCH] Update `test_funcattrs.py` from 3.14.3 --- Lib/test/test_funcattrs.py | 93 +++++++++++++++++++++++++++++++++----- 1 file changed, 81 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_funcattrs.py b/Lib/test/test_funcattrs.py index 0370ce8d9..f24521341 100644 --- a/Lib/test/test_funcattrs.py +++ b/Lib/test/test_funcattrs.py @@ -1,6 +1,9 @@ import textwrap import types +import typing import unittest +import warnings +from test import support def global_function(): @@ -47,8 +50,7 @@ class FunctionPropertiesTest(FuncAttrsTest): def test_module(self): self.assertEqual(self.b.__module__, __name__) - # TODO: RUSTPYTHON - @unittest.expectedFailure + @unittest.expectedFailure # TODO: RUSTPYTHON def test_dir_includes_correct_attrs(self): self.b.known_attr = 7 self.assertIn('known_attr', dir(self.b), @@ -71,13 +73,40 @@ class FunctionPropertiesTest(FuncAttrsTest): test.__code__ = self.b.__code__ self.assertEqual(test(), 3) # self.b always returns 3, arbitrarily + @unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: DeprecationWarning not triggered + def test_invalid___code___assignment(self): + def A(): pass + def B(): yield + async def C(): yield + async def D(x): await x + + for src in [A, B, C, D]: + for dst in [A, B, C, D]: + if src == dst: + continue + + assert src.__code__.co_flags != dst.__code__.co_flags + prev = dst.__code__ + try: + with self.assertWarnsRegex(DeprecationWarning, 'code object of non-matching type'): + dst.__code__ = src.__code__ + finally: + with warnings.catch_warnings(): + warnings.filterwarnings('ignore', '', DeprecationWarning) + dst.__code__ = prev + def test___globals__(self): self.assertIs(self.b.__globals__, globals()) self.cannot_set_attr(self.b, '__globals__', 2, (AttributeError, TypeError)) def test___builtins__(self): - self.assertIs(self.b.__builtins__, __builtins__) + if __name__ == "__main__": + builtins_dict = __builtins__.__dict__ + else: + builtins_dict = __builtins__ + + self.assertIs(self.b.__builtins__, builtins_dict) self.cannot_set_attr(self.b, '__builtins__', 2, (AttributeError, TypeError)) @@ -87,7 +116,7 @@ class FunctionPropertiesTest(FuncAttrsTest): ns = {} func2 = type(func)(func.__code__, ns) self.assertIs(func2.__globals__, ns) - self.assertIs(func2.__builtins__, __builtins__) + self.assertIs(func2.__builtins__, builtins_dict) # Make sure that the function actually works. self.assertEqual(func2("abc"), 3) @@ -192,8 +221,24 @@ class FunctionPropertiesTest(FuncAttrsTest): # __qualname__ must be a string self.cannot_set_attr(self.b, '__qualname__', 7, TypeError) - # TODO: RUSTPYTHON - @unittest.expectedFailure + def test___type_params__(self): + def generic[T](): pass + def not_generic(): pass + lambda_ = lambda: ... + T, = generic.__type_params__ + self.assertIsInstance(T, typing.TypeVar) + self.assertEqual(generic.__type_params__, (T,)) + for func in (not_generic, lambda_): + with self.subTest(func=func): + self.assertEqual(func.__type_params__, ()) + with self.assertRaises(TypeError): + del func.__type_params__ + with self.assertRaises(TypeError): + func.__type_params__ = 42 + func.__type_params__ = (T,) + self.assertEqual(func.__type_params__, (T,)) + + @unittest.expectedFailure # TODO: RUSTPYTHON def test___code__(self): num_one, num_two = 7, 8 def a(): pass @@ -224,15 +269,13 @@ class FunctionPropertiesTest(FuncAttrsTest): self.fail("__code__ with different numbers of free vars should " "not be possible") - # TODO: RUSTPYTHON - @unittest.expectedFailure + @unittest.expectedFailure # TODO: RUSTPYTHON def test_blank_func_defaults(self): self.assertEqual(self.b.__defaults__, None) del self.b.__defaults__ self.assertEqual(self.b.__defaults__, None) - # TODO: RUSTPYTHON - @unittest.expectedFailure + @unittest.expectedFailure # TODO: RUSTPYTHON def test_func_default_args(self): def first_func(a, b): return a+b @@ -393,8 +436,7 @@ def empty_cell(empty=True): class CellTest(unittest.TestCase): - # TODO: RUSTPYTHON - @unittest.expectedFailure + @unittest.expectedFailure # TODO: RUSTPYTHON def test_comparison(self): # These tests are here simply to exercise the comparison code; # their presence should not be interpreted as providing any @@ -443,6 +485,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()