[go: up one dir, main page]

File: encoding.py

package info (click to toggle)
comix 4.0.4-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 2,088 kB
  • sloc: python: 5,996; makefile: 40; xml: 20
file content (21 lines) | stat: -rw-r--r-- 617 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""encoding.py - Encoding handler."""

import sys

_filesystemencoding = sys.getfilesystemencoding()


def to_unicode(string):
    """Convert <string> to unicode. First try the default filesystem
    encoding, and then fall back on some common encodings. If none
    of the convertions are successful, "???" is returned.
    """
    if isinstance(string, unicode):
        return string
    for encoding in (_filesystemencoding, 'utf-8', 'latin-1'):
        try:
            ustring = unicode(string, encoding)
            return ustring
        except (UnicodeError, LookupError):
            pass
    return u'???'