sixish: handle Unicode strings in the environment

Python 2 wants ASCII/bytes (not unicode strings) while Python 3 wants
strings (not bytes).  Add a helper to deal with this on the fly.

Bug: None
Test: unittests pass
Change-Id: Ic28182608647973749390e3a66731472a2010c35
diff --git a/pre-upload.py b/pre-upload.py
index 681a269..66ac88b 100755
--- a/pre-upload.py
+++ b/pre-upload.py
@@ -45,6 +45,7 @@
 import rh.config
 import rh.git
 import rh.hooks
+import rh.sixish
 import rh.terminal
 import rh.utils
 
@@ -283,7 +284,7 @@
         os.environ['PREUPLOAD_COMMIT'] = commit
         diff = rh.git.get_affected_files(commit)
         desc = rh.git.get_commit_desc(commit)
-        os.environ['PREUPLOAD_COMMIT_MESSAGE'] = desc
+        rh.sixish.setenv('PREUPLOAD_COMMIT_MESSAGE', desc)
 
         commit_summary = desc.split('\n', 1)[0]
         output.commit_start(commit=commit, commit_summary=commit_summary)
diff --git a/rh/sixish.py b/rh/sixish.py
index 08f3731..693598c 100644
--- a/rh/sixish.py
+++ b/rh/sixish.py
@@ -55,3 +55,15 @@
     string_types = basestring
 else:
     string_types = str
+
+
+def setenv(var, val):
+    """Set |var| in the environment to |val|.
+
+    Python 2 wants ASCII strings, not unicode.
+    Python 3 only wants unicode strings.
+    """
+    try:
+        os.environ[var] = val
+    except UnicodeEncodeError:
+        os.environ[var] = val.encode('utf-8')