Next / Previous / Contents / Shipman's homepage

14. Sox._unicodify(): Force to Unicode

sox.py
# - - -   S o x . _ u n i c o d i f y

    def _unicodify(self, s):
        '''Insure that s is unicode.

          [ type(s) is unicode or str ->
              if type(s) is unicode ->
                return s
              else if s is unicode-okay ->
                return s as unicode
              else -> raise SoxError ]
        '''
        #-- 1
        if type(s) is unicode:
            return s

        #-- 2
        try:
            u = unicode(s, DEFAULT_ENCODING)
        except UnicodeDecodeError:

            raise SoxError("String %s cannot be converted to "
                "Unicode." % s)
            
        #-- 3
        return u