Files
RustPython/extra_tests/snippets/syntax_function2.py
Alok Pandey f90a5cf650 Fix del obj.__dict__ to match CPython behavior (fixes #5355) (#7568)
* Fix del obj.__dict__ to match CPython behavior (issue #5355)

* Address CodeRabbit concerns: fix GC clearing and improve thread safety of lazy __dict__ recreation

* Fix del obj.__dict__: improve GC safety, implement lazy re-creation in setattr, and enable passing CPython tests

* Restore expectedFailure for test_has_inline_values

* Fix ObjExt::new call site to include has_dict parameter

* Remove stray test.py to avoid CI syntax errors

* Remove debug txt files and clean test_class.py comments

* Delete Lib/test/test_class.py

* Restore test_class.py with correct changes (remove expectedFailure, no deletion)

* Fix clippy warnings: remove unused into_inner, collapse nested if-let

* Fix rustfmt formatting and ruff PEP 8 E302 blank line

* Align __dict__ error messages and ensure safety for function/partial objects

* Fix compilation errors: change &self to &Py<Self> in __dict__ methods

* Fix compilation errors: resolve borrow-after-move and replace transpose on PySetterValue

- type.rs: Replace invalid .transpose() on PySetterValue with explicit
  match on Assign/Delete variants in subtype_set_dict
- function.rs: Fix borrow-after-move in set___dict__ by capturing class
  name before downcast; use as_object() for instance_dict/set_dict calls
- _functools.rs: Same borrow-after-move fix and as_object() calls for
  PyPartial's __dict__ getter/setter

* Fix compilation errors: resolve borrow-after-move and replace transpose on PySetterValue

* Fix snippet formatting and mark test_remote as expected failure

* Fix test_remote by removing HAS_DICT flag from function type

* Fix lint formatting error

* Remove unnecessary print statement in test_del_dict

* Fix trailing newlines in snippet test

* Trigger CI

* Align __dict__ generic setter behavior

* Move __dict__ deletion tests to relevant snippets

---------

Co-authored-by: Jeong, YunWon <jeong@youknowone.org>
2026-04-30 02:09:16 +00:00

113 lines
1.6 KiB
Python

from testutils import assert_raises
__name__ = "function"
def foo():
"""test"""
return 42
assert foo() == 42
assert foo.__doc__ == "test"
assert foo.__name__ == "foo"
assert foo.__qualname__ == "foo"
assert foo.__module__ == "function"
assert foo.__globals__ is globals()
def my_func(a,):
return a+2
assert my_func(2) == 4
def fubar():
return 42,
assert fubar() == (42,)
def f1():
"""test1"""
pass
assert f1.__doc__ == "test1"
def f2():
'''test2'''
pass
assert f2.__doc__ == "test2"
def f3():
"""
test3
"""
pass
assert f3.__doc__ == "\ntest3\n"
def f4():
"test4"
pass
assert f4.__doc__ == "test4"
assert type(lambda: None).__doc__.startswith("Create a function object."), type(f4).__doc__
def revdocstr(f):
d = f.__doc__
d = d + 'w00t'
f.__doc__ = d
return f
@revdocstr
def f5():
"""abc"""
assert f5.__doc__ == 'abcw00t', f5.__doc__
def f6():
def nested():
pass
assert nested.__name__ == "nested"
assert nested.__qualname__ == "f6.<locals>.nested"
f6()
def f7():
# PEP 649: annotations are deferred, so void is not evaluated at definition time
try:
def t() -> void: # noqa: F821
pass
except NameError:
return True
return False
assert not f7() # PEP 649: no NameError because annotation is deferred
def f8() -> int:
return 10
assert f8() == 10
with assert_raises(SyntaxError):
exec('print(keyword=10, 20)')
def f9():
pass
assert f9.__doc__ == None
try:
del f9.__dict__
assert False, "TypeError expected for function dict deletion"
except TypeError:
pass