#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/048.1cd96bbd57e860ebcd8608aabd5b35b9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to