Opened 4 years ago

Closed 23 months ago

#20017 closed Bug (needsinfo)

format_html not working with lazy objects on jython

Reported by: andreas@… Owned by: nobody
Component: Python 2 Version: 1.5
Severity: Normal Keywords:
Cc: Florian Apolloner, bmispelon@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no


django.utils.html.format_html does not convert lazy string objects to unicode on jython. E.g. the hidden csrf_token field looks like follows:

<input type="hidden" name="csrfmiddlewaretoken" value="&lt;django.utils.functional.__proxy__ object at 0x1c7&gt;">

I attached a patch that fixes this bug.

Attachments (3)

jython_format_html_fix.diff (759 bytes) - added by andreas@… 4 years ago.
jython_format_html_fix_v2.diff (754 bytes) - added by andreas@… 4 years ago.
jython_lazy_fix.diff (519 bytes) - added by andreas@… 4 years ago.

Download all attachments as: .zip

Change History (13)

Changed 4 years ago by andreas@…

Attachment: jython_format_html_fix.diff added

comment:1 Changed 4 years ago by anonymous

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Type: UncategorizedBug

comment:2 Changed 4 years ago by Florian Apolloner

Cc: Florian Apolloner added

Is that only an issue on jython or on python too?

comment:3 Changed 4 years ago by andreas@…

As far as I can see, it is only an issue on jython. I guess there is a difference in the behaviour of the string.format method. However, '%s' place holders are working fine with lazy string objects.

comment:4 Changed 4 years ago by Aymeric Augustin

Needs tests: set
Patch needs improvement: set
Triage Stage: UnreviewedAccepted

We should review carefully what the expected type and what the actual type are (str or unicode?), and make sure this is the correct level to fix the problem. If we're indeed relying on an implicit cast, and that trips up Jython, we should make it explicit. (str is unicode-based on Jython and that could explain the problem.)

I would also like to see a test that fails on CPython; without such a test we could easily reintroduce this problem in the future.

Minor comment on the patch: args_safe should use a list comprehension rather than map/lambda.

Changed 4 years ago by andreas@…

comment:5 Changed 4 years ago by andreas@…

I attached a new version of the patch that is using a list comprehension instead of a map+lambda expression. Unfortunately I was not able to write a test that reproduces the Jython behaviour on CPython.

Changed 4 years ago by andreas@…

Attachment: jython_lazy_fix.diff added

comment:6 Changed 4 years ago by andreas@…

I was able to fix the problem on a lower level, by assigning the __text_cast method not only on the __unicode__ method of the Promise class, but also on the __str__ method. But I am not sure if that is the right thing to do.

comment:7 Changed 4 years ago by Baptiste Mispelon

This looks like it might be related to #20221.

Can you try applying the patch I made for it [1] and telling me if the problem is still there?



comment:8 Changed 4 years ago by Baptiste Mispelon

Cc: bmispelon@… added

comment:9 Changed 3 years ago by andreas@…

Unfortunately your patch does not solve the problem with jython.

comment:10 Changed 23 months ago by Tim Graham

Resolution: needsinfo
Status: newclosed

django-jython has been updated to work with Django 1.7. I'd think we would have heard about this issue if it were still a problem. Please reopen with more details if it can still be reproduced.

Note: See TracTickets for help on using tickets.
Back to Top