Opened 2 years ago

Closed 8 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: apollo13, 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@… 2 years ago.
jython_format_html_fix_v2.diff (754 bytes) - added by andreas@… 2 years ago.
jython_lazy_fix.diff (519 bytes) - added by andreas@… 2 years ago.

Download all attachments as: .zip

Change History (13)

Changed 2 years ago by andreas@…

comment:1 Changed 2 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Type changed from Uncategorized to Bug

comment:2 Changed 2 years ago by apollo13

  • Cc apollo13 added

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

comment:3 Changed 2 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 2 years ago by aaugustin

  • Needs tests set
  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Accepted

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 2 years ago by andreas@…

comment:5 Changed 2 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 2 years ago by andreas@…

comment:6 Changed 2 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 2 years ago by bmispelon

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 Changed 2 years ago by bmispelon

  • Cc bmispelon@… added

comment:9 Changed 2 years ago by andreas@…

Unfortunately your patch does not solve the problem with jython.

comment:10 Changed 8 months ago by timgraham

  • Resolution set to needsinfo
  • Status changed from new to closed

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