﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
21733	@python_2_unicode_compatible causes infinite recursion when module imported more than once	ntucker	nobody	"Sometimes a module that uses @python_2_unicode_compatible is imported multiple times. Possibly because a different pathname is used to import it. When this occurs, it makes __unicode__ be the __str__ function that was originally assigned, which calls __unicode__. So anytime __unicode__ is called on the object, there is infinite recursion.

A simple check whether the __str__ function has already been assigned will prevent setting __unicode__ to the provided recursive function. This will also guard against other cases of multiple @python_2_unicode_compatible calls.



{{{
class _STR(object):
    def __call__(self, obj):
        return self.__unicode__().encode('utf-8')
_str = _STR()
def python_2_unicode_compatible(klass):
    """"""
A decorator that defines __unicode__ and __str__ methods under Python 2.
Under Python 3 it does nothing.

To support Python 2 and 3 with a single code base, define a __str__ method
returning text and apply this decorator to the class.
""""""
    if six.PY2:
        if '__str__' not in klass.__dict__:
            raise ValueError(""@python_2_unicode_compatible cannot be applied ""
                             ""to %s because it doesn't define __str__()."" %
                             klass.__name__)
        # this was already applied once
        if klass.__str__ == _str:
            return klass
        klass.__unicode__ = klass.__str__
        klass.__str__ = _str
    return klass
}}}


I will provide patch if this is accepted. Above is example code."	Bug	closed	Utilities	1.6	Normal	invalid			Unreviewed	1	0	0	0	0	0
