[Django] #29412: django.utils.text.slugify / SafeString

7 views
Skip to first unread message

Django

unread,
May 17, 2018, 3:31:51 AM5/17/18
to django-...@googlegroups.com
#29412: django.utils.text.slugify / SafeString
-----------------------------------------+------------------------
Reporter: Andreas Pelme | Owner: nobody
Type: Uncategorized | Status: new
Component: Utilities | Version: 2.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
https://github.com/django/django/commit/ccfd1295f986cdf628d774937d0b38a14584721f
changed `SafeString.__str__` to return itself rather than a native string.

This works fine in most places. However, interning (`sys.intern`) a
`SafeString` does not work, it needs to be converted to a regular string
first.

I use slugify to generate file names and passing it on to pathlib.Path.
Path interns strings and this breaks:

{{{
>>> from django.utils.text import slugify
>>> import pathlib
>>> pathlib.Path(slugify('foo'))
Traceback (most recent call last):
File "<input>", line 1, in <module>
File
"/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pathlib.py",
line 999, in __new__
self = cls._from_parts(args, init=False)
File
"/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pathlib.py",
line 656, in _from_parts
drv, root, parts = self._parse_args(args)
File
"/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pathlib.py",
line 649, in _parse_args
return cls._flavour.parse_parts(parts)
File
"/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pathlib.py",
line 69, in parse_parts
parsed.append(sys.intern(rel))
TypeError: can't intern SafeText
>>> pathlib.Path(str(slugify('foo')))
Traceback (most recent call last):
File "<input>", line 1, in <module>
File
"/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pathlib.py",
line 999, in __new__
self = cls._from_parts(args, init=False)
File
"/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pathlib.py",
line 656, in _from_parts
drv, root, parts = self._parse_args(args)
File
"/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pathlib.py",
line 649, in _parse_args
return cls._flavour.parse_parts(parts)
File
"/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pathlib.py",
line 69, in parse_parts
parsed.append(sys.intern(rel))
TypeError: can't intern SafeText
>>> pathlib.Path(slugify('foo') + '') # workaround
PosixPath('foo')

}}}

The only way to work around this is to use some kind of string operation
that will turn it into a native string but that feels like a workaround.

Maybe the problem is not SafeString but rather that
`django.utils.text.slugify` that does return a SafeString instead of a
regular string? The builtin slugify filter could still return a safe
string?

--
Ticket URL: <https://code.djangoproject.com/ticket/29412>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 17, 2018, 5:49:01 AM5/17/18
to django-...@googlegroups.com
#29412: django.utils.text.slugify / SafeString
-------------------------------+------------------------------------

Reporter: Andreas Pelme | Owner: nobody
Type: Uncategorized | Status: new
Component: Utilities | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Claude Paroz):

* stage: Unreviewed => Accepted


Comment:

I would also question the usage of `mark_safe` in `slugify`. I think the
only backwards incompatibility would be that the result of adding a
`SafeString` to a `slugify()` value would not be safe. I don't think it's
much of an issue, if it's documented in release notes.

--
Ticket URL: <https://code.djangoproject.com/ticket/29412#comment:1>

Django

unread,
Jun 11, 2018, 11:05:12 PM6/11/18
to django-...@googlegroups.com
#29412: django.utils.text.slugify / SafeString
-------------------------------+----------------------------------------
Reporter: Andreas Pelme | Owner: Windson yang
Type: Uncategorized | Status: assigned
Component: Utilities | Version: 2.0

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+----------------------------------------
Changes (by Windson yang):

* owner: nobody => Windson yang
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/29412#comment:2>

Django

unread,
Jun 12, 2018, 3:18:24 AM6/12/18
to django-...@googlegroups.com
#29412: django.utils.text.slugify / SafeString
-------------------------------+----------------------------------------
Reporter: Andreas Pelme | Owner: Windson yang
Type: Bug | Status: assigned
Component: Utilities | Version: 2.0

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+----------------------------------------
Changes (by Claude Paroz):

* type: Uncategorized => Bug


--
Ticket URL: <https://code.djangoproject.com/ticket/29412#comment:3>

Django

unread,
Jul 14, 2018, 4:51:58 AM7/14/18
to django-...@googlegroups.com
#29412: django.utils.text.slugify / SafeString
-------------------------------+------------------------------------
Reporter: Andreas Pelme | Owner: (none)
Type: Bug | Status: new
Component: Utilities | Version: 2.0

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Claude Paroz):

* owner: Windson yang => (none)
* status: assigned => new
* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/10179 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/29412#comment:4>

Django

unread,
Jul 20, 2018, 11:05:38 AM7/20/18
to django-...@googlegroups.com
#29412: django.utils.text.slugify() shouldn't return a SafeString
-------------------------------------+-------------------------------------

Reporter: Andreas Pelme | Owner: (none)
Type: Bug | Status: new
Component: Utilities | Version: 2.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/29412#comment:5>

Django

unread,
Jul 20, 2018, 11:06:16 AM7/20/18
to django-...@googlegroups.com
#29412: django.utils.text.slugify() shouldn't return a SafeString
-------------------------------------+-------------------------------------
Reporter: Andreas Pelme | Owner: Tim
| Graham <timograham@…>
Type: Bug | Status: closed
Component: Utilities | Version: 2.0
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* owner: (none) => Tim Graham <timograham@…>
* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"b004bd62e83588fdb0e533fdeec993aec358281d" b004bd62]:
{{{
#!CommitTicketReference repository=""
revision="b004bd62e83588fdb0e533fdeec993aec358281d"
Fixed #29412 -- Stopped marking slugify() result as HTML safe.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29412#comment:6>

Reply all
Reply to author
Forward
0 new messages