Opened 12 years ago

Closed 10 years 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

Description

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@… 12 years ago.
jython_format_html_fix_v2.diff (754 bytes ) - added by andreas@… 12 years ago.
jython_lazy_fix.diff (519 bytes ) - added by andreas@… 12 years ago.

Download all attachments as: .zip

Change History (13)

by andreas@…, 12 years ago

Attachment: jython_format_html_fix.diff added

comment:1 by anonymous, 12 years ago

Type: UncategorizedBug

comment:2 by Florian Apolloner, 12 years ago

Cc: Florian Apolloner added

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

comment:3 by andreas@…, 12 years ago

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 by Aymeric Augustin, 12 years ago

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.

by andreas@…, 12 years ago

comment:5 by andreas@…, 12 years ago

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.

by andreas@…, 12 years ago

Attachment: jython_lazy_fix.diff added

comment:6 by andreas@…, 12 years ago

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 by Baptiste Mispelon, 12 years ago

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?

Thanks.

[1] https://github.com/django/django/pull/1007

comment:8 by Baptiste Mispelon, 12 years ago

Cc: bmispelon@… added

comment:9 by andreas@…, 12 years ago

Unfortunately your patch does not solve the problem with jython.

comment:10 by Tim Graham, 10 years ago

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