[patch] deprecation warnings branch

Robert Collins robertc at robertcollins.net
Fri Jan 6 01:50:36 GMT 2006


On Thu, 2006-01-05 at 19:06 -0600, John Arbash Meinel wrote:
> Robert Collins wrote:
> > On Thu, 2006-01-05 at 15:56 -0600, John Arbash Meinel wrote:
> > 
> >>So how does staticmethod and classmethod do it? Do they cheat because
> >>they are C code? Are there any other standard decorators that we could
> >>try and figure out the correct actions?
> > 
> > 
> > I'm not sure, but thats quite possible. If you see one, tell me ;).
> > 
> 
> Well, looking here:
> http://www.python.org/peps/pep-0318.html
> 
> It looks like if you are decorating a function you use 'func.func_name'
> to set the name. I'm not sure how this differs from func.__name__
> (This is most of the way down @ 'Enforce function argument and return
> types')

Yup. I used __name__ because that is consistent with class, function and
method objects.

> 
> Digging into it a little bit more, I found the useful parameters:
> func.func_code.co_argcount
> func.func_code.co_varnames
> func.func_defaults
> 
> However, trying to set any of them seems to give me 'TypeError: readonly
> attribute'.

Yes, this is the code object for the function. In methods its
func.im_func.func_code. 

> You can at least do:
> === modified file 'bzrlib/symbol_versioning.py'
> --- bzrlib/symbol_versioning.py
> +++ bzrlib/symbol_versioning.py
> @@ -111,3 +111,4 @@
>      _decorate_docstring(callable, deprecation_version, label,
>                          decorated_callable)
>      decorated_callable.__name__ = callable.__name__
> +    decorated_callable.__module__ = callable.__module__
> 
> Which fixes up the module attribute.

Did that in my branch yesterday :).

> Reading inspect.py, it seems like you do have to get at
> func_code.co_varnames to fix these things up. Which I assume isn't
> possible because the python interpreter needs them itself.
> 
> So I'm guessing that the only way we could reproduce it properly would
> be to use some sort of dynamically generated string, which then uses
> eval() to create the object. I always avoid using eval() myself.

Yes, AFAICT we have to replace the code object - that is dynamically
write the decorator method to have the right signature.

There is a much lower tech solution - format the method into the
docstring as the first line or some such.

Rob


-- 
GPG key available at: <http://www.robertcollins.net/keys.txt>.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20060106/36b6e724/attachment.pgp 


More information about the bazaar mailing list