Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#21316 closed Cleanup/optimization (fixed)

mark_safe documentation should note that strings will become unsafe if modified.

Reported by: dev@… Owned by: Tim Graham
Component: Documentation Version: 1.4
Severity: Normal Keywords: mark_safe
Cc: vijay_shanker Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

This took me a while to figure out what was happening and it would be good to have this documented somewhere.

In [1]: from django.utils.safestring import mark_safe

In [2]: s = "<blink>hello world</blink>    "

In [3]: type(s)
Out[3]: str

In [4]: t = mark_safe(s)

# t is now 'safe': 
In [5]: type(t)
Out[5]: django.utils.safestring.SafeBytes

# let's remove that unnecessary white space....
In [6]: type(t.strip())
Out[6]: str    # and we're not safe anymore.

Attachments (1)

mark_safe_21316.txt (1.5 KB ) - added by vijay_shanker 11 years ago.
added a sentence and a code example to how modified strings are not safe

Download all attachments as: .zip

Change History (8)

comment:1 by Tim Graham, 11 years ago

Component: Template systemDocumentation
Easy pickings: set
Triage Stage: UnreviewedAccepted

If you'd like to provide a patch, I'll be happy to review and commit it.

comment:2 by vijay_shanker, 11 years ago

Owner: changed from nobody to vijay_shanker
Status: newassigned

by vijay_shanker, 11 years ago

Attachment: mark_safe_21316.txt added

added a sentence and a code example to how modified strings are not safe

comment:3 by vijay_shanker, 11 years ago

Cc: vijay_shanker added

comment:4 by Tim Graham, 11 years ago

Has patch: set
Owner: changed from vijay_shanker to Tim Graham
Triage Stage: AcceptedReady for checkin

I will go ahead and commit this with the following fixes:

  • The patch introduces some undesired whitespace in random places. I suggest you set your edit to remove trailing whitespace.
  • The patch uses tabs for indentation; it should use spaces instead.

Tips:

  • Don't forget to check "Has patch" on the ticket so it shows up for review.
  • Use a diff or patch file extension rather than txt so that Trac does syntax highlighting when viewing the patch.

Thank-you for the contribution.

comment:5 by Tim Graham <timograham@…>, 11 years ago

Resolution: fixed
Status: assignedclosed

In 1edef50880f740e00fa1cd501c2ac56ec6fd10e4:

Fixed #21316 -- Documented that modifying safe strings makes them unsafe.

Thanks dev@… for the suggestion and vijay_shanker for the patch.

comment:6 by Tim Graham <timograham@…>, 11 years ago

In f832f0d2c309ac31bbf3616c34495c48509b0fb3:

[1.5.x] Fixed #21316 -- Documented that modifying safe strings makes them unsafe.

Thanks dev@… for the suggestion and vijay_shanker for the patch.

Backport of 1edef50880 from master

comment:7 by Tim Graham <timograham@…>, 11 years ago

In da0c7723fa71f7273d5bd917a6aecc4243232a4e:

[1.6.x] Fixed #21316 -- Documented that modifying safe strings makes them unsafe.

Thanks dev@… for the suggestion and vijay_shanker for the patch.

Backport of 1edef50880 from master

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