Issue1730

classification
Title: functools.partial incorrectly makes __doc__ property readonly
Type: crash Severity: major
Components: Core Versions: 2.5.2rc
Milestone:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: alex.gronholm Nosy List: alex.gronholm, hyao, jeff250, smeatonj
Priority: Keywords: patch

Created on 2011-04-11.03:26:26 by smeatonj, last changed 2012-04-13.23:02:07 by alex.gronholm.

Files
File name Uploaded Description Edit Remove
partial-attribute-assignment.patch jeff250, 2012-04-01.03:30:17 Patch with proposed fix plus test
Messages
msg6477 (view) Author: Josh Smeaton (smeatonj) Date: 2011-04-11.03:36:23
Actual Jython version is: 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06)

Example (Jython):

(InteractiveConsole)
>>> from functools import partial
>>> def my_func(arg1):     
...     """my_func doc string"""
...     return "arg: %s" %arg1
... 
>>> my_func("hi")
'arg: hi'
>>> my_func.__doc__
'my_func doc string'
>>> partial
<type '_functools.partial'>
>>> partial_func = partial(my_func, 'hi')
>>> partial_func()
'arg: hi'
>>> partial_func.__doc__
>>> partial_func.__doc__ = my_func.__doc__
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: readonly attribute
>>> 

Same thing in Python 2.7:

Python 2.7.1 (r271:86882M, Nov 30 2010, 10:35:34) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from functools import partial
>>> def my_func(arg1):
...     """my_func doc string"""
...     return "arg: %s" % arg1
... 
>>> my_func("hi")
'arg: hi'
>>> my_func.__doc__
'my_func doc string'
>>> partial_func = partial(my_func, "hi")
>>> partial_func()
'arg: hi'
>>> partial_func.__doc__
'partial(func, *args, **keywords) - new function with partial application\n    of the given arguments and keywords.\n'
>>> partial_func.__doc__ = my_func.__doc__
>>> 

This was discovered using Django 1.3. For those familiar with Django, any template tags registered as a @register.simple_tag will encounter this issue. On further investigation, it will also break any include tags, which are common to the majority of django sites.

The documentation for Jython functools, http://www.jython.org/docs/library/functools.html#partial-objects , says that only the .func, .args, and .keywords are readonly.

The Python documentation for partial even has an example showing the modification of the __doc__ string.
msg6540 (view) Author: H Yao (hyao) Date: 2011-06-03.13:12:01
confirmed about readonly __doc__ attribute after applying functools.partial in jython 2.5.2.

And in you python 2.7 session, it doesn't show __doc__ is readonly; from your code snippet, it seems to be ok.

I tested on both python 2.6.5 and python 3.1.2, the above mentioned __doc__ attribute is not readonly.

hope it clarifies things a bit,
msg6966 (view) Author: Jeffrey Knockel (jeff250) Date: 2012-03-27.07:07:28
As the documentation says, "partial objects are like function objects in that they... can have attributes."  So aside from the three read-only attributes, partial objects should not just take __doc__ but any other attribute.

In cpython,

>>> from functools import partial
>>> basetwo = partial(int, base=2)
>>> basetwo.x = 123
>>> basetwo.x
123

But in jython,

>>> from functools import partial
>>> basetwo = partial(int, base=2)
>>> basetwo.x = 123
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '_functools.partial' object has no attribute 'x'
msg7008 (view) Author: Jeffrey Knockel (jeff250) Date: 2012-04-01.03:30:17
Attaching a patch with proposed fix plus test.  Confirmed there are no created regrtest failures.  Feedback welcome. :)
msg7050 (view) Author: Alex Grönholm (alex.gronholm) Date: 2012-04-13.23:02:06
Fixed in 2.5.3b2
History
Date User Action Args
2012-04-13 23:02:07alex.gronholmsetstatus: open -> closed
assignee: alex.gronholm
resolution: fixed
messages: + msg7050
nosy: + alex.gronholm
2012-04-01 03:30:18jeff250setfiles: + partial-attribute-assignment.patch
keywords: + patch
messages: + msg7008
2012-03-27 07:07:28jeff250setnosy: + jeff250
messages: + msg6966
2011-06-03 13:12:01hyaosetnosy: + hyao
messages: + msg6540
2011-04-11 03:36:24smeatonjsetmessages: + msg6477
2011-04-11 03:26:26smeatonjcreate