[pypy-issue] [issue1293] Assigning to func_code changes function docstring

PJ Eby tracker at bugs.pypy.org
Thu Oct 18 01:04:34 CEST 2012


New submission from PJ Eby <pje at telecommunity.com>:

In CPython, assigning to func_code does not change the
function's docstring:

$ py27
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def f1(x,*y,**z): "f1"
...
>>> def f2(x,*y,**z): "f2"
...
>>> f2.func_code = f1.func_code
>>> print f2.__doc__
f2

But in PyPy, it does:

$ pypy
Python 2.7.2 (341e1e3821ff, Jun 07 2012, 15:43:00)
[PyPy 1.9.0 with MSC v.1500 32 bit] on win32
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``Python 2.x is not dead''
>>>> def f1(x,*y,**z): "f1"
>>>> def f2(x,*y,**z): "f2"
>>>> f2.func_code = f1.func_code
>>>> print f2.__doc__
f1

This appears to be due to an optimization where function
objects' doc slot is lazily initialized; assigning to
func_code should ensure this initialization occurs before
actually changing the code.  (Probably just by calling
the fget_func_doc() method.)

----------
messages: 4859
nosy: pje, pypy-issue
priority: bug
status: unread
title: Assigning to func_code changes function docstring

________________________________________
PyPy bug tracker <tracker at bugs.pypy.org>
<https://bugs.pypy.org/issue1293>
________________________________________


More information about the pypy-issue mailing list