[Django] #33525: decorator @html_safe removes completely old class.__str__ method.

26 views
Skip to first unread message

Django

unread,
Feb 18, 2022, 9:13:25 AM2/18/22
to django-...@googlegroups.com
#33525: decorator @html_safe removes completely old class.__str__ method.
-------------------------------------+-------------------------------------
Reporter: Maxim | Owner: nobody
Danilov |
Type: | Status: new
Cleanup/optimization |
Component: Utilities | Version: 4.0
Severity: Normal | Keywords: html_string,
Triage Stage: | SafeString
Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
I can not to get normal __str__ method from safe_string.

Fro example:


{{{
@html_safe
class TranslatableText(str):
"""Class like a string but returns values depends on get_language."""
}}}

After that str(TranslatableText) is always SafeString. If i want call old
__str__ method it is not possible: html_safe destroy it in
django\utils\html.py, rows 376-378
{{{
klass_str = klass.__str__
klass.__str__ = lambda self: mark_safe(klass_str(self))
klass.__html__ = lambda self: str(self)
return klass
}}}
But it is easy to repair:
{{{
klass._old_str_method = klass.__str__
klass.__str__ = lambda self: mark_safe(klass._old_str_method(self))
klass.__html__ = lambda self: str(self)
return klass
}}}
After that, not only can i get SafeString object, it is also possible to
call klass._old_str_method and get BaseString object

--
Ticket URL: <https://code.djangoproject.com/ticket/33525>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 18, 2022, 1:13:37 PM2/18/22
to django-...@googlegroups.com
#33525: decorator @html_safe removes completely old class.__str__ method.
-------------------------------------+-------------------------------------
Reporter: Maxim Danilov | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: Utilities | Version: 4.0
Severity: Normal | Resolution: wontfix

Keywords: html_string, | Triage Stage:
SafeString | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => wontfix


Comment:

Thanks for this ticket, however accessing and old `__str__()` method after
`@html_safe` sounds really niche. You can always create your own decorator
for this.

--
Ticket URL: <https://code.djangoproject.com/ticket/33525#comment:1>

Reply all
Reply to author
Forward
0 new messages