Opened 18 months ago

Closed 18 months ago

Last modified 18 months ago

#21316 closed Cleanup/optimization (fixed)

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

Reported by: dev@… Owned by: timo
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 18 months 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 Changed 18 months ago by timo

  • Component changed from Template system to Documentation
  • Easy pickings set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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

comment:2 Changed 18 months ago by vijay_shanker

  • Owner changed from nobody to vijay_shanker
  • Status changed from new to assigned

Changed 18 months ago by vijay_shanker

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

comment:3 Changed 18 months ago by vijay_shanker

  • Cc vijay_shanker added

comment:4 Changed 18 months ago by timo

  • Has patch set
  • Owner changed from vijay_shanker to timo
  • Triage Stage changed from Accepted to Ready 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 Changed 18 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

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 Changed 18 months ago by Tim Graham <timograham@…>

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 Changed 18 months ago by Tim Graham <timograham@…>

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