Gregg Lind avatar Gregg Lind committed d538d84

py2 and py3 compliant utf8 util function

in Py3, this is vestigial and maintained for compatability.

Comments (0)

Files changed (1)

 from fnmatch import fnmatchcase
+import sys
+PYTHON3 = sys.version_info > (3,0,0)
 class DictMixin(object):
     """Implement the dict API using keys() and __*item__ methods.
         if locked:
-def utf8(data):
+def _utf8_python2(data):
     """Convert a basestring to a valid UTF-8 str."""
     if isinstance(data, str):
         return data.decode("utf-8", "replace").encode("utf-8")
         return data.encode("utf-8")
     else: raise TypeError("only unicode/str types can be converted to UTF-8")
+def _utf8_python3(data):
+    # drop-in replacement for _utf8_python2
+    if isinstance(data,str):
+        return data
+    else:
+        raise TypeError("not a basestring")
+utf8 = [_utf8_python2,_utf8_python3][PYTHON3]
 def dict_match(d, key, default=None):
         return d[key]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.