Q:

length of SafeUnicode inside django template

I noticed that if you pass a variable called some_copy containing a unicode string into a Django template:
some_copy <type 'unicode'>
some_copy|length <type 'int'> 
some_copy|safe <class 'django.utils.safestring.SafeUnicode'>
some_copy|safe|length <class 'django.utils.safestring.SafeString'>  

I noticed that the length filter is just calling len() on the input, so tested importing django.utils.safestring.SafeUnicode on a terminal and it seems to just return <type 'int'> when you call len().  Is there something strange here about how the safe filter interacts with other filters?  I know there are notes suggesting not to use other filters after safe as they could render the output less safe.  Also, this code was producing a bug and no longer contains the calls to safe|length, but I was curious what was going on.

A:

From the source code:
@register.filter(is_safe=True)
def length(value):
    """Returns the length of the value - useful for lists."""
    try:
        return len(value)
    except (ValueError, TypeError):
        return ''

The @register.filter(is_safe=True) tells Django to convert the output of the length function to a SaveData subclass instance by calling mark_safe. In the case of an integer input, mark_safe returns a SafeString instance. 

