#21733: @python_2_unicode_compatible causes infinite recursion when module
imported
more than once
---------------------------+--------------------------------------
Reporter: ntucker | Owner: nobody
Type: Bug | Status: new
Component: Utilities | Version: 1.6
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------+--------------------------------------
Changes (by aaugustin):
* keywords: python2.7.3 =>
* easy: 1 => 0
Comment:
'''tl;dr'''
Don't import modules twice.
Django doesn't require putting a directory and a subdirectory on
PYTHONPATH since 1.4 and will most likely actively prevent it (i.e. refuse
to start) in future versions.
----
'''explanation'''
I assume the problem only happens on Django model classes. The root cause
isn't obvious and doesn't lie in python_2_unicode_compatible.
The second import should create a new class and apply the proper
transformations to its methods. Unfortunately, the devious
`ModelBase.__new__` metaclass gets in the way and returns the existing
class, to which the decorator has already been applied. So it gets applied
a second time and that breaks.
As part of the app-loading refactor, I plan to forbid importing models
twice to remove that, err, interesting behavior. This is tracked in #21711
and will make it impossible to trigger this issue.
----
That said, mistakes such as applying the decorator to a subclass that
doesn't override `__str__` also trigger an infinite recursion. I'd like to
raise a specific exception when the decorator is applied twice to the same
methods (ie. twice to the same class, or to a class and a subclass when no
subclass has overridden `__str__` in the mean time).
The appropriate way to achive this is to set an attribute on the
`__unicode__` method when it has been created by the decorator, and to
test for that attribute and raise an exception if it's set.
--
Ticket URL: <https://code.djangoproject.com/ticket/21733#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.a2229f4f04b161be6fddd21f1bf16c21%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.