[Python-checkins] r43367 - in python/trunk/Lib: contextlib.py test/test_contextlib.py

Nick Coghlan ncoghlan at gmail.com
Tue Mar 28 11:59:46 CEST 2006


phillip.eby wrote:
> Author: phillip.eby
> Date: Tue Mar 28 02:07:24 2006
> New Revision: 43367
> 
> Modified:
>    python/trunk/Lib/contextlib.py
>    python/trunk/Lib/test/test_contextlib.py
> Log:
> Fix contextlib not copying function attributes
> 
> 
> Modified: python/trunk/Lib/contextlib.py
> ==============================================================================
> --- python/trunk/Lib/contextlib.py	(original)
> +++ python/trunk/Lib/contextlib.py	Tue Mar 28 02:07:24 2006
> @@ -78,6 +78,7 @@
>      try:
>          helper.__name__ = func.__name__
>          helper.__doc__ = func.__doc__
> +        helper.__dict__ = func.__dict__
>      except:
>          pass
>      return helper

I realise it isn't likely to be an issue for contextmanager in particular, but 
I believe it's generally better to avoid referencing the original dict 
directly and instead do:
         helper.__dict__.update(func.__dict__)

The checked in version may do strange things if the decorator doesn't hold the 
sole reference to func (i.e., it's being applied as a function, rather than 
using decorator syntax).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-checkins mailing list