mirror of
https://github.com/python/cpython.git
synced 2025-07-30 06:34:15 +00:00
Fix contextlib not copying function attributes
This commit is contained in:
parent
d207b4f376
commit
35fd142435
2 changed files with 16 additions and 0 deletions
|
@ -78,6 +78,7 @@ def contextmanager(func):
|
||||||
try:
|
try:
|
||||||
helper.__name__ = func.__name__
|
helper.__name__ = func.__name__
|
||||||
helper.__doc__ = func.__doc__
|
helper.__doc__ = func.__doc__
|
||||||
|
helper.__dict__ = func.__dict__
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
return helper
|
return helper
|
||||||
|
|
|
@ -84,6 +84,21 @@ class ContextManagerTestCase(unittest.TestCase):
|
||||||
raise ZeroDivisionError(999)
|
raise ZeroDivisionError(999)
|
||||||
self.assertEqual(state, [1, 42, 999])
|
self.assertEqual(state, [1, 42, 999])
|
||||||
|
|
||||||
|
def test_contextmanager_attribs(self):
|
||||||
|
def attribs(**kw):
|
||||||
|
def decorate(func):
|
||||||
|
for k,v in kw.items():
|
||||||
|
setattr(func,k,v)
|
||||||
|
return func
|
||||||
|
return decorate
|
||||||
|
@contextmanager
|
||||||
|
@attribs(foo='bar')
|
||||||
|
def baz(spam):
|
||||||
|
"""Whee!"""
|
||||||
|
self.assertEqual(baz.__name__,'baz')
|
||||||
|
self.assertEqual(baz.foo, 'bar')
|
||||||
|
self.assertEqual(baz.__doc__, "Whee!")
|
||||||
|
|
||||||
class NestedTestCase(unittest.TestCase):
|
class NestedTestCase(unittest.TestCase):
|
||||||
|
|
||||||
# XXX This needs more work
|
# XXX This needs more work
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue